Debian Codegraph @

generic/bltTree.c

raw symbols ◀ ▶ SCIP
4 Debian patches touch this file
1
2 /*
3 * bltTree.c --
4 *
5 * Copyright 1998-1999 Lucent Technologies, Inc.
6 *
7 * Permission to use, copy, modify, and distribute this software and
8 * its documentation for any purpose and without fee is hereby
9 * granted, provided that the above copyright notice appear in all
10 * copies and that both that the copyright notice and warranty
11 * disclaimer appear in supporting documentation, and that the names
12 * of Lucent Technologies or any of their entities not be used in
13 * advertising or publicity pertaining to distribution of the software
14 * without specific, written prior permission.
15 *
16 * Lucent Technologies disclaims all warranties with regard to this
17 * software, including all implied warranties of merchantability and
18 * fitness. In no event shall Lucent Technologies be liable for any
19 * special, indirect or consequential damages or any damages
20 * whatsoever resulting from loss of use, data or profits, whether in
21 * an action of contract, negligence or other tortuous action, arising
22 * out of or in connection with the use or performance of this
23 * software.
24 *
25 * The "tree" data object was created by George A. Howlett.
26 * Extensive cleanups and enhancements by Peter MacDonald.
27 *
28 */
29
30 #include "bltInt.h"
31
32 /* TODO:
33 * traces and notifiers should be in one list in tree object.
34 * notifier is always fired.
35 * incorporate first/next tag routines ?
36 */
37
38
39 #ifndef NO_TREE
40
41 #include "bltTree.h"
42
43
44 static int IsTclDict(Tcl_Interp *interp,Tcl_Obj *objPtr) {
45 static Tcl_ObjType *dictType = NULL;
46
47 if (dictType == NULL) {
48 Tcl_Obj * obj;
49 obj = Tcl_NewDictObj();
•50 dictType = (Tcl_ObjType *)obj->typePtr;
51 Tcl_DecrRefCount(obj);
52 }
53 return (objPtr->typePtr == dictType);
54 }
55
56 /* 2 = per-tree key, 1 for per-interp, 0 for global (original behavior). */
57 int bltTreeUseLocalKeys = 0;
58
59 static Tcl_InterpDeleteProc TreeInterpDeleteProc;
60 static Blt_TreeApplyProc SizeApplyProc;
61 static Tcl_IdleProc NotifyIdleProc;
62
63 #define TREE_THREAD_KEY "BLT Tree Data"
64 #define TREE_MAGIC ((unsigned int) 0x46170277)
65 #define TREE_DESTROYED (1<<0)
66
67 typedef struct Blt_TreeNodeStruct Node;
68 typedef struct Blt_TreeClientStruct TreeClient;
69 typedef struct Blt_TreeObjectStruct TreeObject;
70 typedef struct Blt_TreeValueStruct Value;
71
72 /*
73 * Blt_TreeValue --
74 *
75 * Tree nodes contain heterogeneous data fields, represented as a
76 * chain of these structures. Each field contains the key of the
77 * field (Blt_TreeKey) and the value (Tcl_Obj) containing the
78 * actual data representations.
79 *
80 */
81 struct Blt_TreeValueStruct {
82 Blt_TreeKey key; /* String identifying the data field */
83 Tcl_Obj *objPtr; /* Data representation. */
84 Blt_Tree owner; /* Non-NULL if privately owned. */
85 Blt_TreeValue next; /* Next value in the chain. */
86 };
87
88 #include <stdio.h>
89 #include <string.h>
90 /* The following header is required for LP64 compilation */
91 #include <stdlib.h>
92
93 #include "bltHash.h"
94
95 static void TreeDestroyValues _ANSI_ARGS_((Blt_TreeNode node));
96
97 static Value *TreeFindValue _ANSI_ARGS_((Blt_TreeNode node,
98 Blt_TreeKey key));
99 static Value *TreeCreateValue _ANSI_ARGS_((Blt_TreeNode node,
100 Blt_TreeKey key, int *newPtr));
101
102 static int TreeDeleteValue _ANSI_ARGS_((Blt_TreeNode node,
103 Blt_TreeValue value));
104
105 static Value *TreeFirstValue _ANSI_ARGS_((Blt_TreeNode,
106 Blt_TreeKeySearch *searchPtr));
107
108 static Value *TreeNextValue _ANSI_ARGS_((Blt_TreeKeySearch *srchPtr));
109
110 /*
111 * When there are this many entries per bucket, on average, rebuild
112 * the hash table to make it larger.
113 */
114
115 #define REBUILD_MULTIPLIER 3
116
117 #if (SIZEOF_VOID_P == 8)
118 #define RANDOM_INDEX(i) HashOneWord(mask, downshift, i)
119 #define BITSPERWORD 64
•120 #define START_LOGSIZE 10
121 #define MAX_LIST_VALUES 40
122 #else
123
•124 #define START_LOGSIZE 5 /* Initial hash table size is 32. */
125 #define MAX_LIST_VALUES 21 /* Convert to hash table when node
126 * value list gets bigger than this
127 * many values. */
128
129 /*
130 * The following macro takes a preliminary integer hash value and
131 * produces an index into a hash tables bucket list. The idea is
132 * to make it so that preliminary values that are arbitrarily similar
133 * will end up in different buckets. The hash function was taken
134 * from a random-number generator.
135 */
136 #define RANDOM_INDEX(i) \
137 (((((long) (i))*1103515245) >> downshift) & mask)
138 #define BITSPERWORD 32
139 #endif
140
141 #define DOWNSHIFT_START (BITSPERWORD - 2)
142
143 /*
144 * Procedure prototypes for static procedures in this file:
145 */
146
147
148 #if (SIZEOF_VOID_P == 8)
149 static Blt_Hash HashOneWord _ANSI_ARGS_((uint64_t mask, unsigned int downshift,
150 CONST void *key));
151
152 #endif /* SIZEOF_VOID_P == 8 */
153
154 /*
155 * The hash table below is used to keep track of all the Blt_TreeKeys
156 * created so far.
157 */
158 static Blt_HashTable keyTable;
159 static int keyTableInitialized = 0;
160
161 typedef struct {
162 Blt_HashTable treeTable; /* Table of trees. */
163 unsigned int nextId;
164 Tcl_Interp *interp;
165 Blt_HashTable keyTable;
166 } TreeInterpData;
167
168 typedef struct {
169 Tcl_Interp *interp;
170 ClientData clientData;
171 Blt_TreeKey key;
172 unsigned int mask;
173 Blt_TreeNotifyEventProc *proc;
174 Blt_TreeNotifyEvent event;
175 int notifyPending;
176 } EventHandler;
177
178 typedef struct {
179 ClientData clientData;
180 char *keyPattern;
181 char *withTag;
182 Node *nodePtr;
183 unsigned int mask;
184 Blt_TreeTraceProc *proc;
185 TreeClient *clientPtr;
186 Blt_ChainLink *linkPtr;
187 } TraceHandler;
188
189 /*
190 * --------------------------------------------------------------
191 *
192 * GetTreeInterpData --
193 *
194 * Creates or retrieves data associated with tree data objects
195 * for a particular thread. We're using Tcl_GetAssocData rather
196 * than the Tcl thread routines so BLT can work with pre-8.0
197 * Tcl versions that don't have thread support.
198 *
199 * Results:
200 * Returns a pointer to the tree interpreter data.
201 *
202 * --------------------------------------------------------------
203 */
204 static TreeInterpData *
205 GetTreeInterpData(Tcl_Interp *interp)
206 {
207 Tcl_InterpDeleteProc *proc;
208 TreeInterpData *dataPtr;
209
210 dataPtr = (TreeInterpData *)
211 Tcl_GetAssocData(interp, TREE_THREAD_KEY, &proc);
212 if (dataPtr == NULL) {
213 dataPtr = Blt_Malloc(sizeof(TreeInterpData));
214 assert(dataPtr);
215 dataPtr->interp = interp;
216 Tcl_SetAssocData(interp, TREE_THREAD_KEY, TreeInterpDeleteProc,
217 dataPtr);
218 Blt_InitHashTable(&dataPtr->treeTable, BLT_STRING_KEYS);
219 Blt_InitHashTable(&dataPtr->keyTable, BLT_STRING_KEYS);
220 }
221 return dataPtr;
222 }
223
224 /*
225 * --------------------------------------------------------------
226 *
227 * NewNode --
228 *
229 * Creates a new node in the tree without installing it. The
230 * number of nodes in the tree is incremented and a unique serial
231 * number is generated for the node.
232 *
233 * Also, all nodes have a label. If no label was provided (name
234 * is NULL) then automatically generate a serial number for the node.
235 *
236 * Results:
237 * Returns a pointer to the new node.
238 *
239 * --------------------------------------------------------------
240 */
241 static Node *
242 NewNode(TreeObject *treeObjPtr, CONST char *name, int inode)
243 {
244 Node *nodePtr;
245
246 /* Create the node structure */
247 nodePtr = Blt_PoolAllocItem(treeObjPtr->nodePool, sizeof(Node));
248 nodePtr->inode = inode;
249 nodePtr->treeObject = treeObjPtr;
250 nodePtr->parent = NULL;
251 nodePtr->depth = 0;
252 nodePtr->flags = 0;
253 nodePtr->next = nodePtr->prev = NULL;
254 nodePtr->first = nodePtr->last = NULL;
255 nodePtr->nChildren = 0;
256 nodePtr->values = NULL;
257 nodePtr->logSize = 0;
258 nodePtr->nValues = 0;
259 nodePtr->label = NULL;
260 if (name != NULL) {
261 nodePtr->label = Blt_TreeKeyGet(NULL, treeObjPtr, name);
262 }
263 treeObjPtr->nNodes++;
264 return nodePtr;
265 }
266
267 /*
268 *----------------------------------------------------------------------
269 *
270 * ReleaseTagTable --
271 *
272 *----------------------------------------------------------------------
273 */
274 static void
275 ReleaseTagTable(Blt_TreeTagTable *tablePtr)
276 {
277 tablePtr->refCount--;
278 if (tablePtr->refCount <= 0) {
279 Blt_HashEntry *hPtr;
280 Blt_HashSearch cursor;
281
282 for(hPtr = Blt_FirstHashEntry(&tablePtr->tagTable, &cursor);
283 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
284 Blt_TreeTagEntry *tPtr;
285
286 tPtr = Blt_GetHashValue(hPtr);
287 Blt_DeleteHashTable(&tPtr->nodeTable);
288 Blt_TreeTagRefDecr(tPtr);
289 }
290 Blt_DeleteHashTable(&tablePtr->tagTable);
291 Blt_Free(tablePtr);
292 }
293 }
294
295 /*
296 * ----------------------------------------------------------------------
297 *
298 * ResetDepths --
299 *
300 * Called after moving a node, resets the depths of each node
301 * for the entire branch (node and it's decendants).
302 *
303 * Results:
304 * None.
305 *
306 * ----------------------------------------------------------------------
307 */
308 static void
309 ResetDepths(
310 Node *nodePtr, /* Root node. */
311 int depth) /* Depth of the node. */
312 {
313 nodePtr->depth = depth;
314 /* Also reset the depth for each descendant node. */
315 for (nodePtr = nodePtr->first; nodePtr != NULL; nodePtr = nodePtr->next) {
316 ResetDepths(nodePtr, depth + 1);
317 }
318 }
319
320 /*
321 *----------------------------------------------------------------------
322 *
323 * LinkBefore --
324 *
325 * Inserts a link preceding a given link.
326 *
327 * Results:
328 * None.
329 *
330 *----------------------------------------------------------------------
331 */
332 static void
333 LinkBefore(
334 Node *parentPtr, /* Parent to hold the new entry. */
335 Node *nodePtr, /* New node to be inserted. */
336 Node *beforePtr) /* Node to link before. */
337 {
338 if (parentPtr->first == NULL) {
339 parentPtr->last = parentPtr->first = nodePtr;
340 } else if (beforePtr == NULL) { /* Append onto the end of the chain */
341 nodePtr->next = NULL;
342 nodePtr->prev = parentPtr->last;
343 parentPtr->last->next = nodePtr;
344 parentPtr->last = nodePtr;
345 } else {
346 nodePtr->prev = beforePtr->prev;
347 nodePtr->next = beforePtr;
348 if (beforePtr == parentPtr->first) {
349 parentPtr->first = nodePtr;
350 } else {
351 beforePtr->prev->next = nodePtr;
352 }
353 beforePtr->prev = nodePtr;
354 }
355 parentPtr->nChildren++;
356 nodePtr->parent = parentPtr;
357 }
358
359
360 /*
361 *----------------------------------------------------------------------
362 *
363 * UnlinkNode --
364 *
365 * Unlinks a link from the chain. The link is not deallocated,
366 * but only removed from the chain.
367 *
368 * Results:
369 * None.
370 *
371 *----------------------------------------------------------------------
372 */
373 static void
374 UnlinkNode(Node *nodePtr)
375 {
376 Node *parentPtr;
377 int unlinked; /* Indicates if the link is actually
378 * removed from the chain. */
379 parentPtr = nodePtr->parent;
380 unlinked = FALSE;
381 if (parentPtr->first == nodePtr) {
382 parentPtr->first = nodePtr->next;
383 unlinked = TRUE;
384 }
385 if (parentPtr->last == nodePtr) {
386 parentPtr->last = nodePtr->prev;
387 unlinked = TRUE;
388 }
389 if (nodePtr->next != NULL) {
390 nodePtr->next->prev = nodePtr->prev;
391 unlinked = TRUE;
392 }
393 if (nodePtr->prev != NULL) {
394 nodePtr->prev->next = nodePtr->next;
395 unlinked = TRUE;
396 }
397 if (unlinked) {
398 parentPtr->nChildren--;
399 }
400 nodePtr->prev = nodePtr->next = nodePtr->parent = NULL;
401 }
402
403 /*
404 * --------------------------------------------------------------
405 *
406 * FreeNode --
407 *
408 * Unlinks a given node from the tree, removes its data, and
409 * frees memory allocated to the node.
410 *
411 * Results:
412 * None.
413 *
414 * --------------------------------------------------------------
415 */
416 static void
417 FreeNode(TreeObject *treeObjPtr, Node *nodePtr)
418 {
419 Blt_HashEntry *hPtr;
420
421 /*
422 * Destroy any data fields associated with this node.
423 */
424 TreeDestroyValues(nodePtr);
425 UnlinkNode(nodePtr);
426 treeObjPtr->nNodes--;
•427 hPtr = Blt_FindHashEntry(&treeObjPtr->nodeTable, (char *)(intptr_t)nodePtr->inode);
428 assert(hPtr);
429 Blt_DeleteHashEntry(&treeObjPtr->nodeTable, hPtr);
430 nodePtr->inode = -1;
431 nodePtr->flags = 0;
432 Blt_PoolFreeItem(treeObjPtr->nodePool, (char *)nodePtr);
433 }
434
435 /*
436 * --------------------------------------------------------------
437 *
438 * NewTreeObject --
439 *
440 * Creates and initializes a new tree object. Trees always
441 * contain a root node, so one is allocated here.
442 *
443 * Results:
444 * Returns a pointer to the new tree object is successful, NULL
445 * otherwise. If a tree can't be generated, interp->result will
446 * contain an error message.
447 *
448 * -------------------------------------------------------------- */
449 static TreeObject *
450 NewTreeObject(TreeInterpData *dataPtr, Tcl_Interp *interp, CONST char *treeName)
451 {
452 TreeObject *treeObjPtr;
453 int isNew;
454 Blt_HashEntry *hPtr;
455
456 treeObjPtr = Blt_Calloc(1, sizeof(TreeObject));
457 if (treeObjPtr == NULL) {
458 if (interp != NULL)
459 Tcl_AppendResult(interp, "can't allocate tree", (char *)NULL);
460 return NULL;
461 }
462 treeObjPtr->name = Blt_Strdup(treeName);
463 treeObjPtr->interp = interp;
464 treeObjPtr->valuePool = Blt_PoolCreate(BLT_FIXED_SIZE_ITEMS);
465 treeObjPtr->nodePool = Blt_PoolCreate(BLT_FIXED_SIZE_ITEMS);
466 treeObjPtr->clients = Blt_ChainCreate();
467 treeObjPtr->depth = 1;
468 treeObjPtr->flags = 0;
469 treeObjPtr->maxKeyList = 0;
470 if (bltTreeUseLocalKeys) {
471 if (bltTreeUseLocalKeys>1) {
472 treeObjPtr->interpKeyPtr = &treeObjPtr->keyTable;
473 } else {
474 treeObjPtr->interpKeyPtr = &dataPtr->keyTable;
475 }
476 }
477 treeObjPtr->notifyFlags = 0;
478 Blt_InitHashTable(&treeObjPtr->keyTable, BLT_STRING_KEYS);
479 Blt_InitHashTableWithPool(&treeObjPtr->nodeTable, BLT_ONE_WORD_KEYS);
480
481 hPtr = Blt_CreateHashEntry(&treeObjPtr->nodeTable, (char *)0, &isNew);
482 treeObjPtr->root = NewNode(treeObjPtr, treeName, 0);
483 Blt_SetHashValue(hPtr, treeObjPtr->root);
484
485 treeObjPtr->tablePtr = &dataPtr->treeTable;
486 treeObjPtr->hashPtr = Blt_CreateHashEntry(treeObjPtr->tablePtr, treeName,
487 &isNew);
488 Blt_SetHashValue(treeObjPtr->hashPtr, treeObjPtr);
489
490 return treeObjPtr;
491 }
492
493 static TreeObject *
494 FindTreeInNamespace(
495 TreeInterpData *dataPtr, /* Interpreter-specific data. */
496 Tcl_Namespace *nsPtr,
497 CONST char *treeName)
498 {
499 Tcl_DString dString;
500 char *name;
501 Blt_HashEntry *hPtr;
502
503 name = Blt_GetQualifiedName(nsPtr, treeName, &dString);
504 hPtr = Blt_FindHashEntry(&dataPtr->treeTable, name);
505 Tcl_DStringFree(&dString);
506 if (hPtr != NULL) {
507 return Blt_GetHashValue(hPtr);
508 }
509 return NULL;
510 }
511
512 /*
513 * ----------------------------------------------------------------------
514 *
515 * GetTreeObject --
516 *
517 * Searches for the tree object associated by the name given.
518 *
519 * Results:
520 * Returns a pointer to the tree if found, otherwise NULL.
521 *
522 * ----------------------------------------------------------------------
523 */
524 static TreeObject *
525 GetTreeObject(Tcl_Interp *interp, CONST char *name, int flags)
526 {
527 CONST char *treeName;
528 Tcl_Namespace *nsPtr; /* Namespace associated with the tree object.
529 * If NULL, indicates to look in first the
530 * current namespace and then the global
531 * for the tree. */
532 TreeInterpData *dataPtr; /* Interpreter-specific data. */
533 TreeObject *treeObjPtr;
534
535 treeObjPtr = NULL;
536 if (Blt_ParseQualifiedName(interp, name, &nsPtr, &treeName) != TCL_OK) {
537 if (interp != NULL)
538 Tcl_AppendResult(interp, "can't find namespace in \"", name, "\"",
539 (char *)NULL);
540 return NULL;
541 }
542 dataPtr = GetTreeInterpData(interp);
543 if (nsPtr != NULL) {
544 treeObjPtr = FindTreeInNamespace(dataPtr, nsPtr, treeName);
545 } else {
546 if (flags & NS_SEARCH_CURRENT) {
547 /* Look first in the current namespace. */
548 nsPtr = Tcl_GetCurrentNamespace(interp);
549 treeObjPtr = FindTreeInNamespace(dataPtr, nsPtr, treeName);
550 }
551 if ((treeObjPtr == NULL) && (flags & NS_SEARCH_GLOBAL)) {
552 nsPtr = Tcl_GetGlobalNamespace(interp);
553 treeObjPtr = FindTreeInNamespace(dataPtr, nsPtr, treeName);
554 }
555 }
556 return treeObjPtr;
557 }
558
559 /*
560 * ----------------------------------------------------------------------
561 *
562 * TeardownTree --
563 *
564 * Destroys an entire branch. This is a special purpose routine
565 * used to speed up the final clean up of the tree.
566 *
567 * Results:
568 * None.
569 *
570 * ----------------------------------------------------------------------
571 */
572 static void
573 TeardownTree(TreeObject *treeObjPtr, Node *nodePtr)
574 {
575 if (nodePtr->first != NULL) {
576 Node *childPtr, *nextPtr;
577
578 for (childPtr = nodePtr->first; childPtr != NULL; childPtr = nextPtr) {
579 nextPtr = childPtr->next;
580 TeardownTree(treeObjPtr, childPtr);
581 }
582 }
583 if (nodePtr->values != NULL) {
584 TreeDestroyValues(nodePtr);
585 }
586 Blt_PoolFreeItem(treeObjPtr->nodePool, (char *)nodePtr);
587 }
588
589 static void
590 DestroyTreeObject(char *treeObj)
591 {
592 Blt_ChainLink *linkPtr;
593 TreeClient *clientPtr;
594 TreeObject *treeObjPtr = (TreeObject*)treeObj;
595
596 if (treeObjPtr->flags & TREE_DESTROYED) return;
597 treeObjPtr->flags |= TREE_DESTROYED;
598 treeObjPtr->nNodes = 0;
599
600 /* Remove the remaining clients. */
601 for (linkPtr = Blt_ChainFirstLink(treeObjPtr->clients); linkPtr != NULL;
602 linkPtr = Blt_ChainNextLink(linkPtr)) {
603 clientPtr = Blt_ChainGetValue(linkPtr);
604 Blt_ChainDestroy(clientPtr->events);
605 Blt_ChainDestroy(clientPtr->traces);
606 Blt_Free(clientPtr);
607 }
608 Blt_ChainDestroy(treeObjPtr->clients);
609
610 TeardownTree(treeObjPtr, treeObjPtr->root);
611 Blt_PoolDestroy(treeObjPtr->nodePool);
612 Blt_PoolDestroy(treeObjPtr->valuePool);
613 Blt_DeleteHashTable(&treeObjPtr->nodeTable);
614 Blt_DeleteHashTable(&treeObjPtr->keyTable);
615
616 if (treeObjPtr->hashPtr != NULL) {
617 /* Remove the entry from the global tree table. */
618 Blt_DeleteHashEntry(treeObjPtr->tablePtr, treeObjPtr->hashPtr);
619 if ((treeObjPtr->tablePtr->numEntries == 0) && (keyTableInitialized)) {
620 keyTableInitialized = FALSE;
621 Blt_DeleteHashTable(&keyTable);
622 }
623 }
624 if (treeObjPtr->name != NULL) {
625 Blt_Free(treeObjPtr->name);
626 }
627 Blt_Free(treeObjPtr);
628 }
629
630 /*
631 * -----------------------------------------------------------------------
632 *
633 * TreeInterpDeleteProc --
634 *
635 * This is called when the interpreter hosting the tree object
636 * is deleted from the interpreter.
637 *
638 * Results:
639 * None.
640 *
641 * Side effects:
642 * Destroys all remaining trees and removes the hash table
643 * used to register tree names.
644 *
645 * ------------------------------------------------------------------------
646 */
647 /* ARGSUSED */
648 static void
649 TreeInterpDeleteProc(
650 ClientData clientData, /* Interpreter-specific data. */
651 Tcl_Interp *interp)
652 {
653 TreeInterpData *dataPtr = clientData;
654 Blt_HashEntry *hPtr;
655 Blt_HashSearch cursor;
656 TreeObject *treeObjPtr;
657
658 for (hPtr = Blt_FirstHashEntry(&dataPtr->treeTable, &cursor);
659 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
660 treeObjPtr = (TreeObject *)Blt_GetHashValue(hPtr);
661 treeObjPtr->hashPtr = NULL;
662 treeObjPtr->delete = 1;
663 Tcl_EventuallyFree(treeObjPtr, DestroyTreeObject);
664 /*DestroyTreeObject(treeObjPtr); */
665 }
666 if (keyTableInitialized) {
667 keyTableInitialized = FALSE;
668 Blt_DeleteHashTable(&keyTable);
669 }
670 Blt_DeleteHashTable(&dataPtr->treeTable);
671 Blt_DeleteHashTable(&dataPtr->keyTable);
672 Tcl_DeleteAssocData(interp, TREE_THREAD_KEY);
673 Blt_Free(dataPtr);
674 }
675
676 /*
677 *----------------------------------------------------------------------
678 *
679 * NotifyIdleProc --
680 *
681 * Used to invoke event handler routines at some idle point.
682 * This routine is called from the Tcl event loop. Errors
683 * generated by the event handler routines are backgrounded.
684 *
685 * Results:
686 * None.
687 *
688 *----------------------------------------------------------------------
689 */
690 static void
691 NotifyIdleProc(ClientData clientData)
692 {
693 EventHandler *notifyPtr = clientData;
694 int result;
695
696 notifyPtr->notifyPending = FALSE;
697 notifyPtr->mask |= TREE_NOTIFY_ACTIVE;
698 result = (*notifyPtr->proc)(notifyPtr->clientData, &notifyPtr->event);
699 notifyPtr->mask &= ~TREE_NOTIFY_ACTIVE;
700 if (result != TCL_OK) {
701 Tcl_BackgroundError(notifyPtr->interp);
702 }
703 }
704
705 /*
706 *----------------------------------------------------------------------
707 *
708 * CheckEventHandlers --
709 *
710 * Traverses the list of client event callbacks and checks
711 * if one matches the given event. A client may trigger an
712 * action that causes the tree to notify it. The can be
713 * prevented by setting the TREE_NOTIFY_FOREIGN_ONLY bit in
714 * the event handler.
715 *
716 * If a matching handler is found, a callback may be called either
717 * immediately or at the next idle time depending upon the
718 * TREE_NOTIFY_WHENIDLE bit.
719 *
720 * Since a handler routine may trigger yet another call to
721 * itself, callbacks are ignored while the event handler is
722 * executing.
723 *
724 * Results:
725 * None.
726 *
727 *----------------------------------------------------------------------
728 */
729 static int
730 CheckEventHandlers(
731 TreeClient *clientPtr,
732 int isSource, /* Indicates if the client is the source
733 * of the event. */
734 Blt_TreeNotifyEvent *eventPtr)
735 {
736 Blt_ChainLink *linkPtr, *nextPtr;
737 EventHandler *notifyPtr;
738
739 eventPtr->tree = clientPtr;
740 for (linkPtr = Blt_ChainFirstLink(clientPtr->events);
741 linkPtr != NULL; linkPtr = nextPtr) {
742 nextPtr = Blt_ChainNextLink(linkPtr);
743 notifyPtr = Blt_ChainGetValue(linkPtr);
744 if ((notifyPtr->mask & TREE_NOTIFY_ACTIVE) ||
745 (notifyPtr->mask & eventPtr->type) == 0) {
746 continue; /* Ignore callbacks that are generated
747 * inside of a notify handler routine. */
748 }
749 if ((isSource) && (notifyPtr->mask & TREE_NOTIFY_FOREIGN_ONLY)) {
750 continue; /* Don't notify yourself. */
751 }
752 if (notifyPtr->mask & TREE_NOTIFY_WHENIDLE) {
753 if (!notifyPtr->notifyPending) {
754 notifyPtr->notifyPending = TRUE;
755 notifyPtr->event = *eventPtr;
756 Tcl_DoWhenIdle(NotifyIdleProc, notifyPtr);
757 }
758 } else {
759 int result;
760
761 notifyPtr->mask |= TREE_NOTIFY_ACTIVE;
762 result = (*notifyPtr->proc) (notifyPtr->clientData, eventPtr);
763 notifyPtr->mask &= ~TREE_NOTIFY_ACTIVE;
764 if (result != TCL_OK) {
765 if (notifyPtr->mask & TREE_NOTIFY_BGERROR) {
766 Tcl_BackgroundError(notifyPtr->interp);
767 }
768 return result;
769 }
770 }
771 }
772 return TCL_OK;
773 }
774
775 /*
776 *----------------------------------------------------------------------
777 *
778 * NotifyClients --
779 *
780 * Traverses the list of clients for a particular tree and
781 * notifies each client that an event occurred. Clients
782 * indicate interest in a particular event through a bit
783 * flag.
784 *
785 *----------------------------------------------------------------------
786 */
787 static int
788 NotifyClients(
789 TreeClient *sourcePtr,
790 TreeObject *treeObjPtr,
791 Node *nodePtr,
792 int eventFlag)
793 {
794 Blt_ChainLink *linkPtr;
795 Blt_TreeNotifyEvent event;
796 TreeClient *clientPtr;
797 int isSource, result;
798
799 if (Tcl_InterpDeleted(treeObjPtr->interp) ||
800 Tcl_InterpDeleted(sourcePtr->root->treeObject->interp)) {
801 return TCL_OK;
802 }
803 event.type = eventFlag;
804 event.inode = nodePtr->inode;
805
806 /*
807 * Issue callbacks to each client indicating that a new node has
808 * been created.
809 */
810 for (linkPtr = Blt_ChainFirstLink(treeObjPtr->clients);
811 linkPtr != NULL; linkPtr = Blt_ChainNextLink(linkPtr)) {
812 clientPtr = Blt_ChainGetValue(linkPtr);
813 isSource = (clientPtr == sourcePtr);
814 result = CheckEventHandlers(clientPtr, isSource, &event);
815 if (result != TCL_OK) {
816 return TCL_ERROR;
817 }
818 if (Blt_TreeNodeDeleted(nodePtr) || nodePtr->inode != event.inode) {
819 return TCL_BREAK;
820 }
821 }
822 return TCL_OK;
823 }
824
825 static void
826 FreeValue(Node *nodePtr, Value *valuePtr)
827 {
828 if (valuePtr->objPtr != NULL) {
829 Tcl_DecrRefCount(valuePtr->objPtr);
830 }
831 Blt_PoolFreeItem(nodePtr->treeObject->valuePool, valuePtr);
832 }
833
834
835 /* Public Routines */
836 /*
837 *----------------------------------------------------------------------
838 *
839 * Blt_TreeGetKey --
840 *
841 * Given a string, returns a unique identifier for the string.
842 *
843 *----------------------------------------------------------------------
844 */
845 Blt_TreeKey
846 Blt_TreeGetKey(string)
847 CONST char *string; /* String to convert. */
848 {
849 Blt_HashEntry *hPtr;
850 int isNew;
851
852 if (!keyTableInitialized) {
853 Blt_InitHashTable(&keyTable, BLT_STRING_KEYS);
854 keyTableInitialized = 1;
855 }
856 hPtr = Blt_CreateHashEntry(&keyTable, string, &isNew);
857 return (Blt_TreeKey)Blt_GetHashKey(&keyTable, hPtr);
858 }
859
860 /*
861 *----------------------------------------------------------------------
862 *
863 * Blt_TreeKeyGet --
864 *
865 * tree-unique keys.
866 * TODO: use per-interp keyTable rather than global one.
867 *
868 *----------------------------------------------------------------------
869 */
870 Blt_TreeKey
871 Blt_TreeKeyGet(interp, treeObjPtr, string)
872 Tcl_Interp *interp;
873 TreeObject *treeObjPtr;
874 CONST char *string; /* String to convert. */
875 {
876 Blt_HashTable *tablePtr;
877 Blt_HashEntry *hPtr;
878 int isNew;
879
880 tablePtr = NULL;
881 if (treeObjPtr != NULL && treeObjPtr->interpKeyPtr != NULL) {
882 tablePtr = treeObjPtr->interpKeyPtr;
883 }
884 if (tablePtr == NULL && interp != NULL && bltTreeUseLocalKeys != 0) {
885 TreeInterpData *dataPtr;
886 dataPtr = GetTreeInterpData(interp);
887 tablePtr = &dataPtr->keyTable;
888 }
889 if (tablePtr == NULL) {
890 return Blt_TreeGetKey(string);
891 }
892 hPtr = Blt_CreateHashEntry(tablePtr, string, &isNew);
893 return (Blt_TreeKey)Blt_GetHashKey(tablePtr, hPtr);
894 }
895
896 /* Clear the unmodified flag for node. */
897 static void SetModified(Node *nodePtr)
898 {
899 nodePtr->flags &= ~TREE_NODE_UNMODIFIED;
900 nodePtr->treeObject->flags &= ~TREE_UNMODIFIED;
901 }
902
903 /*
904 *----------------------------------------------------------------------
905 *
906 * Blt_TreeCreateNode --
907 *
908 * Creates a new node in the given parent node. The name and
909 * position in the parent are also provided.
910 *
911 *----------------------------------------------------------------------
912 */
913 Blt_TreeNode
914 Blt_TreeCreateNode(
915 TreeClient *clientPtr, /* The tree client that is creating
916 * this node. If NULL, indicates to
917 * trigger notify events on behalf of
918 * the initiating client also. */
919 Node *parentPtr, /* Parent node where the new node will
920 * be inserted. */
921 CONST char *name, /* Name of node. */
922 int pos) /* Position in the parent's list of children
923 * where to insert the new node. */
924 {
925 Blt_HashEntry *hPtr;
926 Node *beforePtr;
927 Node *nodePtr; /* Node to be inserted. */
928 TreeObject *treeObjPtr;
929 int inode;
930 int isNew;
931
932 treeObjPtr = parentPtr->treeObject;
933
934 /* Generate an unique serial number for this node. */
935 do {
936 inode = treeObjPtr->nextInode++;
•937 hPtr = Blt_CreateHashEntry(&treeObjPtr->nodeTable,(char *)(intptr_t)inode,
938 &isNew);
939 } while (!isNew);
940 nodePtr = NewNode(treeObjPtr, name, inode);
941 Blt_SetHashValue(hPtr, nodePtr);
942
943 if ((pos == -1) || (pos >= (int)parentPtr->nChildren)) {
944 beforePtr = NULL;
945 } else {
946 beforePtr = parentPtr->first;
947 while ((pos > 0) && (beforePtr != NULL)) {
948 pos--;
949 beforePtr = beforePtr->next;
950 }
951 }
952 LinkBefore(parentPtr, nodePtr, beforePtr);
953 nodePtr->depth = parentPtr->depth + 1;
954 /*
955 * Issue callbacks to each client indicating that a new node has
956 * been created.
957 */
958 if (NotifyClients(clientPtr, treeObjPtr, nodePtr, TREE_NOTIFY_CREATE) != TCL_OK) {
959 nodePtr->flags |= TREE_NODE_INSERT_FAIL;
960 Blt_TreeDeleteNode(clientPtr, nodePtr);
961 return NULL;
962 }
963 treeObjPtr->flags &= ~TREE_UNMODIFIED;
964 return nodePtr;
965 }
966 /*
967 *----------------------------------------------------------------------
968 *
969 * Blt_TreeInsertPost --
970 *
971 * Trigger notify for -insert
972 *
973 *----------------------------------------------------------------------
974 */
975 Blt_TreeNode
976 Blt_TreeInsertPost(
977 TreeClient *clientPtr, /* The tree client that is creating
978 * this node. If NULL, indicates to
979 * trigger notify events on behalf of
980 * the initiating client also. */
981 Node *nodePtr) /* node */
982 {
983 if (NotifyClients(clientPtr, nodePtr->treeObject, nodePtr, TREE_NOTIFY_INSERT) != TCL_OK) {
984 nodePtr->flags |= TREE_NODE_INSERT_FAIL;
985 Blt_TreeDeleteNode(clientPtr, nodePtr);
986 return NULL;
987 }
988 return nodePtr;
989 }
990
991 int
992 Blt_TreeNotifyGet(
993 TreeClient *clientPtr, /* The tree client that is creating
994 * this node. If NULL, indicates to
995 * trigger notify events on behalf of
996 * the initiating client also. */
997 Node *nodePtr) /* node */
998 {
999 if (nodePtr->nValues != 0) return TCL_OK;
1000 return NotifyClients(clientPtr, nodePtr->treeObject, nodePtr, TREE_NOTIFY_GET);
1001 }
1002
1003 /*
1004 *----------------------------------------------------------------------
1005 *
1006 * Blt_TreeCreateNodeWithId --
1007 *
1008 * Like Blt_TreeCreateNode, but provides a specific id to use
1009 * for the node. If the tree already contains a node by that
1010 * id, NULL is returned.
1011 *
1012 *----------------------------------------------------------------------
1013 */
1014 Blt_TreeNode
1015 Blt_TreeCreateNodeWithId(
1016 TreeClient *clientPtr,
1017 Node *parentPtr, /* Parent node where the new node will
1018 * be inserted. */
1019 CONST char *name, /* Name of node. */
1020 unsigned int inode,/* Requested id of the new node. If a
1021 * node by this id already exists in the
1022 * tree, no node is created. */
1023 int position) /* Position in the parent's list of children
1024 * where to insert the new node. */
1025 {
1026 Blt_HashEntry *hPtr;
1027 Node *beforePtr;
1028 Node *nodePtr; /* Node to be inserted. */
1029 TreeObject *treeObjPtr;
1030 int isNew, result;
1031
1032 treeObjPtr = parentPtr->treeObject;
•1033 hPtr = Blt_CreateHashEntry(&treeObjPtr->nodeTable,(char *)(intptr_t)inode, &isNew);
1034 if (!isNew) {
1035 return NULL;
1036 }
1037 nodePtr = NewNode(treeObjPtr, name, inode);
1038 Blt_SetHashValue(hPtr, nodePtr);
1039
1040 if ((position == -1) || (position >= (int)parentPtr->nChildren)) {
1041 beforePtr = NULL;
1042 } else {
1043 beforePtr = parentPtr->first;
1044 while ((position > 0) && (beforePtr != NULL)) {
1045 position--;
1046 beforePtr = beforePtr->next;
1047 }
1048 }
1049 LinkBefore(parentPtr, nodePtr, beforePtr);
1050 nodePtr->depth = parentPtr->depth + 1;
1051 /*
1052 * Issue callbacks to each client indicating that a new node has
1053 * been created.
1054 */
1055 result=NotifyClients(clientPtr, treeObjPtr, nodePtr, TREE_NOTIFY_CREATE);
1056 if (result != TCL_OK) {
1057 if (result != TCL_BREAK) {
1058 nodePtr->flags |= TREE_NODE_INSERT_FAIL;
1059 Blt_TreeDeleteNode(clientPtr, nodePtr);
1060 }
1061 return NULL;
1062 }
1063 treeObjPtr->flags &= ~TREE_UNMODIFIED;
1064 return nodePtr;
1065 }
1066
1067 /*
1068 *----------------------------------------------------------------------
1069 *
1070 * Blt_TreeMoveNode --
1071 *
1072 * Move an entry into a new location in the hierarchy.
1073 *
1074 *----------------------------------------------------------------------
1075 */
1076 /*ARGSUSED*/
1077 int
1078 Blt_TreeMoveNode(
1079 TreeClient *clientPtr,
1080 Node *nodePtr,
1081 Node *parentPtr,
1082 Node *beforePtr)
1083 {
1084 TreeObject *treeObjPtr = nodePtr->treeObject;
1085 int newDepth, result;
1086
1087 if (nodePtr == beforePtr) {
1088 return TCL_ERROR;
1089 }
1090 if ((beforePtr != NULL) && (beforePtr->parent != parentPtr)) {
1091 return TCL_ERROR;
1092 }
1093 if (nodePtr->parent == NULL) {
1094 return TCL_ERROR; /* Can't move root. */
1095 }
1096 /* Verify that the node isn't an ancestor of the new parent. */
1097 if (Blt_TreeIsAncestor(nodePtr, parentPtr)) {
1098 return TCL_ERROR;
1099 }
1100 /*
1101 * Issue callbacks to each client indicating that a node has
1102 * been moved.
1103 */
1104 if (NotifyClients(clientPtr, treeObjPtr, nodePtr, TREE_NOTIFY_MOVE) != TCL_OK) {
1105 return TCL_ERROR;
1106 }
1107
1108 UnlinkNode(nodePtr);
1109 /*
1110 * Relink the node as a child of the new parent.
1111 */
1112 LinkBefore(parentPtr, nodePtr, beforePtr);
1113 newDepth = parentPtr->depth + 1;
1114 if (nodePtr->depth != newDepth) {
1115 /* Reset the depths of all descendant nodes. */
1116 ResetDepths(nodePtr, newDepth);
1117 }
1118 /*
1119 * Issue callbacks to each client indicating that a node has
1120 * been moved.
1121 */
1122 if ((result=NotifyClients(clientPtr, treeObjPtr, nodePtr, TREE_NOTIFY_MOVEPOST)) != TCL_OK) {
1123 return result;
1124 }
1125
1126 return TCL_OK;
1127 }
1128
1129 int
1130 Blt_TreeDeleteNode(TreeClient *clientPtr, Node *nodePtr)
1131 {
1132 TreeObject *treeObjPtr = nodePtr->treeObject;
1133 Node *childPtr, *nextPtr;
1134 int result;
1135
1136 /*
1137 * Issue callbacks to each client indicating that the node can
1138 * no longer be used.
1139 */
1140 if (Blt_TreeNodeDeleted(nodePtr)) {
1141 return TCL_OK;
1142 }
1143 if ((nodePtr->flags & TREE_NODE_INSERT_FAIL) == 0 &&
1144 (result=NotifyClients(clientPtr, treeObjPtr, nodePtr, TREE_NOTIFY_DELETE)) != TCL_OK) {
1145 return result;
1146 }
1147 nodePtr->flags &= ~TREE_NODE_FIXED_FIELDS;
1148
1149 /* In depth-first order, delete each descendant node. */
1150 for (childPtr = nodePtr->first; childPtr != NULL; childPtr = nextPtr) {
1151 nextPtr = childPtr->next;
1152 Blt_TreeDeleteNode(clientPtr, childPtr);
1153 }
1154
1155 /* Now remove the actual node. */
1156 FreeNode(treeObjPtr, nodePtr);
1157 if (treeObjPtr->nodeTable.numEntries <= 1) {
1158 treeObjPtr->nextInode = 1;
1159 }
1160 return TCL_OK;
1161 }
1162
1163 Blt_TreeNode
1164 Blt_TreeGetNode(TreeClient *clientPtr, unsigned int inode)
1165 {
1166 TreeObject *treeObjPtr = clientPtr->treeObject;
1167 Blt_HashEntry *hPtr;
1168
•1169 hPtr = Blt_FindHashEntry(&treeObjPtr->nodeTable, (char *)(intptr_t)inode);
1170 if (hPtr != NULL) {
1171 return (Blt_TreeNode)Blt_GetHashValue(hPtr);
1172 }
1173 return NULL;
1174 }
1175
1176 /*
1177 static Node*
1178 GetNode(TreeObject *treeObjPtr, unsigned int inode)
1179 {
1180 Blt_HashEntry *hPtr;
1181
•1182 hPtr = Blt_FindHashEntry(&treeObjPtr->nodeTable, (char *)(intptr_t)inode);
1183 if (hPtr != NULL) {
1184 return (Node*)Blt_GetHashValue(hPtr);
1185 }
1186 return NULL;
1187 }
1188 */
1189
1190 Blt_TreeTrace
1191 Blt_TreeCreateTrace(
1192 TreeClient *clientPtr,
1193 Node *nodePtr,
1194 CONST char *keyPattern,
1195 CONST char *tagName,
1196 unsigned int mask,
1197 Blt_TreeTraceProc *proc,
1198 ClientData clientData)
1199 {
1200 TraceHandler *tracePtr;
1201
1202 tracePtr = Blt_Calloc(1, sizeof (TraceHandler));
1203 assert(tracePtr);
1204 tracePtr->linkPtr = Blt_ChainAppend(clientPtr->traces, tracePtr);
1205 if (keyPattern != NULL) {
1206 tracePtr->keyPattern = Blt_Strdup(keyPattern);
1207 }
1208 if (tagName != NULL) {
1209 tracePtr->withTag = Blt_Strdup(tagName);
1210 }
1211 tracePtr->clientPtr = clientPtr;
1212 tracePtr->proc = proc;
1213 tracePtr->clientData = clientData;
1214 tracePtr->mask = mask;
1215 tracePtr->nodePtr = nodePtr;
1216 return (Blt_TreeTrace)tracePtr;
1217 }
1218
1219 void
1220 Blt_TreeDeleteTrace(Blt_TreeTrace trace)
1221 {
1222 TraceHandler *tracePtr = (TraceHandler *)trace;
1223
1224 Blt_ChainDeleteLink(tracePtr->clientPtr->traces, tracePtr->linkPtr);
1225 if (tracePtr->keyPattern != NULL) {
1226 Blt_Free(tracePtr->keyPattern);
1227 }
1228 if (tracePtr->withTag != NULL) {
1229 Blt_Free(tracePtr->withTag);
1230 }
1231 Blt_Free(tracePtr);
1232 }
1233
1234 int
1235 Blt_TreeRelabelNode(TreeClient *clientPtr, Node *nodePtr, CONST char *string)
1236 {
•1237 int result;
1238 if ((result=NotifyClients(clientPtr, clientPtr->treeObject, nodePtr,
1239 TREE_NOTIFY_RELABEL)) != TCL_OK) {
1240 return result;
1241 }
1242 nodePtr->label = Blt_TreeKeyGet(NULL, clientPtr->treeObject,string);
1243 /*
1244 * Issue callbacks to each client indicating that a new node has
1245 * been created.
1246 */
1247 SetModified(nodePtr);
1248 result = NotifyClients(clientPtr, clientPtr->treeObject, nodePtr,
1249 TREE_NOTIFY_RELABELPOST);
1250 return result;
1251 }
1252
1253 int
1254 Blt_TreeNotifyAttach(TreeClient *clientPtr)
1255 {
1256 /* return NotifyClients(clientPtr, clientPtr->treeObject, clientPtr->root,
1257 TREE_NOTIFY_ATTACH); */
1258 return TCL_OK;
1259 }
1260
1261
1262 int
1263 Blt_TreeRelabelNode2(Node *nodePtr, CONST char *string)
1264 {
1265 nodePtr->label = Blt_TreeKeyGet(NULL, nodePtr->treeObject,string);
1266 SetModified(nodePtr);
1267 return TCL_OK;
1268 }
1269
1270 /*
1271 *----------------------------------------------------------------------
1272 *
1273 * Blt_TreeFindChild --
1274 *
1275 * Searches for the named node in a parent's chain of siblings.
1276 *
1277 *
1278 * Results:
1279 * If found, the child node is returned, otherwise NULL.
1280 *
1281 *----------------------------------------------------------------------
1282 */
1283 Blt_TreeNode
1284 Blt_TreeFindChild(Node *parentPtr, CONST char *string)
1285 {
1286 Blt_TreeKey label;
1287 register Node *nodePtr;
1288
1289 label = Blt_TreeKeyGet(NULL, parentPtr->treeObject,string);
1290 for (nodePtr = parentPtr->first; nodePtr != NULL; nodePtr = nodePtr->next) {
1291 if (label == nodePtr->label) {
1292 return nodePtr;
1293 }
1294 }
1295 return NULL;
1296 }
1297 /*
1298 * Like Blt_TreeFindChild above, but looks forward firstN elements
1299 * from front, then falls back to reverse search starting from the end.
1300 * This speeds up insertion at the end of really long trees in treeview.
1301 * If firstN is < 0, just calls Blt_TreeFindChild.
1302 */
1303 Blt_TreeNode
1304 Blt_TreeFindChildRev(Node *parentPtr, CONST char *string, int firstN)
1305 {
1306 Blt_TreeKey label;
1307 register Node *nodePtr, *endNode;
1308 int n;
1309
1310 if (firstN<0) {
1311 return Blt_TreeFindChild(parentPtr, string);
1312 }
1313 label = Blt_TreeKeyGet(NULL, parentPtr->treeObject,string);
1314 for (nodePtr = parentPtr->first, n = 0;
1315 nodePtr != NULL && n < firstN;
1316 nodePtr = nodePtr->next, n++) {
1317 if (label == nodePtr->label) {
1318 return nodePtr;
1319 }
1320 }
1321 if (nodePtr == NULL) {
1322 return NULL;
1323 }
1324 endNode = nodePtr;
1325 for (nodePtr = parentPtr->last; nodePtr != NULL; nodePtr = nodePtr->prev) {
1326 if (label == nodePtr->label) {
1327 return nodePtr;
1328 }
1329 if (nodePtr == endNode) break;
1330 }
1331 return NULL;
1332 }
1333
1334 /*
1335 *----------------------------------------------------------------------
1336 *
1337 * Blt_TreeNodePosition --
1338 *
1339 * Returns the position of the node in its parent's list of
1340 * children. The root's position is 0.
1341 *
1342 *----------------------------------------------------------------------
1343 */
1344 int
1345 Blt_TreeNodePosition(Node *nodePtr)
1346 {
1347 Node *parentPtr;
1348 int count;
1349
1350 count = 0;
1351 parentPtr = nodePtr->parent;
1352 if (parentPtr != NULL) {
1353 Node *childPtr;
1354
1355 for (childPtr = parentPtr->first; childPtr != NULL;
1356 childPtr = childPtr->next) {
1357 if (nodePtr == childPtr) {
1358 break;
1359 }
1360 count++;
1361 }
1362 }
1363 return count;
1364 }
1365
1366
1367 /*
1368 *----------------------------------------------------------------------
1369 *
1370 * Blt_TreePrevNode --
1371 *
1372 * Returns the "previous" node in the tree. This node (in
1373 * depth-first order) is its parent, if the node has no siblings
1374 * that are previous to it. Otherwise it is the last descendant
1375 * of the last sibling. In this case, descend the sibling's
1376 * hierarchy, using the last child at any ancestor, with we
1377 * we find a leaf.
1378 *
1379 *----------------------------------------------------------------------
1380 */
1381 Blt_TreeNode
1382 Blt_TreePrevNode(Node *rootPtr, Node *nodePtr)
1383 {
1384 Node *prevPtr;
1385
1386 if (nodePtr == rootPtr) {
1387 return NULL; /* The root is the first node. */
1388 }
1389 prevPtr = nodePtr->prev;
1390 if (prevPtr == NULL) {
1391 /* There are no siblings previous to this one, so pick the parent. */
1392 return nodePtr->parent;
1393 }
1394 /*
1395 * Traverse down the right-most thread, in order to select the
1396 * next entry. Stop when we reach a leaf.
1397 */
1398 nodePtr = prevPtr;
1399 while ((prevPtr = nodePtr->last) != NULL) {
1400 nodePtr = prevPtr;
1401 }
1402 return nodePtr;
1403 }
1404
1405 /*
1406 *----------------------------------------------------------------------
1407 *
1408 * Blt_TreeNextNode --
1409 *
1410 * Returns the "next" node in relation to the given node.
1411 * The next node (in depth-first order) is either the first
1412 * child of the given node the next sibling if the node has
1413 * no children (the node is a leaf). If the given node is the
1414 * last sibling, then try it's parent next sibling. Continue
1415 * until we either find a next sibling for some ancestor or
1416 * we reach the root node. In this case the current node is
1417 * the last node in the tree.
1418 *
1419 *----------------------------------------------------------------------
1420 */
1421 Blt_TreeNode
1422 Blt_TreeNextNode(Node *rootPtr, Node *nodePtr)
1423 {
1424 Node *nextPtr;
1425
1426 /* Pick the first sub-node. */
1427 nextPtr = nodePtr->first;
1428 if (nextPtr != NULL) {
1429 return nextPtr;
1430 }
1431 /*
1432 * Back up until we can find a level where we can pick a
1433 * "next sibling". For the last entry we'll thread our
1434 * way back to the root.
1435 */
1436 while (nodePtr != rootPtr) {
1437 nextPtr = nodePtr->next;
1438 if (nextPtr != NULL) {
1439 return nextPtr;
1440 }
1441 nodePtr = nodePtr->parent;
1442 }
1443 return NULL; /* At root, no next node. */
1444 }
1445
1446
1447 int
1448 Blt_TreeIsBefore(Node *n1Ptr, Node *n2Ptr)
1449 {
1450 int depth;
1451 register int i;
1452 Node *nodePtr;
1453
1454 if (n1Ptr == n2Ptr) {
1455 return FALSE;
1456 }
1457 depth = MIN(n1Ptr->depth, n2Ptr->depth);
1458 if (depth == 0) { /* One of the nodes is root. */
1459 return (n1Ptr->parent == NULL);
1460 }
1461 /*
1462 * Traverse back from the deepest node, until both nodes are at
1463 * the same depth. Check if this ancestor node is the same for
1464 * both nodes.
1465 */
1466 for (i = n1Ptr->depth; i > depth; i--) {
1467 n1Ptr = n1Ptr->parent;
1468 }
1469 if (n1Ptr == n2Ptr) {
1470 return FALSE;
1471 }
1472 for (i = n2Ptr->depth; i > depth; i--) {
1473 n2Ptr = n2Ptr->parent;
1474 }
1475 if (n2Ptr == n1Ptr) {
1476 return TRUE;
1477 }
1478
1479 /*
1480 * First find the mutual ancestor of both nodes. Look at each
1481 * preceding ancestor level-by-level for both nodes. Eventually
1482 * we'll find a node that's the parent of both ancestors. Then
1483 * find the first ancestor in the parent's list of subnodes.
1484 */
1485 for (i = depth; i > 0; i--) {
1486 if (n1Ptr->parent == n2Ptr->parent) {
1487 break;
1488 }
1489 n1Ptr = n1Ptr->parent;
1490 n2Ptr = n2Ptr->parent;
1491 }
1492 for (nodePtr = n1Ptr->parent->first; nodePtr != NULL;
1493 nodePtr = nodePtr->next) {
1494 if (nodePtr == n1Ptr) {
1495 return TRUE;
1496 } else if (nodePtr == n2Ptr) {
1497 return FALSE;
1498 }
1499 }
1500 return FALSE;
1501 }
1502
1503 static int
1504 CallTraces(
1505 Tcl_Interp *interp,
1506 TreeClient *sourcePtr, /* Client holding a reference to the
1507 * tree. If NULL, indicates to
1508 * execute all handlers, including
1509 * those of the caller. */
1510 TreeObject *treeObjPtr, /* Tree that was changed. */
1511 Node *nodePtr, /* Node that received the event. */
1512 Blt_TreeKey key,
1513 unsigned int flags,
1514 int *cnt)
1515 {
1516 Blt_ChainLink *l1Ptr, *l2Ptr;
1517 TreeClient *clientPtr;
1518 TraceHandler *tracePtr;
1519 unsigned int inode = nodePtr->inode;
1520 int result;
1521
1522 for(l1Ptr = Blt_ChainFirstLink(treeObjPtr->clients);
1523 l1Ptr != NULL; l1Ptr = Blt_ChainNextLink(l1Ptr)) {
1524 clientPtr = Blt_ChainGetValue(l1Ptr);
1525 for(l2Ptr = Blt_ChainFirstLink(clientPtr->traces);
1526 l2Ptr != NULL; l2Ptr = Blt_ChainNextLink(l2Ptr)) {
1527 tracePtr = Blt_ChainGetValue(l2Ptr);
1528 if ((tracePtr->mask & flags) == 0) {
1529 continue; /* Flags don't match. */
1530 }
1531 if ((clientPtr == sourcePtr) &&
1532 (tracePtr->mask & TREE_TRACE_FOREIGN_ONLY)) {
1533 continue; /* This client initiated the trace. */
1534 }
1535 if ((tracePtr->nodePtr != NULL) && (tracePtr->nodePtr != nodePtr)) {
1536 continue; /* Nodes don't match. */
1537 }
1538 if ((tracePtr->keyPattern != NULL) &&
1539 (!Tcl_StringMatch(key, tracePtr->keyPattern))) {
1540 continue; /* Key pattern doesn't match. */
1541 }
1542 if ((tracePtr->withTag != NULL) &&
1543 (!Blt_TreeHasTag(clientPtr, nodePtr, tracePtr->withTag))) {
1544 continue; /* Doesn't have the tag. */
1545 }
1546 nodePtr->flags |= TREE_TRACE_ACTIVE;
1547 *cnt += 1;
1548
1549 Tcl_Preserve(treeObjPtr);
1550 result = (*tracePtr->proc) (tracePtr->clientData, treeObjPtr->interp,
1551 nodePtr, key, flags);
1552 if (result != TCL_OK) {
1553 Tcl_Release(treeObjPtr);
1554 if ((tracePtr->mask & TREE_TRACE_BGERROR) && interp != NULL) {
1555 Tcl_BackgroundError(interp);
1556 } else {
1557 nodePtr->flags &= ~TREE_TRACE_ACTIVE;
1558 return TCL_ERROR;
1559 }
1560 }
1561 nodePtr->flags &= ~TREE_TRACE_ACTIVE;
1562 if (Blt_TreeNodeDeleted(nodePtr) || nodePtr->inode != inode) {
1563 Tcl_Release(treeObjPtr);
1564 return TCL_ERROR;
1565 }
1566 if (treeObjPtr->delete) {
1567 Tcl_Release(treeObjPtr);
1568 if (interp != NULL) {
1569 Tcl_AppendResult(interp, "tree deleted", 0);
1570 }
1571 return TCL_ERROR;
1572 }
1573 Tcl_Release(treeObjPtr);
1574 /* nodePtr = GetNode(treeObjPtr, inode);
1575 if (nodePtr == NULL) {
1576 if (interp != NULL) {
1577 Tcl_AppendResult(interp, "node deleted", 0);
1578 }
1579 return TCL_ERROR;
1580 }*/
1581 }
1582 }
1583 return TCL_OK;
1584 }
1585
1586 static Value *
1587 GetTreeValue(
1588 Tcl_Interp *interp,
1589 TreeClient *clientPtr,
1590 Node *nodePtr,
1591 Blt_TreeKey key)
1592 {
1593 register Value *valuePtr;
1594
1595 valuePtr = TreeFindValue(nodePtr, key);
1596 if (valuePtr == NULL) {
1597 if (interp != NULL) {
1598 Tcl_AppendResult(interp, "can't find field \"", key, "\"",
1599 (char *)NULL);
1600 }
1601 return NULL;
1602 }
1603 if ((valuePtr->owner != NULL) && (valuePtr->owner != clientPtr)) {
1604 if (interp != NULL) {
1605 Tcl_AppendResult(interp, "can't access private field \"",
1606 key, "\"", (char *)NULL);
1607 }
1608 return NULL;
1609 }
1610 return valuePtr;
1611 }
1612
1613 int
1614 Blt_TreePrivateValue(
1615 Tcl_Interp *interp,
1616 TreeClient *clientPtr,
1617 Node *nodePtr,
1618 Blt_TreeKey key)
1619 {
1620 Value *valuePtr;
1621
1622 valuePtr = TreeFindValue(nodePtr, key);
1623 if (valuePtr == NULL) {
1624 if (interp != NULL) {
1625 Tcl_AppendResult(interp, "can't find field \"", key, "\"",
1626 (char *)NULL);
1627 }
1628 return TCL_ERROR;
1629 }
1630 valuePtr->owner = clientPtr;
1631 return TCL_OK;
1632 }
1633
1634 int
1635 Blt_TreePublicValue(
1636 Tcl_Interp *interp,
1637 TreeClient *clientPtr,
1638 Node *nodePtr,
1639 Blt_TreeKey key)
1640 {
1641 Value *valuePtr;
1642
1643 valuePtr = TreeFindValue(nodePtr, key);
1644 if (valuePtr == NULL) {
1645 if (interp != NULL) {
1646 Tcl_AppendResult(interp, "can't find field \"", key, "\"",
1647 (char *)NULL);
1648 }
1649 return TCL_ERROR;
1650 }
1651 if (valuePtr->owner != clientPtr) {
1652 if (interp != NULL) {
1653 Tcl_AppendResult(interp, "not the owner of \"", key, "\"",
1654 (char *)NULL);
1655 }
1656 return TCL_ERROR;
1657 }
1658 valuePtr->owner = NULL;
1659 return TCL_OK;
1660 }
1661
1662 int
1663 Blt_TreeValueExistsByKey(clientPtr, nodePtr, key)
1664 TreeClient *clientPtr;
1665 Node *nodePtr;
1666 Blt_TreeKey key;
1667 {
1668 register Value *valuePtr;
1669 int cnt;
1670 TreeObject *treeObjPtr = nodePtr->treeObject;
1671 Tcl_Interp *interp = treeObjPtr->interp;
1672
1673 valuePtr = GetTreeValue((Tcl_Interp *)NULL, clientPtr, nodePtr, key);
1674 if (valuePtr == NULL && (!(nodePtr->flags & TREE_TRACE_ACTIVE))) {
1675 if (CallTraces(interp, clientPtr, treeObjPtr, nodePtr, key,
1676 TREE_TRACE_EXISTS, &cnt) != TCL_OK) {
1677 Tcl_ResetResult(interp);
1678 } else {
1679 valuePtr = GetTreeValue((Tcl_Interp *)NULL, clientPtr, nodePtr, key);
1680 }
1681 }
1682 if (valuePtr == NULL) {
1683 return FALSE;
1684 }
1685 return TRUE;
1686 }
1687
1688 int
1689 Blt_TreeGetValueByKey(
1690 Tcl_Interp *interp,
1691 TreeClient *clientPtr,
1692 Node *nodePtr,
1693 Blt_TreeKey key,
1694 Tcl_Obj **objPtrPtr)
1695 {
1696 register Value *valuePtr;
1697 TreeObject *treeObjPtr = nodePtr->treeObject;
1698 int cnt = 0;
1699
1700 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
1701 if (CallTraces(interp, clientPtr, treeObjPtr, nodePtr, key,
1702 TREE_TRACE_READ, &cnt) != TCL_OK) {
1703 return TCL_ERROR;
1704 }
1705 }
1706 valuePtr = GetTreeValue(interp, clientPtr, nodePtr, key);
1707 if (valuePtr == NULL) {
1708 return TCL_ERROR;
1709 }
1710 *objPtrPtr = valuePtr->objPtr;
1711 return TCL_OK;
1712 }
1713
1714 int
1715 bltTreeGetValueByKey(
1716 Tcl_Interp *interp,
1717 TreeClient *clientPtr,
1718 Node *nodePtr,
1719 Blt_TreeKey key,
1720 Value** valuePtrPtr)
1721 {
1722 register Value *valuePtr;
1723 TreeObject *treeObjPtr = nodePtr->treeObject;
1724 int cnt = 0;
1725
1726 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
1727 if (CallTraces(interp, clientPtr, treeObjPtr, nodePtr, key,
1728 TREE_TRACE_READ, &cnt) != TCL_OK) {
1729 return TCL_ERROR;
1730 }
1731 }
1732 valuePtr = GetTreeValue(interp, clientPtr, nodePtr, key);
1733 if (valuePtr == NULL) {
1734 return TCL_ERROR;
1735 }
1736 *valuePtrPtr = valuePtr;
1737 return TCL_OK;
1738 }
1739
1740 static void
1741 UpdateOldValue(
1742 TreeClient *clientPtr,
1743 Tcl_Obj *objPtr)
1744 {
1745 if (clientPtr->oldValue != NULL) {
1746 Tcl_DecrRefCount(clientPtr->oldValue);
1747 }
1748 clientPtr->oldValue = objPtr;
1749 }
1750
1751 void
1752 Blt_TreeOldValue(
1753 Tcl_Interp *interp,
1754 TreeClient *clientPtr,
1755 Tcl_Obj **oldPtr,
1756 Tcl_Obj *newPtr)
1757 {
1758 if (newPtr) {
1759 if (clientPtr->oldValue != NULL) {
1760 Tcl_DecrRefCount(clientPtr->oldValue);
1761 }
1762 clientPtr->oldValue = newPtr;
1763 Tcl_IncrRefCount(clientPtr->oldValue);
1764 } else if (oldPtr != NULL) {
1765 *oldPtr = clientPtr->oldValue;
1766 }
1767 }
1768
1769 int
1770 Blt_TreeSetValueByKey(
1771 Tcl_Interp *interp,
1772 TreeClient *clientPtr,
1773 Node *nodePtr, /* Node to be updated. */
1774 Blt_TreeKey key, /* Identifies the field key. */
1775 Tcl_Obj *objPtr) /* New value of field. */
1776 {
1777 TreeObject *treeObjPtr;
1778 Value *valuePtr;
1779 unsigned int flags;
1780 int isNew = 0, cnt = 0;
1781
1782 if (nodePtr == NULL) {
1783 return TCL_ERROR;
1784 }
1785 treeObjPtr = nodePtr->treeObject;
1786 assert(objPtr != NULL);
1787 if (nodePtr->flags & TREE_NODE_FIXED_FIELDS) {
1788 valuePtr = TreeFindValue(nodePtr, key);
1789 if (valuePtr == NULL) {
1790 if (interp != NULL) {
1791 Tcl_AppendResult(interp, "fixed field \"", key, "\"",
1792 (char *)NULL);
1793 }
1794 return TCL_ERROR;
1795 }
1796 } else {
1797 valuePtr = TreeCreateValue(nodePtr, key, &isNew);
1798 }
1799 if ((valuePtr->owner != NULL) && (valuePtr->owner != clientPtr)) {
1800 if (interp != NULL) {
1801 Tcl_AppendResult(interp, "can't set private field \"",
1802 key, "\"", (char *)NULL);
1803 }
1804 return TCL_ERROR;
1805 }
1806 SetModified(nodePtr);
1807 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
1808 UpdateOldValue(clientPtr, valuePtr->objPtr);
1809 valuePtr->objPtr = NULL;
1810 }
1811 if (objPtr != valuePtr->objPtr) {
1812 Tcl_IncrRefCount(objPtr);
1813 if (valuePtr->objPtr != NULL) {
1814 Tcl_DecrRefCount(valuePtr->objPtr);
1815 }
1816 valuePtr->objPtr = objPtr;
1817 }
1818 flags = TREE_TRACE_WRITE;
1819 if (isNew) {
1820 flags |= TREE_TRACE_CREATE;
1821 }
1822 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
1823 return CallTraces(interp, clientPtr, treeObjPtr, nodePtr, valuePtr->key,
1824 flags, &cnt);
1825 }
1826 return TCL_OK;
1827 }
1828
1829 int
1830 Blt_TreeUnsetValueByKey(
1831 Tcl_Interp *interp,
1832 TreeClient *clientPtr,
1833 Node *nodePtr, /* Node to be updated. */
1834 Blt_TreeKey key) /* Name of field in node. */
1835 {
1836 TreeObject *treeObjPtr = nodePtr->treeObject;
1837 Value *valuePtr;
1838 int cnt = 0;
1839
1840 if (nodePtr->flags & TREE_NODE_FIXED_FIELDS) {
1841 if (interp != NULL) {
1842 Tcl_AppendResult(interp, "fixed field", 0);
1843 }
1844 return TCL_ERROR;
1845 }
1846 valuePtr = TreeFindValue(nodePtr, key);
1847 if (valuePtr == NULL) {
1848 return TCL_OK; /* It's okay to unset values that don't
1849 * exist in the node. */
1850 }
1851 if ((valuePtr->owner != NULL) && (valuePtr->owner != clientPtr)) {
1852 if (interp != NULL) {
1853 Tcl_AppendResult(interp, "can't unset private field \"",
1854 key, "\"", (char *)NULL);
1855 }
1856 return TCL_ERROR;
1857 }
1858 SetModified(nodePtr);
1859 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
1860 UpdateOldValue(clientPtr, valuePtr->objPtr);
1861 valuePtr->objPtr = NULL;
1862 }
1863 TreeDeleteValue(nodePtr, valuePtr);
1864 return CallTraces(interp, clientPtr, treeObjPtr, nodePtr, key, TREE_TRACE_UNSET, &cnt);
1865 }
1866
1867 static int
1868 ParseParentheses(
1869 Tcl_Interp *interp,
1870 CONST char *string,
1871 char **leftPtr,
1872 char **rightPtr)
1873 {
1874 register char *p;
1875 char *left, *right;
1876
1877 left = right = NULL;
1878 for (p = (char *)string; *p != '\0'; p++) {
1879 if (*p == '(') {
1880 left = p;
1881 } else if (*p == ')') {
1882 right = p;
1883 }
1884 }
1885 if (left != right) {
1886 if (((left != NULL) && (right == NULL)) ||
1887 ((left == NULL) && (right != NULL)) ||
1888 (left > right) || (right != (p - 1))) {
1889 if (interp != NULL) {
1890 Tcl_AppendResult(interp, "bad array specification \"", string,
1891 "\"", (char *)NULL);
1892 }
1893 return TCL_ERROR;
1894 }
1895 }
1896 *leftPtr = left;
1897 *rightPtr = right;
1898 return TCL_OK;
1899 }
1900
1901 int
1902 Blt_TreeGetValue(
1903 Tcl_Interp *interp,
1904 TreeClient *clientPtr,
1905 Node *nodePtr,
1906 CONST char *string, /* String identifying the field in node. */
1907 Tcl_Obj **objPtrPtr)
1908 {
1909 char *left, *right;
1910 int result;
1911
1912 if (ParseParentheses(interp, string, &left, &right) != TCL_OK) {
1913 return TCL_ERROR;
1914 }
1915 if (left != NULL) {
1916 Tcl_DString kStr, iStr;
1917 Tcl_DStringInit(&kStr);
1918 Tcl_DStringInit(&iStr);
1919 Tcl_DStringAppend(&kStr, left+1, right-left-1);
1920 Tcl_DStringAppend(&iStr, string, left-string);
1921 result = Blt_TreeGetArrayValue(interp, clientPtr, nodePtr,
1922 Tcl_DStringValue(&iStr), Tcl_DStringValue(&kStr), objPtrPtr);
1923 Tcl_DStringFree(&kStr);
1924 Tcl_DStringFree(&iStr);
1925 } else {
1926 result = Blt_TreeGetValueByKey(interp, clientPtr, nodePtr,
1927 Blt_TreeKeyGet(NULL, clientPtr->treeObject,string), objPtrPtr);
1928 }
1929 return result;
1930 }
1931
1932 int
1933 Blt_TreeSetValue(
1934 Tcl_Interp *interp,
1935 TreeClient *clientPtr,
1936 Node *nodePtr, /* Node to be updated. */
1937 CONST char *string, /* String identifying the field in node. */
1938 Tcl_Obj *valueObjPtr) /* New value of field. If NULL, field
1939 * is deleted. */
1940 {
1941 char *left, *right;
1942 int result;
1943
1944 if (nodePtr->flags & TREE_NODE_FIXED_FIELDS) {
1945 return Blt_TreeUpdateValue( interp, clientPtr, nodePtr, string, valueObjPtr);
1946 }
1947 if (ParseParentheses(interp, string, &left, &right) != TCL_OK) {
1948 return TCL_ERROR;
1949 }
1950 if (left != NULL) {
1951 Tcl_DString kStr, iStr;
1952 Tcl_DStringInit(&kStr);
1953 Tcl_DStringInit(&iStr);
1954 Tcl_DStringAppend(&kStr, left+1, right-left-1);
1955 Tcl_DStringAppend(&iStr, string, left-string);
1956 result = Blt_TreeSetArrayValue(interp, clientPtr, nodePtr,
1957 Tcl_DStringValue(&iStr), Tcl_DStringValue(&kStr), valueObjPtr);
1958 Tcl_DStringFree(&kStr);
1959 Tcl_DStringFree(&iStr);
1960 } else {
1961 result = Blt_TreeSetValueByKey(interp, clientPtr, nodePtr,
1962 Blt_TreeKeyGet(NULL, clientPtr->treeObject,string), valueObjPtr);
1963 }
1964 return result;
1965 }
1966
1967 int
1968 Blt_TreeUpdateValue(
1969 Tcl_Interp *interp,
1970 TreeClient *clientPtr,
1971 Node *nodePtr, /* Node to be updated. */
1972 CONST char *string, /* String identifying the field in node. */
1973 Tcl_Obj *valueObjPtr) /* New value of field. If NULL, field
1974 * is deleted. */
1975 {
1976 char *left, *right;
1977 int result;
1978 Blt_TreeKey key;
1979
1980 if (ParseParentheses(interp, string, &left, &right) != TCL_OK) {
1981 return TCL_ERROR;
1982 }
1983 if (left != NULL) {
1984 Tcl_DString kStr, iStr;
1985 Tcl_DStringInit(&kStr);
1986 Tcl_DStringInit(&iStr);
1987 Tcl_DStringAppend(&kStr, left+1, right-left-1);
1988 Tcl_DStringAppend(&iStr, string, left-string);
1989 result = Blt_TreeUpdateArrayValue(interp, clientPtr, nodePtr,
1990 Tcl_DStringValue(&iStr), Tcl_DStringValue(&kStr), valueObjPtr);
1991 Tcl_DStringFree(&kStr);
1992 Tcl_DStringFree(&iStr);
1993 } else {
1994 key = Blt_TreeKeyGet(NULL, clientPtr->treeObject,string);
1995 if (GetTreeValue((Tcl_Interp *)NULL, clientPtr, nodePtr, key) == NULL) {
1996 if (interp != NULL) {
1997 Tcl_AppendResult(interp, "unknown key: ", string, 0);
1998 }
1999 return TCL_ERROR;
2000 }
2001 result = Blt_TreeSetValueByKey(interp, clientPtr, nodePtr,
2002 key, valueObjPtr);
2003 }
2004 return result;
2005 }
2006
2007 int
2008 Blt_TreeUnsetValue(
2009 Tcl_Interp *interp,
2010 TreeClient *clientPtr,
2011 Node *nodePtr, /* Node to be updated. */
2012 CONST char *string) /* String identifying the field in node. */
2013 {
2014 char *left, *right;
2015 int result;
2016
2017 if (nodePtr->flags & TREE_NODE_FIXED_FIELDS) {
2018 if (interp != NULL) {
2019 Tcl_AppendResult(interp, "fixed field", 0);
2020 }
2021 return TCL_ERROR;
2022 }
2023 if (ParseParentheses(interp, string, &left, &right) != TCL_OK) {
2024 return TCL_ERROR;
2025 }
2026 if (left != NULL) {
2027 Tcl_DString kStr, iStr;
2028 Tcl_DStringInit(&kStr);
2029 Tcl_DStringInit(&iStr);
2030 Tcl_DStringAppend(&kStr, left+1, right-left-1);
2031 Tcl_DStringAppend(&iStr, string, left-string);
2032 result = Blt_TreeUnsetArrayValue(interp, clientPtr, nodePtr,
2033 Tcl_DStringValue(&iStr), Tcl_DStringValue(&kStr));
2034 Tcl_DStringFree(&kStr);
2035 Tcl_DStringFree(&iStr);
2036 } else {
2037 result = Blt_TreeUnsetValueByKey(interp, clientPtr, nodePtr,
2038 Blt_TreeKeyGet(NULL, clientPtr->treeObject,string));
2039 }
2040 return result;
2041 }
2042
2043 int
2044 Blt_TreeValueExists(TreeClient *clientPtr, Node *nodePtr, CONST char *string)
2045 {
2046 char *left, *right;
2047 int result;
2048
2049 if (ParseParentheses((Tcl_Interp *)NULL, string, &left, &right) != TCL_OK) {
2050 return FALSE;
2051 }
2052 if (left != NULL) {
2053 Tcl_DString kStr, iStr;
2054 Tcl_DStringInit(&kStr);
2055 Tcl_DStringInit(&iStr);
2056 Tcl_DStringAppend(&kStr, left+1, right-left-1);
2057 Tcl_DStringAppend(&iStr, string, left-string);
2058 result = Blt_TreeArrayValueExists(clientPtr, nodePtr,
2059 Tcl_DStringValue(&iStr), Tcl_DStringValue(&kStr));
2060 Tcl_DStringFree(&kStr);
2061 Tcl_DStringFree(&iStr);
2062 } else {
2063 result = Blt_TreeValueExistsByKey(clientPtr, nodePtr,
2064 Blt_TreeKeyGet(NULL, clientPtr->treeObject,string));
2065 }
2066 return result;
2067 }
2068
2069 Blt_TreeKey
2070 Blt_TreeFirstKey(
2071 TreeClient *clientPtr,
2072 Node *nodePtr,
2073 Blt_TreeKeySearch *iterPtr)
2074 {
2075 Value *valuePtr;
2076
2077 valuePtr = TreeFirstValue(nodePtr, iterPtr);
2078 if (valuePtr == NULL) {
2079 return NULL;
2080 }
2081 while ((valuePtr->owner != NULL) && (valuePtr->owner != clientPtr)) {
2082 valuePtr = TreeNextValue(iterPtr);
2083 if (valuePtr == NULL) {
2084 return NULL;
2085 }
2086 }
2087 return valuePtr->key;
2088 }
2089
2090 Blt_TreeKey
2091 Blt_TreeNextKey(TreeClient *clientPtr, Blt_TreeKeySearch *iterPtr)
2092 {
2093 Value *valuePtr;
2094
2095 valuePtr = TreeNextValue(iterPtr);
2096 if (valuePtr == NULL) {
2097 return NULL;
2098 }
2099 while ((valuePtr->owner != NULL) && (valuePtr->owner != clientPtr)) {
2100 valuePtr = TreeNextValue(iterPtr);
2101 if (valuePtr == NULL) {
2102 return NULL;
2103 }
2104 }
2105 return valuePtr->key;
2106 }
2107
2108 int Blt_TreeCountKeys(TreeClient *clientPtr, Node *nodePtr) {
2109 int cnt = 0;
2110 Blt_TreeKey key;
2111 Blt_TreeKeySearch cursor;
2112
2113
2114 for (key = Blt_TreeFirstKey(clientPtr, nodePtr, &cursor); key != NULL;
2115 key = Blt_TreeNextKey(clientPtr, &cursor)) {
2116 cnt++;
2117 }
2118 return cnt;
2119 }
2120
2121
2122 int
2123 Blt_TreeIsAncestor(Node *n1Ptr, Node *n2Ptr)
2124 {
2125 if (n2Ptr != NULL) {
2126 n2Ptr = n2Ptr->parent;
2127 while (n2Ptr != NULL) {
2128 if (n2Ptr == n1Ptr) {
2129 return TRUE;
2130 }
2131 n2Ptr = n2Ptr->parent;
2132 }
2133 }
2134 return FALSE;
2135 }
2136
2137 /*
2138 *----------------------------------------------------------------------
2139 *
2140 * Blt_TreeSortNode --
2141 *
2142 * Sorts the subnodes at a given node.
2143 *
2144 * Results:
2145 * Always returns TCL_OK.
2146 *
2147 *----------------------------------------------------------------------
2148 */
2149 int
2150 Blt_TreeSortNode(
2151 TreeClient *clientPtr,
2152 Node *nodePtr,
2153 Blt_TreeCompareNodesProc *proc)
2154 {
2155 Node **nodeArr;
2156 Node *childPtr;
2157 int nNodes;
2158 register Node **p;
2159
2160 nNodes = nodePtr->nChildren;
2161 if (nNodes < 2) {
2162 return TCL_OK;
2163 }
2164 nodeArr = Blt_Malloc((nNodes + 1) * sizeof(Node *));
2165 if (nodeArr == NULL) {
2166 return TCL_ERROR; /* Out of memory. */
2167 }
2168 for (p = nodeArr, childPtr = nodePtr->first; childPtr != NULL;
2169 childPtr = childPtr->next, p++) {
2170 *p = childPtr;
2171 }
2172 *p = NULL;
2173
2174 qsort((char *)nodeArr, nNodes, sizeof(Node *), (QSortCompareProc *)proc);
2175 for (p = nodeArr; *p != NULL; p++) {
2176 UnlinkNode(*p);
2177 LinkBefore(nodePtr, *p, (Blt_TreeNode)NULL);
2178 }
2179 Blt_Free(nodeArr);
2180 return NotifyClients(clientPtr, nodePtr->treeObject, nodePtr, TREE_NOTIFY_SORT);
2181 }
2182
2183 #define TEST_RESULT(result) \
2184 switch (result) { \
2185 case TCL_CONTINUE: \
2186 return TCL_OK; \
2187 case TCL_OK: \
2188 break; \
2189 default: \
2190 return (result); \
2191 }
2192
2193 int
2194 Blt_TreeApply(
2195 Node *nodePtr, /* Root node of subtree. */
2196 Blt_TreeApplyProc *proc, /* Procedure to call for each node. */
2197 ClientData clientData) /* One-word of data passed when calling
2198 * proc. */
2199 {
2200 Node *childPtr, *nextPtr;
2201 int result;
2202
2203 for (childPtr = nodePtr->first; childPtr != NULL; childPtr = nextPtr) {
2204
2205 /*
2206 * Get the next link in the chain before calling Blt_TreeApply
2207 * recursively. This is because the apply callback may delete
2208 * the node and its link.
2209 */
2210
2211 nextPtr = childPtr->next;
2212 if (Blt_TreeNodeDeleted(childPtr)) return TCL_OK;
2213 result = Blt_TreeApply(childPtr, proc, clientData);
2214 TEST_RESULT(result);
2215 }
2216 if (Blt_TreeNodeDeleted(nodePtr)) return TCL_OK;
2217 return (*proc) (nodePtr, clientData, TREE_POSTORDER);
2218 }
2219
2220 int
2221 Blt_TreeApplyDFS(
2222 Node *nodePtr, /* Root node of subtree. */
2223 Blt_TreeApplyProc *proc, /* Procedure to call for each node. */
2224 ClientData clientData, /* One-word of data passed when calling
2225 * proc. */
2226 int order) /* Order of traversal. */
2227 {
2228 Node *childPtr, *nextPtr;
2229 int result;
2230
2231 if (Blt_TreeNodeDeleted(nodePtr)) return TCL_OK;
2232 if (order & TREE_PREORDER) {
2233 result = (*proc) (nodePtr, clientData, TREE_PREORDER);
2234 TEST_RESULT(result);
2235 }
2236 childPtr = nodePtr->first;
2237 if (order & TREE_INORDER) {
2238 if (childPtr != NULL) {
2239 result = Blt_TreeApplyDFS(childPtr, proc, clientData, order);
2240 TEST_RESULT(result);
2241 childPtr = childPtr->next;
2242 }
2243 result = (*proc) (nodePtr, clientData, TREE_INORDER);
2244 TEST_RESULT(result);
2245 }
2246 for (/* empty */; childPtr != NULL; childPtr = nextPtr) {
2247 /*
2248 * Get the next link in the chain before calling
2249 * Blt_TreeApply recursively. This is because the
2250 * apply callback may delete the node and its link.
2251 */
2252 nextPtr = childPtr->next;
2253 if (Blt_TreeNodeDeleted(childPtr)) break;
2254 result = Blt_TreeApplyDFS(childPtr, proc, clientData, order);
2255 TEST_RESULT(result);
2256 }
2257 if (Blt_TreeNodeDeleted(nodePtr)) return TCL_OK;
2258 if (order & TREE_POSTORDER) {
2259 return (*proc) (nodePtr, clientData, TREE_POSTORDER);
2260 }
2261 return TCL_OK;
2262 }
2263
2264 int
2265 Blt_TreeApplyBFS(nodePtr, proc, clientData)
2266 Node *nodePtr; /* Root node of subtree. */
2267 Blt_TreeApplyProc *proc; /* Procedure to call for each node. */
2268 ClientData clientData; /* One-word of data passed when calling
2269 * proc. */
2270 {
2271 Blt_Chain *queuePtr;
2272 Blt_ChainLink *linkPtr, *nextPtr;
2273 Node *childPtr;
2274 int result;
2275
2276 queuePtr = Blt_ChainCreate();
2277 linkPtr = Blt_ChainAppend(queuePtr, nodePtr);
2278 while (linkPtr != NULL) {
2279 nodePtr = Blt_ChainGetValue(linkPtr);
2280 /* Add the children to the queue. */
2281 for (childPtr = nodePtr->first; childPtr != NULL;
2282 childPtr = childPtr->next) {
2283 Blt_ChainAppend(queuePtr, childPtr);
2284 }
2285 if (Blt_TreeNodeDeleted(nodePtr)) break;
2286 /* Process the node. */
2287 result = (*proc) (nodePtr, clientData, TREE_BREADTHFIRST);
2288 switch (result) {
2289 case TCL_CONTINUE:
2290 Blt_ChainDestroy(queuePtr);
2291 return TCL_OK;
2292 case TCL_OK:
2293 break;
2294 default:
2295 Blt_ChainDestroy(queuePtr);
2296 return result;
2297 }
2298 /* Remove the node from the queue. */
2299 nextPtr = Blt_ChainNextLink(linkPtr);
2300 Blt_ChainDeleteLink(queuePtr, linkPtr);
2301 linkPtr = nextPtr;
2302 }
2303 Blt_ChainDestroy(queuePtr);
2304 return TCL_OK;
2305 }
2306
2307 static TreeClient *
2308 NewTreeClient(TreeObject *treeObjPtr, int sharetags)
2309 {
2310 TreeClient *clientPtr;
2311
2312 clientPtr = Blt_Calloc(1, sizeof(TreeClient));
2313 if (clientPtr != NULL) {
2314 Blt_TreeTagTable *tablePtr;
2315 int hascl = (treeObjPtr->clients->headPtr != NULL);
2316
2317 clientPtr->magic = TREE_MAGIC;
2318 clientPtr->linkPtr = Blt_ChainAppend(treeObjPtr->clients, clientPtr);
2319 clientPtr->events = Blt_ChainCreate();
2320 clientPtr->traces = Blt_ChainCreate();
2321 clientPtr->treeObject = treeObjPtr;
2322 clientPtr->root = treeObjPtr->root;
2323 if (sharetags && hascl) {
2324 TreeClient *ctPtr;
2325 ctPtr = Blt_ChainGetValue(treeObjPtr->clients->headPtr);
2326 if (ctPtr && ctPtr->tagTablePtr) {
2327 clientPtr->tagTablePtr = ctPtr->tagTablePtr;
2328 clientPtr->tagTablePtr->refCount++;
2329 }
2330 }
2331 if (clientPtr->tagTablePtr == NULL) {
2332 tablePtr = Blt_Malloc(sizeof(Blt_TreeTagTable));
2333 Blt_InitHashTable(&tablePtr->tagTable, BLT_STRING_KEYS);
2334 tablePtr->refCount = 1;
2335 clientPtr->tagTablePtr = tablePtr;
2336 }
2337 }
2338 return clientPtr;
2339 }
2340
2341 int
2342 Blt_TreeCreate(
2343 Tcl_Interp *interp, /* Interpreter to report errors back to. */
2344 CONST char *name, /* Name of tree in namespace. Tree
2345 * must not already exist. */
2346 TreeClient **clientPtrPtr) /* (out) Client token of newly created
2347 * tree. Releasing the token will
2348 * free the tree. If NULL, no token
2349 * is generated. */
2350
2351 {
2352 Tcl_DString dString;
2353 Tcl_Namespace *nsPtr;
2354 TreeInterpData *dataPtr;
2355 TreeObject *treeObjPtr;
2356 CONST char *treeName;
2357 char string[200];
2358
2359 dataPtr = GetTreeInterpData(interp);
2360 if (name != NULL) {
2361 /* Check if this tree already exists the current namespace. */
2362 treeObjPtr = GetTreeObject(interp, name, NS_SEARCH_CURRENT);
2363 if (treeObjPtr != NULL) {
2364 if (interp != NULL)
2365 Tcl_AppendResult(interp, "a tree object \"", name,
2366 "\" already exists", (char *)NULL);
2367 return TCL_ERROR;
2368 }
2369 } else {
2370 /* Generate a unique tree name in the current namespace. */
2371 do {
2372 sprintf(string, "tree%d", dataPtr->nextId++);
2373 } while (GetTreeObject(interp, string, NS_SEARCH_CURRENT) != NULL);
2374 name = string;
2375 }
2376 /*
2377 * Tear apart and put back together the namespace-qualified name
2378 * of the tree. This is to ensure that naming is consistent.
2379 */
2380 treeName = name;
2381 if (Blt_ParseQualifiedName(interp, name, &nsPtr, &treeName) != TCL_OK) {
2382 if (interp != NULL)
2383 Tcl_AppendResult(interp, "can't find namespace in \"", name, "\"",
2384 (char *)NULL);
2385 return TCL_ERROR;
2386 }
2387 if (nsPtr == NULL) {
2388 /*
2389 * Note: Unlike Tcl_CreateCommand, an unqualified name
2390 * doesn't imply the global namespace, but the current one.
2391 */
2392 nsPtr = Tcl_GetCurrentNamespace(interp);
2393 }
2394 name = Blt_GetQualifiedName(nsPtr, treeName, &dString);
2395 treeObjPtr = NewTreeObject(dataPtr, interp, name);
2396 if (treeObjPtr == NULL) {
2397 if (interp != NULL)
2398 Tcl_AppendResult(interp, "can't allocate tree \"", name, "\"",
2399 (char *)NULL);
2400 Tcl_DStringFree(&dString);
2401 return TCL_ERROR;
2402 }
2403 Tcl_DStringFree(&dString);
2404 if (clientPtrPtr != NULL) {
2405 TreeClient *clientPtr;
2406
2407 clientPtr = NewTreeClient(treeObjPtr, 0);
2408 if (clientPtr == NULL) {
2409 if (interp != NULL)
2410 Tcl_AppendResult(interp, "can't allocate tree token",(char *)NULL);
2411 return TCL_ERROR;
2412 }
2413 *clientPtrPtr = clientPtr;
2414 }
2415 return TCL_OK;
2416 }
2417
2418 int
2419 Blt_TreeGetToken(
2420 Tcl_Interp *interp, /* Interpreter to report errors back to. */
2421 CONST char *name, /* Name of tree in namespace. */
2422 TreeClient **clientPtrPtr)
2423 {
2424 TreeClient *clientPtr;
2425 TreeObject *treeObjPtr;
2426
2427 treeObjPtr = GetTreeObject(interp, name, NS_SEARCH_BOTH);
2428 if (treeObjPtr == NULL) {
2429 if (interp != NULL)
2430 Tcl_AppendResult(interp, "can't find a tree object \"", name, "\"",
2431 (char *)NULL);
2432 return TCL_ERROR;
2433 }
2434 clientPtr = NewTreeClient(treeObjPtr, 0);
2435 if (clientPtr == NULL) {
2436 if (interp != NULL)
2437 Tcl_AppendResult(interp, "can't allocate token for tree \"",
2438 name, "\"", (char *)NULL);
2439 return TCL_ERROR;
2440 }
2441 *clientPtrPtr = clientPtr;
2442 return TCL_OK;
2443 }
2444
2445 int
2446 Blt_TreeGetTokenTag(
2447 Tcl_Interp *interp, /* Interpreter to report errors back to. */
2448 CONST char *name, /* Name of tree in namespace. */
2449 TreeClient **clientPtrPtr)
2450 {
2451 TreeClient *clientPtr;
2452 TreeObject *treeObjPtr;
2453
2454 treeObjPtr = GetTreeObject(interp, name, NS_SEARCH_BOTH);
2455 if (treeObjPtr == NULL) {
2456 if (interp != NULL)
2457 Tcl_AppendResult(interp, "can't find a tree object \"", name, "\"",
2458 (char *)NULL);
2459 return TCL_ERROR;
2460 }
2461 clientPtr = NewTreeClient(treeObjPtr, 1);
2462 if (clientPtr == NULL) {
2463 if (interp != NULL)
2464 Tcl_AppendResult(interp, "can't allocate token for tree \"",
2465 name, "\"", (char *)NULL);
2466 return TCL_ERROR;
2467 }
2468 *clientPtrPtr = clientPtr;
2469 return TCL_OK;
2470 }
2471
2472 void
2473 Blt_TreeReleaseToken(TreeClient *clientPtr)
2474 {
2475 TreeObject *treeObjPtr;
2476 Blt_ChainLink *linkPtr;
2477 EventHandler *notifyPtr;
2478 TraceHandler *tracePtr;
2479
2480 if (clientPtr->magic != TREE_MAGIC) {
2481 fprintf(stderr, "invalid tree object token 0x%lx\n",
2482 (unsigned long)clientPtr);
2483 return;
2484 }
2485 /* Remove any traces that may be set. */
2486 for (linkPtr = Blt_ChainFirstLink(clientPtr->traces); linkPtr != NULL;
2487 linkPtr = Blt_ChainNextLink(linkPtr)) {
2488 tracePtr = Blt_ChainGetValue(linkPtr);
2489 if (tracePtr->keyPattern != NULL) {
2490 Blt_Free(tracePtr->keyPattern);
2491 }
2492 Blt_Free(tracePtr);
2493 }
2494 Blt_ChainDestroy(clientPtr->traces);
2495 /* And any event handlers. */
2496 for(linkPtr = Blt_ChainFirstLink(clientPtr->events);
2497 linkPtr != NULL; linkPtr = Blt_ChainNextLink(linkPtr)) {
2498 notifyPtr = Blt_ChainGetValue(linkPtr);
2499 if (notifyPtr->notifyPending) {
2500 Tcl_CancelIdleCall(NotifyIdleProc, notifyPtr);
2501 }
2502 Blt_Free(notifyPtr);
2503 }
2504 if (clientPtr->tagTablePtr != NULL) {
2505 ReleaseTagTable(clientPtr->tagTablePtr);
2506 }
2507 Blt_ChainDestroy(clientPtr->events);
2508 treeObjPtr = clientPtr->treeObject;
2509 if (treeObjPtr != NULL) {
2510 /* Remove the client from the server's list */
2511 Blt_ChainDeleteLink(treeObjPtr->clients, clientPtr->linkPtr);
2512 if (Blt_ChainGetLength(treeObjPtr->clients) == 0) {
2513 treeObjPtr->delete = 1;
2514 Tcl_EventuallyFree(treeObjPtr, DestroyTreeObject);
2515 /* DestroyTreeObject(treeObjPtr); */
2516 }
2517 }
2518 clientPtr->magic = 0;
2519 Blt_Free(clientPtr);
2520 }
2521
2522 int
2523 Blt_TreeExists(interp, name)
2524 Tcl_Interp *interp; /* Interpreter to report errors back to. */
2525 CONST char *name; /* Name of tree in designated namespace. */
2526 {
2527 TreeObject *treeObjPtr;
2528
2529 treeObjPtr = GetTreeObject(interp, name, NS_SEARCH_BOTH);
2530 if (treeObjPtr == NULL) {
2531 Tcl_ResetResult(interp);
2532 return 0;
2533 }
2534 return 1;
2535 }
2536
2537 /*ARGSUSED*/
2538 static int
2539 SizeApplyProc(
2540 Node *nodePtr, /* Not used. */
2541 ClientData clientData,
2542 int order) /* Not used. */
2543 {
2544 int *sumPtr = clientData;
2545 *sumPtr = *sumPtr + 1;
2546 return TCL_OK;
2547 }
2548
2549 int
2550 Blt_TreeSize(Node *nodePtr)
2551 {
2552 int sum;
2553
2554 sum = 0;
2555 Blt_TreeApply(nodePtr, SizeApplyProc, &sum);
2556 return sum;
2557 }
2558
2559
2560 void
2561 Blt_TreeCreateEventHandler(
2562 TreeClient *clientPtr,
2563 unsigned int mask,
2564 Blt_TreeNotifyEventProc *proc,
2565 ClientData clientData)
2566 {
2567 Blt_ChainLink *linkPtr;
2568 EventHandler *notifyPtr;
2569
2570 notifyPtr = NULL; /* Suppress compiler warning. */
2571
2572 /* Check if the event is already handled. */
2573 for(linkPtr = Blt_ChainFirstLink(clientPtr->events);
2574 linkPtr != NULL; linkPtr = Blt_ChainNextLink(linkPtr)) {
2575 notifyPtr = Blt_ChainGetValue(linkPtr);
2576 if ((notifyPtr->proc == proc) &&
2577 (notifyPtr->mask == mask) &&
2578 (notifyPtr->clientData == clientData)) {
2579 break;
2580 }
2581 }
2582 if (linkPtr == NULL) {
2583 notifyPtr = Blt_Malloc(sizeof (EventHandler));
2584 assert(notifyPtr);
2585 linkPtr = Blt_ChainAppend(clientPtr->events, notifyPtr);
2586 }
2587 if (proc == NULL) {
2588 Blt_ChainDeleteLink(clientPtr->events, linkPtr);
2589 Blt_Free(notifyPtr);
2590 } else {
2591 notifyPtr->proc = proc;
2592 notifyPtr->clientData = clientData;
2593 notifyPtr->mask = mask;
2594 notifyPtr->notifyPending = FALSE;
2595 notifyPtr->interp = clientPtr->treeObject->interp;
2596 }
2597 }
2598
2599 void
2600 Blt_TreeDeleteEventHandler(
2601 TreeClient *clientPtr,
2602 unsigned int mask,
2603 Blt_TreeNotifyEventProc *proc,
2604 ClientData clientData)
2605 {
2606 Blt_ChainLink *linkPtr;
2607 EventHandler *notifyPtr;
2608
2609 if (!clientPtr) return;
2610 for(linkPtr = Blt_ChainFirstLink(clientPtr->events);
2611 linkPtr != NULL; linkPtr = Blt_ChainNextLink(linkPtr)) {
2612 notifyPtr = Blt_ChainGetValue(linkPtr);
2613 if ((notifyPtr->proc == proc) && (notifyPtr->mask == mask) &&
2614 (notifyPtr->clientData == clientData)) {
2615 if (notifyPtr->notifyPending) {
2616 Tcl_CancelIdleCall(NotifyIdleProc, notifyPtr);
2617 }
2618 Blt_ChainDeleteLink(clientPtr->events, linkPtr);
2619 Blt_Free(notifyPtr);
2620 return;
2621 }
2622 }
2623 }
2624
2625 /*
2626 *----------------------------------------------------------------------
2627 *
2628 * Blt_TreeNodePath --
2629 *
2630 *----------------------------------------------------------------------
2631 */
2632 char *
2633 Blt_TreeNodePath(Node *nodePtr, Tcl_DString *resultPtr)
2634 {
2635 char **nameArr; /* Used to stack the component names. */
2636 char *staticSpace[64];
2637 int nLevels;
2638 register int i;
2639
2640 nLevels = nodePtr->depth;
2641 if (nLevels > 64) {
2642 nameArr = Blt_Malloc(nLevels * sizeof(char *));
2643 assert(nameArr);
2644 } else {
2645 nameArr = staticSpace;
2646 }
2647 for (i = nLevels - 1; i >= 0; i--) {
2648 /* Save the name of each ancestor in the name array.
2649 * Note that we ignore the root. */
2650 nameArr[i] = nodePtr->label;
2651 nodePtr = nodePtr->parent;
2652 }
2653 /* Append each the names in the array. */
2654 Tcl_DStringInit(resultPtr);
2655 for (i = 0; i < nLevels; i++) {
2656 Tcl_DStringAppendElement(resultPtr, nameArr[i]);
2657 }
2658 if (nameArr != staticSpace) {
2659 Blt_Free(nameArr);
2660 }
2661 return Tcl_DStringValue(resultPtr);
2662 }
2663
2664 char *
2665 Blt_TreeNodePathStr(Node *nodePtr, Tcl_DString *resultPtr, char *prefix, char *delim)
2666 {
2667 char **nameArr; /* Used to stack the component names. */
2668 char *staticSpace[64];
2669 int nLevels;
2670 register int i;
2671
2672 nLevels = nodePtr->depth;
2673 if (nLevels > 64) {
2674 nameArr = Blt_Malloc(nLevels * sizeof(char *));
2675 assert(nameArr);
2676 } else {
2677 nameArr = staticSpace;
2678 }
2679 for (i = nLevels - 1; i >= 0; i--) {
2680 /* Save the name of each ancestor in the name array.
2681 * Note that we ignore the root. */
2682 nameArr[i] = nodePtr->label;
2683 nodePtr = nodePtr->parent;
2684 }
2685 /* Append each of the names in the array. */
2686 Tcl_DStringInit(resultPtr);
2687 if (prefix != NULL) {
2688 Tcl_DStringAppend(resultPtr, prefix, -1);
2689 }
2690 for (i = 0; i < nLevels; i++) {
2691 if (i > 0 && delim != NULL) {
2692 Tcl_DStringAppend(resultPtr, delim, -1);
2693 }
2694 Tcl_DStringAppend(resultPtr, nameArr[i], -1);
2695 }
2696 if (nameArr != staticSpace) {
2697 Blt_Free(nameArr);
2698 }
2699 return Tcl_DStringValue(resultPtr);
2700 }
2701
2702 int
2703 Blt_TreeArrayValueExists(
2704 TreeClient *clientPtr,
2705 Node *nodePtr,
2706 CONST char *arrayName,
2707 CONST char *elemName)
2708 {
2709 Blt_TreeKey key;
2710 Blt_HashEntry *hPtr;
2711 Blt_HashTable *tablePtr;
2712 register Value *valuePtr;
2713 int cnt;
2714 TreeObject *treeObjPtr = nodePtr->treeObject;
2715 Tcl_Interp *interp = treeObjPtr->interp;
2716
2717 key = Blt_TreeKeyGet(NULL, clientPtr->treeObject,arrayName);
2718
2719 valuePtr = GetTreeValue((Tcl_Interp *)NULL, clientPtr, nodePtr, key);
2720 if (valuePtr == NULL && (!(nodePtr->flags & TREE_TRACE_ACTIVE))) {
2721 if (CallTraces(interp, clientPtr, treeObjPtr, nodePtr, key,
2722 TREE_TRACE_EXISTS, &cnt) != TCL_OK) {
2723 Tcl_ResetResult(interp);
2724 } else {
2725 valuePtr = GetTreeValue((Tcl_Interp *)NULL, clientPtr, nodePtr, key);
2726 }
2727 }
2728 if (valuePtr == NULL) {
2729 return FALSE;
2730 }
2731 if (IsTclDict(interp, valuePtr->objPtr)) {
2732 /* Preserve type if this was a dict */
2733 int result;
2734 Tcl_Obj *keyPtr, *valueObjPtr = NULL;
2735
2736 keyPtr = Tcl_NewStringObj(elemName, -1);
2737 Tcl_IncrRefCount(keyPtr);
2738 result = Tcl_DictObjGet(interp, valuePtr->objPtr, keyPtr, &valueObjPtr);
2739 Tcl_DecrRefCount(keyPtr);
2740 if (result != TCL_OK) {
2741 return FALSE;
2742 }
2743 if (valueObjPtr == NULL) {
2744 return FALSE;
2745 }
2746 return TRUE;
2747 }
2748
2749 if (Blt_IsArrayObj(valuePtr->objPtr) == 0 && Tcl_IsShared(valuePtr->objPtr)) {
2750 Tcl_DecrRefCount(valuePtr->objPtr);
2751 valuePtr->objPtr = Tcl_DuplicateObj(valuePtr->objPtr);
2752 Tcl_IncrRefCount(valuePtr->objPtr);
2753 }
2754 if (Blt_GetArrayFromObj((Tcl_Interp *)NULL, valuePtr->objPtr, &tablePtr)
2755 != TCL_OK) {
2756 return FALSE;
2757 }
2758 hPtr = Blt_FindHashEntry(tablePtr, elemName);
2759 return (hPtr != NULL);
2760 }
2761
2762 int
2763 Blt_TreeGetArrayValue(
2764 Tcl_Interp *interp,
2765 TreeClient *clientPtr,
2766 Node *nodePtr,
2767 CONST char *arrayName,
2768 CONST char *elemName,
2769 Tcl_Obj **valueObjPtrPtr)
2770 {
2771 Blt_TreeKey key;
2772 Blt_HashEntry *hPtr;
2773 Blt_HashTable *tablePtr;
2774 register Value *valuePtr;
2775 int cnt = 0;
2776
2777 key = Blt_TreeKeyGet(interp, clientPtr->treeObject,arrayName);
2778
2779 /* Reading any element of the array can cause a trace to fire. */
2780 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
2781 if (CallTraces(interp, clientPtr, nodePtr->treeObject, nodePtr, key,
2782 TREE_TRACE_READ, &cnt) != TCL_OK) {
2783 return TCL_ERROR;
2784 }
2785 }
2786 valuePtr = GetTreeValue(interp, clientPtr, nodePtr, key);
2787 if (valuePtr == NULL) {
2788 return TCL_ERROR;
2789 }
2790 if (IsTclDict(interp, valuePtr->objPtr)) {
2791 /* Preserve type if this was a dict */
2792 int result;
2793 Tcl_Obj *keyPtr;
2794 keyPtr = Tcl_NewStringObj(elemName, -1);
2795 Tcl_IncrRefCount(keyPtr);
2796 result = Tcl_DictObjGet(interp, valuePtr->objPtr, keyPtr, valueObjPtrPtr);
2797 Tcl_DecrRefCount(keyPtr);
2798 if (result != TCL_OK) {
2799 return result;
2800 }
2801 if (*valueObjPtrPtr == NULL) {
2802 if (interp != NULL) {
2803 Tcl_AppendResult(interp, "can't find \"", arrayName, "(",
2804 elemName, ")\"", (char *)NULL);
2805 }
2806 return TCL_ERROR;
2807 }
2808 return TCL_OK;
2809 }
2810 if (Blt_IsArrayObj(valuePtr->objPtr) == 0 && Tcl_IsShared(valuePtr->objPtr)) {
2811 Tcl_DecrRefCount(valuePtr->objPtr);
2812 valuePtr->objPtr = Tcl_DuplicateObj(valuePtr->objPtr);
2813 Tcl_IncrRefCount(valuePtr->objPtr);
2814 }
2815 if (Blt_GetArrayFromObj(interp, valuePtr->objPtr, &tablePtr) != TCL_OK) {
2816 return TCL_ERROR;
2817 }
2818 hPtr = Blt_FindHashEntry(tablePtr, elemName);
2819 if (hPtr == NULL) {
2820 if (interp != NULL) {
2821 Tcl_AppendResult(interp, "can't find \"", arrayName, "(",
2822 elemName, ")\"", (char *)NULL);
2823 }
2824 return TCL_ERROR;
2825 }
2826
2827 *valueObjPtrPtr = (Tcl_Obj *)Blt_GetHashValue(hPtr);
2828 return TCL_OK;
2829 }
2830
2831 static int
2832 TreeSetArrayValue(
2833 Tcl_Interp *interp,
2834 TreeClient *clientPtr,
2835 Node *nodePtr, /* Node to be updated. */
2836 CONST char *arrayName,
2837 CONST char *elemName,
2838 Tcl_Obj *valueObjPtr, /* New value of element. */
2839 int create) /* Create the node key if required. */
2840 {
2841 Blt_TreeKey key;
2842 Blt_HashEntry *hPtr;
2843 Blt_HashTable *tablePtr;
2844 register Value *valuePtr;
2845 unsigned int flags;
2846 int isNew, cnt = 0;
2847
2848 assert(valueObjPtr != NULL);
2849
2850 /*
2851 * Search for the array in the list of data fields. If one
2852 * doesn't exist, create it.
2853 */
2854 key = Blt_TreeKeyGet(interp, clientPtr->treeObject,arrayName);
2855 valuePtr = GetTreeValue((Tcl_Interp *)NULL, clientPtr, nodePtr, key);
2856 if (valuePtr == NULL && create == 0) {
2857 return TCL_ERROR;
2858 }
2859 if (valuePtr == NULL) {
2860 if ((nodePtr->flags & TREE_NODE_FIXED_FIELDS)) {
2861 return TCL_ERROR;
2862 }
2863 valuePtr = TreeCreateValue(nodePtr, key, &isNew);
2864 isNew = 1;
2865 } else {
2866 isNew = 0;
2867 }
2868 if ((valuePtr->owner != NULL) && (valuePtr->owner != clientPtr)) {
2869 if (interp != NULL) {
2870 Tcl_AppendResult(interp, "can't set private field \"",
2871 key, "\"", (char *)NULL);
2872 }
2873 return TCL_ERROR;
2874 }
2875 flags = TREE_TRACE_WRITE;
2876 if (isNew) {
2877 valuePtr->objPtr = Blt_NewArrayObj(0, (Tcl_Obj **)NULL);
2878 Tcl_IncrRefCount(valuePtr->objPtr);
2879 flags |= TREE_TRACE_CREATE;
2880
2881 } else if (Tcl_IsShared(valuePtr->objPtr)) {
2882 Tcl_DecrRefCount(valuePtr->objPtr);
2883 valuePtr->objPtr = Tcl_DuplicateObj(valuePtr->objPtr);
2884 Tcl_IncrRefCount(valuePtr->objPtr);
2885 }
2886
2887 if ((clientPtr->treeObject->flags & TREE_DICT_KEYS) &&
2888 IsTclDict(interp, valuePtr->objPtr)) {
2889 int dSiz;
2890
2891 if (Tcl_DictObjSize(interp, valuePtr->objPtr, &dSiz) != TCL_OK) {
2892 return TCL_ERROR;
2893 }
2894 }
2895 if (IsTclDict(interp, valuePtr->objPtr)) {
2896 /* Preserve type if this was a dict */
2897 int result;
2898 Tcl_Obj *keyPtr, *valObjPtr;
2899
2900 keyPtr = Tcl_NewStringObj(elemName, -1);
2901 Tcl_IncrRefCount(keyPtr);
2902 if (!create) {
2903 result = Tcl_DictObjGet(interp, valuePtr->objPtr, keyPtr, &valObjPtr);
2904 if (result != TCL_OK || valObjPtr == NULL) {
2905 Tcl_AppendResult(interp, "can't find field: ", elemName, 0);
2906 Tcl_DecrRefCount(keyPtr);
2907 return TCL_ERROR;
2908 }
2909 }
2910 result = Tcl_DictObjPut(interp, valuePtr->objPtr, keyPtr, valueObjPtr);
2911 Tcl_DecrRefCount(keyPtr);
2912 if (result != TCL_OK) {
2913 return result;
2914 }
2915 goto finishset;
2916 }
2917
2918
2919 if (Blt_GetArrayFromObj(interp, valuePtr->objPtr, &tablePtr) != TCL_OK) {
2920 return TCL_ERROR;
2921 }
2922 Tcl_InvalidateStringRep(valuePtr->objPtr);
2923 if (create) {
2924 hPtr = Blt_CreateHashEntry(tablePtr, elemName, &isNew);
2925 assert(hPtr);
2926 } else {
2927 hPtr = Blt_FindHashEntry(tablePtr, elemName);
2928 if (hPtr == NULL) {
2929 if (interp != NULL) {
2930 Tcl_AppendResult(interp, "can't find array field: ", elemName, 0);
2931 }
2932 return TCL_ERROR;
2933 }
2934 isNew = 0;
2935 }
2936
2937 SetModified(nodePtr);
2938 Tcl_IncrRefCount(valueObjPtr);
2939 if (!isNew) {
2940 Tcl_Obj *oldValueObjPtr;
2941
2942 /* An element by the same name already exists. Decrement the
2943 * reference count of the old value. */
2944
2945 oldValueObjPtr = (Tcl_Obj *)Blt_GetHashValue(hPtr);
2946 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
2947 UpdateOldValue(clientPtr, oldValueObjPtr);
2948 oldValueObjPtr = NULL;
2949 }
2950 if (oldValueObjPtr != NULL) {
2951 Tcl_DecrRefCount(oldValueObjPtr);
2952 }
2953 } else {
2954 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
2955 UpdateOldValue(clientPtr, NULL);
2956 }
2957 }
2958 Blt_SetHashValue(hPtr, valueObjPtr);
2959
2960 finishset:
2961 /*
2962 * We don't handle traces on a per array element basis. Setting
2963 * any element can fire traces for the value.
2964 */
2965 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
2966 return CallTraces(interp, clientPtr, nodePtr->treeObject, nodePtr,
2967 valuePtr->key, flags, &cnt);
2968 }
2969 return TCL_OK;
2970 }
2971
2972 int
2973 Blt_TreeSetArrayValue(
2974 Tcl_Interp *interp,
2975 TreeClient *clientPtr,
2976 Node *nodePtr, /* Node to be updated. */
2977 CONST char *arrayName,
2978 CONST char *elemName,
2979 Tcl_Obj *valueObjPtr) /* New value of element. */
2980 {
2981 return TreeSetArrayValue(interp, clientPtr, nodePtr, arrayName, elemName, valueObjPtr, 1);
2982 }
2983
2984 int
2985 Blt_TreeUpdateArrayValue(
2986 Tcl_Interp *interp,
2987 TreeClient *clientPtr,
2988 Node *nodePtr, /* Node to be updated. */
2989 CONST char *arrayName,
2990 CONST char *elemName,
2991 Tcl_Obj *valueObjPtr) /* New value of element. */
2992 {
2993 return TreeSetArrayValue(interp, clientPtr, nodePtr, arrayName, elemName, valueObjPtr, 0);
2994 }
2995
2996 int
2997 Blt_TreeUnsetArrayValue(
2998 Tcl_Interp *interp,
2999 TreeClient *clientPtr,
3000 Node *nodePtr, /* Node to be updated. */
3001 CONST char *arrayName,
3002 CONST char *elemName)
3003 {
3004 Blt_TreeKey key; /* Name of field in node. */
3005 Blt_HashEntry *hPtr;
3006 Blt_HashTable *tablePtr;
3007 Tcl_Obj *valueObjPtr;
3008 Value *valuePtr;
3009 int cnt = 0;
3010
3011 key = Blt_TreeKeyGet(interp, clientPtr->treeObject,arrayName);
3012 valuePtr = TreeFindValue(nodePtr, key);
3013 if (valuePtr == NULL) {
3014 return TCL_OK;
3015 }
3016 if ((valuePtr->owner != NULL) && (valuePtr->owner != clientPtr)) {
3017 if (interp != NULL) {
3018 Tcl_AppendResult(interp, "can't unset private field \"",
3019 key, "\"", (char *)NULL);
3020 }
3021 return TCL_ERROR;
3022 }
3023
3024 if (Tcl_IsShared(valuePtr->objPtr)) {
3025 Tcl_DecrRefCount(valuePtr->objPtr);
3026 valuePtr->objPtr = Tcl_DuplicateObj(valuePtr->objPtr);
3027 Tcl_IncrRefCount(valuePtr->objPtr);
3028 }
3029
3030 if (IsTclDict(interp, valuePtr->objPtr)) {
3031 /* Preserve type if this was a dict */
3032 int result;
3033 Tcl_Obj *keyPtr;
3034 keyPtr = Tcl_NewStringObj(elemName, -1);
3035 Tcl_IncrRefCount(keyPtr);
3036 result = Tcl_DictObjRemove(interp, valuePtr->objPtr, keyPtr);
3037 Tcl_DecrRefCount(keyPtr);
3038 if (result != TCL_OK) {
3039 return result;
3040 }
3041 goto finishrm;
3042 }
3043
3044 if (Blt_GetArrayFromObj(interp, valuePtr->objPtr, &tablePtr) != TCL_OK) {
3045 return TCL_ERROR;
3046 }
3047 hPtr = Blt_FindHashEntry(tablePtr, elemName);
3048 if (hPtr == NULL) {
3049 goto finishrm; /* Element doesn't exist, Ok. */
3050 }
3051 SetModified(nodePtr);
3052 valueObjPtr = (Tcl_Obj *)Blt_GetHashValue(hPtr);
3053 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
3054 UpdateOldValue(clientPtr, valueObjPtr);
3055 } else {
3056 Tcl_DecrRefCount(valueObjPtr);
3057 }
3058 Blt_DeleteHashEntry(tablePtr, hPtr);
3059 Tcl_InvalidateStringRep(valuePtr->objPtr);
3060
3061 finishrm:
3062 /*
3063 * Un-setting any element in the array can cause the trace on the value
3064 * to fire.
3065 */
3066 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
3067 return CallTraces(interp, clientPtr, nodePtr->treeObject, nodePtr,
3068 valuePtr->key, TREE_TRACE_WRITE, &cnt);
3069 }
3070 return TCL_OK;
3071 }
3072
3073 int
3074 Blt_TreeArrayNames(
3075 Tcl_Interp *interp,
3076 TreeClient *clientPtr,
3077 Node *nodePtr,
3078 CONST char *arrayName,
3079 Tcl_Obj *listObjPtr,
3080 CONST char *pattern)
3081 {
3082 Blt_HashEntry *hPtr;
3083 Blt_HashSearch cursor;
3084 Blt_HashTable *tablePtr;
3085 Tcl_Obj *objPtr;
3086 Value *valuePtr;
3087 char *key;
3088
3089 key = Blt_TreeKeyGet(interp, clientPtr->treeObject,arrayName);
3090 valuePtr = GetTreeValue(interp, clientPtr, nodePtr, key);
3091 if (valuePtr == NULL) {
3092 return TCL_ERROR;
3093 }
3094 if (IsTclDict(interp, valuePtr->objPtr)) {
3095 /* Preserve type if this was a dict */
3096
3097 Tcl_DictSearch search;
3098 Tcl_Obj *keyPtr;
3099 int done;
3100
3101 Tcl_DictObjFirst(NULL, valuePtr->objPtr, &search, &keyPtr, NULL, &done);
3102 for (; !done ; Tcl_DictObjNext(&search, &keyPtr, NULL, &done)) {
3103 if (!pattern || Tcl_StringMatch(Tcl_GetString(keyPtr), pattern)) {
3104 Tcl_ListObjAppendElement(NULL, listObjPtr, keyPtr);
3105 }
3106 }
3107 Tcl_DictObjDone(&search);
3108 return TCL_OK;
3109 }
3110
3111 if (Blt_IsArrayObj(valuePtr->objPtr) == 0 && Tcl_IsShared(valuePtr->objPtr)) {
3112 Tcl_DecrRefCount(valuePtr->objPtr);
3113 valuePtr->objPtr = Tcl_DuplicateObj(valuePtr->objPtr);
3114 Tcl_IncrRefCount(valuePtr->objPtr);
3115 }
3116 if (Blt_GetArrayFromObj(interp, valuePtr->objPtr, &tablePtr) != TCL_OK) {
3117 return TCL_ERROR;
3118 }
3119 /*tablePtr = (Blt_HashTable *)valuePtr->objPtr; */
3120 for (hPtr = Blt_FirstHashEntry(tablePtr, &cursor); hPtr != NULL;
3121 hPtr = Blt_NextHashEntry(&cursor)) {
3122 char *str;
3123
3124 str = Blt_GetHashKey(tablePtr, hPtr);
3125 if (pattern == NULL || Tcl_StringMatch(str, pattern)) {
3126 objPtr = Tcl_NewStringObj(str, -1);
3127 Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
3128 }
3129 }
3130 return TCL_OK;
3131 }
3132
3133 int
3134 Blt_TreeArrayValues(
3135 Tcl_Interp *interp,
3136 TreeClient *clientPtr,
3137 Node *nodePtr,
3138 CONST char *arrayName,
3139 Tcl_Obj *listObjPtr,
3140 int names)
3141 {
3142 Blt_HashEntry *hPtr;
3143 Blt_HashSearch cursor;
3144 Blt_HashTable *tablePtr;
3145 Tcl_Obj *objPtr;
3146 Value *valuePtr;
3147 char *key;
3148
3149 key = Blt_TreeKeyGet(interp, clientPtr->treeObject,arrayName);
3150 if ( bltTreeGetValueByKey(interp, clientPtr, nodePtr, key, &valuePtr) != TCL_OK) {
3151 return TCL_ERROR;
3152 }
3153 if (IsTclDict(interp, valuePtr->objPtr)) {
3154 /* Preserve type if this was a dict */
3155
3156 Tcl_DictSearch search;
3157 Tcl_Obj *keyPtr;
3158 int done;
3159 int result;
3160
3161 Tcl_DictObjFirst(NULL, valuePtr->objPtr, &search, &keyPtr, NULL, &done);
3162 for (; !done ; Tcl_DictObjNext(&search, &keyPtr, NULL, &done)) {
3163 Tcl_Obj *valueObjPtr;
3164 if (names) {
3165 Tcl_ListObjAppendElement(NULL, listObjPtr, keyPtr);
3166 }
3167
3168 valueObjPtr = NULL;
3169 result = Tcl_DictObjGet(interp, valuePtr->objPtr, keyPtr, &valueObjPtr);
3170 if (result != TCL_OK) {
3171 continue;
3172 }
3173 if (valueObjPtr == NULL) {
3174 valueObjPtr = Tcl_NewStringObj("",-1);
3175 }
3176 Tcl_ListObjAppendElement(NULL, listObjPtr, valueObjPtr);
3177 }
3178 Tcl_DictObjDone(&search);
3179 return TCL_OK;
3180 }
3181 if (Blt_IsArrayObj(valuePtr->objPtr) == 0 && Tcl_IsShared(valuePtr->objPtr)) {
3182 Tcl_DecrRefCount(valuePtr->objPtr);
3183 valuePtr->objPtr = Tcl_DuplicateObj(valuePtr->objPtr);
3184 Tcl_IncrRefCount(valuePtr->objPtr);
3185 }
3186 if (Blt_GetArrayFromObj(interp, valuePtr->objPtr, &tablePtr) != TCL_OK) {
3187 return TCL_ERROR;
3188 }
3189 /*tablePtr = (Blt_HashTable *)valuePtr->objPtr; */
3190 for (hPtr = Blt_FirstHashEntry(tablePtr, &cursor); hPtr != NULL;
3191 hPtr = Blt_NextHashEntry(&cursor)) {
3192 if (names) {
3193 Tcl_ListObjAppendElement(interp, listObjPtr,
3194 Tcl_NewStringObj( Blt_GetHashKey(tablePtr, hPtr), -1));
3195 }
3196 objPtr = Blt_GetHashValue(hPtr);
3197 Tcl_ListObjAppendElement(interp, listObjPtr, objPtr?objPtr:Tcl_NewStringObj("",-1));
3198 }
3199 return TCL_OK;
3200 }
3201
3202
3203 /*
3204 *----------------------------------------------------------------------
3205 *
3206 * Blt_TreeShareTagTable --
3207 *
3208 *----------------------------------------------------------------------
3209 */
3210 int
3211 Blt_TreeShareTagTable(
3212 TreeClient *sourcePtr,
3213 TreeClient *targetPtr)
3214 {
3215 sourcePtr->tagTablePtr->refCount++;
3216 if (targetPtr->tagTablePtr != NULL) {
3217 ReleaseTagTable(targetPtr->tagTablePtr);
3218 }
3219 targetPtr->tagTablePtr = sourcePtr->tagTablePtr;
3220 return TCL_OK;
3221 }
3222
3223 int
3224 Blt_TreeTagTableIsShared(TreeClient *clientPtr)
3225 {
3226 return (clientPtr->tagTablePtr->refCount > 1);
3227 }
3228
3229 void
3230 Blt_TreeClearTags(TreeClient *clientPtr, Blt_TreeNode node)
3231 {
3232 Blt_HashEntry *hPtr, *h2Ptr;
3233 Blt_HashSearch cursor;
3234
3235 for (hPtr = Blt_FirstHashEntry(&clientPtr->tagTablePtr->tagTable, &cursor);
3236 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
3237 Blt_TreeTagEntry *tPtr;
3238
3239 tPtr = Blt_GetHashValue(hPtr);
3240 h2Ptr = Blt_FindHashEntry(&tPtr->nodeTable, (char *)node);
3241 if (h2Ptr != NULL) {
3242 SetModified(node);
3243 Blt_DeleteHashEntry(&tPtr->nodeTable, h2Ptr);
3244 }
3245 }
3246 }
3247
3248 int
3249 Blt_TreeHasTag(
3250 TreeClient *clientPtr,
3251 Blt_TreeNode node,
3252 CONST char *tagName)
3253 {
3254 Blt_HashEntry *hPtr;
3255 Blt_TreeTagEntry *tPtr;
3256
3257 if (strcmp(tagName, "all") == 0) {
3258 return TRUE;
3259 }
3260 if (strcmp(tagName, "nonroot") == 0) {
3261 return TRUE;
3262 }
3263 if (strcmp(tagName, "rootchildren") == 0) {
3264 return TRUE;
3265 }
3266 if ((strcmp(tagName, "root") == 0) &&
3267 (node == Blt_TreeRootNode(clientPtr))) {
3268 return TRUE;
3269 }
3270 hPtr = Blt_FindHashEntry(&clientPtr->tagTablePtr->tagTable, tagName);
3271 if (hPtr == NULL) {
3272 return FALSE;
3273 }
3274 tPtr = Blt_GetHashValue(hPtr);
3275 hPtr = Blt_FindHashEntry(&tPtr->nodeTable, (char *)node);
3276 if (hPtr == NULL) {
3277 return FALSE;
3278 }
3279 return TRUE;
3280 }
3281
3282 int
3283 Blt_TreeAddTag(
3284 TreeClient *clientPtr,
3285 Blt_TreeNode node,
3286 CONST char *tagName)
3287 {
3288 int isNew, isNewN, cnt = 0, flags;
3289 Blt_HashEntry *hPtr;
3290 Blt_HashTable *tablePtr;
3291 Blt_TreeTagEntry *tPtr;
3292 Tcl_Interp *interp = clientPtr->treeObject->interp;
3293
3294 if ((strcmp(tagName, "all") == 0) || (strcmp(tagName, "root") == 0)
3295 || (strcmp(tagName, "nonroot") == 0) || (strcmp(tagName, "rootchildren") == 0)) {
3296 Tcl_AppendResult(interp, "reserved tag", 0);
3297 return TCL_ERROR;
3298 }
3299 tablePtr = &clientPtr->tagTablePtr->tagTable;
3300 hPtr = Blt_CreateHashEntry(tablePtr, tagName, &isNewN);
3301 assert(hPtr);
3302 if (isNewN) {
3303
3304 tPtr = Blt_Calloc(sizeof(Blt_TreeTagEntry), 1);
3305 Blt_InitHashTable(&tPtr->nodeTable, BLT_ONE_WORD_KEYS);
3306 Blt_SetHashValue(hPtr, tPtr);
3307 tPtr->hashPtr = hPtr;
3308 tPtr->tagName = Blt_GetHashKey(tablePtr, hPtr);
3309 Blt_TreeTagRefIncr(tPtr);
3310 } else {
3311 tPtr = Blt_GetHashValue(hPtr);
3312 }
3313 if (node == NULL) {
3314 return TCL_OK;
3315 }
3316 if (!(node->flags & TREE_TRACE_ACTIVE)) {
3317 int result;
3318 flags = TREE_TRACE_TAGADD;
3319 if (tPtr->nodeTable.numEntries > 0) {
3320 flags |= TREE_TRACE_TAGMULTIPLE;
3321 }
•3322 result = CallTraces(interp, clientPtr, node->treeObject, node, (Blt_TreeKey)tagName,
3323 flags, &cnt);
3324 if (result == TCL_BREAK) {
3325 return TCL_OK;
3326 }
3327 if (result != TCL_OK) {
3328 return result;
3329 }
3330 }
3331 hPtr = Blt_CreateHashEntry(&tPtr->nodeTable, (char *)node, &isNew);
3332 assert(hPtr);
3333 if (isNew) {
3334 SetModified(node);
3335 Blt_SetHashValue(hPtr, node);
3336 }
3337 return TCL_OK;
3338 }
3339
3340 /* Trigger tag delete traces. */
3341 int
3342 Blt_TreeTagDelTrace(
3343 TreeClient *clientPtr,
3344 Blt_TreeNode node,
3345 CONST char *tagName)
3346 {
3347 Tcl_Interp *interp = clientPtr->treeObject->interp;
3348 int cnt;
3349
3350 if (!(node->flags & TREE_TRACE_ACTIVE)) {
•3351 return CallTraces(interp, clientPtr, node->treeObject, node, (Blt_TreeKey)tagName,
3352 (TREE_TRACE_TAGDELETE), &cnt);
3353 }
3354 return TCL_OK;
3355 }
3356
3357 int
3358 Blt_TreeForgetTag(TreeClient *clientPtr, CONST char *tagName)
3359 {
3360 Blt_HashEntry *hPtr;
3361 if ((strcmp(tagName, "all") == 0) || (strcmp(tagName, "root") == 0)
3362 || (strcmp(tagName, "nonroot") == 0)
3363 || (strcmp(tagName, "rootchildren") == 0)) {
3364 return TCL_OK;
3365 }
3366
3367 hPtr = Blt_FindHashEntry(&clientPtr->tagTablePtr->tagTable, tagName);
3368 if (hPtr != NULL) {
3369 Blt_TreeTagEntry *tPtr;
3370 Blt_TreeNode node;
3371 Blt_HashSearch cursor;
3372
3373 Blt_DeleteHashEntry(&clientPtr->tagTablePtr->tagTable, hPtr);
3374 tPtr = Blt_GetHashValue(hPtr);
3375 for (hPtr = Blt_FirstHashEntry(&tPtr->nodeTable, &cursor);
3376 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
3377
3378 node = Blt_GetHashKey(&tPtr->nodeTable, hPtr);
3379 if (Blt_TreeTagDelTrace(clientPtr, node, tagName) != TCL_OK) {
3380 return TCL_ERROR;
3381 }
3382 SetModified(node);
3383 }
3384 Blt_DeleteHashTable(&tPtr->nodeTable);
3385 Blt_TreeTagRefDecr(tPtr);
3386 }
3387 return TCL_OK;
3388 }
3389
3390 /*
3391 *----------------------------------------------------------------------
3392 *
3393 * Blt_TreeTagHashTable --
3394 *
3395 *----------------------------------------------------------------------
3396 */
3397 Blt_HashTable *
3398 Blt_TreeTagHashTable(TreeClient *clientPtr, CONST char *tagName)
3399 {
3400 Blt_HashEntry *hPtr;
3401
3402 hPtr = Blt_FindHashEntry(&clientPtr->tagTablePtr->tagTable, tagName);
3403 if (hPtr != NULL) {
3404 Blt_TreeTagEntry *tPtr;
3405
3406 tPtr = Blt_GetHashValue(hPtr);
3407 return &tPtr->nodeTable;
3408 }
3409 return NULL;
3410 }
3411
3412 Blt_TreeTagEntry *
3413 Blt_TreeTagHashEntry(TreeClient *clientPtr, CONST char *tagName)
3414 {
3415 Blt_HashEntry *hPtr;
3416
3417 hPtr = Blt_FindHashEntry(&clientPtr->tagTablePtr->tagTable, tagName);
3418 if (hPtr != NULL) {
3419 Blt_TreeTagEntry *tPtr;
3420
3421 tPtr = Blt_GetHashValue(hPtr);
3422 return tPtr;
3423 }
3424 return NULL;
3425 }
3426
3427 Blt_HashEntry *
3428 Blt_TreeFirstTag(TreeClient *clientPtr, Blt_HashSearch *cursorPtr)
3429 {
3430 return Blt_FirstHashEntry(&clientPtr->tagTablePtr->tagTable, cursorPtr);
3431 }
3432
3433 #if (SIZEOF_VOID_P == 8)
3434 /*
3435 *----------------------------------------------------------------------
3436 *
3437 * HashOneWord --
3438 *
3439 * Compute a one-word hash value of a 64-bit word, which then can
3440 * be used to generate a hash index.
3441 *
3442 * From Knuth, it's a multiplicative hash. Multiplies an unsigned
3443 * 64-bit value with the golden ratio (sqrt(5) - 1) / 2. The
3444 * downshift value is 64 - n, when n is the log2 of the size of
3445 * the hash table.
3446 *
3447 * Results:
3448 * The return value is a one-word summary of the information in
3449 * 64 bit word.
3450 *
3451 * Side effects:
3452 * None.
3453 *
3454 *----------------------------------------------------------------------
3455 */
3456 static Blt_Hash
3457 HashOneWord(
3458 uint64_t mask,
3459 unsigned int downshift,
3460 CONST void *key)
3461 {
3462 uint64_t a0, a1;
3463 uint64_t y0, y1;
3464 uint64_t y2, y3;
3465 uint64_t p1, p2;
3466 uint64_t result;
3467
3468 /* Compute key * GOLDEN_RATIO in 128-bit arithmetic */
3469 a0 = (uint64_t)key & 0x00000000FFFFFFFF;
3470 a1 = (uint64_t)key >> 32;
3471
3472 y0 = a0 * 0x000000007f4a7c13;
3473 y1 = a0 * 0x000000009e3779b9;
3474 y2 = a1 * 0x000000007f4a7c13;
3475 y3 = a1 * 0x000000009e3779b9;
3476 y1 += y0 >> 32; /* Can't carry */
3477 y1 += y2; /* Might carry */
3478 if (y1 < y2) {
3479 y3 += (1LL << 32); /* Propagate */
3480 }
3481
3482 /* 128-bit product: p1 = loword, p2 = hiword */
3483 p1 = ((y1 & 0x00000000FFFFFFFF) << 32) + (y0 & 0x00000000FFFFFFFF);
3484 p2 = y3 + (y1 >> 32);
3485
3486 /* Left shift the value downward by the size of the table */
3487 if (downshift > 0) {
3488 if (downshift < 64) {
3489 result = ((p2 << (64 - downshift)) | (p1 >> (downshift & 63)));
3490 } else {
3491 result = p2 >> (downshift & 63);
3492 }
3493 } else {
3494 result = p1;
3495 }
3496 /* Finally mask off the high bits */
3497 return (Blt_Hash)(result & mask);
3498 }
3499
3500 #endif /* SIZEOF_VOID_P == 8 */
3501
3502 /*
3503 *----------------------------------------------------------------------
3504 *
3505 * RebuildTable --
3506 *
3507 * This procedure is invoked when the ratio of entries to hash
3508 * buckets becomes too large. It creates a new table with a
3509 * larger bucket array and moves all of the entries into the
3510 * new table.
3511 *
3512 * Results:
3513 * None.
3514 *
3515 * Side effects:
3516 * Memory gets reallocated and entries get re-hashed to new
3517 * buckets.
3518 *
3519 *----------------------------------------------------------------------
3520 */
3521 static void
3522 RebuildTable(Node *nodePtr) /* Table to enlarge. */
3523 {
3524 Value **newBucketPtr, **oldBuckets;
3525 register Value **bucketPtr, **endPtr;
3526 register Value *valuePtr, *nextPtr;
3527 unsigned int downshift;
3528 unsigned long mask;
3529 Value **buckets;
3530 size_t nBuckets;
3531
3532 oldBuckets = (Value **)nodePtr->values;
3533 nBuckets = (1 << nodePtr->logSize);
3534 endPtr = oldBuckets + nBuckets;
3535
3536 /*
3537 * Allocate and initialize the new bucket array, and set up
3538 * hashing constants for new array size.
3539 */
3540 nodePtr->logSize += 2;
3541 nBuckets = (1 << nodePtr->logSize);
3542 buckets = Blt_Calloc(nBuckets, sizeof(Value *));
3543
3544 /*
3545 * Move all of the existing entries into the new bucket array,
3546 * based on their hash values.
3547 */
3548 mask = nBuckets - 1;
3549 downshift = DOWNSHIFT_START - nodePtr->logSize;
3550 for (bucketPtr = oldBuckets; bucketPtr < endPtr; bucketPtr++) {
3551 for (valuePtr = *bucketPtr; valuePtr != NULL; valuePtr = nextPtr) {
3552 nextPtr = valuePtr->next;
3553 newBucketPtr = buckets + RANDOM_INDEX(valuePtr->key);
3554 valuePtr->next = *newBucketPtr;
3555 *newBucketPtr = valuePtr;
3556 }
3557 }
3558 nodePtr->values = (Value *)buckets;
3559 Blt_Free(oldBuckets);
3560 }
3561
3562 static void
3563 ConvertValues(Node *nodePtr)
3564 {
3565 unsigned int nBuckets;
3566 Value **buckets;
3567 unsigned int mask;
3568 int downshift;
3569 Value *valuePtr, *nextPtr, **bucketPtr;
3570
3571 /*
3572 * Convert list of values into a hash table.
3573 */
3574 nodePtr->logSize = START_LOGSIZE;
3575 nBuckets = 1 << nodePtr->logSize;
3576 buckets = Blt_Calloc(nBuckets, sizeof(Value *));
3577 mask = nBuckets - 1;
3578 downshift = DOWNSHIFT_START - nodePtr->logSize;
3579 for (valuePtr = nodePtr->values; valuePtr != NULL; valuePtr = nextPtr) {
3580 nextPtr = valuePtr->next;
3581 bucketPtr = buckets + RANDOM_INDEX(valuePtr->key);
3582 valuePtr->next = *bucketPtr;
3583 *bucketPtr = valuePtr;
3584 }
3585 nodePtr->values = (Value *)buckets;
3586 }
3587
3588 /*
3589 *----------------------------------------------------------------------
3590 *
3591 * TreeDeleteValue --
3592 *
3593 * Remove a single entry from a hash table.
3594 *
3595 * Results:
3596 * None.
3597 *
3598 * Side effects:
3599 * The entry given by entryPtr is deleted from its table and
3600 * should never again be used by the caller. It is up to the
3601 * caller to free the clientData field of the entry, if that
3602 * is relevant.
3603 *
3604 *----------------------------------------------------------------------
3605 */
3606 static int
3607 TreeDeleteValue(Node *nodePtr, Blt_TreeValue value)
3608 {
3609 Value *valuePtr = value;
3610 register Value *prevPtr;
3611
3612 if (nodePtr->logSize > 0) {
3613 Value **bucketPtr;
3614 unsigned int downshift;
3615 unsigned long mask;
3616
3617 mask = (1 << nodePtr->logSize) - 1;
3618 downshift = DOWNSHIFT_START - nodePtr->logSize;
3619
3620 bucketPtr = (Value **)nodePtr->values + RANDOM_INDEX(valuePtr->key);
3621 if (*bucketPtr == valuePtr) {
3622 *bucketPtr = valuePtr->next;
3623 } else {
3624 for (prevPtr = *bucketPtr; /*empty*/; prevPtr = prevPtr->next) {
3625 if (prevPtr == NULL) {
3626 return TCL_ERROR; /* Can't find value in hash bucket. */
3627 }
3628 if (prevPtr->next == valuePtr) {
3629 prevPtr->next = valuePtr->next;
3630 break;
3631 }
3632 }
3633 }
3634 } else {
3635 prevPtr = NULL;
3636 for (valuePtr = nodePtr->values; valuePtr != NULL;
3637 valuePtr = valuePtr->next) {
3638 if (valuePtr == value) {
3639 break;
3640 }
3641 prevPtr = valuePtr;
3642 }
3643 if (valuePtr == NULL) {
3644 return TCL_ERROR; /* Can't find value in list. */
3645 }
3646 if (prevPtr == NULL) {
3647 nodePtr->values = valuePtr->next;
3648 } else {
3649 prevPtr->next = valuePtr->next;
3650 }
3651 }
3652 nodePtr->nValues--;
3653 FreeValue(nodePtr, valuePtr);
3654 return TCL_OK;
3655 }
3656
3657 /*
3658 *----------------------------------------------------------------------
3659 *
3660 * TreeDestroyValues --
3661 *
3662 * Free up everything associated with a hash table except for
3663 * the record for the table itself.
3664 *
3665 * Results:
3666 * None.
3667 *
3668 * Side effects:
3669 * The hash table is no longer useable.
3670 *
3671 *----------------------------------------------------------------------
3672 */
3673 static void
3674 TreeDestroyValues(Node *nodePtr)
3675 {
3676 register Value *valuePtr;
3677 Value *nextPtr;
3678
3679 /*
3680 * Free up all the entries in the table.
3681 */
3682 if (nodePtr->values == NULL) {
3683 return;
3684 }
3685 if (nodePtr->logSize > 0) {
3686 Value **buckets;
3687 int nBuckets;
3688 int i;
3689
3690 buckets = (Value **)nodePtr->values;
3691 nBuckets = (1 << nodePtr->logSize);
3692 for (i = 0; i < nBuckets; i++) {
3693 for (valuePtr = buckets[i]; valuePtr != NULL; valuePtr = nextPtr) {
3694 nextPtr = valuePtr->next;
3695 FreeValue(nodePtr, valuePtr);
3696 }
3697 }
3698 Blt_Free(buckets);
3699 } else {
3700 for (valuePtr = nodePtr->values; valuePtr != NULL; valuePtr = nextPtr) {
3701 nextPtr = valuePtr->next;
3702 FreeValue(nodePtr, valuePtr);
3703 }
3704 }
3705 nodePtr->values = NULL;
3706 nodePtr->nValues = 0;
3707 nodePtr->logSize = 0;
3708 }
3709
3710 /*
3711 *----------------------------------------------------------------------
3712 *
3713 * TreeFirstValue --
3714 *
3715 * Locate the first entry in a hash table and set up a record
3716 * that can be used to step through all the remaining entries
3717 * of the table.
3718 *
3719 * Results:
3720 * The return value is a pointer to the first value in tablePtr,
3721 * or NULL if tablePtr has no entries in it. The memory at
3722 * *searchPtr is initialized so that subsequent calls to
3723 * Blt_TreeNextValue will return all of the values in the table,
3724 * one at a time.
3725 *
3726 * Side effects:
3727 * None.
3728 *
3729 *----------------------------------------------------------------------
3730 */
3731 static Value *
3732 TreeFirstValue(
3733 Node *nodePtr,
3734 Blt_TreeKeySearch *searchPtr) /* Place to store information about
3735 * progress through the table. */
3736 {
3737 searchPtr->node = nodePtr;
3738 searchPtr->nextIndex = 0;
3739 searchPtr->cnt = 1;
3740 if (nodePtr->logSize > 0) {
3741 searchPtr->nextValue = NULL;
3742 } else {
3743 searchPtr->nextValue = nodePtr->values;
3744 }
3745 return TreeNextValue(searchPtr);
3746 }
3747
3748 /*
3749 *----------------------------------------------------------------------
3750 *
3751 * TreeNextValue --
3752 *
3753 * Once a hash table enumeration has been initiated by calling
3754 * Blt_TreeFirstValue, this procedure may be called to return
3755 * successive elements of the table.
3756 *
3757 * Results:
3758 * The return value is the next entry in the hash table being
3759 * enumerated, or NULL if the end of the table is reached.
3760 *
3761 * Side effects:
3762 * None.
3763 *
3764 *----------------------------------------------------------------------
3765 */
3766 static Value *
3767 TreeNextValue(
3768 Blt_TreeKeySearch *searchPtr) /* Place to store information about
3769 * progress through the table. Must
3770 * have been initialized by calling
3771 * Blt_TreeFirstValue. */
3772 {
3773 Value *valuePtr;
3774
3775 if (searchPtr->node->logSize > 0) {
3776 size_t nBuckets;
3777 Value **buckets;
3778
3779 nBuckets = (1 << searchPtr->node->logSize);
3780 buckets = (Value **)searchPtr->node->values;
3781 while (searchPtr->nextValue == NULL) {
3782 if (searchPtr->nextIndex >= nBuckets) {
3783 return NULL;
3784 }
3785 searchPtr->nextValue = buckets[searchPtr->nextIndex];
3786 searchPtr->nextIndex++;
3787 }
3788 }
3789 searchPtr->cnt++;
3790 /* Sanity check. */
3791 if (searchPtr->cnt>100000000) return NULL;
3792 valuePtr = searchPtr->nextValue;
3793 if (valuePtr != NULL) {
3794 searchPtr->nextValue = valuePtr->next;
3795 }
3796 return valuePtr;
3797 }
3798
3799 /*
3800 *----------------------------------------------------------------------
3801 *
3802 * TreeFindValue --
3803 *
3804 * Given a hash table with one-word keys, and a one-word key, find
3805 * the entry with a matching key.
3806 *
3807 * Results:
3808 * The return value is a token for the matching entry in the
3809 * hash table, or NULL if there was no matching entry.
3810 *
3811 * Side effects:
3812 * None.
3813 *
3814 *----------------------------------------------------------------------
3815 */
3816 static Value *
3817 TreeFindValue(
3818 Node *nodePtr,
3819 Blt_TreeKey key) /* Key to use to find matching entry. */
3820 {
3821 register Value *valuePtr;
3822 Value *bucket;
3823
3824 if (nodePtr->logSize > 0) {
3825 unsigned int downshift;
3826 unsigned long mask;
3827
3828 mask = (1 << nodePtr->logSize) - 1;
3829 downshift = DOWNSHIFT_START - nodePtr->logSize;
3830 bucket = ((Value **)(nodePtr->values))[RANDOM_INDEX((void *)key)];
3831 } else {
3832 bucket = nodePtr->values; /* Single chain list. */
3833 }
3834 /*
3835 * Search all of the entries in the appropriate bucket.
3836 */
3837 for (valuePtr = bucket; valuePtr != NULL; valuePtr = valuePtr->next) {
3838 if (valuePtr->key == key) {
3839 return valuePtr;
3840 }
3841 }
3842 return NULL;
3843 }
3844
3845 /*
3846 *----------------------------------------------------------------------
3847 *
3848 * Blt_TreeCreateValue --
3849 *
3850 * Find the value with a matching key. If there is no matching
3851 * value, then create a new one.
3852 *
3853 * Results:
3854 * The return value is a pointer to the matching value. If this
3855 * is a newly-created value, then *newPtr will be set to a non-zero
3856 * value; otherwise *newPtr will be set to 0.
3857 *
3858 * Side effects:
3859 * A new value may be added to the hash table.
3860 *
3861 *----------------------------------------------------------------------
3862 */
3863 static Value *
3864 TreeCreateValue(
3865 Node *nodePtr,
3866 Blt_TreeKey key, /* Key to use to find or create matching
3867 * entry. */
3868 int *newPtr) /* Store info here telling whether a new
3869 * entry was created. */
3870 {
3871 register Value *valuePtr;
3872 TreeObject *treeObjPtr = nodePtr->treeObject;
3873 int maxVals;
3874
3875 if (treeObjPtr->maxKeyList>0) {
3876 maxVals = treeObjPtr->maxKeyList;
3877 } else {
3878 maxVals = MAX_LIST_VALUES;
3879 }
3880 /*
3881 * Check if there as so many values that storage should be
3882 * converted from a hash table instead of a list.
3883 */
3884 if ((nodePtr->logSize == 0) && (nodePtr->nValues >= maxVals)) {
3885 ConvertValues(nodePtr);
3886 }
3887 if (nodePtr->logSize > 0) {
3888 Value **bucketPtr;
3889 size_t nBuckets;
3890 unsigned int downshift;
3891 unsigned long mask;
3892
3893 nBuckets = (1 << nodePtr->logSize);
3894 mask = nBuckets - 1;
3895 downshift = DOWNSHIFT_START - nodePtr->logSize;
3896 bucketPtr = (Value **)nodePtr->values + RANDOM_INDEX((void *)key);
3897
3898 *newPtr = FALSE;
3899 for (valuePtr = *bucketPtr; valuePtr != NULL;
3900 valuePtr = valuePtr->next) {
3901 if (valuePtr->key == key) {
3902 return valuePtr;
3903 }
3904 }
3905
3906 /* Value not found. Add a new value to the bucket. */
3907 *newPtr = TRUE;
3908 valuePtr = Blt_PoolAllocItem(nodePtr->treeObject->valuePool,
3909 sizeof(Value));
3910 valuePtr->key = key;
3911 valuePtr->owner = NULL;
3912 valuePtr->next = *bucketPtr;
3913 valuePtr->objPtr = NULL;
3914 *bucketPtr = valuePtr;
3915 nodePtr->nValues++;
3916
3917 /*
3918 * If the table has exceeded a decent size, rebuild it with many
3919 * more buckets.
3920 */
3921 if ((unsigned int)nodePtr->nValues >= (nBuckets * 3)) {
3922 RebuildTable(nodePtr);
3923 }
3924 } else {
3925 Value *prevPtr;
3926
3927 prevPtr = NULL;
3928 *newPtr = FALSE;
3929 for (valuePtr = nodePtr->values; valuePtr != NULL;
3930 valuePtr = valuePtr->next) {
3931 if (valuePtr->key == key) {
3932 return valuePtr;
3933 }
3934 prevPtr = valuePtr;
3935 }
3936 /* Value not found. Add a new value to the list. */
3937 *newPtr = TRUE;
3938 valuePtr = Blt_PoolAllocItem(nodePtr->treeObject->valuePool,
3939 sizeof(Value));
3940 valuePtr->key = key;
3941 valuePtr->owner = NULL;
3942 valuePtr->next = NULL;
3943 valuePtr->objPtr = NULL;
3944 if (prevPtr == NULL) {
3945 nodePtr->values = valuePtr;
3946 } else {
3947 prevPtr->next = valuePtr;
3948 }
3949 nodePtr->nValues++;
3950 }
3951 return valuePtr;
3952 }
3953
3954
3955 Blt_TreeNode Blt_TreeEndNode (Blt_TreeNode node,
3956 unsigned int nodeFlags) {
3957 /* TODO: finish. */
3958 return NULL;
3959 }
3960
3961 #undef Blt_TreeFirstChild
3962 #undef Blt_TreeLastChild
3963 #undef Blt_TreeNextSibling
3964 #undef Blt_TreePrevSibling
3965 #undef Blt_TreeChangeRoot
3966
3967 Blt_TreeNode Blt_TreeFirstChild (Blt_TreeNode parent) {
3968 return parent->first;
3969 }
3970
3971 Blt_TreeNode Blt_TreeNextSibling (Blt_TreeNode node) {
3972 return (node == NULL ? NULL : node->next);
3973 }
3974
3975 Blt_TreeNode Blt_TreeLastChild (Blt_TreeNode parent) {
3976 return parent->last;
3977 }
3978
3979 Blt_TreeNode Blt_TreePrevSibling (Blt_TreeNode node) {
3980 return (node == NULL ? NULL : node->prev);
3981 }
3982
3983 Blt_TreeNode Blt_TreeChangeRoot (Blt_Tree tree, Blt_TreeNode node) {
3984 tree->root = node;
3985 return node;
3986 }
3987
3988 #endif /* NO_TREE */