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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
|
/*
* Dksets.c
*
* $Id$
*
* Sets
*
* This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
* project.
*
* Copyright (C) 1998-2012 OpenLink Software
*
* This project is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License, dated June 1991.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "Dk.h"
void
DBG_NAME (dk_set_push) (DBG_PARAMS s_node_t ** set, void *item)
{
s_node_t *newn = (s_node_t *) DK_ALLOC (sizeof (s_node_t));
newn->next = *set;
newn->data = item;
*set = newn;
}
void
DBG_NAME (dk_set_pushnew) (DBG_PARAMS s_node_t ** set, void *item)
{
if (!dk_set_member (*set, item))
{
s_node_t *newn = (s_node_t *) DK_ALLOC (sizeof (s_node_t));
newn->next = *set;
newn->data = item;
*set = newn;
}
}
void *
DBG_NAME (dk_set_pop) (DBG_PARAMS s_node_t ** set)
{
if (*set)
{
void *item;
s_node_t *old = *set;
*set = old->next;
item = old->data;
DK_FREE (old, sizeof (s_node_t));
return item;
}
return NULL;
}
int
DBG_NAME (dk_set_delete) (DBG_PARAMS dk_set_t * set, void *item)
{
s_node_t *node = *set;
dk_set_t *previous = set;
while (node)
{
if (node->data == item)
{
*previous = node->next;
DK_FREE (node, sizeof (s_node_t));
return 1;
}
previous = &(node->next);
node = node->next;
}
return 0;
}
void *
DBG_NAME (dk_set_delete_nth) (DBG_PARAMS dk_set_t * set, int idx)
{
s_node_t *node = *set;
dk_set_t *previous = set;
if (0 > idx)
return NULL;
while (node)
{
if (0 == idx)
{
void *res = node->data;
*previous = node->next;
DK_FREE (node, sizeof (s_node_t));
return res;
}
previous = &(node->next);
node = node->next;
idx--;
}
return NULL;
}
uint32
dk_set_length (s_node_t * set)
{
uint32 count;
for (count = 0; set; set = set->next)
count++;
return count;
}
dk_set_t
dk_set_last (dk_set_t set)
{
dk_set_t s;
if (!set)
return set;
else
{
for (s = set; s->next; s = s->next)
;
return s;
}
}
dk_set_t
dk_set_conc (dk_set_t s1, dk_set_t s2)
{
dk_set_t last = dk_set_last (s1);
if (last)
{
last->next = s2;
return s1;
}
else
{
return s2;
}
}
dk_set_t
DBG_NAME (dk_set_cons) (DBG_PARAMS void *s1, dk_set_t s2)
{
dk_set_t tmp = (dk_set_t) DK_ALLOC (sizeof (s_node_t));
tmp->data = s1;
tmp->next = s2;
return tmp;
}
void
DBG_NAME (dk_set_free) (DBG_PARAMS s_node_t * set)
{
dk_set_t next;
while (set)
{
next = set->next;
DK_FREE ((void *) set, sizeof (s_node_t));
set = next;
}
}
s_node_t *
dk_set_member (s_node_t * set, void *elt)
{
while (set)
{
if (elt == set->data)
return set;
set = set->next;
}
return NULL;
}
void **
DBG_NAME (dk_set_to_array) (DBG_PARAMS s_node_t * set)
{
void **array;
uint32 len;
uint32 inx;
len = dk_set_length (set);
array = (void **) DBG_NAME (dk_alloc_box) (DBG_ARGS len * sizeof (void *), DV_ARRAY_OF_POINTER);
inx = 0;
DO_SET (void *, elt, &set)
{
array[inx++] = elt;
}
END_DO_SET ();
return array;
}
caddr_t
DBG_NAME (list_to_array) (DBG_PARAMS s_node_t * set)
{
void **array;
uint32 len;
uint32 inx;
len = dk_set_length (set);
array = (void **) DBG_NAME (dk_alloc_box) (DBG_ARGS len * sizeof (void *), DV_ARRAY_OF_POINTER);
inx = 0;
DO_SET (void *, elt, &set)
{
array[inx++] = elt;
}
END_DO_SET ();
DBG_NAME (dk_set_free) (DBG_ARGS set);
return (caddr_t) array;
}
caddr_t
DBG_NAME (copy_list_to_array) (DBG_PARAMS s_node_t * set)
{
void **array;
uint32 len;
uint32 inx;
len = dk_set_length (set);
array = (void **) DBG_NAME (dk_alloc_box) (DBG_ARGS len * sizeof (void *), DV_ARRAY_OF_POINTER);
inx = 0;
DO_SET (void *, elt, &set)
{
array[inx++] = elt;
}
END_DO_SET ();
return (caddr_t) array;
}
caddr_t
DBG_NAME (revlist_to_array) (DBG_PARAMS s_node_t * set)
{
void **array;
uint32 len;
uint32 inx;
inx = len = dk_set_length (set);
array = (void **) DBG_NAME (dk_alloc_box) (DBG_ARGS len * sizeof (void *), DV_ARRAY_OF_POINTER);
DO_SET (void *, elt, &set)
{
array[--inx] = elt;
}
END_DO_SET ();
DBG_NAME (dk_set_free) (DBG_ARGS set);
return (caddr_t) array;
}
dk_set_t
dk_set_nreverse (dk_set_t set)
{
dk_set_t next;
dk_set_t next2;
if (!set)
return NULL;
next = set->next;
set->next = NULL;
for (;;)
{
if (!next)
return set;
next2 = next->next;
next->next = set;
set = next;
next = next2;
}
}
#define STEP_2(node) \
if (node) node = node -> next; \
if (node) node = node -> next;
/* true if non-circular and all nodes are valid pointers. */
void
dk_set_check_straight (dk_set_t set)
{
dk_set_t fast = set;
dk_set_t slow = set;
STEP_2 (fast);
while (slow)
{
if (slow == fast)
{
GPF_T1 ("Circular list");
}
dk_alloc_assert (slow);
STEP_2 (fast);
slow = slow->next;
}
}
int
dk_set_position (dk_set_t set, void *elt)
{
int nth = 0;
while (set)
{
if (set->data == elt)
return nth;
nth++;
set = set->next;
}
return -1;
}
int
dk_set_position_of_string (dk_set_t set, const char *strg)
{
int nth = 0;
while (set)
{
if (!strcmp ((const char *) set->data, strg))
return nth;
nth++;
set = set->next;
}
return -1;
}
void *
dk_set_get_keyword (dk_set_t set, const char *key_strg, void *dflt_val)
{
while (set)
{
if (!strcmp ((const char *) set->data, key_strg))
return set->next->data;
set = set->next->next;
}
return dflt_val;
}
void **
dk_set_getptr_keyword (dk_set_t set, const char *key_strg)
{
while (set)
{
if (!strcmp ((const char *) set->data, key_strg))
return &(set->next->data);
set = set->next->next;
}
return NULL;
}
void *
dk_set_nth (dk_set_t set, int nth)
{
int inx;
for (inx = 0; inx < nth; inx++)
{
if (set)
set = set->next;
else
break;
}
if (set)
return (set->data);
else
return NULL;
}
dk_set_t
DBG_NAME (dk_set_copy) (DBG_PARAMS dk_set_t s)
{
dk_set_t r = NULL;
dk_set_t *last = &r;
while (s)
{
dk_set_t n = (dk_set_t) DK_ALLOC (sizeof (s_node_t));
*last = n;
n->data = s->data;
n->next = NULL;
last = &n->next;
s = s->next;
}
return r;
}
int
dk_set_is_subset (dk_set_t super, dk_set_t sub)
{
DO_SET (void *, elt, &sub)
{
if (!dk_set_member (super, elt))
return 0;
}
END_DO_SET ();
return 1;
}
/* Original signatures should exist for EXE-EXPORTed functions */
#ifdef MALLOC_DEBUG
#undef dk_set_delete
int
dk_set_delete (dk_set_t * set, void *item)
{
return dbg_dk_set_delete (__FILE__, __LINE__, set, item);
}
#undef dk_set_delete_nth
void *
dk_set_delete_nth (dk_set_t * set, int n)
{
return dbg_dk_set_delete_nth (__FILE__, __LINE__, set, n);
}
#undef dk_set_push
void
dk_set_push (s_node_t ** set, void *item)
{
dbg_dk_set_push (__FILE__, __LINE__, set, item);
}
#undef list_to_array
caddr_t
list_to_array (dk_set_t l)
{
return dbg_list_to_array (__FILE__, __LINE__, l);
}
#undef copy_list_to_array
caddr_t
copy_list_to_array (dk_set_t l)
{
return dbg_copy_list_to_array (__FILE__, __LINE__, l);
}
#undef revlist_to_array
caddr_t
revlist_to_array (dk_set_t l)
{
return dbg_revlist_to_array (__FILE__, __LINE__, l);
}
#endif
|