Debian Codegraph @

generic/bltTree.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 4 — not the installed (fully patched) source.
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 = 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 *)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 *)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 *)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 *)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 *)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, inode;
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 inode = nodePtr->inode;
1249 result = NotifyClients(clientPtr, clientPtr->treeObject, nodePtr,
1250 TREE_NOTIFY_RELABELPOST);
1251 return result;
1252 }
1253
1254 int
1255 Blt_TreeNotifyAttach(TreeClient *clientPtr)
1256 {
1257 /* return NotifyClients(clientPtr, clientPtr->treeObject, clientPtr->root,
1258 TREE_NOTIFY_ATTACH); */
1259 return TCL_OK;
1260 }
1261
1262
1263 int
1264 Blt_TreeRelabelNode2(Node *nodePtr, CONST char *string)
1265 {
1266 nodePtr->label = Blt_TreeKeyGet(NULL, nodePtr->treeObject,string);
1267 SetModified(nodePtr);
1268 return TCL_OK;
1269 }
1270
1271 /*
1272 *----------------------------------------------------------------------
1273 *
1274 * Blt_TreeFindChild --
1275 *
1276 * Searches for the named node in a parent's chain of siblings.
1277 *
1278 *
1279 * Results:
1280 * If found, the child node is returned, otherwise NULL.
1281 *
1282 *----------------------------------------------------------------------
1283 */
1284 Blt_TreeNode
1285 Blt_TreeFindChild(Node *parentPtr, CONST char *string)
1286 {
1287 Blt_TreeKey label;
1288 register Node *nodePtr;
1289
1290 label = Blt_TreeKeyGet(NULL, parentPtr->treeObject,string);
1291 for (nodePtr = parentPtr->first; nodePtr != NULL; nodePtr = nodePtr->next) {
1292 if (label == nodePtr->label) {
1293 return nodePtr;
1294 }
1295 }
1296 return NULL;
1297 }
1298 /*
1299 * Like Blt_TreeFindChild above, but looks forward firstN elements
1300 * from front, then falls back to reverse search starting from the end.
1301 * This speeds up insertion at the end of really long trees in treeview.
1302 * If firstN is < 0, just calls Blt_TreeFindChild.
1303 */
1304 Blt_TreeNode
1305 Blt_TreeFindChildRev(Node *parentPtr, CONST char *string, int firstN)
1306 {
1307 Blt_TreeKey label;
1308 register Node *nodePtr, *endNode;
1309 int n;
1310
1311 if (firstN<0) {
1312 return Blt_TreeFindChild(parentPtr, string);
1313 }
1314 label = Blt_TreeKeyGet(NULL, parentPtr->treeObject,string);
1315 for (nodePtr = parentPtr->first, n = 0;
1316 nodePtr != NULL && n < firstN;
1317 nodePtr = nodePtr->next, n++) {
1318 if (label == nodePtr->label) {
1319 return nodePtr;
1320 }
1321 }
1322 if (nodePtr == NULL) {
1323 return NULL;
1324 }
1325 endNode = nodePtr;
1326 for (nodePtr = parentPtr->last; nodePtr != NULL; nodePtr = nodePtr->prev) {
1327 if (label == nodePtr->label) {
1328 return nodePtr;
1329 }
1330 if (nodePtr == endNode) break;
1331 }
1332 return NULL;
1333 }
1334
1335 /*
1336 *----------------------------------------------------------------------
1337 *
1338 * Blt_TreeNodePosition --
1339 *
1340 * Returns the position of the node in its parent's list of
1341 * children. The root's position is 0.
1342 *
1343 *----------------------------------------------------------------------
1344 */
1345 int
1346 Blt_TreeNodePosition(Node *nodePtr)
1347 {
1348 Node *parentPtr;
1349 int count;
1350
1351 count = 0;
1352 parentPtr = nodePtr->parent;
1353 if (parentPtr != NULL) {
1354 Node *childPtr;
1355
1356 for (childPtr = parentPtr->first; childPtr != NULL;
1357 childPtr = childPtr->next) {
1358 if (nodePtr == childPtr) {
1359 break;
1360 }
1361 count++;
1362 }
1363 }
1364 return count;
1365 }
1366
1367
1368 /*
1369 *----------------------------------------------------------------------
1370 *
1371 * Blt_TreePrevNode --
1372 *
1373 * Returns the "previous" node in the tree. This node (in
1374 * depth-first order) is its parent, if the node has no siblings
1375 * that are previous to it. Otherwise it is the last descendant
1376 * of the last sibling. In this case, descend the sibling's
1377 * hierarchy, using the last child at any ancestor, with we
1378 * we find a leaf.
1379 *
1380 *----------------------------------------------------------------------
1381 */
1382 Blt_TreeNode
1383 Blt_TreePrevNode(Node *rootPtr, Node *nodePtr)
1384 {
1385 Node *prevPtr;
1386
1387 if (nodePtr == rootPtr) {
1388 return NULL; /* The root is the first node. */
1389 }
1390 prevPtr = nodePtr->prev;
1391 if (prevPtr == NULL) {
1392 /* There are no siblings previous to this one, so pick the parent. */
1393 return nodePtr->parent;
1394 }
1395 /*
1396 * Traverse down the right-most thread, in order to select the
1397 * next entry. Stop when we reach a leaf.
1398 */
1399 nodePtr = prevPtr;
1400 while ((prevPtr = nodePtr->last) != NULL) {
1401 nodePtr = prevPtr;
1402 }
1403 return nodePtr;
1404 }
1405
1406 /*
1407 *----------------------------------------------------------------------
1408 *
1409 * Blt_TreeNextNode --
1410 *
1411 * Returns the "next" node in relation to the given node.
1412 * The next node (in depth-first order) is either the first
1413 * child of the given node the next sibling if the node has
1414 * no children (the node is a leaf). If the given node is the
1415 * last sibling, then try it's parent next sibling. Continue
1416 * until we either find a next sibling for some ancestor or
1417 * we reach the root node. In this case the current node is
1418 * the last node in the tree.
1419 *
1420 *----------------------------------------------------------------------
1421 */
1422 Blt_TreeNode
1423 Blt_TreeNextNode(Node *rootPtr, Node *nodePtr)
1424 {
1425 Node *nextPtr;
1426
1427 /* Pick the first sub-node. */
1428 nextPtr = nodePtr->first;
1429 if (nextPtr != NULL) {
1430 return nextPtr;
1431 }
1432 /*
1433 * Back up until we can find a level where we can pick a
1434 * "next sibling". For the last entry we'll thread our
1435 * way back to the root.
1436 */
1437 while (nodePtr != rootPtr) {
1438 nextPtr = nodePtr->next;
1439 if (nextPtr != NULL) {
1440 return nextPtr;
1441 }
1442 nodePtr = nodePtr->parent;
1443 }
1444 return NULL; /* At root, no next node. */
1445 }
1446
1447
1448 int
1449 Blt_TreeIsBefore(Node *n1Ptr, Node *n2Ptr)
1450 {
1451 int depth;
1452 register int i;
1453 Node *nodePtr;
1454
1455 if (n1Ptr == n2Ptr) {
1456 return FALSE;
1457 }
1458 depth = MIN(n1Ptr->depth, n2Ptr->depth);
1459 if (depth == 0) { /* One of the nodes is root. */
1460 return (n1Ptr->parent == NULL);
1461 }
1462 /*
1463 * Traverse back from the deepest node, until both nodes are at
1464 * the same depth. Check if this ancestor node is the same for
1465 * both nodes.
1466 */
1467 for (i = n1Ptr->depth; i > depth; i--) {
1468 n1Ptr = n1Ptr->parent;
1469 }
1470 if (n1Ptr == n2Ptr) {
1471 return FALSE;
1472 }
1473 for (i = n2Ptr->depth; i > depth; i--) {
1474 n2Ptr = n2Ptr->parent;
1475 }
1476 if (n2Ptr == n1Ptr) {
1477 return TRUE;
1478 }
1479
1480 /*
1481 * First find the mutual ancestor of both nodes. Look at each
1482 * preceding ancestor level-by-level for both nodes. Eventually
1483 * we'll find a node that's the parent of both ancestors. Then
1484 * find the first ancestor in the parent's list of subnodes.
1485 */
1486 for (i = depth; i > 0; i--) {
1487 if (n1Ptr->parent == n2Ptr->parent) {
1488 break;
1489 }
1490 n1Ptr = n1Ptr->parent;
1491 n2Ptr = n2Ptr->parent;
1492 }
1493 for (nodePtr = n1Ptr->parent->first; nodePtr != NULL;
1494 nodePtr = nodePtr->next) {
1495 if (nodePtr == n1Ptr) {
1496 return TRUE;
1497 } else if (nodePtr == n2Ptr) {
1498 return FALSE;
1499 }
1500 }
1501 return FALSE;
1502 }
1503
1504 static int
1505 CallTraces(
1506 Tcl_Interp *interp,
1507 TreeClient *sourcePtr, /* Client holding a reference to the
1508 * tree. If NULL, indicates to
1509 * execute all handlers, including
1510 * those of the caller. */
1511 TreeObject *treeObjPtr, /* Tree that was changed. */
1512 Node *nodePtr, /* Node that received the event. */
1513 Blt_TreeKey key,
1514 unsigned int flags,
1515 int *cnt)
1516 {
1517 Blt_ChainLink *l1Ptr, *l2Ptr;
1518 TreeClient *clientPtr;
1519 TraceHandler *tracePtr;
1520 unsigned int inode = nodePtr->inode;
1521 int result;
1522
1523 for(l1Ptr = Blt_ChainFirstLink(treeObjPtr->clients);
1524 l1Ptr != NULL; l1Ptr = Blt_ChainNextLink(l1Ptr)) {
1525 clientPtr = Blt_ChainGetValue(l1Ptr);
1526 for(l2Ptr = Blt_ChainFirstLink(clientPtr->traces);
1527 l2Ptr != NULL; l2Ptr = Blt_ChainNextLink(l2Ptr)) {
1528 tracePtr = Blt_ChainGetValue(l2Ptr);
1529 if ((tracePtr->mask & flags) == 0) {
1530 continue; /* Flags don't match. */
1531 }
1532 if ((clientPtr == sourcePtr) &&
1533 (tracePtr->mask & TREE_TRACE_FOREIGN_ONLY)) {
1534 continue; /* This client initiated the trace. */
1535 }
1536 if ((tracePtr->nodePtr != NULL) && (tracePtr->nodePtr != nodePtr)) {
1537 continue; /* Nodes don't match. */
1538 }
1539 if ((tracePtr->keyPattern != NULL) &&
1540 (!Tcl_StringMatch(key, tracePtr->keyPattern))) {
1541 continue; /* Key pattern doesn't match. */
1542 }
1543 if ((tracePtr->withTag != NULL) &&
1544 (!Blt_TreeHasTag(clientPtr, nodePtr, tracePtr->withTag))) {
1545 continue; /* Doesn't have the tag. */
1546 }
1547 nodePtr->flags |= TREE_TRACE_ACTIVE;
1548 *cnt += 1;
1549
1550 Tcl_Preserve(treeObjPtr);
1551 result = (*tracePtr->proc) (tracePtr->clientData, treeObjPtr->interp,
1552 nodePtr, key, flags);
1553 if (result != TCL_OK) {
1554 Tcl_Release(treeObjPtr);
1555 if ((tracePtr->mask & TREE_TRACE_BGERROR) && interp != NULL) {
1556 Tcl_BackgroundError(interp);
1557 } else {
1558 nodePtr->flags &= ~TREE_TRACE_ACTIVE;
1559 return TCL_ERROR;
1560 }
1561 }
1562 nodePtr->flags &= ~TREE_TRACE_ACTIVE;
1563 if (Blt_TreeNodeDeleted(nodePtr) || nodePtr->inode != inode) {
1564 Tcl_Release(treeObjPtr);
1565 return TCL_ERROR;
1566 }
1567 if (treeObjPtr->delete) {
1568 Tcl_Release(treeObjPtr);
1569 if (interp != NULL) {
1570 Tcl_AppendResult(interp, "tree deleted", 0);
1571 }
1572 return TCL_ERROR;
1573 }
1574 Tcl_Release(treeObjPtr);
1575 /* nodePtr = GetNode(treeObjPtr, inode);
1576 if (nodePtr == NULL) {
1577 if (interp != NULL) {
1578 Tcl_AppendResult(interp, "node deleted", 0);
1579 }
1580 return TCL_ERROR;
1581 }*/
1582 }
1583 }
1584 return TCL_OK;
1585 }
1586
1587 static Value *
1588 GetTreeValue(
1589 Tcl_Interp *interp,
1590 TreeClient *clientPtr,
1591 Node *nodePtr,
1592 Blt_TreeKey key)
1593 {
1594 register Value *valuePtr;
1595
1596 valuePtr = TreeFindValue(nodePtr, key);
1597 if (valuePtr == NULL) {
1598 if (interp != NULL) {
1599 Tcl_AppendResult(interp, "can't find field \"", key, "\"",
1600 (char *)NULL);
1601 }
1602 return NULL;
1603 }
1604 if ((valuePtr->owner != NULL) && (valuePtr->owner != clientPtr)) {
1605 if (interp != NULL) {
1606 Tcl_AppendResult(interp, "can't access private field \"",
1607 key, "\"", (char *)NULL);
1608 }
1609 return NULL;
1610 }
1611 return valuePtr;
1612 }
1613
1614 int
1615 Blt_TreePrivateValue(
1616 Tcl_Interp *interp,
1617 TreeClient *clientPtr,
1618 Node *nodePtr,
1619 Blt_TreeKey key)
1620 {
1621 Value *valuePtr;
1622
1623 valuePtr = TreeFindValue(nodePtr, key);
1624 if (valuePtr == NULL) {
1625 if (interp != NULL) {
1626 Tcl_AppendResult(interp, "can't find field \"", key, "\"",
1627 (char *)NULL);
1628 }
1629 return TCL_ERROR;
1630 }
1631 valuePtr->owner = clientPtr;
1632 return TCL_OK;
1633 }
1634
1635 int
1636 Blt_TreePublicValue(
1637 Tcl_Interp *interp,
1638 TreeClient *clientPtr,
1639 Node *nodePtr,
1640 Blt_TreeKey key)
1641 {
1642 Value *valuePtr;
1643
1644 valuePtr = TreeFindValue(nodePtr, key);
1645 if (valuePtr == NULL) {
1646 if (interp != NULL) {
1647 Tcl_AppendResult(interp, "can't find field \"", key, "\"",
1648 (char *)NULL);
1649 }
1650 return TCL_ERROR;
1651 }
1652 if (valuePtr->owner != clientPtr) {
1653 if (interp != NULL) {
1654 Tcl_AppendResult(interp, "not the owner of \"", key, "\"",
1655 (char *)NULL);
1656 }
1657 return TCL_ERROR;
1658 }
1659 valuePtr->owner = NULL;
1660 return TCL_OK;
1661 }
1662
1663 int
1664 Blt_TreeValueExistsByKey(clientPtr, nodePtr, key)
1665 TreeClient *clientPtr;
1666 Node *nodePtr;
1667 Blt_TreeKey key;
1668 {
1669 register Value *valuePtr;
1670 int cnt;
1671 TreeObject *treeObjPtr = nodePtr->treeObject;
1672 Tcl_Interp *interp = treeObjPtr->interp;
1673
1674 valuePtr = GetTreeValue((Tcl_Interp *)NULL, clientPtr, nodePtr, key);
1675 if (valuePtr == NULL && (!(nodePtr->flags & TREE_TRACE_ACTIVE))) {
1676 if (CallTraces(interp, clientPtr, treeObjPtr, nodePtr, key,
1677 TREE_TRACE_EXISTS, &cnt) != TCL_OK) {
1678 Tcl_ResetResult(interp);
1679 } else {
1680 valuePtr = GetTreeValue((Tcl_Interp *)NULL, clientPtr, nodePtr, key);
1681 }
1682 }
1683 if (valuePtr == NULL) {
1684 return FALSE;
1685 }
1686 return TRUE;
1687 }
1688
1689 int
1690 Blt_TreeGetValueByKey(
1691 Tcl_Interp *interp,
1692 TreeClient *clientPtr,
1693 Node *nodePtr,
1694 Blt_TreeKey key,
1695 Tcl_Obj **objPtrPtr)
1696 {
1697 register Value *valuePtr;
1698 TreeObject *treeObjPtr = nodePtr->treeObject;
1699 int cnt = 0;
1700
1701 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
1702 if (CallTraces(interp, clientPtr, treeObjPtr, nodePtr, key,
1703 TREE_TRACE_READ, &cnt) != TCL_OK) {
1704 return TCL_ERROR;
1705 }
1706 }
1707 valuePtr = GetTreeValue(interp, clientPtr, nodePtr, key);
1708 if (valuePtr == NULL) {
1709 return TCL_ERROR;
1710 }
1711 *objPtrPtr = valuePtr->objPtr;
1712 return TCL_OK;
1713 }
1714
1715 int
1716 bltTreeGetValueByKey(
1717 Tcl_Interp *interp,
1718 TreeClient *clientPtr,
1719 Node *nodePtr,
1720 Blt_TreeKey key,
1721 Value** valuePtrPtr)
1722 {
1723 register Value *valuePtr;
1724 TreeObject *treeObjPtr = nodePtr->treeObject;
1725 int cnt = 0;
1726
1727 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
1728 if (CallTraces(interp, clientPtr, treeObjPtr, nodePtr, key,
1729 TREE_TRACE_READ, &cnt) != TCL_OK) {
1730 return TCL_ERROR;
1731 }
1732 }
1733 valuePtr = GetTreeValue(interp, clientPtr, nodePtr, key);
1734 if (valuePtr == NULL) {
1735 return TCL_ERROR;
1736 }
1737 *valuePtrPtr = valuePtr;
1738 return TCL_OK;
1739 }
1740
1741 static void
1742 UpdateOldValue(
1743 TreeClient *clientPtr,
1744 Tcl_Obj *objPtr)
1745 {
1746 if (clientPtr->oldValue != NULL) {
1747 Tcl_DecrRefCount(clientPtr->oldValue);
1748 }
1749 clientPtr->oldValue = objPtr;
1750 }
1751
1752 void
1753 Blt_TreeOldValue(
1754 Tcl_Interp *interp,
1755 TreeClient *clientPtr,
1756 Tcl_Obj **oldPtr,
1757 Tcl_Obj *newPtr)
1758 {
1759 if (newPtr) {
1760 if (clientPtr->oldValue != NULL) {
1761 Tcl_DecrRefCount(clientPtr->oldValue);
1762 }
1763 clientPtr->oldValue = newPtr;
1764 Tcl_IncrRefCount(clientPtr->oldValue);
1765 } else if (oldPtr != NULL) {
1766 *oldPtr = clientPtr->oldValue;
1767 }
1768 }
1769
1770 int
1771 Blt_TreeSetValueByKey(
1772 Tcl_Interp *interp,
1773 TreeClient *clientPtr,
1774 Node *nodePtr, /* Node to be updated. */
1775 Blt_TreeKey key, /* Identifies the field key. */
1776 Tcl_Obj *objPtr) /* New value of field. */
1777 {
1778 TreeObject *treeObjPtr;
1779 Value *valuePtr;
1780 unsigned int flags;
1781 int isNew = 0, cnt = 0;
1782
1783 if (nodePtr == NULL) {
1784 return TCL_ERROR;
1785 }
1786 treeObjPtr = nodePtr->treeObject;
1787 assert(objPtr != NULL);
1788 if (nodePtr->flags & TREE_NODE_FIXED_FIELDS) {
1789 valuePtr = TreeFindValue(nodePtr, key);
1790 if (valuePtr == NULL) {
1791 if (interp != NULL) {
1792 Tcl_AppendResult(interp, "fixed field \"", key, "\"",
1793 (char *)NULL);
1794 }
1795 return TCL_ERROR;
1796 }
1797 } else {
1798 valuePtr = TreeCreateValue(nodePtr, key, &isNew);
1799 }
1800 if ((valuePtr->owner != NULL) && (valuePtr->owner != clientPtr)) {
1801 if (interp != NULL) {
1802 Tcl_AppendResult(interp, "can't set private field \"",
1803 key, "\"", (char *)NULL);
1804 }
1805 return TCL_ERROR;
1806 }
1807 SetModified(nodePtr);
1808 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
1809 UpdateOldValue(clientPtr, valuePtr->objPtr);
1810 valuePtr->objPtr = NULL;
1811 }
1812 if (objPtr != valuePtr->objPtr) {
1813 Tcl_IncrRefCount(objPtr);
1814 if (valuePtr->objPtr != NULL) {
1815 Tcl_DecrRefCount(valuePtr->objPtr);
1816 }
1817 valuePtr->objPtr = objPtr;
1818 }
1819 flags = TREE_TRACE_WRITE;
1820 if (isNew) {
1821 flags |= TREE_TRACE_CREATE;
1822 }
1823 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
1824 return CallTraces(interp, clientPtr, treeObjPtr, nodePtr, valuePtr->key,
1825 flags, &cnt);
1826 }
1827 return TCL_OK;
1828 }
1829
1830 int
1831 Blt_TreeUnsetValueByKey(
1832 Tcl_Interp *interp,
1833 TreeClient *clientPtr,
1834 Node *nodePtr, /* Node to be updated. */
1835 Blt_TreeKey key) /* Name of field in node. */
1836 {
1837 TreeObject *treeObjPtr = nodePtr->treeObject;
1838 Value *valuePtr;
1839 int cnt = 0;
1840
1841 if (nodePtr->flags & TREE_NODE_FIXED_FIELDS) {
1842 if (interp != NULL) {
1843 Tcl_AppendResult(interp, "fixed field", 0);
1844 }
1845 return TCL_ERROR;
1846 }
1847 valuePtr = TreeFindValue(nodePtr, key);
1848 if (valuePtr == NULL) {
1849 return TCL_OK; /* It's okay to unset values that don't
1850 * exist in the node. */
1851 }
1852 if ((valuePtr->owner != NULL) && (valuePtr->owner != clientPtr)) {
1853 if (interp != NULL) {
1854 Tcl_AppendResult(interp, "can't unset private field \"",
1855 key, "\"", (char *)NULL);
1856 }
1857 return TCL_ERROR;
1858 }
1859 SetModified(nodePtr);
1860 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
1861 UpdateOldValue(clientPtr, valuePtr->objPtr);
1862 valuePtr->objPtr = NULL;
1863 }
1864 TreeDeleteValue(nodePtr, valuePtr);
1865 return CallTraces(interp, clientPtr, treeObjPtr, nodePtr, key, TREE_TRACE_UNSET, &cnt);
1866 }
1867
1868 static int
1869 ParseParentheses(
1870 Tcl_Interp *interp,
1871 CONST char *string,
1872 char **leftPtr,
1873 char **rightPtr)
1874 {
1875 register char *p;
1876 char *left, *right;
1877
1878 left = right = NULL;
1879 for (p = (char *)string; *p != '\0'; p++) {
1880 if (*p == '(') {
1881 left = p;
1882 } else if (*p == ')') {
1883 right = p;
1884 }
1885 }
1886 if (left != right) {
1887 if (((left != NULL) && (right == NULL)) ||
1888 ((left == NULL) && (right != NULL)) ||
1889 (left > right) || (right != (p - 1))) {
1890 if (interp != NULL) {
1891 Tcl_AppendResult(interp, "bad array specification \"", string,
1892 "\"", (char *)NULL);
1893 }
1894 return TCL_ERROR;
1895 }
1896 }
1897 *leftPtr = left;
1898 *rightPtr = right;
1899 return TCL_OK;
1900 }
1901
1902 int
1903 Blt_TreeGetValue(
1904 Tcl_Interp *interp,
1905 TreeClient *clientPtr,
1906 Node *nodePtr,
1907 CONST char *string, /* String identifying the field in node. */
1908 Tcl_Obj **objPtrPtr)
1909 {
1910 char *left, *right;
1911 int result;
1912
1913 if (ParseParentheses(interp, string, &left, &right) != TCL_OK) {
1914 return TCL_ERROR;
1915 }
1916 if (left != NULL) {
1917 Tcl_DString kStr, iStr;
1918 Tcl_DStringInit(&kStr);
1919 Tcl_DStringInit(&iStr);
1920 Tcl_DStringAppend(&kStr, left+1, right-left-1);
1921 Tcl_DStringAppend(&iStr, string, left-string);
1922 result = Blt_TreeGetArrayValue(interp, clientPtr, nodePtr,
1923 Tcl_DStringValue(&iStr), Tcl_DStringValue(&kStr), objPtrPtr);
1924 Tcl_DStringFree(&kStr);
1925 Tcl_DStringFree(&iStr);
1926 } else {
1927 result = Blt_TreeGetValueByKey(interp, clientPtr, nodePtr,
1928 Blt_TreeKeyGet(NULL, clientPtr->treeObject,string), objPtrPtr);
1929 }
1930 return result;
1931 }
1932
1933 int
1934 Blt_TreeSetValue(
1935 Tcl_Interp *interp,
1936 TreeClient *clientPtr,
1937 Node *nodePtr, /* Node to be updated. */
1938 CONST char *string, /* String identifying the field in node. */
1939 Tcl_Obj *valueObjPtr) /* New value of field. If NULL, field
1940 * is deleted. */
1941 {
1942 char *left, *right;
1943 int result;
1944
1945 if (nodePtr->flags & TREE_NODE_FIXED_FIELDS) {
1946 return Blt_TreeUpdateValue( interp, clientPtr, nodePtr, string, valueObjPtr);
1947 }
1948 if (ParseParentheses(interp, string, &left, &right) != TCL_OK) {
1949 return TCL_ERROR;
1950 }
1951 if (left != NULL) {
1952 Tcl_DString kStr, iStr;
1953 Tcl_DStringInit(&kStr);
1954 Tcl_DStringInit(&iStr);
1955 Tcl_DStringAppend(&kStr, left+1, right-left-1);
1956 Tcl_DStringAppend(&iStr, string, left-string);
1957 result = Blt_TreeSetArrayValue(interp, clientPtr, nodePtr,
1958 Tcl_DStringValue(&iStr), Tcl_DStringValue(&kStr), valueObjPtr);
1959 Tcl_DStringFree(&kStr);
1960 Tcl_DStringFree(&iStr);
1961 } else {
1962 result = Blt_TreeSetValueByKey(interp, clientPtr, nodePtr,
1963 Blt_TreeKeyGet(NULL, clientPtr->treeObject,string), valueObjPtr);
1964 }
1965 return result;
1966 }
1967
1968 int
1969 Blt_TreeUpdateValue(
1970 Tcl_Interp *interp,
1971 TreeClient *clientPtr,
1972 Node *nodePtr, /* Node to be updated. */
1973 CONST char *string, /* String identifying the field in node. */
1974 Tcl_Obj *valueObjPtr) /* New value of field. If NULL, field
1975 * is deleted. */
1976 {
1977 char *left, *right;
1978 int result;
1979 Blt_TreeKey key;
1980
1981 if (ParseParentheses(interp, string, &left, &right) != TCL_OK) {
1982 return TCL_ERROR;
1983 }
1984 if (left != NULL) {
1985 Tcl_DString kStr, iStr;
1986 Tcl_DStringInit(&kStr);
1987 Tcl_DStringInit(&iStr);
1988 Tcl_DStringAppend(&kStr, left+1, right-left-1);
1989 Tcl_DStringAppend(&iStr, string, left-string);
1990 result = Blt_TreeUpdateArrayValue(interp, clientPtr, nodePtr,
1991 Tcl_DStringValue(&iStr), Tcl_DStringValue(&kStr), valueObjPtr);
1992 Tcl_DStringFree(&kStr);
1993 Tcl_DStringFree(&iStr);
1994 } else {
1995 key = Blt_TreeKeyGet(NULL, clientPtr->treeObject,string);
1996 if (GetTreeValue((Tcl_Interp *)NULL, clientPtr, nodePtr, key) == NULL) {
1997 if (interp != NULL) {
1998 Tcl_AppendResult(interp, "unknown key: ", string, 0);
1999 }
2000 return TCL_ERROR;
2001 }
2002 result = Blt_TreeSetValueByKey(interp, clientPtr, nodePtr,
2003 key, valueObjPtr);
2004 }
2005 return result;
2006 }
2007
2008 int
2009 Blt_TreeUnsetValue(
2010 Tcl_Interp *interp,
2011 TreeClient *clientPtr,
2012 Node *nodePtr, /* Node to be updated. */
2013 CONST char *string) /* String identifying the field in node. */
2014 {
2015 char *left, *right;
2016 int result;
2017
2018 if (nodePtr->flags & TREE_NODE_FIXED_FIELDS) {
2019 if (interp != NULL) {
2020 Tcl_AppendResult(interp, "fixed field", 0);
2021 }
2022 return TCL_ERROR;
2023 }
2024 if (ParseParentheses(interp, string, &left, &right) != TCL_OK) {
2025 return TCL_ERROR;
2026 }
2027 if (left != NULL) {
2028 Tcl_DString kStr, iStr;
2029 Tcl_DStringInit(&kStr);
2030 Tcl_DStringInit(&iStr);
2031 Tcl_DStringAppend(&kStr, left+1, right-left-1);
2032 Tcl_DStringAppend(&iStr, string, left-string);
2033 result = Blt_TreeUnsetArrayValue(interp, clientPtr, nodePtr,
2034 Tcl_DStringValue(&iStr), Tcl_DStringValue(&kStr));
2035 Tcl_DStringFree(&kStr);
2036 Tcl_DStringFree(&iStr);
2037 } else {
2038 result = Blt_TreeUnsetValueByKey(interp, clientPtr, nodePtr,
2039 Blt_TreeKeyGet(NULL, clientPtr->treeObject,string));
2040 }
2041 return result;
2042 }
2043
2044 int
2045 Blt_TreeValueExists(TreeClient *clientPtr, Node *nodePtr, CONST char *string)
2046 {
2047 char *left, *right;
2048 int result;
2049
2050 if (ParseParentheses((Tcl_Interp *)NULL, string, &left, &right) != TCL_OK) {
2051 return FALSE;
2052 }
2053 if (left != NULL) {
2054 Tcl_DString kStr, iStr;
2055 Tcl_DStringInit(&kStr);
2056 Tcl_DStringInit(&iStr);
2057 Tcl_DStringAppend(&kStr, left+1, right-left-1);
2058 Tcl_DStringAppend(&iStr, string, left-string);
2059 result = Blt_TreeArrayValueExists(clientPtr, nodePtr,
2060 Tcl_DStringValue(&iStr), Tcl_DStringValue(&kStr));
2061 Tcl_DStringFree(&kStr);
2062 Tcl_DStringFree(&iStr);
2063 } else {
2064 result = Blt_TreeValueExistsByKey(clientPtr, nodePtr,
2065 Blt_TreeKeyGet(NULL, clientPtr->treeObject,string));
2066 }
2067 return result;
2068 }
2069
2070 Blt_TreeKey
2071 Blt_TreeFirstKey(
2072 TreeClient *clientPtr,
2073 Node *nodePtr,
2074 Blt_TreeKeySearch *iterPtr)
2075 {
2076 Value *valuePtr;
2077
2078 valuePtr = TreeFirstValue(nodePtr, iterPtr);
2079 if (valuePtr == NULL) {
2080 return NULL;
2081 }
2082 while ((valuePtr->owner != NULL) && (valuePtr->owner != clientPtr)) {
2083 valuePtr = TreeNextValue(iterPtr);
2084 if (valuePtr == NULL) {
2085 return NULL;
2086 }
2087 }
2088 return valuePtr->key;
2089 }
2090
2091 Blt_TreeKey
2092 Blt_TreeNextKey(TreeClient *clientPtr, Blt_TreeKeySearch *iterPtr)
2093 {
2094 Value *valuePtr;
2095
2096 valuePtr = TreeNextValue(iterPtr);
2097 if (valuePtr == NULL) {
2098 return NULL;
2099 }
2100 while ((valuePtr->owner != NULL) && (valuePtr->owner != clientPtr)) {
2101 valuePtr = TreeNextValue(iterPtr);
2102 if (valuePtr == NULL) {
2103 return NULL;
2104 }
2105 }
2106 return valuePtr->key;
2107 }
2108
2109 int Blt_TreeCountKeys(TreeClient *clientPtr, Node *nodePtr) {
2110 int cnt = 0;
2111 Blt_TreeKey key;
2112 Blt_TreeKeySearch cursor;
2113
2114
2115 for (key = Blt_TreeFirstKey(clientPtr, nodePtr, &cursor); key != NULL;
2116 key = Blt_TreeNextKey(clientPtr, &cursor)) {
2117 cnt++;
2118 }
2119 return cnt;
2120 }
2121
2122
2123 int
2124 Blt_TreeIsAncestor(Node *n1Ptr, Node *n2Ptr)
2125 {
2126 if (n2Ptr != NULL) {
2127 n2Ptr = n2Ptr->parent;
2128 while (n2Ptr != NULL) {
2129 if (n2Ptr == n1Ptr) {
2130 return TRUE;
2131 }
2132 n2Ptr = n2Ptr->parent;
2133 }
2134 }
2135 return FALSE;
2136 }
2137
2138 /*
2139 *----------------------------------------------------------------------
2140 *
2141 * Blt_TreeSortNode --
2142 *
2143 * Sorts the subnodes at a given node.
2144 *
2145 * Results:
2146 * Always returns TCL_OK.
2147 *
2148 *----------------------------------------------------------------------
2149 */
2150 int
2151 Blt_TreeSortNode(
2152 TreeClient *clientPtr,
2153 Node *nodePtr,
2154 Blt_TreeCompareNodesProc *proc)
2155 {
2156 Node **nodeArr;
2157 Node *childPtr;
2158 int nNodes;
2159 register Node **p;
2160
2161 nNodes = nodePtr->nChildren;
2162 if (nNodes < 2) {
2163 return TCL_OK;
2164 }
2165 nodeArr = Blt_Malloc((nNodes + 1) * sizeof(Node *));
2166 if (nodeArr == NULL) {
2167 return TCL_ERROR; /* Out of memory. */
2168 }
2169 for (p = nodeArr, childPtr = nodePtr->first; childPtr != NULL;
2170 childPtr = childPtr->next, p++) {
2171 *p = childPtr;
2172 }
2173 *p = NULL;
2174
2175 qsort((char *)nodeArr, nNodes, sizeof(Node *), (QSortCompareProc *)proc);
2176 for (p = nodeArr; *p != NULL; p++) {
2177 UnlinkNode(*p);
2178 LinkBefore(nodePtr, *p, (Blt_TreeNode)NULL);
2179 }
2180 Blt_Free(nodeArr);
2181 return NotifyClients(clientPtr, nodePtr->treeObject, nodePtr, TREE_NOTIFY_SORT);
2182 }
2183
2184 #define TEST_RESULT(result) \
2185 switch (result) { \
2186 case TCL_CONTINUE: \
2187 return TCL_OK; \
2188 case TCL_OK: \
2189 break; \
2190 default: \
2191 return (result); \
2192 }
2193
2194 int
2195 Blt_TreeApply(
2196 Node *nodePtr, /* Root node of subtree. */
2197 Blt_TreeApplyProc *proc, /* Procedure to call for each node. */
2198 ClientData clientData) /* One-word of data passed when calling
2199 * proc. */
2200 {
2201 Node *childPtr, *nextPtr;
2202 int result;
2203
2204 for (childPtr = nodePtr->first; childPtr != NULL; childPtr = nextPtr) {
2205
2206 /*
2207 * Get the next link in the chain before calling Blt_TreeApply
2208 * recursively. This is because the apply callback may delete
2209 * the node and its link.
2210 */
2211
2212 nextPtr = childPtr->next;
2213 if (Blt_TreeNodeDeleted(childPtr)) return TCL_OK;
2214 result = Blt_TreeApply(childPtr, proc, clientData);
2215 TEST_RESULT(result);
2216 }
2217 if (Blt_TreeNodeDeleted(nodePtr)) return TCL_OK;
2218 return (*proc) (nodePtr, clientData, TREE_POSTORDER);
2219 }
2220
2221 int
2222 Blt_TreeApplyDFS(
2223 Node *nodePtr, /* Root node of subtree. */
2224 Blt_TreeApplyProc *proc, /* Procedure to call for each node. */
2225 ClientData clientData, /* One-word of data passed when calling
2226 * proc. */
2227 int order) /* Order of traversal. */
2228 {
2229 Node *childPtr, *nextPtr;
2230 int result;
2231
2232 if (Blt_TreeNodeDeleted(nodePtr)) return TCL_OK;
2233 if (order & TREE_PREORDER) {
2234 result = (*proc) (nodePtr, clientData, TREE_PREORDER);
2235 TEST_RESULT(result);
2236 }
2237 childPtr = nodePtr->first;
2238 if (order & TREE_INORDER) {
2239 if (childPtr != NULL) {
2240 result = Blt_TreeApplyDFS(childPtr, proc, clientData, order);
2241 TEST_RESULT(result);
2242 childPtr = childPtr->next;
2243 }
2244 result = (*proc) (nodePtr, clientData, TREE_INORDER);
2245 TEST_RESULT(result);
2246 }
2247 for (/* empty */; childPtr != NULL; childPtr = nextPtr) {
2248 /*
2249 * Get the next link in the chain before calling
2250 * Blt_TreeApply recursively. This is because the
2251 * apply callback may delete the node and its link.
2252 */
2253 nextPtr = childPtr->next;
2254 if (Blt_TreeNodeDeleted(childPtr)) break;
2255 result = Blt_TreeApplyDFS(childPtr, proc, clientData, order);
2256 TEST_RESULT(result);
2257 }
2258 if (Blt_TreeNodeDeleted(nodePtr)) return TCL_OK;
2259 if (order & TREE_POSTORDER) {
2260 return (*proc) (nodePtr, clientData, TREE_POSTORDER);
2261 }
2262 return TCL_OK;
2263 }
2264
2265 int
2266 Blt_TreeApplyBFS(nodePtr, proc, clientData)
2267 Node *nodePtr; /* Root node of subtree. */
2268 Blt_TreeApplyProc *proc; /* Procedure to call for each node. */
2269 ClientData clientData; /* One-word of data passed when calling
2270 * proc. */
2271 {
2272 Blt_Chain *queuePtr;
2273 Blt_ChainLink *linkPtr, *nextPtr;
2274 Node *childPtr;
2275 int result;
2276
2277 queuePtr = Blt_ChainCreate();
2278 linkPtr = Blt_ChainAppend(queuePtr, nodePtr);
2279 while (linkPtr != NULL) {
2280 nodePtr = Blt_ChainGetValue(linkPtr);
2281 /* Add the children to the queue. */
2282 for (childPtr = nodePtr->first; childPtr != NULL;
2283 childPtr = childPtr->next) {
2284 Blt_ChainAppend(queuePtr, childPtr);
2285 }
2286 if (Blt_TreeNodeDeleted(nodePtr)) break;
2287 /* Process the node. */
2288 result = (*proc) (nodePtr, clientData, TREE_BREADTHFIRST);
2289 switch (result) {
2290 case TCL_CONTINUE:
2291 Blt_ChainDestroy(queuePtr);
2292 return TCL_OK;
2293 case TCL_OK:
2294 break;
2295 default:
2296 Blt_ChainDestroy(queuePtr);
2297 return result;
2298 }
2299 /* Remove the node from the queue. */
2300 nextPtr = Blt_ChainNextLink(linkPtr);
2301 Blt_ChainDeleteLink(queuePtr, linkPtr);
2302 linkPtr = nextPtr;
2303 }
2304 Blt_ChainDestroy(queuePtr);
2305 return TCL_OK;
2306 }
2307
2308 static TreeClient *
2309 NewTreeClient(TreeObject *treeObjPtr, int sharetags)
2310 {
2311 TreeClient *clientPtr;
2312
2313 clientPtr = Blt_Calloc(1, sizeof(TreeClient));
2314 if (clientPtr != NULL) {
2315 Blt_TreeTagTable *tablePtr;
2316 int hascl = (treeObjPtr->clients->headPtr != NULL);
2317
2318 clientPtr->magic = TREE_MAGIC;
2319 clientPtr->linkPtr = Blt_ChainAppend(treeObjPtr->clients, clientPtr);
2320 clientPtr->events = Blt_ChainCreate();
2321 clientPtr->traces = Blt_ChainCreate();
2322 clientPtr->treeObject = treeObjPtr;
2323 clientPtr->root = treeObjPtr->root;
2324 if (sharetags && hascl) {
2325 TreeClient *ctPtr;
2326 ctPtr = Blt_ChainGetValue(treeObjPtr->clients->headPtr);
2327 if (ctPtr && ctPtr->tagTablePtr) {
2328 clientPtr->tagTablePtr = ctPtr->tagTablePtr;
2329 clientPtr->tagTablePtr->refCount++;
2330 }
2331 }
2332 if (clientPtr->tagTablePtr == NULL) {
2333 tablePtr = Blt_Malloc(sizeof(Blt_TreeTagTable));
2334 Blt_InitHashTable(&tablePtr->tagTable, BLT_STRING_KEYS);
2335 tablePtr->refCount = 1;
2336 clientPtr->tagTablePtr = tablePtr;
2337 }
2338 }
2339 return clientPtr;
2340 }
2341
2342 int
2343 Blt_TreeCreate(
2344 Tcl_Interp *interp, /* Interpreter to report errors back to. */
2345 CONST char *name, /* Name of tree in namespace. Tree
2346 * must not already exist. */
2347 TreeClient **clientPtrPtr) /* (out) Client token of newly created
2348 * tree. Releasing the token will
2349 * free the tree. If NULL, no token
2350 * is generated. */
2351
2352 {
2353 Tcl_DString dString;
2354 Tcl_Namespace *nsPtr;
2355 TreeInterpData *dataPtr;
2356 TreeObject *treeObjPtr;
2357 CONST char *treeName;
2358 char string[200];
2359
2360 dataPtr = GetTreeInterpData(interp);
2361 if (name != NULL) {
2362 /* Check if this tree already exists the current namespace. */
2363 treeObjPtr = GetTreeObject(interp, name, NS_SEARCH_CURRENT);
2364 if (treeObjPtr != NULL) {
2365 if (interp != NULL)
2366 Tcl_AppendResult(interp, "a tree object \"", name,
2367 "\" already exists", (char *)NULL);
2368 return TCL_ERROR;
2369 }
2370 } else {
2371 /* Generate a unique tree name in the current namespace. */
2372 do {
2373 sprintf(string, "tree%d", dataPtr->nextId++);
2374 } while (GetTreeObject(interp, string, NS_SEARCH_CURRENT) != NULL);
2375 name = string;
2376 }
2377 /*
2378 * Tear apart and put back together the namespace-qualified name
2379 * of the tree. This is to ensure that naming is consistent.
2380 */
2381 treeName = name;
2382 if (Blt_ParseQualifiedName(interp, name, &nsPtr, &treeName) != TCL_OK) {
2383 if (interp != NULL)
2384 Tcl_AppendResult(interp, "can't find namespace in \"", name, "\"",
2385 (char *)NULL);
2386 return TCL_ERROR;
2387 }
2388 if (nsPtr == NULL) {
2389 /*
2390 * Note: Unlike Tcl_CreateCommand, an unqualified name
2391 * doesn't imply the global namespace, but the current one.
2392 */
2393 nsPtr = Tcl_GetCurrentNamespace(interp);
2394 }
2395 name = Blt_GetQualifiedName(nsPtr, treeName, &dString);
2396 treeObjPtr = NewTreeObject(dataPtr, interp, name);
2397 if (treeObjPtr == NULL) {
2398 if (interp != NULL)
2399 Tcl_AppendResult(interp, "can't allocate tree \"", name, "\"",
2400 (char *)NULL);
2401 Tcl_DStringFree(&dString);
2402 return TCL_ERROR;
2403 }
2404 Tcl_DStringFree(&dString);
2405 if (clientPtrPtr != NULL) {
2406 TreeClient *clientPtr;
2407
2408 clientPtr = NewTreeClient(treeObjPtr, 0);
2409 if (clientPtr == NULL) {
2410 if (interp != NULL)
2411 Tcl_AppendResult(interp, "can't allocate tree token",(char *)NULL);
2412 return TCL_ERROR;
2413 }
2414 *clientPtrPtr = clientPtr;
2415 }
2416 return TCL_OK;
2417 }
2418
2419 int
2420 Blt_TreeGetToken(
2421 Tcl_Interp *interp, /* Interpreter to report errors back to. */
2422 CONST char *name, /* Name of tree in namespace. */
2423 TreeClient **clientPtrPtr)
2424 {
2425 TreeClient *clientPtr;
2426 TreeObject *treeObjPtr;
2427
2428 treeObjPtr = GetTreeObject(interp, name, NS_SEARCH_BOTH);
2429 if (treeObjPtr == NULL) {
2430 if (interp != NULL)
2431 Tcl_AppendResult(interp, "can't find a tree object \"", name, "\"",
2432 (char *)NULL);
2433 return TCL_ERROR;
2434 }
2435 clientPtr = NewTreeClient(treeObjPtr, 0);
2436 if (clientPtr == NULL) {
2437 if (interp != NULL)
2438 Tcl_AppendResult(interp, "can't allocate token for tree \"",
2439 name, "\"", (char *)NULL);
2440 return TCL_ERROR;
2441 }
2442 *clientPtrPtr = clientPtr;
2443 return TCL_OK;
2444 }
2445
2446 int
2447 Blt_TreeGetTokenTag(
2448 Tcl_Interp *interp, /* Interpreter to report errors back to. */
2449 CONST char *name, /* Name of tree in namespace. */
2450 TreeClient **clientPtrPtr)
2451 {
2452 TreeClient *clientPtr;
2453 TreeObject *treeObjPtr;
2454
2455 treeObjPtr = GetTreeObject(interp, name, NS_SEARCH_BOTH);
2456 if (treeObjPtr == NULL) {
2457 if (interp != NULL)
2458 Tcl_AppendResult(interp, "can't find a tree object \"", name, "\"",
2459 (char *)NULL);
2460 return TCL_ERROR;
2461 }
2462 clientPtr = NewTreeClient(treeObjPtr, 1);
2463 if (clientPtr == NULL) {
2464 if (interp != NULL)
2465 Tcl_AppendResult(interp, "can't allocate token for tree \"",
2466 name, "\"", (char *)NULL);
2467 return TCL_ERROR;
2468 }
2469 *clientPtrPtr = clientPtr;
2470 return TCL_OK;
2471 }
2472
2473 void
2474 Blt_TreeReleaseToken(TreeClient *clientPtr)
2475 {
2476 TreeObject *treeObjPtr;
2477 Blt_ChainLink *linkPtr;
2478 EventHandler *notifyPtr;
2479 TraceHandler *tracePtr;
2480
2481 if (clientPtr->magic != TREE_MAGIC) {
2482 fprintf(stderr, "invalid tree object token 0x%lx\n",
2483 (unsigned long)clientPtr);
2484 return;
2485 }
2486 /* Remove any traces that may be set. */
2487 for (linkPtr = Blt_ChainFirstLink(clientPtr->traces); linkPtr != NULL;
2488 linkPtr = Blt_ChainNextLink(linkPtr)) {
2489 tracePtr = Blt_ChainGetValue(linkPtr);
2490 if (tracePtr->keyPattern != NULL) {
2491 Blt_Free(tracePtr->keyPattern);
2492 }
2493 Blt_Free(tracePtr);
2494 }
2495 Blt_ChainDestroy(clientPtr->traces);
2496 /* And any event handlers. */
2497 for(linkPtr = Blt_ChainFirstLink(clientPtr->events);
2498 linkPtr != NULL; linkPtr = Blt_ChainNextLink(linkPtr)) {
2499 notifyPtr = Blt_ChainGetValue(linkPtr);
2500 if (notifyPtr->notifyPending) {
2501 Tcl_CancelIdleCall(NotifyIdleProc, notifyPtr);
2502 }
2503 Blt_Free(notifyPtr);
2504 }
2505 if (clientPtr->tagTablePtr != NULL) {
2506 ReleaseTagTable(clientPtr->tagTablePtr);
2507 }
2508 Blt_ChainDestroy(clientPtr->events);
2509 treeObjPtr = clientPtr->treeObject;
2510 if (treeObjPtr != NULL) {
2511 /* Remove the client from the server's list */
2512 Blt_ChainDeleteLink(treeObjPtr->clients, clientPtr->linkPtr);
2513 if (Blt_ChainGetLength(treeObjPtr->clients) == 0) {
2514 treeObjPtr->delete = 1;
2515 Tcl_EventuallyFree(treeObjPtr, DestroyTreeObject);
2516 /* DestroyTreeObject(treeObjPtr); */
2517 }
2518 }
2519 clientPtr->magic = 0;
2520 Blt_Free(clientPtr);
2521 }
2522
2523 int
2524 Blt_TreeExists(interp, name)
2525 Tcl_Interp *interp; /* Interpreter to report errors back to. */
2526 CONST char *name; /* Name of tree in designated namespace. */
2527 {
2528 TreeObject *treeObjPtr;
2529
2530 treeObjPtr = GetTreeObject(interp, name, NS_SEARCH_BOTH);
2531 if (treeObjPtr == NULL) {
2532 Tcl_ResetResult(interp);
2533 return 0;
2534 }
2535 return 1;
2536 }
2537
2538 /*ARGSUSED*/
2539 static int
2540 SizeApplyProc(
2541 Node *nodePtr, /* Not used. */
2542 ClientData clientData,
2543 int order) /* Not used. */
2544 {
2545 int *sumPtr = clientData;
2546 *sumPtr = *sumPtr + 1;
2547 return TCL_OK;
2548 }
2549
2550 int
2551 Blt_TreeSize(Node *nodePtr)
2552 {
2553 int sum;
2554
2555 sum = 0;
2556 Blt_TreeApply(nodePtr, SizeApplyProc, &sum);
2557 return sum;
2558 }
2559
2560
2561 void
2562 Blt_TreeCreateEventHandler(
2563 TreeClient *clientPtr,
2564 unsigned int mask,
2565 Blt_TreeNotifyEventProc *proc,
2566 ClientData clientData)
2567 {
2568 Blt_ChainLink *linkPtr;
2569 EventHandler *notifyPtr;
2570
2571 notifyPtr = NULL; /* Suppress compiler warning. */
2572
2573 /* Check if the event is already handled. */
2574 for(linkPtr = Blt_ChainFirstLink(clientPtr->events);
2575 linkPtr != NULL; linkPtr = Blt_ChainNextLink(linkPtr)) {
2576 notifyPtr = Blt_ChainGetValue(linkPtr);
2577 if ((notifyPtr->proc == proc) &&
2578 (notifyPtr->mask == mask) &&
2579 (notifyPtr->clientData == clientData)) {
2580 break;
2581 }
2582 }
2583 if (linkPtr == NULL) {
2584 notifyPtr = Blt_Malloc(sizeof (EventHandler));
2585 assert(notifyPtr);
2586 linkPtr = Blt_ChainAppend(clientPtr->events, notifyPtr);
2587 }
2588 if (proc == NULL) {
2589 Blt_ChainDeleteLink(clientPtr->events, linkPtr);
2590 Blt_Free(notifyPtr);
2591 } else {
2592 notifyPtr->proc = proc;
2593 notifyPtr->clientData = clientData;
2594 notifyPtr->mask = mask;
2595 notifyPtr->notifyPending = FALSE;
2596 notifyPtr->interp = clientPtr->treeObject->interp;
2597 }
2598 }
2599
2600 void
2601 Blt_TreeDeleteEventHandler(
2602 TreeClient *clientPtr,
2603 unsigned int mask,
2604 Blt_TreeNotifyEventProc *proc,
2605 ClientData clientData)
2606 {
2607 Blt_ChainLink *linkPtr;
2608 EventHandler *notifyPtr;
2609
2610 if (!clientPtr) return;
2611 for(linkPtr = Blt_ChainFirstLink(clientPtr->events);
2612 linkPtr != NULL; linkPtr = Blt_ChainNextLink(linkPtr)) {
2613 notifyPtr = Blt_ChainGetValue(linkPtr);
2614 if ((notifyPtr->proc == proc) && (notifyPtr->mask == mask) &&
2615 (notifyPtr->clientData == clientData)) {
2616 if (notifyPtr->notifyPending) {
2617 Tcl_CancelIdleCall(NotifyIdleProc, notifyPtr);
2618 }
2619 Blt_ChainDeleteLink(clientPtr->events, linkPtr);
2620 Blt_Free(notifyPtr);
2621 return;
2622 }
2623 }
2624 }
2625
2626 /*
2627 *----------------------------------------------------------------------
2628 *
2629 * Blt_TreeNodePath --
2630 *
2631 *----------------------------------------------------------------------
2632 */
2633 char *
2634 Blt_TreeNodePath(Node *nodePtr, Tcl_DString *resultPtr)
2635 {
2636 char **nameArr; /* Used to stack the component names. */
2637 char *staticSpace[64];
2638 int nLevels;
2639 register int i;
2640
2641 nLevels = nodePtr->depth;
2642 if (nLevels > 64) {
2643 nameArr = Blt_Malloc(nLevels * sizeof(char *));
2644 assert(nameArr);
2645 } else {
2646 nameArr = staticSpace;
2647 }
2648 for (i = nLevels - 1; i >= 0; i--) {
2649 /* Save the name of each ancestor in the name array.
2650 * Note that we ignore the root. */
2651 nameArr[i] = nodePtr->label;
2652 nodePtr = nodePtr->parent;
2653 }
2654 /* Append each the names in the array. */
2655 Tcl_DStringInit(resultPtr);
2656 for (i = 0; i < nLevels; i++) {
2657 Tcl_DStringAppendElement(resultPtr, nameArr[i]);
2658 }
2659 if (nameArr != staticSpace) {
2660 Blt_Free(nameArr);
2661 }
2662 return Tcl_DStringValue(resultPtr);
2663 }
2664
2665 char *
2666 Blt_TreeNodePathStr(Node *nodePtr, Tcl_DString *resultPtr, char *prefix, char *delim)
2667 {
2668 char **nameArr; /* Used to stack the component names. */
2669 char *staticSpace[64];
2670 int nLevels;
2671 register int i;
2672
2673 nLevels = nodePtr->depth;
2674 if (nLevels > 64) {
2675 nameArr = Blt_Malloc(nLevels * sizeof(char *));
2676 assert(nameArr);
2677 } else {
2678 nameArr = staticSpace;
2679 }
2680 for (i = nLevels - 1; i >= 0; i--) {
2681 /* Save the name of each ancestor in the name array.
2682 * Note that we ignore the root. */
2683 nameArr[i] = nodePtr->label;
2684 nodePtr = nodePtr->parent;
2685 }
2686 /* Append each of the names in the array. */
2687 Tcl_DStringInit(resultPtr);
2688 if (prefix != NULL) {
2689 Tcl_DStringAppend(resultPtr, prefix, -1);
2690 }
2691 for (i = 0; i < nLevels; i++) {
2692 if (i > 0 && delim != NULL) {
2693 Tcl_DStringAppend(resultPtr, delim, -1);
2694 }
2695 Tcl_DStringAppend(resultPtr, nameArr[i], -1);
2696 }
2697 if (nameArr != staticSpace) {
2698 Blt_Free(nameArr);
2699 }
2700 return Tcl_DStringValue(resultPtr);
2701 }
2702
2703 int
2704 Blt_TreeArrayValueExists(
2705 TreeClient *clientPtr,
2706 Node *nodePtr,
2707 CONST char *arrayName,
2708 CONST char *elemName)
2709 {
2710 Blt_TreeKey key;
2711 Blt_HashEntry *hPtr;
2712 Blt_HashTable *tablePtr;
2713 register Value *valuePtr;
2714 int cnt;
2715 TreeObject *treeObjPtr = nodePtr->treeObject;
2716 Tcl_Interp *interp = treeObjPtr->interp;
2717
2718 key = Blt_TreeKeyGet(NULL, clientPtr->treeObject,arrayName);
2719
2720 valuePtr = GetTreeValue((Tcl_Interp *)NULL, clientPtr, nodePtr, key);
2721 if (valuePtr == NULL && (!(nodePtr->flags & TREE_TRACE_ACTIVE))) {
2722 if (CallTraces(interp, clientPtr, treeObjPtr, nodePtr, key,
2723 TREE_TRACE_EXISTS, &cnt) != TCL_OK) {
2724 Tcl_ResetResult(interp);
2725 } else {
2726 valuePtr = GetTreeValue((Tcl_Interp *)NULL, clientPtr, nodePtr, key);
2727 }
2728 }
2729 if (valuePtr == NULL) {
2730 return FALSE;
2731 }
2732 if (IsTclDict(interp, valuePtr->objPtr)) {
2733 /* Preserve type if this was a dict */
2734 int result;
2735 Tcl_Obj *keyPtr, *valueObjPtr = NULL;
2736
2737 keyPtr = Tcl_NewStringObj(elemName, -1);
2738 Tcl_IncrRefCount(keyPtr);
2739 result = Tcl_DictObjGet(interp, valuePtr->objPtr, keyPtr, &valueObjPtr);
2740 Tcl_DecrRefCount(keyPtr);
2741 if (result != TCL_OK) {
2742 return FALSE;
2743 }
2744 if (valueObjPtr == NULL) {
2745 return FALSE;
2746 }
2747 return TRUE;
2748 }
2749
2750 if (Blt_IsArrayObj(valuePtr->objPtr) == 0 && Tcl_IsShared(valuePtr->objPtr)) {
2751 Tcl_DecrRefCount(valuePtr->objPtr);
2752 valuePtr->objPtr = Tcl_DuplicateObj(valuePtr->objPtr);
2753 Tcl_IncrRefCount(valuePtr->objPtr);
2754 }
2755 if (Blt_GetArrayFromObj((Tcl_Interp *)NULL, valuePtr->objPtr, &tablePtr)
2756 != TCL_OK) {
2757 return FALSE;
2758 }
2759 hPtr = Blt_FindHashEntry(tablePtr, elemName);
2760 return (hPtr != NULL);
2761 }
2762
2763 int
2764 Blt_TreeGetArrayValue(
2765 Tcl_Interp *interp,
2766 TreeClient *clientPtr,
2767 Node *nodePtr,
2768 CONST char *arrayName,
2769 CONST char *elemName,
2770 Tcl_Obj **valueObjPtrPtr)
2771 {
2772 Blt_TreeKey key;
2773 Blt_HashEntry *hPtr;
2774 Blt_HashTable *tablePtr;
2775 register Value *valuePtr;
2776 int cnt = 0;
2777
2778 key = Blt_TreeKeyGet(interp, clientPtr->treeObject,arrayName);
2779
2780 /* Reading any element of the array can cause a trace to fire. */
2781 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
2782 if (CallTraces(interp, clientPtr, nodePtr->treeObject, nodePtr, key,
2783 TREE_TRACE_READ, &cnt) != TCL_OK) {
2784 return TCL_ERROR;
2785 }
2786 }
2787 valuePtr = GetTreeValue(interp, clientPtr, nodePtr, key);
2788 if (valuePtr == NULL) {
2789 return TCL_ERROR;
2790 }
2791 if (IsTclDict(interp, valuePtr->objPtr)) {
2792 /* Preserve type if this was a dict */
2793 int result;
2794 Tcl_Obj *keyPtr;
2795 keyPtr = Tcl_NewStringObj(elemName, -1);
2796 Tcl_IncrRefCount(keyPtr);
2797 result = Tcl_DictObjGet(interp, valuePtr->objPtr, keyPtr, valueObjPtrPtr);
2798 Tcl_DecrRefCount(keyPtr);
2799 if (result != TCL_OK) {
2800 return result;
2801 }
2802 if (*valueObjPtrPtr == NULL) {
2803 if (interp != NULL) {
2804 Tcl_AppendResult(interp, "can't find \"", arrayName, "(",
2805 elemName, ")\"", (char *)NULL);
2806 }
2807 return TCL_ERROR;
2808 }
2809 return TCL_OK;
2810 }
2811 if (Blt_IsArrayObj(valuePtr->objPtr) == 0 && Tcl_IsShared(valuePtr->objPtr)) {
2812 Tcl_DecrRefCount(valuePtr->objPtr);
2813 valuePtr->objPtr = Tcl_DuplicateObj(valuePtr->objPtr);
2814 Tcl_IncrRefCount(valuePtr->objPtr);
2815 }
2816 if (Blt_GetArrayFromObj(interp, valuePtr->objPtr, &tablePtr) != TCL_OK) {
2817 return TCL_ERROR;
2818 }
2819 hPtr = Blt_FindHashEntry(tablePtr, elemName);
2820 if (hPtr == NULL) {
2821 if (interp != NULL) {
2822 Tcl_AppendResult(interp, "can't find \"", arrayName, "(",
2823 elemName, ")\"", (char *)NULL);
2824 }
2825 return TCL_ERROR;
2826 }
2827
2828 *valueObjPtrPtr = (Tcl_Obj *)Blt_GetHashValue(hPtr);
2829 return TCL_OK;
2830 }
2831
2832 static int
2833 TreeSetArrayValue(
2834 Tcl_Interp *interp,
2835 TreeClient *clientPtr,
2836 Node *nodePtr, /* Node to be updated. */
2837 CONST char *arrayName,
2838 CONST char *elemName,
2839 Tcl_Obj *valueObjPtr, /* New value of element. */
2840 int create) /* Create the node key if required. */
2841 {
2842 Blt_TreeKey key;
2843 Blt_HashEntry *hPtr;
2844 Blt_HashTable *tablePtr;
2845 register Value *valuePtr;
2846 unsigned int flags;
2847 int isNew, cnt = 0;
2848
2849 assert(valueObjPtr != NULL);
2850
2851 /*
2852 * Search for the array in the list of data fields. If one
2853 * doesn't exist, create it.
2854 */
2855 key = Blt_TreeKeyGet(interp, clientPtr->treeObject,arrayName);
2856 valuePtr = GetTreeValue((Tcl_Interp *)NULL, clientPtr, nodePtr, key);
2857 if (valuePtr == NULL && create == 0) {
2858 return TCL_ERROR;
2859 }
2860 if (valuePtr == NULL) {
2861 if ((nodePtr->flags & TREE_NODE_FIXED_FIELDS)) {
2862 return TCL_ERROR;
2863 }
2864 valuePtr = TreeCreateValue(nodePtr, key, &isNew);
2865 isNew = 1;
2866 } else {
2867 isNew = 0;
2868 }
2869 if ((valuePtr->owner != NULL) && (valuePtr->owner != clientPtr)) {
2870 if (interp != NULL) {
2871 Tcl_AppendResult(interp, "can't set private field \"",
2872 key, "\"", (char *)NULL);
2873 }
2874 return TCL_ERROR;
2875 }
2876 flags = TREE_TRACE_WRITE;
2877 if (isNew) {
2878 valuePtr->objPtr = Blt_NewArrayObj(0, (Tcl_Obj **)NULL);
2879 Tcl_IncrRefCount(valuePtr->objPtr);
2880 flags |= TREE_TRACE_CREATE;
2881
2882 } else if (Tcl_IsShared(valuePtr->objPtr)) {
2883 Tcl_DecrRefCount(valuePtr->objPtr);
2884 valuePtr->objPtr = Tcl_DuplicateObj(valuePtr->objPtr);
2885 Tcl_IncrRefCount(valuePtr->objPtr);
2886 }
2887
2888 if ((clientPtr->treeObject->flags & TREE_DICT_KEYS) &&
2889 IsTclDict(interp, valuePtr->objPtr)) {
2890 int dSiz;
2891
2892 if (Tcl_DictObjSize(interp, valuePtr->objPtr, &dSiz) != TCL_OK) {
2893 return TCL_ERROR;
2894 }
2895 }
2896 if (IsTclDict(interp, valuePtr->objPtr)) {
2897 /* Preserve type if this was a dict */
2898 int result;
2899 Tcl_Obj *keyPtr, *valObjPtr;
2900
2901 keyPtr = Tcl_NewStringObj(elemName, -1);
2902 Tcl_IncrRefCount(keyPtr);
2903 if (!create) {
2904 result = Tcl_DictObjGet(interp, valuePtr->objPtr, keyPtr, &valObjPtr);
2905 if (result != TCL_OK || valObjPtr == NULL) {
2906 Tcl_AppendResult(interp, "can't find field: ", elemName, 0);
2907 Tcl_DecrRefCount(keyPtr);
2908 return TCL_ERROR;
2909 }
2910 }
2911 result = Tcl_DictObjPut(interp, valuePtr->objPtr, keyPtr, valueObjPtr);
2912 Tcl_DecrRefCount(keyPtr);
2913 if (result != TCL_OK) {
2914 return result;
2915 }
2916 goto finishset;
2917 }
2918
2919
2920 if (Blt_GetArrayFromObj(interp, valuePtr->objPtr, &tablePtr) != TCL_OK) {
2921 return TCL_ERROR;
2922 }
2923 Tcl_InvalidateStringRep(valuePtr->objPtr);
2924 if (create) {
2925 hPtr = Blt_CreateHashEntry(tablePtr, elemName, &isNew);
2926 assert(hPtr);
2927 } else {
2928 hPtr = Blt_FindHashEntry(tablePtr, elemName);
2929 if (hPtr == NULL) {
2930 if (interp != NULL) {
2931 Tcl_AppendResult(interp, "can't find array field: ", elemName, 0);
2932 }
2933 return TCL_ERROR;
2934 }
2935 isNew = 0;
2936 }
2937
2938 SetModified(nodePtr);
2939 Tcl_IncrRefCount(valueObjPtr);
2940 if (!isNew) {
2941 Tcl_Obj *oldValueObjPtr;
2942
2943 /* An element by the same name already exists. Decrement the
2944 * reference count of the old value. */
2945
2946 oldValueObjPtr = (Tcl_Obj *)Blt_GetHashValue(hPtr);
2947 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
2948 UpdateOldValue(clientPtr, oldValueObjPtr);
2949 oldValueObjPtr = NULL;
2950 }
2951 if (oldValueObjPtr != NULL) {
2952 Tcl_DecrRefCount(oldValueObjPtr);
2953 }
2954 } else {
2955 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
2956 UpdateOldValue(clientPtr, NULL);
2957 }
2958 }
2959 Blt_SetHashValue(hPtr, valueObjPtr);
2960
2961 finishset:
2962 /*
2963 * We don't handle traces on a per array element basis. Setting
2964 * any element can fire traces for the value.
2965 */
2966 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
2967 return CallTraces(interp, clientPtr, nodePtr->treeObject, nodePtr,
2968 valuePtr->key, flags, &cnt);
2969 }
2970 return TCL_OK;
2971 }
2972
2973 int
2974 Blt_TreeSetArrayValue(
2975 Tcl_Interp *interp,
2976 TreeClient *clientPtr,
2977 Node *nodePtr, /* Node to be updated. */
2978 CONST char *arrayName,
2979 CONST char *elemName,
2980 Tcl_Obj *valueObjPtr) /* New value of element. */
2981 {
2982 return TreeSetArrayValue(interp, clientPtr, nodePtr, arrayName, elemName, valueObjPtr, 1);
2983 }
2984
2985 int
2986 Blt_TreeUpdateArrayValue(
2987 Tcl_Interp *interp,
2988 TreeClient *clientPtr,
2989 Node *nodePtr, /* Node to be updated. */
2990 CONST char *arrayName,
2991 CONST char *elemName,
2992 Tcl_Obj *valueObjPtr) /* New value of element. */
2993 {
2994 return TreeSetArrayValue(interp, clientPtr, nodePtr, arrayName, elemName, valueObjPtr, 0);
2995 }
2996
2997 int
2998 Blt_TreeUnsetArrayValue(
2999 Tcl_Interp *interp,
3000 TreeClient *clientPtr,
3001 Node *nodePtr, /* Node to be updated. */
3002 CONST char *arrayName,
3003 CONST char *elemName)
3004 {
3005 Blt_TreeKey key; /* Name of field in node. */
3006 Blt_HashEntry *hPtr;
3007 Blt_HashTable *tablePtr;
3008 Tcl_Obj *valueObjPtr;
3009 Value *valuePtr;
3010 int cnt = 0;
3011
3012 key = Blt_TreeKeyGet(interp, clientPtr->treeObject,arrayName);
3013 valuePtr = TreeFindValue(nodePtr, key);
3014 if (valuePtr == NULL) {
3015 return TCL_OK;
3016 }
3017 if ((valuePtr->owner != NULL) && (valuePtr->owner != clientPtr)) {
3018 if (interp != NULL) {
3019 Tcl_AppendResult(interp, "can't unset private field \"",
3020 key, "\"", (char *)NULL);
3021 }
3022 return TCL_ERROR;
3023 }
3024
3025 if (Tcl_IsShared(valuePtr->objPtr)) {
3026 Tcl_DecrRefCount(valuePtr->objPtr);
3027 valuePtr->objPtr = Tcl_DuplicateObj(valuePtr->objPtr);
3028 Tcl_IncrRefCount(valuePtr->objPtr);
3029 }
3030
3031 if (IsTclDict(interp, valuePtr->objPtr)) {
3032 /* Preserve type if this was a dict */
3033 int result;
3034 Tcl_Obj *keyPtr;
3035 keyPtr = Tcl_NewStringObj(elemName, -1);
3036 Tcl_IncrRefCount(keyPtr);
3037 result = Tcl_DictObjRemove(interp, valuePtr->objPtr, keyPtr);
3038 Tcl_DecrRefCount(keyPtr);
3039 if (result != TCL_OK) {
3040 return result;
3041 }
3042 goto finishrm;
3043 }
3044
3045 if (Blt_GetArrayFromObj(interp, valuePtr->objPtr, &tablePtr) != TCL_OK) {
3046 return TCL_ERROR;
3047 }
3048 hPtr = Blt_FindHashEntry(tablePtr, elemName);
3049 if (hPtr == NULL) {
3050 goto finishrm; /* Element doesn't exist, Ok. */
3051 }
3052 SetModified(nodePtr);
3053 valueObjPtr = (Tcl_Obj *)Blt_GetHashValue(hPtr);
3054 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
3055 UpdateOldValue(clientPtr, valueObjPtr);
3056 } else {
3057 Tcl_DecrRefCount(valueObjPtr);
3058 }
3059 Blt_DeleteHashEntry(tablePtr, hPtr);
3060 Tcl_InvalidateStringRep(valuePtr->objPtr);
3061
3062 finishrm:
3063 /*
3064 * Un-setting any element in the array can cause the trace on the value
3065 * to fire.
3066 */
3067 if (!(nodePtr->flags & TREE_TRACE_ACTIVE)) {
3068 return CallTraces(interp, clientPtr, nodePtr->treeObject, nodePtr,
3069 valuePtr->key, TREE_TRACE_WRITE, &cnt);
3070 }
3071 return TCL_OK;
3072 }
3073
3074 int
3075 Blt_TreeArrayNames(
3076 Tcl_Interp *interp,
3077 TreeClient *clientPtr,
3078 Node *nodePtr,
3079 CONST char *arrayName,
3080 Tcl_Obj *listObjPtr,
3081 CONST char *pattern)
3082 {
3083 Blt_HashEntry *hPtr;
3084 Blt_HashSearch cursor;
3085 Blt_HashTable *tablePtr;
3086 Tcl_Obj *objPtr;
3087 Value *valuePtr;
3088 char *key;
3089
3090 key = Blt_TreeKeyGet(interp, clientPtr->treeObject,arrayName);
3091 valuePtr = GetTreeValue(interp, clientPtr, nodePtr, key);
3092 if (valuePtr == NULL) {
3093 return TCL_ERROR;
3094 }
3095 if (IsTclDict(interp, valuePtr->objPtr)) {
3096 /* Preserve type if this was a dict */
3097
3098 Tcl_DictSearch search;
3099 Tcl_Obj *keyPtr;
3100 int done;
3101
3102 Tcl_DictObjFirst(NULL, valuePtr->objPtr, &search, &keyPtr, NULL, &done);
3103 for (; !done ; Tcl_DictObjNext(&search, &keyPtr, NULL, &done)) {
3104 if (!pattern || Tcl_StringMatch(Tcl_GetString(keyPtr), pattern)) {
3105 Tcl_ListObjAppendElement(NULL, listObjPtr, keyPtr);
3106 }
3107 }
3108 Tcl_DictObjDone(&search);
3109 return TCL_OK;
3110 }
3111
3112 if (Blt_IsArrayObj(valuePtr->objPtr) == 0 && Tcl_IsShared(valuePtr->objPtr)) {
3113 Tcl_DecrRefCount(valuePtr->objPtr);
3114 valuePtr->objPtr = Tcl_DuplicateObj(valuePtr->objPtr);
3115 Tcl_IncrRefCount(valuePtr->objPtr);
3116 }
3117 if (Blt_GetArrayFromObj(interp, valuePtr->objPtr, &tablePtr) != TCL_OK) {
3118 return TCL_ERROR;
3119 }
3120 /*tablePtr = (Blt_HashTable *)valuePtr->objPtr; */
3121 for (hPtr = Blt_FirstHashEntry(tablePtr, &cursor); hPtr != NULL;
3122 hPtr = Blt_NextHashEntry(&cursor)) {
3123 char *str;
3124
3125 str = Blt_GetHashKey(tablePtr, hPtr);
3126 if (pattern == NULL || Tcl_StringMatch(str, pattern)) {
3127 objPtr = Tcl_NewStringObj(str, -1);
3128 Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
3129 }
3130 }
3131 return TCL_OK;
3132 }
3133
3134 int
3135 Blt_TreeArrayValues(
3136 Tcl_Interp *interp,
3137 TreeClient *clientPtr,
3138 Node *nodePtr,
3139 CONST char *arrayName,
3140 Tcl_Obj *listObjPtr,
3141 int names)
3142 {
3143 Blt_HashEntry *hPtr;
3144 Blt_HashSearch cursor;
3145 Blt_HashTable *tablePtr;
3146 Tcl_Obj *objPtr;
3147 Value *valuePtr;
3148 char *key;
3149
3150 key = Blt_TreeKeyGet(interp, clientPtr->treeObject,arrayName);
3151 if ( bltTreeGetValueByKey(interp, clientPtr, nodePtr, key, &valuePtr) != TCL_OK) {
3152 return TCL_ERROR;
3153 }
3154 if (IsTclDict(interp, valuePtr->objPtr)) {
3155 /* Preserve type if this was a dict */
3156
3157 Tcl_DictSearch search;
3158 Tcl_Obj *keyPtr;
3159 int done;
3160 int result;
3161
3162 Tcl_DictObjFirst(NULL, valuePtr->objPtr, &search, &keyPtr, NULL, &done);
3163 for (; !done ; Tcl_DictObjNext(&search, &keyPtr, NULL, &done)) {
3164 Tcl_Obj *valueObjPtr;
3165 if (names) {
3166 Tcl_ListObjAppendElement(NULL, listObjPtr, keyPtr);
3167 }
3168
3169 valueObjPtr = NULL;
3170 result = Tcl_DictObjGet(interp, valuePtr->objPtr, keyPtr, &valueObjPtr);
3171 if (result != TCL_OK) {
3172 continue;
3173 }
3174 if (valueObjPtr == NULL) {
3175 valueObjPtr = Tcl_NewStringObj("",-1);
3176 }
3177 Tcl_ListObjAppendElement(NULL, listObjPtr, valueObjPtr);
3178 }
3179 Tcl_DictObjDone(&search);
3180 return TCL_OK;
3181 }
3182 if (Blt_IsArrayObj(valuePtr->objPtr) == 0 && Tcl_IsShared(valuePtr->objPtr)) {
3183 Tcl_DecrRefCount(valuePtr->objPtr);
3184 valuePtr->objPtr = Tcl_DuplicateObj(valuePtr->objPtr);
3185 Tcl_IncrRefCount(valuePtr->objPtr);
3186 }
3187 if (Blt_GetArrayFromObj(interp, valuePtr->objPtr, &tablePtr) != TCL_OK) {
3188 return TCL_ERROR;
3189 }
3190 /*tablePtr = (Blt_HashTable *)valuePtr->objPtr; */
3191 for (hPtr = Blt_FirstHashEntry(tablePtr, &cursor); hPtr != NULL;
3192 hPtr = Blt_NextHashEntry(&cursor)) {
3193 if (names) {
3194 Tcl_ListObjAppendElement(interp, listObjPtr,
3195 Tcl_NewStringObj( Blt_GetHashKey(tablePtr, hPtr), -1));
3196 }
3197 objPtr = Blt_GetHashValue(hPtr);
3198 Tcl_ListObjAppendElement(interp, listObjPtr, objPtr?objPtr:Tcl_NewStringObj("",-1));
3199 }
3200 return TCL_OK;
3201 }
3202
3203
3204 /*
3205 *----------------------------------------------------------------------
3206 *
3207 * Blt_TreeShareTagTable --
3208 *
3209 *----------------------------------------------------------------------
3210 */
3211 int
3212 Blt_TreeShareTagTable(
3213 TreeClient *sourcePtr,
3214 TreeClient *targetPtr)
3215 {
3216 sourcePtr->tagTablePtr->refCount++;
3217 if (targetPtr->tagTablePtr != NULL) {
3218 ReleaseTagTable(targetPtr->tagTablePtr);
3219 }
3220 targetPtr->tagTablePtr = sourcePtr->tagTablePtr;
3221 return TCL_OK;
3222 }
3223
3224 int
3225 Blt_TreeTagTableIsShared(TreeClient *clientPtr)
3226 {
3227 return (clientPtr->tagTablePtr->refCount > 1);
3228 }
3229
3230 void
3231 Blt_TreeClearTags(TreeClient *clientPtr, Blt_TreeNode node)
3232 {
3233 Blt_HashEntry *hPtr, *h2Ptr;
3234 Blt_HashSearch cursor;
3235
3236 for (hPtr = Blt_FirstHashEntry(&clientPtr->tagTablePtr->tagTable, &cursor);
3237 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
3238 Blt_TreeTagEntry *tPtr;
3239
3240 tPtr = Blt_GetHashValue(hPtr);
3241 h2Ptr = Blt_FindHashEntry(&tPtr->nodeTable, (char *)node);
3242 if (h2Ptr != NULL) {
3243 SetModified(node);
3244 Blt_DeleteHashEntry(&tPtr->nodeTable, h2Ptr);
3245 }
3246 }
3247 }
3248
3249 int
3250 Blt_TreeHasTag(
3251 TreeClient *clientPtr,
3252 Blt_TreeNode node,
3253 CONST char *tagName)
3254 {
3255 Blt_HashEntry *hPtr;
3256 Blt_TreeTagEntry *tPtr;
3257
3258 if (strcmp(tagName, "all") == 0) {
3259 return TRUE;
3260 }
3261 if (strcmp(tagName, "nonroot") == 0) {
3262 return TRUE;
3263 }
3264 if (strcmp(tagName, "rootchildren") == 0) {
3265 return TRUE;
3266 }
3267 if ((strcmp(tagName, "root") == 0) &&
3268 (node == Blt_TreeRootNode(clientPtr))) {
3269 return TRUE;
3270 }
3271 hPtr = Blt_FindHashEntry(&clientPtr->tagTablePtr->tagTable, tagName);
3272 if (hPtr == NULL) {
3273 return FALSE;
3274 }
3275 tPtr = Blt_GetHashValue(hPtr);
3276 hPtr = Blt_FindHashEntry(&tPtr->nodeTable, (char *)node);
3277 if (hPtr == NULL) {
3278 return FALSE;
3279 }
3280 return TRUE;
3281 }
3282
3283 int
3284 Blt_TreeAddTag(
3285 TreeClient *clientPtr,
3286 Blt_TreeNode node,
3287 CONST char *tagName)
3288 {
3289 int isNew, isNewN, cnt = 0, flags;
3290 Blt_HashEntry *hPtr;
3291 Blt_HashTable *tablePtr;
3292 Blt_TreeTagEntry *tPtr;
3293 Tcl_Interp *interp = clientPtr->treeObject->interp;
3294
3295 if ((strcmp(tagName, "all") == 0) || (strcmp(tagName, "root") == 0)
3296 || (strcmp(tagName, "nonroot") == 0) || (strcmp(tagName, "rootchildren") == 0)) {
3297 Tcl_AppendResult(interp, "reserved tag", 0);
3298 return TCL_ERROR;
3299 }
3300 tablePtr = &clientPtr->tagTablePtr->tagTable;
3301 hPtr = Blt_CreateHashEntry(tablePtr, tagName, &isNewN);
3302 assert(hPtr);
3303 if (isNewN) {
3304
3305 tPtr = Blt_Calloc(sizeof(Blt_TreeTagEntry), 1);
3306 Blt_InitHashTable(&tPtr->nodeTable, BLT_ONE_WORD_KEYS);
3307 Blt_SetHashValue(hPtr, tPtr);
3308 tPtr->hashPtr = hPtr;
3309 tPtr->tagName = Blt_GetHashKey(tablePtr, hPtr);
3310 Blt_TreeTagRefIncr(tPtr);
3311 } else {
3312 tPtr = Blt_GetHashValue(hPtr);
3313 }
3314 if (node == NULL) {
3315 return TCL_OK;
3316 }
3317 if (!(node->flags & TREE_TRACE_ACTIVE)) {
3318 int result;
3319 flags = TREE_TRACE_TAGADD;
3320 if (tPtr->nodeTable.numEntries > 0) {
3321 flags |= TREE_TRACE_TAGMULTIPLE;
3322 }
3323 result = CallTraces(interp, clientPtr, node->treeObject, node, tagName,
3324 flags, &cnt);
3325 if (result == TCL_BREAK) {
3326 return TCL_OK;
3327 }
3328 if (result != TCL_OK) {
3329 return result;
3330 }
3331 }
3332 hPtr = Blt_CreateHashEntry(&tPtr->nodeTable, (char *)node, &isNew);
3333 assert(hPtr);
3334 if (isNew) {
3335 SetModified(node);
3336 Blt_SetHashValue(hPtr, node);
3337 }
3338 return TCL_OK;
3339 }
3340
3341 /* Trigger tag delete traces. */
3342 int
3343 Blt_TreeTagDelTrace(
3344 TreeClient *clientPtr,
3345 Blt_TreeNode node,
3346 CONST char *tagName)
3347 {
3348 Tcl_Interp *interp = clientPtr->treeObject->interp;
3349 int cnt;
3350
3351 if (!(node->flags & TREE_TRACE_ACTIVE)) {
3352 return CallTraces(interp, clientPtr, node->treeObject, node, tagName,
3353 (TREE_TRACE_TAGDELETE), &cnt);
3354 }
3355 return TCL_OK;
3356 }
3357
3358 int
3359 Blt_TreeForgetTag(TreeClient *clientPtr, CONST char *tagName)
3360 {
3361 Blt_HashEntry *hPtr;
3362 if ((strcmp(tagName, "all") == 0) || (strcmp(tagName, "root") == 0)
3363 || (strcmp(tagName, "nonroot") == 0)
3364 || (strcmp(tagName, "rootchildren") == 0)) {
3365 return TCL_OK;
3366 }
3367
3368 hPtr = Blt_FindHashEntry(&clientPtr->tagTablePtr->tagTable, tagName);
3369 if (hPtr != NULL) {
3370 Blt_TreeTagEntry *tPtr;
3371 Blt_TreeNode node;
3372 Blt_HashSearch cursor;
3373
3374 Blt_DeleteHashEntry(&clientPtr->tagTablePtr->tagTable, hPtr);
3375 tPtr = Blt_GetHashValue(hPtr);
3376 for (hPtr = Blt_FirstHashEntry(&tPtr->nodeTable, &cursor);
3377 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
3378
3379 node = Blt_GetHashKey(&tPtr->nodeTable, hPtr);
3380 if (Blt_TreeTagDelTrace(clientPtr, node, tagName) != TCL_OK) {
3381 return TCL_ERROR;
3382 }
3383 SetModified(node);
3384 }
3385 Blt_DeleteHashTable(&tPtr->nodeTable);
3386 Blt_TreeTagRefDecr(tPtr);
3387 }
3388 return TCL_OK;
3389 }
3390
3391 /*
3392 *----------------------------------------------------------------------
3393 *
3394 * Blt_TreeTagHashTable --
3395 *
3396 *----------------------------------------------------------------------
3397 */
3398 Blt_HashTable *
3399 Blt_TreeTagHashTable(TreeClient *clientPtr, CONST char *tagName)
3400 {
3401 Blt_HashEntry *hPtr;
3402
3403 hPtr = Blt_FindHashEntry(&clientPtr->tagTablePtr->tagTable, tagName);
3404 if (hPtr != NULL) {
3405 Blt_TreeTagEntry *tPtr;
3406
3407 tPtr = Blt_GetHashValue(hPtr);
3408 return &tPtr->nodeTable;
3409 }
3410 return NULL;
3411 }
3412
3413 Blt_TreeTagEntry *
3414 Blt_TreeTagHashEntry(TreeClient *clientPtr, CONST char *tagName)
3415 {
3416 Blt_HashEntry *hPtr;
3417
3418 hPtr = Blt_FindHashEntry(&clientPtr->tagTablePtr->tagTable, tagName);
3419 if (hPtr != NULL) {
3420 Blt_TreeTagEntry *tPtr;
3421
3422 tPtr = Blt_GetHashValue(hPtr);
3423 return tPtr;
3424 }
3425 return NULL;
3426 }
3427
3428 Blt_HashEntry *
3429 Blt_TreeFirstTag(TreeClient *clientPtr, Blt_HashSearch *cursorPtr)
3430 {
3431 return Blt_FirstHashEntry(&clientPtr->tagTablePtr->tagTable, cursorPtr);
3432 }
3433
3434 #if (SIZEOF_VOID_P == 8)
3435 /*
3436 *----------------------------------------------------------------------
3437 *
3438 * HashOneWord --
3439 *
3440 * Compute a one-word hash value of a 64-bit word, which then can
3441 * be used to generate a hash index.
3442 *
3443 * From Knuth, it's a multiplicative hash. Multiplies an unsigned
3444 * 64-bit value with the golden ratio (sqrt(5) - 1) / 2. The
3445 * downshift value is 64 - n, when n is the log2 of the size of
3446 * the hash table.
3447 *
3448 * Results:
3449 * The return value is a one-word summary of the information in
3450 * 64 bit word.
3451 *
3452 * Side effects:
3453 * None.
3454 *
3455 *----------------------------------------------------------------------
3456 */
3457 static Blt_Hash
3458 HashOneWord(
3459 uint64_t mask,
3460 unsigned int downshift,
3461 CONST void *key)
3462 {
3463 uint64_t a0, a1;
3464 uint64_t y0, y1;
3465 uint64_t y2, y3;
3466 uint64_t p1, p2;
3467 uint64_t result;
3468
3469 /* Compute key * GOLDEN_RATIO in 128-bit arithmetic */
3470 a0 = (uint64_t)key & 0x00000000FFFFFFFF;
3471 a1 = (uint64_t)key >> 32;
3472
3473 y0 = a0 * 0x000000007f4a7c13;
3474 y1 = a0 * 0x000000009e3779b9;
3475 y2 = a1 * 0x000000007f4a7c13;
3476 y3 = a1 * 0x000000009e3779b9;
3477 y1 += y0 >> 32; /* Can't carry */
3478 y1 += y2; /* Might carry */
3479 if (y1 < y2) {
3480 y3 += (1LL << 32); /* Propagate */
3481 }
3482
3483 /* 128-bit product: p1 = loword, p2 = hiword */
3484 p1 = ((y1 & 0x00000000FFFFFFFF) << 32) + (y0 & 0x00000000FFFFFFFF);
3485 p2 = y3 + (y1 >> 32);
3486
3487 /* Left shift the value downward by the size of the table */
3488 if (downshift > 0) {
3489 if (downshift < 64) {
3490 result = ((p2 << (64 - downshift)) | (p1 >> (downshift & 63)));
3491 } else {
3492 result = p2 >> (downshift & 63);
3493 }
3494 } else {
3495 result = p1;
3496 }
3497 /* Finally mask off the high bits */
3498 return (Blt_Hash)(result & mask);
3499 }
3500
3501 #endif /* SIZEOF_VOID_P == 8 */
3502
3503 /*
3504 *----------------------------------------------------------------------
3505 *
3506 * RebuildTable --
3507 *
3508 * This procedure is invoked when the ratio of entries to hash
3509 * buckets becomes too large. It creates a new table with a
3510 * larger bucket array and moves all of the entries into the
3511 * new table.
3512 *
3513 * Results:
3514 * None.
3515 *
3516 * Side effects:
3517 * Memory gets reallocated and entries get re-hashed to new
3518 * buckets.
3519 *
3520 *----------------------------------------------------------------------
3521 */
3522 static void
3523 RebuildTable(Node *nodePtr) /* Table to enlarge. */
3524 {
3525 Value **newBucketPtr, **oldBuckets;
3526 register Value **bucketPtr, **endPtr;
3527 register Value *valuePtr, *nextPtr;
3528 unsigned int downshift;
3529 unsigned long mask;
3530 Value **buckets;
3531 size_t nBuckets;
3532
3533 oldBuckets = (Value **)nodePtr->values;
3534 nBuckets = (1 << nodePtr->logSize);
3535 endPtr = oldBuckets + nBuckets;
3536
3537 /*
3538 * Allocate and initialize the new bucket array, and set up
3539 * hashing constants for new array size.
3540 */
3541 nodePtr->logSize += 2;
3542 nBuckets = (1 << nodePtr->logSize);
3543 buckets = Blt_Calloc(nBuckets, sizeof(Value *));
3544
3545 /*
3546 * Move all of the existing entries into the new bucket array,
3547 * based on their hash values.
3548 */
3549 mask = nBuckets - 1;
3550 downshift = DOWNSHIFT_START - nodePtr->logSize;
3551 for (bucketPtr = oldBuckets; bucketPtr < endPtr; bucketPtr++) {
3552 for (valuePtr = *bucketPtr; valuePtr != NULL; valuePtr = nextPtr) {
3553 nextPtr = valuePtr->next;
3554 newBucketPtr = buckets + RANDOM_INDEX(valuePtr->key);
3555 valuePtr->next = *newBucketPtr;
3556 *newBucketPtr = valuePtr;
3557 }
3558 }
3559 nodePtr->values = (Value *)buckets;
3560 Blt_Free(oldBuckets);
3561 }
3562
3563 static void
3564 ConvertValues(Node *nodePtr)
3565 {
3566 unsigned int nBuckets;
3567 Value **buckets;
3568 unsigned int mask;
3569 int downshift;
3570 Value *valuePtr, *nextPtr, **bucketPtr;
3571
3572 /*
3573 * Convert list of values into a hash table.
3574 */
3575 nodePtr->logSize = START_LOGSIZE;
3576 nBuckets = 1 << nodePtr->logSize;
3577 buckets = Blt_Calloc(nBuckets, sizeof(Value *));
3578 mask = nBuckets - 1;
3579 downshift = DOWNSHIFT_START - nodePtr->logSize;
3580 for (valuePtr = nodePtr->values; valuePtr != NULL; valuePtr = nextPtr) {
3581 nextPtr = valuePtr->next;
3582 bucketPtr = buckets + RANDOM_INDEX(valuePtr->key);
3583 valuePtr->next = *bucketPtr;
3584 *bucketPtr = valuePtr;
3585 }
3586 nodePtr->values = (Value *)buckets;
3587 }
3588
3589 /*
3590 *----------------------------------------------------------------------
3591 *
3592 * TreeDeleteValue --
3593 *
3594 * Remove a single entry from a hash table.
3595 *
3596 * Results:
3597 * None.
3598 *
3599 * Side effects:
3600 * The entry given by entryPtr is deleted from its table and
3601 * should never again be used by the caller. It is up to the
3602 * caller to free the clientData field of the entry, if that
3603 * is relevant.
3604 *
3605 *----------------------------------------------------------------------
3606 */
3607 static int
3608 TreeDeleteValue(Node *nodePtr, Blt_TreeValue value)
3609 {
3610 Value *valuePtr = value;
3611 register Value *prevPtr;
3612
3613 if (nodePtr->logSize > 0) {
3614 Value **bucketPtr;
3615 unsigned int downshift;
3616 unsigned long mask;
3617
3618 mask = (1 << nodePtr->logSize) - 1;
3619 downshift = DOWNSHIFT_START - nodePtr->logSize;
3620
3621 bucketPtr = (Value **)nodePtr->values + RANDOM_INDEX(valuePtr->key);
3622 if (*bucketPtr == valuePtr) {
3623 *bucketPtr = valuePtr->next;
3624 } else {
3625 for (prevPtr = *bucketPtr; /*empty*/; prevPtr = prevPtr->next) {
3626 if (prevPtr == NULL) {
3627 return TCL_ERROR; /* Can't find value in hash bucket. */
3628 }
3629 if (prevPtr->next == valuePtr) {
3630 prevPtr->next = valuePtr->next;
3631 break;
3632 }
3633 }
3634 }
3635 } else {
3636 prevPtr = NULL;
3637 for (valuePtr = nodePtr->values; valuePtr != NULL;
3638 valuePtr = valuePtr->next) {
3639 if (valuePtr == value) {
3640 break;
3641 }
3642 prevPtr = valuePtr;
3643 }
3644 if (valuePtr == NULL) {
3645 return TCL_ERROR; /* Can't find value in list. */
3646 }
3647 if (prevPtr == NULL) {
3648 nodePtr->values = valuePtr->next;
3649 } else {
3650 prevPtr->next = valuePtr->next;
3651 }
3652 }
3653 nodePtr->nValues--;
3654 FreeValue(nodePtr, valuePtr);
3655 return TCL_OK;
3656 }
3657
3658 /*
3659 *----------------------------------------------------------------------
3660 *
3661 * TreeDestroyValues --
3662 *
3663 * Free up everything associated with a hash table except for
3664 * the record for the table itself.
3665 *
3666 * Results:
3667 * None.
3668 *
3669 * Side effects:
3670 * The hash table is no longer useable.
3671 *
3672 *----------------------------------------------------------------------
3673 */
3674 static void
3675 TreeDestroyValues(Node *nodePtr)
3676 {
3677 register Value *valuePtr;
3678 Value *nextPtr;
3679
3680 /*
3681 * Free up all the entries in the table.
3682 */
3683 if (nodePtr->values == NULL) {
3684 return;
3685 }
3686 if (nodePtr->logSize > 0) {
3687 Value **buckets;
3688 int nBuckets;
3689 int i;
3690
3691 buckets = (Value **)nodePtr->values;
3692 nBuckets = (1 << nodePtr->logSize);
3693 for (i = 0; i < nBuckets; i++) {
3694 for (valuePtr = buckets[i]; valuePtr != NULL; valuePtr = nextPtr) {
3695 nextPtr = valuePtr->next;
3696 FreeValue(nodePtr, valuePtr);
3697 }
3698 }
3699 Blt_Free(buckets);
3700 } else {
3701 for (valuePtr = nodePtr->values; valuePtr != NULL; valuePtr = nextPtr) {
3702 nextPtr = valuePtr->next;
3703 FreeValue(nodePtr, valuePtr);
3704 }
3705 }
3706 nodePtr->values = NULL;
3707 nodePtr->nValues = 0;
3708 nodePtr->logSize = 0;
3709 }
3710
3711 /*
3712 *----------------------------------------------------------------------
3713 *
3714 * TreeFirstValue --
3715 *
3716 * Locate the first entry in a hash table and set up a record
3717 * that can be used to step through all the remaining entries
3718 * of the table.
3719 *
3720 * Results:
3721 * The return value is a pointer to the first value in tablePtr,
3722 * or NULL if tablePtr has no entries in it. The memory at
3723 * *searchPtr is initialized so that subsequent calls to
3724 * Blt_TreeNextValue will return all of the values in the table,
3725 * one at a time.
3726 *
3727 * Side effects:
3728 * None.
3729 *
3730 *----------------------------------------------------------------------
3731 */
3732 static Value *
3733 TreeFirstValue(
3734 Node *nodePtr,
3735 Blt_TreeKeySearch *searchPtr) /* Place to store information about
3736 * progress through the table. */
3737 {
3738 searchPtr->node = nodePtr;
3739 searchPtr->nextIndex = 0;
3740 searchPtr->cnt = 1;
3741 if (nodePtr->logSize > 0) {
3742 searchPtr->nextValue = NULL;
3743 } else {
3744 searchPtr->nextValue = nodePtr->values;
3745 }
3746 return TreeNextValue(searchPtr);
3747 }
3748
3749 /*
3750 *----------------------------------------------------------------------
3751 *
3752 * TreeNextValue --
3753 *
3754 * Once a hash table enumeration has been initiated by calling
3755 * Blt_TreeFirstValue, this procedure may be called to return
3756 * successive elements of the table.
3757 *
3758 * Results:
3759 * The return value is the next entry in the hash table being
3760 * enumerated, or NULL if the end of the table is reached.
3761 *
3762 * Side effects:
3763 * None.
3764 *
3765 *----------------------------------------------------------------------
3766 */
3767 static Value *
3768 TreeNextValue(
3769 Blt_TreeKeySearch *searchPtr) /* Place to store information about
3770 * progress through the table. Must
3771 * have been initialized by calling
3772 * Blt_TreeFirstValue. */
3773 {
3774 Value *valuePtr;
3775
3776 if (searchPtr->node->logSize > 0) {
3777 size_t nBuckets;
3778 Value **buckets;
3779
3780 nBuckets = (1 << searchPtr->node->logSize);
3781 buckets = (Value **)searchPtr->node->values;
3782 while (searchPtr->nextValue == NULL) {
3783 if (searchPtr->nextIndex >= nBuckets) {
3784 return NULL;
3785 }
3786 searchPtr->nextValue = buckets[searchPtr->nextIndex];
3787 searchPtr->nextIndex++;
3788 }
3789 }
3790 searchPtr->cnt++;
3791 /* Sanity check. */
3792 if (searchPtr->cnt>100000000) return NULL;
3793 valuePtr = searchPtr->nextValue;
3794 if (valuePtr != NULL) {
3795 searchPtr->nextValue = valuePtr->next;
3796 }
3797 return valuePtr;
3798 }
3799
3800 /*
3801 *----------------------------------------------------------------------
3802 *
3803 * TreeFindValue --
3804 *
3805 * Given a hash table with one-word keys, and a one-word key, find
3806 * the entry with a matching key.
3807 *
3808 * Results:
3809 * The return value is a token for the matching entry in the
3810 * hash table, or NULL if there was no matching entry.
3811 *
3812 * Side effects:
3813 * None.
3814 *
3815 *----------------------------------------------------------------------
3816 */
3817 static Value *
3818 TreeFindValue(
3819 Node *nodePtr,
3820 Blt_TreeKey key) /* Key to use to find matching entry. */
3821 {
3822 register Value *valuePtr;
3823 Value *bucket;
3824
3825 if (nodePtr->logSize > 0) {
3826 unsigned int downshift;
3827 unsigned long mask;
3828
3829 mask = (1 << nodePtr->logSize) - 1;
3830 downshift = DOWNSHIFT_START - nodePtr->logSize;
3831 bucket = ((Value **)(nodePtr->values))[RANDOM_INDEX((void *)key)];
3832 } else {
3833 bucket = nodePtr->values; /* Single chain list. */
3834 }
3835 /*
3836 * Search all of the entries in the appropriate bucket.
3837 */
3838 for (valuePtr = bucket; valuePtr != NULL; valuePtr = valuePtr->next) {
3839 if (valuePtr->key == key) {
3840 return valuePtr;
3841 }
3842 }
3843 return NULL;
3844 }
3845
3846 /*
3847 *----------------------------------------------------------------------
3848 *
3849 * Blt_TreeCreateValue --
3850 *
3851 * Find the value with a matching key. If there is no matching
3852 * value, then create a new one.
3853 *
3854 * Results:
3855 * The return value is a pointer to the matching value. If this
3856 * is a newly-created value, then *newPtr will be set to a non-zero
3857 * value; otherwise *newPtr will be set to 0.
3858 *
3859 * Side effects:
3860 * A new value may be added to the hash table.
3861 *
3862 *----------------------------------------------------------------------
3863 */
3864 static Value *
3865 TreeCreateValue(
3866 Node *nodePtr,
3867 Blt_TreeKey key, /* Key to use to find or create matching
3868 * entry. */
3869 int *newPtr) /* Store info here telling whether a new
3870 * entry was created. */
3871 {
3872 register Value *valuePtr;
3873 TreeObject *treeObjPtr = nodePtr->treeObject;
3874 int maxVals;
3875
3876 if (treeObjPtr->maxKeyList>0) {
3877 maxVals = treeObjPtr->maxKeyList;
3878 } else {
3879 maxVals = MAX_LIST_VALUES;
3880 }
3881 /*
3882 * Check if there as so many values that storage should be
3883 * converted from a hash table instead of a list.
3884 */
3885 if ((nodePtr->logSize == 0) && (nodePtr->nValues >= maxVals)) {
3886 ConvertValues(nodePtr);
3887 }
3888 if (nodePtr->logSize > 0) {
3889 Value **bucketPtr;
3890 size_t nBuckets;
3891 unsigned int downshift;
3892 unsigned long mask;
3893
3894 nBuckets = (1 << nodePtr->logSize);
3895 mask = nBuckets - 1;
3896 downshift = DOWNSHIFT_START - nodePtr->logSize;
3897 bucketPtr = (Value **)nodePtr->values + RANDOM_INDEX((void *)key);
3898
3899 *newPtr = FALSE;
3900 for (valuePtr = *bucketPtr; valuePtr != NULL;
3901 valuePtr = valuePtr->next) {
3902 if (valuePtr->key == key) {
3903 return valuePtr;
3904 }
3905 }
3906
3907 /* Value not found. Add a new value to the bucket. */
3908 *newPtr = TRUE;
3909 valuePtr = Blt_PoolAllocItem(nodePtr->treeObject->valuePool,
3910 sizeof(Value));
3911 valuePtr->key = key;
3912 valuePtr->owner = NULL;
3913 valuePtr->next = *bucketPtr;
3914 valuePtr->objPtr = NULL;
3915 *bucketPtr = valuePtr;
3916 nodePtr->nValues++;
3917
3918 /*
3919 * If the table has exceeded a decent size, rebuild it with many
3920 * more buckets.
3921 */
3922 if ((unsigned int)nodePtr->nValues >= (nBuckets * 3)) {
3923 RebuildTable(nodePtr);
3924 }
3925 } else {
3926 Value *prevPtr;
3927
3928 prevPtr = NULL;
3929 *newPtr = FALSE;
3930 for (valuePtr = nodePtr->values; valuePtr != NULL;
3931 valuePtr = valuePtr->next) {
3932 if (valuePtr->key == key) {
3933 return valuePtr;
3934 }
3935 prevPtr = valuePtr;
3936 }
3937 /* Value not found. Add a new value to the list. */
3938 *newPtr = TRUE;
3939 valuePtr = Blt_PoolAllocItem(nodePtr->treeObject->valuePool,
3940 sizeof(Value));
3941 valuePtr->key = key;
3942 valuePtr->owner = NULL;
3943 valuePtr->next = NULL;
3944 valuePtr->objPtr = NULL;
3945 if (prevPtr == NULL) {
3946 nodePtr->values = valuePtr;
3947 } else {
3948 prevPtr->next = valuePtr;
3949 }
3950 nodePtr->nValues++;
3951 }
3952 return valuePtr;
3953 }
3954
3955
3956 Blt_TreeNode Blt_TreeEndNode (Blt_TreeNode node,
3957 unsigned int nodeFlags) {
3958 /* TODO: finish. */
3959 return NULL;
3960 }
3961
3962 #undef Blt_TreeFirstChild
3963 #undef Blt_TreeLastChild
3964 #undef Blt_TreeNextSibling
3965 #undef Blt_TreePrevSibling
3966 #undef Blt_TreeChangeRoot
3967
3968 Blt_TreeNode Blt_TreeFirstChild (Blt_TreeNode parent) {
3969 return parent->first;
3970 }
3971
3972 Blt_TreeNode Blt_TreeNextSibling (Blt_TreeNode node) {
3973 return (node == NULL ? NULL : node->next);
3974 }
3975
3976 Blt_TreeNode Blt_TreeLastChild (Blt_TreeNode parent) {
3977 return parent->last;
3978 }
3979
3980 Blt_TreeNode Blt_TreePrevSibling (Blt_TreeNode node) {
3981 return (node == NULL ? NULL : node->prev);
3982 }
3983
3984 Blt_TreeNode Blt_TreeChangeRoot (Blt_Tree tree, Blt_TreeNode node) {
3985 tree->root = node;
3986 return node;
3987 }
3988
3989 #endif /* NO_TREE */