1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
/*
* Copyright (C) 1999-2002 Stijn van Dongen.
*/
#include "util/array.h"
#include "util/alloc.h"
#include "util/types.h"
#include <string.h>
void mcxSplice
( void* base1pptr
, const void* base2ptr
, int size /* size of base1 and base2 members */
, int *pn_base1 /* # base1 elements currently in use */
, int *pN_base1 /* # base1 elements for which is malloced */
, int O_base1 /* splice relative to this element */
, int d_base1 /* delete this number of elements */
, int c_base2 /* number of elements to copy */
)
{ char **ppr1 = (char **) base1pptr
; const char *ptr2 = (const char *) base2ptr
; int n_base1 = *pn_base1
; int N_base1 = *pN_base1
; int m_base1 = n_base1 - d_base1 + c_base2
; int o_base1 = O_base1 >= 0
?
O_base1
:
n_base1 + O_base1 + 1
; const char *dieMsg = ""
; if (0)
{ die
: fprintf
( stderr
, "[mcxSplice PBD] %s\n"
"[mcxSplice] [n1, %d] [N1, %d] [o1, %d] [d1, %d] [c2, %d]\n"
, dieMsg
, n_base1, N_base1, O_base1, d_base1, c_base2
)
; exit(1)
; }
if ( n_base1 < 0
|| N_base1 < 0
|| n_base1 > N_base1
|| d_base1 < 0
|| c_base2 < 0
)
{ dieMsg = "integer arguments not consistent"
; goto die
; }
if (o_base1 < 0 || o_base1 > n_base1)
{ dieMsg = "computed splice offset not in bounds"
; goto die
; }
if (*ppr1 == NULL && ptr2 == NULL)
{ dieMsg = "source and destination both void"
; goto die
; }
if (o_base1 + d_base1 > n_base1)
{ dieMsg = "not that many elements to delete"
; goto die
; }
if (m_base1 > N_base1)
*ppr1 = mcxRealloc(*ppr1, size*m_base1, RETURN_ON_FAIL)
, *pN_base1 = N_base1 = m_base1
; if (m_base1 && *ppr1 == NULL)
mcxMemDenied(stderr, "mcxSplice", "void", m_base1)
, exit(1)
; if (o_base1 < n_base1)
memmove
( *ppr1 + size*(o_base1 + c_base2)
, *ppr1 + size*(o_base1 + d_base1)
, size*(n_base1 - o_base1 - d_base1)
)
; if (c_base2)
memcpy
( *ppr1 + size * (o_base1)
, ptr2
, size*(c_base2)
)
; *pn_base1 = m_base1
; }
int mcxSplitsearch
( void* key
, void* base
, int nr
, int size
, int (*cmp)(const void*, const void*)
, mcxbool right
)
{ int u = nr-1
; int l = 0
; int left = right == 0 ? 1 : 0
; if (!base)
{ fprintf(stderr, "[mcxSplitsearch] warning: received void argument\n")
; return -1
; }
else if (!cmp(((char*)base)+(nr-1)*size, key) )
{ return left ? nr-1 : -1
; }
else if ( cmp(((char*)base)+0, key) )
{ return left ? -1 : 0
; }
/* invariant: cmp(base[u], key)
!cmp(base[l], key)
*/
while (l+1 < u)
{ int m = (l+u)/2
; if (!cmp(((char*)base)+m*size, key) )
l = m
; else
u = m
; }
return u - left
; }
int mcxDedup
( void* base
, int nmemb
, int size
, int (*cmp)(const void *, const void *)
, void (*merge)(void *, const void *)
)
{ int k = 0
; int l = 0
; while (l < nmemb)
{
if (k != l)
memcpy(((char*)base) + k * size, ((char*)base) + l * size, size)
; while
( ++l < nmemb
&& ( cmp
? (!cmp(((char*)base) + k * size, ((char*)base) + l * size))
: (!memcmp(((char*)base) + k*size, ((char*)base) + l*size, size))
)
)
{ if (merge)
{ merge(((char*)base) + k * size, ((char*)base) + l * size)
; }
}
; k++
; }
return k
; }
/* =======================================================================
*
* The stuff below is a little bit nonsensical. Kept it for nostalgia.
*
* =======================================================================
*/
#if 0
typedef struct
{
void* ls
; int n
; int n_alloc /* For future functionality */
; int size
;
} mcxArray ;
typedef struct
{
mcxArray* ls
; int n
; int n_alloc /* For future functionality */
;
} mcxTable ;
mcxArray* mcxArrayInit
(
mcxArray *ar
, int n
, int size
, void* (*obInit) (void *)
) ;
mcxTable* mcxTableNew
(
int n
, int len
, int size
, void* (*obInit) (void *)
) ;
void mcxArrayRelease
(
mcxArray* ar
, void (*obRelease)(void *)
) ;
void mcxArrayFree
(
mcxArray** arpp
, void (*obRelease)(void *)
) ;
my_inline void mcxArrayFree
(
mcxArray** arpp
, void (*obRelease)(void *)
)
{
if (*arpp)
{ mcxArrayRelease(*arpp, obRelease)
; mcxFree(*arpp)
; *arpp = NULL
; }
; }
void mcxTableFree
(
mcxTable** tablepp
, void (*obRelease)(void *)
) ;
mcxArray* mcxArrayInit
(
mcxArray* ar
, int n
, int size
, void* (*obInit) (void *)
)
{
char* ob
; if (!ar)
ar = (mcxArray*) mcxAlloc(sizeof(mcxArray), EXIT_ON_FAIL)
; ar->ls = (void*) mcxAlloc
( n * size
, RETURN_ON_FAIL
)
; if (n && !ar->ls)
mcxMemDenied(stderr, "mcxArray", "void", n)
, exit(1)
; ar->n = n
; ar->n_alloc = n
; ar->size = size
; ob = ar->ls
; if (obInit)
{ while (--n >= 0)
{
obInit(ob)
; ob += size
; }
; }
; return ar
; }
mcxTable* mcxTableNew
(
int n
, int len
, int size
, void* (*obInit) (void *)
)
{
mcxArray *ar
; mcxTable *table = (mcxTable*) mcxAlloc
( sizeof(mcxTable)
, EXIT_ON_FAIL
)
; table->ls = (mcxArray*) mcxAlloc
( n*sizeof(mcxArray)
, RETURN_ON_FAIL
)
; if (n && !table->ls)
mcxMemDenied(stderr, "mcxTableNew", "mcxArray", n)
, exit(1)
; table->n = n
; ar = table->ls
; while (--n >= 0)
mcxArrayInit(ar, len, size, obInit)
, ar++
; return table
; }
void mcxArrayRelease
(
mcxArray* ar
, void (*obRelease)(void *)
)
{
if (ar)
{
int n = ar->n
; char* ob = (char*) ar->ls
; if (obRelease)
{ while (--n >= 0)
{ obRelease(ob)
; ob += ar->size
; }
; }
; mcxFree(ar->ls)
; ar->ls = NULL
; ar->n = 0
; ar->n_alloc = 0
; ar->size = 0
; }
; }
void mcxTableFree
(
mcxTable** tablepp
, void (*obRelease)(void *)
)
{
if (*tablepp)
{
int n = (*tablepp)->n
; mcxArray * ar = (*tablepp)->ls
; while (--n >= 0)
mcxArrayRelease(ar, obRelease)
, ar++
; mcxFree((*tablepp)->ls)
; mcxFree(*tablepp)
; (*tablepp) = NULL
; }
; }
#endif
|