1 /*   (C) Copyright 2000, 2001, 2002, 2003, 2004, 2005 Stijn van Dongen
2  *   (C) Copyright 2006, 2007, 2008, 2009  Stijn van Dongen
3  *
4  * This file is part of tingea.  You can redistribute and/or modify tingea
5  * under the terms of the GNU General Public License; either version 3 of the
6  * License or (at your option) any later version.  You should have received a
7  * copy of the GPL along with tingea, in the file COPYING.
8 */
9 
10 #ifndef tingea_array_h
11 #define tingea_array_h
12 
13 #include "types.h"
14 
15 mcxstatus mcxSplice
16 (  void*           base1pp   /*  _address_ of pointer to elements       */
17 ,  const void*     base2p    /*  pointer to elements                    */
18 ,  dim             size      /*  size of base1 and base2 members        */
19 ,  dim          *n_base1     /*  total length of elements after base1   */
20 ,  dim          *N_base1     /*  number of alloc'ed elements for base1  */
21 ,  ofs           o_base1     /*  splice relative to this ofset          */
22 ,  dim           d_base1     /*  delete this number of elements         */
23 ,  dim           c_base2     /*  number of elements to copy             */
24 )  ;
25 
26 
27 dim mcxDedup
28 (  void*          base
29 ,  dim            nmemb
30 ,  dim            size
31 ,  int            (*cmp)(const void *, const void *)
32 ,  void           (*merge)(void *, const void *)
33 )  ;
34 
35 
36 mcxstatus mcxResize
37 (  void*          mempp
38 ,  dim            size
39 ,  dim*           ct
40 ,  dim            newct
41 ,  mcxOnFail      ON_FAIL
42 )  ;
43 
44 
45 /* Return largest element smaller than or equal to key.
46  * return NULL if no element is smaller than key.
47  * Returns rightmost element in case entries sort identically,
48  * (note: mcxBsearchCeil will then return the leftmost element)
49  *
50  * base should be sorted according to cmp
51 */
52 
53 void* mcxBsearchFloor
54 (  const void *key
55 ,  const void *base
56 ,  dim nmemb
57 ,  dim size
58 ,  int (*cmp)(const void *, const void *)
59 )  ;
60 
61 
62 /* Return smallest element larger than or equal to key.
63  * return NULL if no element is larger than key.
64  * Returns leftmost element in case entries sort identically,
65  * (note: mcxBsearchFloor will then return the rightmost element)
66  *
67  * base should be sorted according to cmp
68 */
69 
70 void* mcxBsearchCeil
71 (  const void *key
72 ,  const void *base
73 ,  dim nmemb
74 ,  dim size
75 ,  int (*cmp)(const void *, const void *)
76 )  ;
77 
78 
79 /* Uses weighted combinations of neighbours when the quartile
80  * range does not fall perfectly on array offsets (i.e.
81  * when the array size is not a multiple of 4.
82 */
83 double mcxMedian
84 (  void* base
85 ,  dim   n
86 ,  dim   sz
87 ,  double (*get)(const void*)
88 ,  double* iqr
89 )  ;
90 
91 
92 /* Fisher Yates shuffle */
93 
94 void mcxShuffle
95 (  void* datap
96 ,  dim   nmem
97 ,  dim   mem_size
98 ,  char* mem_cell    /* should have mem_size size */
99 )  ;
100 
101 
102 
103 
104 
105 
106 
107 typedef struct
108 {  void*       mempptr
109 ;  dim         size
110 ;  dim         n
111 ;  dim         n_alloc
112 ;  float       factor
113 ;  mcxbool     bFinalized
114 ;
115 }  mcxBuf      ;
116 
117 
118 
119 /*
120  *    *mempptr should be peekable; NULL or valid memory pointer.
121 */
122 
123 mcxstatus mcxBufInit
124 (  mcxBuf*     buf
125 ,  void*       mempptr
126 ,  dim         size
127 ,  dim         n
128 )  ;
129 
130 
131 /*
132  *    Extends the buffer by n_request unitialized chunks and returns a pointer
133  *    to this space. It is the caller's responsibility to treat this space
134  *    consistently. The counter buf->n is increased by n_request.
135  *
136  *    If we cannot extend (realloc), a NULL pointer is returned;
137  *    the original space is left intact.
138  *
139  *    Returns NULL on (alloc) failure
140 */
141 
142 void* mcxBufExtend
143 (  mcxBuf*     buf
144 ,  dim         n_request
145 ,  mcxOnFail   ON_FAIL
146 )  ;
147 
148 
149 /*
150  *    Make superfluous memory reclaimable by system,
151  *    prepare for discarding buf (but not *(buf->memptr)!)
152  *
153  *    If for some bizarre reason we cannot shrink (realloc),
154  *    errno is set to ENOMEM.
155  *    the original space is left intact. Its size is in buf.n .
156 */
157 
158 dim mcxBufFinalize
159 (  mcxBuf*  buf
160 )  ;
161 
162 
163 /*
164  *    Make buffer refer to a new variable. Size cannot be changed,
165  *    so variable should be of same type as before.
166 */
167 
168 void mcxBufReset
169 (  mcxBuf*  buf
170 ,  void*    mempptr
171 )  ;
172 
173 #endif
174 
175 
176