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 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
|
/*
* bltList.c --
*
* The module implements generic linked lists.
*
* Copyright 1991-1998 Lucent Technologies, Inc.
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appear in all
* copies and that both that the copyright notice and warranty
* disclaimer appear in supporting documentation, and that the names
* of Lucent Technologies any of their entities not be used in
* advertising or publicity pertaining to distribution of the software
* without specific, written prior permission.
*
* Lucent Technologies disclaims all warranties with regard to this
* software, including all implied warranties of merchantability and
* fitness. In no event shall Lucent Technologies be liable for any
* special, indirect or consequential damages or any damages
* whatsoever resulting from loss of use, data or profits, whether in
* an action of contract, negligence or other tortuous action, arising
* out of or in connection with the use or performance of this
* software.
*/
#include "bltInt.h"
#include "bltList.h"
static struct Blt_ListNodeStruct *
FindString(listPtr, key)
struct Blt_ListStruct *listPtr; /* List to search */
CONST char *key; /* Key to match */
{
register struct Blt_ListNodeStruct *nodePtr;
char c;
c = key[0];
for (nodePtr = listPtr->headPtr; nodePtr != NULL;
nodePtr = nodePtr->nextPtr) {
if ((c == nodePtr->key.string[0]) &&
(strcmp(key, nodePtr->key.string) == 0)) {
return nodePtr;
}
}
return NULL;
}
static Blt_ListNode
FindOneWord(listPtr, key)
struct Blt_ListStruct *listPtr; /* List to search */
CONST char *key; /* Key to match */
{
register struct Blt_ListNodeStruct *nodePtr;
for (nodePtr = listPtr->headPtr; nodePtr != NULL;
nodePtr = nodePtr->nextPtr) {
if (key == nodePtr->key.oneWordValue) {
return nodePtr;
}
}
return NULL;
}
static Blt_ListNode
FindArray(listPtr, key)
struct Blt_ListStruct *listPtr; /* List to search */
CONST char *key; /* Key to match */
{
register struct Blt_ListNodeStruct *nodePtr;
int nBytes;
nBytes = sizeof(int) * listPtr->type;
for (nodePtr = listPtr->headPtr; nodePtr != NULL;
nodePtr = nodePtr->nextPtr) {
if (memcmp(key, nodePtr->key.words, nBytes) == 0) {
return nodePtr;
}
}
return NULL;
}
/*
*----------------------------------------------------------------------
*
* FreeNode --
*
* Free the memory allocated for the node.
*
* Results:
* None.
*
*----------------------------------------------------------------------
*/
static void
FreeNode(nodePtr)
struct Blt_ListNodeStruct *nodePtr;
{
Blt_Free(nodePtr);
}
/*
*----------------------------------------------------------------------
*
* Blt_ListCreate --
*
* Creates a new linked list structure and initializes its pointers
*
* Results:
* Returns a pointer to the newly created list structure.
*
*----------------------------------------------------------------------
*/
/*LINTLIBRARY*/
Blt_List
Blt_ListCreate(type)
int type;
{
struct Blt_ListStruct *listPtr;
listPtr = Blt_Malloc(sizeof(struct Blt_ListStruct));
if (listPtr != NULL) {
Blt_ListInit(listPtr, type);
}
return listPtr;
}
/*
*----------------------------------------------------------------------
*
* Blt_ListCreateNode --
*
* Creates a list node holder. This routine does not insert
* the node into the list, nor does it no attempt to maintain
* consistency of the keys. For example, more than one node
* may use the same key.
*
* Results:
* The return value is the pointer to the newly created node.
*
* Side Effects:
* The key is not copied, only the Uid is kept. It is assumed
* this key will not change in the life of the node.
*
*----------------------------------------------------------------------
*/
/*LINTLIBRARY*/
Blt_ListNode
Blt_ListCreateNode(listPtr, key)
struct Blt_ListStruct *listPtr;
CONST char *key; /* Unique key to reference object */
{
register struct Blt_ListNodeStruct *nodePtr;
int keySize;
if (listPtr->type == BLT_STRING_KEYS) {
keySize = strlen(key) + 1;
} else if (listPtr->type == BLT_ONE_WORD_KEYS) {
keySize = sizeof(int);
} else {
keySize = sizeof(int) * listPtr->type;
}
nodePtr = Blt_Calloc(1, sizeof(struct Blt_ListNodeStruct) + keySize - 4);
assert(nodePtr);
nodePtr->clientData = NULL;
nodePtr->nextPtr = nodePtr->prevPtr = NULL;
nodePtr->listPtr = listPtr;
switch (listPtr->type) {
case BLT_STRING_KEYS:
strcpy(nodePtr->key.string, key);
break;
case BLT_ONE_WORD_KEYS:
nodePtr->key.oneWordValue = key;
break;
default:
memcpy(nodePtr->key.words, key, keySize);
break;
}
return nodePtr;
}
/*
*----------------------------------------------------------------------
*
* Blt_ListReset --
*
* Removes all the entries from a list, removing pointers to the
* objects and keys (not the objects or keys themselves). The
* node counter is reset to zero.
*
* Results:
* None.
*
*----------------------------------------------------------------------
*/
/*LINTLIBRARY*/
void
Blt_ListReset(listPtr)
struct Blt_ListStruct *listPtr; /* List to clear */
{
if (listPtr != NULL) {
register struct Blt_ListNodeStruct *oldPtr;
register struct Blt_ListNodeStruct *nodePtr = listPtr->headPtr;
while (nodePtr != NULL) {
oldPtr = nodePtr;
nodePtr = nodePtr->nextPtr;
FreeNode(oldPtr);
}
Blt_ListInit(listPtr, listPtr->type);
}
}
/*
*----------------------------------------------------------------------
*
* Blt_ListDestroy
*
* Frees all list structures
*
* Results:
* Returns a pointer to the newly created list structure.
*
*----------------------------------------------------------------------
*/
/*LINTLIBRARY*/
void
Blt_ListDestroy(listPtr)
struct Blt_ListStruct *listPtr;
{
if (listPtr != NULL) {
Blt_ListReset(listPtr);
Blt_Free(listPtr);
}
}
/*
*----------------------------------------------------------------------
*
* Blt_ListInit --
*
* Initializes a linked list.
*
* Results:
* None.
*
*----------------------------------------------------------------------
*/
/*LINTLIBRARY*/
void
Blt_ListInit(listPtr, type)
struct Blt_ListStruct *listPtr;
int type;
{
listPtr->nNodes = 0;
listPtr->headPtr = listPtr->tailPtr = NULL;
listPtr->type = type;
}
/*
*----------------------------------------------------------------------
*
* Blt_ListLinkAfter --
*
* Inserts an node following a given node.
*
* Results:
* None.
*
*----------------------------------------------------------------------
*/
/*LINTLIBRARY*/
void
Blt_ListLinkAfter(listPtr, nodePtr, afterPtr)
struct Blt_ListStruct *listPtr;
struct Blt_ListNodeStruct *nodePtr;
struct Blt_ListNodeStruct *afterPtr;
{
if (listPtr->headPtr == NULL) {
listPtr->tailPtr = listPtr->headPtr = nodePtr;
} else {
if (afterPtr == NULL) {
/* Prepend to the front of the list */
nodePtr->nextPtr = listPtr->headPtr;
nodePtr->prevPtr = NULL;
listPtr->headPtr->prevPtr = nodePtr;
listPtr->headPtr = nodePtr;
} else {
nodePtr->nextPtr = afterPtr->nextPtr;
nodePtr->prevPtr = afterPtr;
if (afterPtr == listPtr->tailPtr) {
listPtr->tailPtr = nodePtr;
} else {
afterPtr->nextPtr->prevPtr = nodePtr;
}
afterPtr->nextPtr = nodePtr;
}
}
nodePtr->listPtr = listPtr;
listPtr->nNodes++;
}
/*
*----------------------------------------------------------------------
*
* Blt_ListLinkBefore --
*
* Inserts an node preceding a given node.
*
* Results:
* None.
*
*----------------------------------------------------------------------
*/
/*LINTLIBRARY*/
void
Blt_ListLinkBefore(listPtr, nodePtr, beforePtr)
struct Blt_ListStruct *listPtr; /* List to contain new node */
struct Blt_ListNodeStruct *nodePtr; /* New node to be inserted */
struct Blt_ListNodeStruct *beforePtr; /* Node to link before */
{
if (listPtr->headPtr == NULL) {
listPtr->tailPtr = listPtr->headPtr = nodePtr;
} else {
if (beforePtr == NULL) {
/* Append onto the end of the list */
nodePtr->nextPtr = NULL;
nodePtr->prevPtr = listPtr->tailPtr;
listPtr->tailPtr->nextPtr = nodePtr;
listPtr->tailPtr = nodePtr;
} else {
nodePtr->prevPtr = beforePtr->prevPtr;
nodePtr->nextPtr = beforePtr;
if (beforePtr == listPtr->headPtr) {
listPtr->headPtr = nodePtr;
} else {
beforePtr->prevPtr->nextPtr = nodePtr;
}
beforePtr->prevPtr = nodePtr;
}
}
nodePtr->listPtr = listPtr;
listPtr->nNodes++;
}
/*
*----------------------------------------------------------------------
*
* Blt_ListUnlinkNode --
*
* Unlinks an node from the given list. The node itself is
* not deallocated, but only removed from the list.
*
* Results:
* None.
*
*----------------------------------------------------------------------
*/
/*LINTLIBRARY*/
void
Blt_ListUnlinkNode(nodePtr)
struct Blt_ListNodeStruct *nodePtr;
{
struct Blt_ListStruct *listPtr;
listPtr = nodePtr->listPtr;
if (listPtr != NULL) {
if (listPtr->headPtr == nodePtr) {
listPtr->headPtr = nodePtr->nextPtr;
}
if (listPtr->tailPtr == nodePtr) {
listPtr->tailPtr = nodePtr->prevPtr;
}
if (nodePtr->nextPtr != NULL) {
nodePtr->nextPtr->prevPtr = nodePtr->prevPtr;
}
if (nodePtr->prevPtr != NULL) {
nodePtr->prevPtr->nextPtr = nodePtr->nextPtr;
}
nodePtr->listPtr = NULL;
listPtr->nNodes--;
}
}
/*
*----------------------------------------------------------------------
*
* Blt_ListGetNode --
*
* Find the first node matching the key given.
*
* Results:
* Returns the pointer to the node. If no node matching
* the key given is found, then NULL is returned.
*
*----------------------------------------------------------------------
*/
/*LINTLIBRARY*/
Blt_ListNode
Blt_ListGetNode(listPtr, key)
struct Blt_ListStruct *listPtr; /* List to search */
CONST char *key; /* Key to match */
{
if (listPtr != NULL) {
switch (listPtr->type) {
case BLT_STRING_KEYS:
return FindString(listPtr, key);
case BLT_ONE_WORD_KEYS:
return FindOneWord(listPtr, key);
default:
return FindArray(listPtr, key);
}
}
return NULL;
}
/*
*----------------------------------------------------------------------
*
* Blt_ListDeleteNode --
*
* Unlinks and deletes the given node.
*
* Results:
* None.
*
*----------------------------------------------------------------------
*/
/*LINTLIBRARY*/
void
Blt_ListDeleteNode(nodePtr)
struct Blt_ListNodeStruct *nodePtr;
{
Blt_ListUnlinkNode(nodePtr);
FreeNode(nodePtr);
}
/*
*----------------------------------------------------------------------
*
* Blt_ListDeleteNodeByKey --
*
* Find the node and free the memory allocated for the node.
*
* Results:
* None.
*
*----------------------------------------------------------------------
*/
/*LINTLIBRARY*/
void
Blt_ListDeleteNodeByKey(listPtr, key)
struct Blt_ListStruct *listPtr;
CONST char *key;
{
struct Blt_ListNodeStruct *nodePtr;
nodePtr = Blt_ListGetNode(listPtr, key);
if (nodePtr != NULL) {
Blt_ListDeleteNode(nodePtr);
}
}
/*LINTLIBRARY*/
Blt_ListNode
Blt_ListAppend(listPtr, key, clientData)
struct Blt_ListStruct *listPtr;
CONST char *key;
ClientData clientData;
{
struct Blt_ListNodeStruct *nodePtr;
nodePtr = Blt_ListCreateNode(listPtr, key);
Blt_ListSetValue(nodePtr, clientData);
Blt_ListAppendNode(listPtr, nodePtr);
return nodePtr;
}
/*LINTLIBRARY*/
Blt_ListNode
Blt_ListPrepend(listPtr, key, clientData)
struct Blt_ListStruct *listPtr;
CONST char *key;
ClientData clientData;
{
struct Blt_ListNodeStruct *nodePtr;
nodePtr = Blt_ListCreateNode(listPtr, key);
Blt_ListSetValue(nodePtr, clientData);
Blt_ListPrependNode(listPtr, nodePtr);
return nodePtr;
}
/*
*----------------------------------------------------------------------
*
* Blt_ListGetNthNode --
*
* Find the node based upon a given position in list.
*
* Results:
* Returns the pointer to the node, if that numbered element
* exists. Otherwise NULL.
*
*----------------------------------------------------------------------
*/
/*LINTLIBRARY*/
Blt_ListNode
Blt_ListGetNthNode(listPtr, position, direction)
struct Blt_ListStruct *listPtr; /* List to traverse */
int position; /* Index of node to select from front
* or back of the list. */
int direction;
{
register struct Blt_ListNodeStruct *nodePtr;
if (listPtr != NULL) {
if (direction > 0) {
for (nodePtr = listPtr->headPtr; nodePtr != NULL;
nodePtr = nodePtr->nextPtr) {
if (position == 0) {
return nodePtr;
}
position--;
}
} else {
for (nodePtr = listPtr->tailPtr; nodePtr != NULL;
nodePtr = nodePtr->prevPtr) {
if (position == 0) {
return nodePtr;
}
position--;
}
}
}
return NULL;
}
/*
*----------------------------------------------------------------------
*
* Blt_ListSort --
*
* Find the node based upon a given position in list.
*
* Results:
* Returns the pointer to the node, if that numbered element
* exists. Otherwise NULL.
*
*----------------------------------------------------------------------
*/
/*LINTLIBRARY*/
void
Blt_ListSort(listPtr, proc)
struct Blt_ListStruct *listPtr; /* List to traverse */
Blt_ListCompareProc *proc;
{
struct Blt_ListNodeStruct **nodeArr;
register struct Blt_ListNodeStruct *nodePtr;
register int i;
if (listPtr->nNodes < 2) {
return;
}
nodeArr = Blt_Malloc(sizeof(Blt_List) * (listPtr->nNodes + 1));
if (nodeArr == NULL) {
return; /* Out of memory. */
}
i = 0;
for (nodePtr = listPtr->headPtr; nodePtr != NULL;
nodePtr = nodePtr->nextPtr) {
nodeArr[i++] = nodePtr;
}
qsort((char *)nodeArr, listPtr->nNodes,
sizeof(struct Blt_ListNodeStruct *), (QSortCompareProc *)proc);
/* Rethread the list. */
nodePtr = nodeArr[0];
listPtr->headPtr = nodePtr;
nodePtr->prevPtr = NULL;
for (i = 1; i < listPtr->nNodes; i++) {
nodePtr->nextPtr = nodeArr[i];
nodePtr->nextPtr->prevPtr = nodePtr;
nodePtr = nodePtr->nextPtr;
}
listPtr->tailPtr = nodePtr;
nodePtr->nextPtr = NULL;
Blt_Free(nodeArr);
}
|