51#include "MagickCore/studio.h"
52#include "MagickCore/exception.h"
53#include "MagickCore/exception-private.h"
54#include "MagickCore/locale_.h"
55#include "MagickCore/log.h"
56#include "MagickCore/memory_.h"
57#include "MagickCore/memory-private.h"
58#include "MagickCore/splay-tree.h"
59#include "MagickCore/semaphore.h"
60#include "MagickCore/string_.h"
65#define MaxSplayTreeDepth 1024
89 (*compare)(
const void *,
const void *);
92 *(*relinquish_key)(
void *),
93 *(*relinquish_value)(
void *);
154MagickExport MagickBooleanType AddValueToSplayTree(
SplayTreeInfo *splay_tree,
155 const void *key,
const void *value)
163 LockSemaphoreInfo(splay_tree->semaphore);
164 SplaySplayTree(splay_tree,key);
166 if (splay_tree->root != (
NodeInfo *) NULL)
168 if (splay_tree->compare != (int (*)(
const void *,
const void *)) NULL)
169 compare=splay_tree->compare(splay_tree->root->key,key);
171 compare=(splay_tree->root->key > key) ? 1 :
172 ((splay_tree->root->key < key) ? -1 : 0);
175 if ((splay_tree->relinquish_value != (
void *(*)(
void *)) NULL) &&
176 (splay_tree->root->value != (
void *) NULL))
177 splay_tree->root->value=splay_tree->relinquish_value(
178 splay_tree->root->value);
179 if ((splay_tree->relinquish_key != (
void *(*)(
void *)) NULL) &&
180 (splay_tree->root->key != (
void *) NULL))
181 splay_tree->root->key=splay_tree->relinquish_key(
182 splay_tree->root->key);
183 splay_tree->root->key=(
void *) key;
184 splay_tree->root->value=(
void *) value;
185 UnlockSemaphoreInfo(splay_tree->semaphore);
189 node=(
NodeInfo *) AcquireMagickMemory(
sizeof(*node));
192 UnlockSemaphoreInfo(splay_tree->semaphore);
195 node->key=(
void *) key;
196 node->value=(
void *) value;
197 if (splay_tree->root == (
NodeInfo *) NULL)
205 node->left=splay_tree->root;
206 node->right=node->left->right;
207 node->left->right=(
NodeInfo *) NULL;
211 node->right=splay_tree->root;
212 node->left=node->right->left;
213 node->right->left=(
NodeInfo *) NULL;
215 splay_tree->root=node;
216 splay_tree->key=(
void *) NULL;
218 UnlockSemaphoreInfo(splay_tree->semaphore);
256 bisect=low+(high-low)/2;
258 if ((low+1) > bisect)
261 node->left=LinkSplayTreeNodes(nodes,low,bisect-1);
262 if ((bisect+1) > high)
265 node->right=LinkSplayTreeNodes(nodes,bisect+1,high);
269static inline int SplayTreeToNodeArray(
NodeInfo *node,
const void *nodes)
286 if (splay_tree->nodes <= 2)
288 splay_tree->balance=MagickFalse;
291 nodes=(
NodeInfo **) AcquireQuantumMemory((
size_t) splay_tree->nodes,
294 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
296 (void) IterateOverSplayTree(splay_tree,SplayTreeToNodeArray,(
const void *)
298 splay_tree->root=LinkSplayTreeNodes(nodes,0,splay_tree->nodes-1);
299 splay_tree->balance=MagickFalse;
300 nodes=(
NodeInfo **) RelinquishMagickMemory(nodes);
333static inline void *GetFirstSplayTreeNode(
SplayTreeInfo *splay_tree)
338 node=splay_tree->root;
339 if (splay_tree->root == (
NodeInfo *) NULL)
341 while (node->left != (
NodeInfo *) NULL)
347 void *(*clone_key)(
void *),
void *(*clone_value)(
void *))
357 assert(splay_tree->signature == MagickCoreSignature);
358 if (IsEventLogging() != MagickFalse)
359 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
360 clone_tree=NewSplayTree(splay_tree->compare,splay_tree->relinquish_key,
361 splay_tree->relinquish_value);
362 LockSemaphoreInfo(splay_tree->semaphore);
363 if (splay_tree->root == (
NodeInfo *) NULL)
365 UnlockSemaphoreInfo(splay_tree->semaphore);
368 next=(
NodeInfo *) GetFirstSplayTreeNode(splay_tree);
371 SplaySplayTree(splay_tree,next);
372 (void) AddValueToSplayTree(clone_tree,clone_key(splay_tree->root->key),
373 clone_value(splay_tree->root->value));
375 node=splay_tree->root->right;
378 while (node->left != (
NodeInfo *) NULL)
383 UnlockSemaphoreInfo(splay_tree->semaphore);
412MagickExport
int CompareSplayTreeString(
const void *target,
const void *source)
418 p=(
const char *) target;
419 q=(
const char *) source;
420 return(LocaleCompare(p,q));
448MagickExport
int CompareSplayTreeStringInfo(
const void *target,
457 return(CompareStringInfo(p,q));
486MagickExport MagickBooleanType DeleteNodeByValueFromSplayTree(
494 assert(splay_tree->signature == MagickCoreSignature);
495 if (IsEventLogging() != MagickFalse)
496 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
497 LockSemaphoreInfo(splay_tree->semaphore);
498 if (splay_tree->root == (
NodeInfo *) NULL)
500 UnlockSemaphoreInfo(splay_tree->semaphore);
503 next=(
NodeInfo *) GetFirstSplayTreeNode(splay_tree);
506 SplaySplayTree(splay_tree,next);
508 node=splay_tree->root->right;
511 while (node->left != (
NodeInfo *) NULL)
515 if (splay_tree->root->value == value)
530 key=splay_tree->root->key;
531 SplaySplayTree(splay_tree,key);
532 splay_tree->key=(
void *) NULL;
533 if (splay_tree->compare != (int (*)(
const void *,
const void *)) NULL)
534 compare=splay_tree->compare(splay_tree->root->key,key);
536 compare=(splay_tree->root->key > key) ? 1 :
537 ((splay_tree->root->key < key) ? -1 : 0);
540 UnlockSemaphoreInfo(splay_tree->semaphore);
543 left=splay_tree->root->left;
544 right=splay_tree->root->right;
545 if ((splay_tree->relinquish_value != (
void *(*)(
void *)) NULL) &&
546 (splay_tree->root->value != (
void *) NULL))
547 splay_tree->root->value=splay_tree->relinquish_value(
548 splay_tree->root->value);
549 if ((splay_tree->relinquish_key != (
void *(*)(
void *)) NULL) &&
550 (splay_tree->root->key != (
void *) NULL))
551 splay_tree->root->key=splay_tree->relinquish_key(
552 splay_tree->root->key);
553 splay_tree->root=(
NodeInfo *) RelinquishMagickMemory(splay_tree->root);
557 splay_tree->root=right;
558 UnlockSemaphoreInfo(splay_tree->semaphore);
561 splay_tree->root=left;
564 while (left->right != (
NodeInfo *) NULL)
568 UnlockSemaphoreInfo(splay_tree->semaphore);
572 UnlockSemaphoreInfo(splay_tree->semaphore);
603MagickExport MagickBooleanType DeleteNodeFromSplayTree(
614 assert(splay_tree->signature == MagickCoreSignature);
615 if (IsEventLogging() != MagickFalse)
616 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
617 if (splay_tree->root == (
NodeInfo *) NULL)
619 LockSemaphoreInfo(splay_tree->semaphore);
620 SplaySplayTree(splay_tree,key);
621 splay_tree->key=(
void *) NULL;
622 if (splay_tree->compare != (int (*)(
const void *,
const void *)) NULL)
623 compare=splay_tree->compare(splay_tree->root->key,key);
625 compare=(splay_tree->root->key > key) ? 1 :
626 ((splay_tree->root->key < key) ? -1 : 0);
629 UnlockSemaphoreInfo(splay_tree->semaphore);
632 left=splay_tree->root->left;
633 right=splay_tree->root->right;
634 if ((splay_tree->relinquish_value != (
void *(*)(
void *)) NULL) &&
635 (splay_tree->root->value != (
void *) NULL))
636 splay_tree->root->value=splay_tree->relinquish_value(
637 splay_tree->root->value);
638 if ((splay_tree->relinquish_key != (
void *(*)(
void *)) NULL) &&
639 (splay_tree->root->key != (
void *) NULL))
640 splay_tree->root->key=splay_tree->relinquish_key(splay_tree->root->key);
641 splay_tree->root=(
NodeInfo *) RelinquishMagickMemory(splay_tree->root);
645 splay_tree->root=right;
646 UnlockSemaphoreInfo(splay_tree->semaphore);
649 splay_tree->root=left;
652 while (left->right != (
NodeInfo *) NULL)
656 UnlockSemaphoreInfo(splay_tree->semaphore);
691 LockSemaphoreInfo(splay_tree->semaphore);
692 if (splay_tree->root != (
NodeInfo *) NULL)
694 if ((splay_tree->relinquish_value != (
void *(*)(
void *)) NULL) &&
695 (splay_tree->root->value != (
void *) NULL))
696 splay_tree->root->value=splay_tree->relinquish_value(
697 splay_tree->root->value);
698 if ((splay_tree->relinquish_key != (
void *(*)(
void *)) NULL) &&
699 (splay_tree->root->key != (
void *) NULL))
700 splay_tree->root->key=splay_tree->relinquish_key(splay_tree->root->key);
701 splay_tree->root->key=(
void *) NULL;
702 for (pend=splay_tree->root; pend != (
NodeInfo *) NULL; )
707 if (active->left != (
NodeInfo *) NULL)
709 if ((splay_tree->relinquish_value != (
void *(*)(
void *)) NULL) &&
710 (active->left->value != (
void *) NULL))
711 active->left->value=splay_tree->relinquish_value(
712 active->left->value);
713 if ((splay_tree->relinquish_key != (
void *(*)(
void *)) NULL) &&
714 (active->left->key != (
void *) NULL))
715 active->left->key=splay_tree->relinquish_key(active->left->key);
716 active->left->key=(
void *) pend;
719 if (active->right != (
NodeInfo *) NULL)
721 if ((splay_tree->relinquish_value != (
void *(*)(
void *)) NULL) &&
722 (active->right->value != (
void *) NULL))
723 active->right->value=splay_tree->relinquish_value(
724 active->right->value);
725 if ((splay_tree->relinquish_key != (
void *(*)(
void *)) NULL) &&
726 (active->right->key != (
void *) NULL))
727 active->right->key=splay_tree->relinquish_key(
729 active->right->key=(
void *) pend;
734 node=(
NodeInfo *) RelinquishMagickMemory(node);
738 splay_tree->signature=(~MagickCoreSignature);
739 UnlockSemaphoreInfo(splay_tree->semaphore);
740 RelinquishSemaphoreInfo(&splay_tree->semaphore);
741 splay_tree=(
SplayTreeInfo *) RelinquishMagickMemory(splay_tree);
769MagickExport
const void *GetNextKeyInSplayTree(
SplayTreeInfo *splay_tree)
778 assert(splay_tree->signature == MagickCoreSignature);
779 if (IsEventLogging() != MagickFalse)
780 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
781 if ((splay_tree->root == (
NodeInfo *) NULL) ||
782 (splay_tree->next == (
void *) NULL))
783 return((
void *) NULL);
784 LockSemaphoreInfo(splay_tree->semaphore);
785 SplaySplayTree(splay_tree,splay_tree->next);
786 splay_tree->next=(
void *) NULL;
787 node=splay_tree->root->right;
790 while (node->left != (
NodeInfo *) NULL)
792 splay_tree->next=node->key;
794 key=splay_tree->root->key;
795 UnlockSemaphoreInfo(splay_tree->semaphore);
823MagickExport
const void *GetNextValueInSplayTree(
SplayTreeInfo *splay_tree)
832 assert(splay_tree->signature == MagickCoreSignature);
833 if (IsEventLogging() != MagickFalse)
834 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
835 if ((splay_tree->root == (
NodeInfo *) NULL) ||
836 (splay_tree->next == (
void *) NULL))
837 return((
void *) NULL);
838 LockSemaphoreInfo(splay_tree->semaphore);
839 SplaySplayTree(splay_tree,splay_tree->next);
840 splay_tree->next=(
void *) NULL;
841 node=splay_tree->root->right;
844 while (node->left != (
NodeInfo *) NULL)
846 splay_tree->next=node->key;
848 value=splay_tree->root->value;
849 UnlockSemaphoreInfo(splay_tree->semaphore);
877MagickExport
const void *GetRootValueFromSplayTree(
SplayTreeInfo *splay_tree)
883 assert(splay_tree->signature == MagickCoreSignature);
884 if (IsEventLogging() != MagickFalse)
885 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
886 value=(
const void *) NULL;
887 LockSemaphoreInfo(splay_tree->semaphore);
888 if (splay_tree->root != (
NodeInfo *) NULL)
889 value=splay_tree->root->value;
890 UnlockSemaphoreInfo(splay_tree->semaphore);
921MagickExport
const void *GetValueFromSplayTree(
SplayTreeInfo *splay_tree,
931 assert(splay_tree->signature == MagickCoreSignature);
932 if (IsEventLogging() != MagickFalse)
933 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
934 if (splay_tree->root == (
NodeInfo *) NULL)
935 return((
void *) NULL);
936 LockSemaphoreInfo(splay_tree->semaphore);
937 SplaySplayTree(splay_tree,key);
938 if (splay_tree->compare != (int (*)(
const void *,
const void *)) NULL)
939 compare=splay_tree->compare(splay_tree->root->key,key);
941 compare=(splay_tree->root->key > key) ? 1 :
942 ((splay_tree->root->key < key) ? -1 : 0);
945 UnlockSemaphoreInfo(splay_tree->semaphore);
946 return((
void *) NULL);
948 value=splay_tree->root->value;
949 UnlockSemaphoreInfo(splay_tree->semaphore);
976MagickExport
size_t GetNumberOfNodesInSplayTree(
980 assert(splay_tree->signature == MagickCoreSignature);
981 if (IsEventLogging() != MagickFalse)
982 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
983 return(splay_tree->nodes);
1014 int (*method)(
NodeInfo *,
const void *),
const void *value)
1045 if (splay_tree->root == (
NodeInfo *) NULL)
1047 nodes=(
NodeInfo **) AcquireQuantumMemory((
size_t) splay_tree->nodes,
1049 transitions=(
unsigned char *) AcquireQuantumMemory((
size_t) splay_tree->nodes,
1050 sizeof(*transitions));
1051 if ((nodes == (
NodeInfo **) NULL) || (transitions == (
unsigned char *) NULL))
1052 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1054 final_transition=MagickFalse;
1055 nodes[0]=splay_tree->root;
1056 transitions[0]=(
unsigned char) LeftTransition;
1057 for (i=0; final_transition == MagickFalse; )
1060 transition=(TransitionType) transitions[i];
1063 case LeftTransition:
1065 transitions[i]=(
unsigned char) DownTransition;
1066 if (node->left == (
NodeInfo *) NULL)
1069 nodes[i]=node->left;
1070 transitions[i]=(
unsigned char) LeftTransition;
1073 case RightTransition:
1075 transitions[i]=(
unsigned char) UpTransition;
1076 if (node->right == (
NodeInfo *) NULL)
1079 nodes[i]=node->right;
1080 transitions[i]=(
unsigned char) LeftTransition;
1083 case DownTransition:
1086 transitions[i]=(
unsigned char) RightTransition;
1087 status=(*method)(node,value);
1089 final_transition=MagickTrue;
1096 final_transition=MagickTrue;
1104 nodes=(
NodeInfo **) RelinquishMagickMemory(nodes);
1105 transitions=(
unsigned char *) RelinquishMagickMemory(transitions);
1142 int (*compare)(
const void *,
const void *),
void *(*relinquish_key)(
void *),
1143 void *(*relinquish_value)(
void *))
1148 splay_tree=(
SplayTreeInfo *) AcquireCriticalMemory(
sizeof(*splay_tree));
1149 (void) memset(splay_tree,0,
sizeof(*splay_tree));
1150 splay_tree->root=(
NodeInfo *) NULL;
1151 splay_tree->compare=compare;
1152 splay_tree->relinquish_key=relinquish_key;
1153 splay_tree->relinquish_value=relinquish_value;
1154 splay_tree->balance=MagickFalse;
1155 splay_tree->key=(
void *) NULL;
1156 splay_tree->next=(
void *) NULL;
1157 splay_tree->nodes=0;
1158 splay_tree->debug=IsEventLogging();
1159 splay_tree->semaphore=AcquireSemaphoreInfo();
1160 splay_tree->signature=MagickCoreSignature;
1190MagickExport
void *RemoveNodeByValueFromSplayTree(
SplayTreeInfo *splay_tree,
1201 assert(splay_tree->signature == MagickCoreSignature);
1202 if (IsEventLogging() != MagickFalse)
1203 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1205 if (splay_tree->root == (
NodeInfo *) NULL)
1207 LockSemaphoreInfo(splay_tree->semaphore);
1208 next=(
NodeInfo *) GetFirstSplayTreeNode(splay_tree);
1211 SplaySplayTree(splay_tree,next);
1213 node=splay_tree->root->right;
1216 while (node->left != (
NodeInfo *) NULL)
1220 if (splay_tree->root->value == value)
1232 key=splay_tree->root->key;
1233 SplaySplayTree(splay_tree,key);
1234 splay_tree->key=(
void *) NULL;
1235 if (splay_tree->compare != (int (*)(
const void *,
const void *)) NULL)
1236 compare=splay_tree->compare(splay_tree->root->key,key);
1238 compare=(splay_tree->root->key > key) ? 1 :
1239 ((splay_tree->root->key < key) ? -1 : 0);
1242 UnlockSemaphoreInfo(splay_tree->semaphore);
1245 left=splay_tree->root->left;
1246 right=splay_tree->root->right;
1247 if ((splay_tree->relinquish_value != (
void *(*)(
void *)) NULL) &&
1248 (splay_tree->root->value != (
void *) NULL))
1249 splay_tree->root->value=splay_tree->relinquish_value(
1250 splay_tree->root->value);
1251 splay_tree->root=(
NodeInfo *) RelinquishMagickMemory(splay_tree->root);
1252 splay_tree->nodes--;
1255 splay_tree->root=right;
1256 UnlockSemaphoreInfo(splay_tree->semaphore);
1259 splay_tree->root=left;
1262 while (left->right != (
NodeInfo *) NULL)
1266 UnlockSemaphoreInfo(splay_tree->semaphore);
1270 UnlockSemaphoreInfo(splay_tree->semaphore);
1299MagickExport
void *RemoveNodeFromSplayTree(
SplayTreeInfo *splay_tree,
1313 assert(splay_tree->signature == MagickCoreSignature);
1314 if (IsEventLogging() != MagickFalse)
1315 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1316 value=(
void *) NULL;
1317 if (splay_tree->root == (
NodeInfo *) NULL)
1319 LockSemaphoreInfo(splay_tree->semaphore);
1320 SplaySplayTree(splay_tree,key);
1321 splay_tree->key=(
void *) NULL;
1322 if (splay_tree->compare != (int (*)(
const void *,
const void *)) NULL)
1323 compare=splay_tree->compare(splay_tree->root->key,key);
1325 compare=(splay_tree->root->key > key) ? 1 :
1326 ((splay_tree->root->key < key) ? -1 : 0);
1329 UnlockSemaphoreInfo(splay_tree->semaphore);
1332 left=splay_tree->root->left;
1333 right=splay_tree->root->right;
1334 value=splay_tree->root->value;
1335 if ((splay_tree->relinquish_key != (
void *(*)(
void *)) NULL) &&
1336 (splay_tree->root->key != (
void *) NULL))
1337 splay_tree->root->key=splay_tree->relinquish_key(splay_tree->root->key);
1338 splay_tree->root=(
NodeInfo *) RelinquishMagickMemory(splay_tree->root);
1339 splay_tree->nodes--;
1342 splay_tree->root=right;
1343 UnlockSemaphoreInfo(splay_tree->semaphore);
1346 splay_tree->root=left;
1349 while (left->right != (
NodeInfo *) NULL)
1353 UnlockSemaphoreInfo(splay_tree->semaphore);
1390 assert(splay_tree->signature == MagickCoreSignature);
1391 if (IsEventLogging() != MagickFalse)
1392 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1393 LockSemaphoreInfo(splay_tree->semaphore);
1394 if (splay_tree->root != (
NodeInfo *) NULL)
1396 if ((splay_tree->relinquish_value != (
void *(*)(
void *)) NULL) &&
1397 (splay_tree->root->value != (
void *) NULL))
1398 splay_tree->root->value=splay_tree->relinquish_value(
1399 splay_tree->root->value);
1400 if ((splay_tree->relinquish_key != (
void *(*)(
void *)) NULL) &&
1401 (splay_tree->root->key != (
void *) NULL))
1402 splay_tree->root->key=splay_tree->relinquish_key(splay_tree->root->key);
1403 splay_tree->root->key=(
void *) NULL;
1404 for (pend=splay_tree->root; pend != (
NodeInfo *) NULL; )
1409 if (active->left != (
NodeInfo *) NULL)
1411 if ((splay_tree->relinquish_value != (
void *(*)(
void *)) NULL) &&
1412 (active->left->value != (
void *) NULL))
1413 active->left->value=splay_tree->relinquish_value(
1414 active->left->value);
1415 if ((splay_tree->relinquish_key != (
void *(*)(
void *)) NULL) &&
1416 (active->left->key != (
void *) NULL))
1417 active->left->key=splay_tree->relinquish_key(active->left->key);
1418 active->left->key=(
void *) pend;
1421 if (active->right != (
NodeInfo *) NULL)
1423 if ((splay_tree->relinquish_value != (
void *(*)(
void *)) NULL) &&
1424 (active->right->value != (
void *) NULL))
1425 active->right->value=splay_tree->relinquish_value(
1426 active->right->value);
1427 if ((splay_tree->relinquish_key != (
void *(*)(
void *)) NULL) &&
1428 (active->right->key != (
void *) NULL))
1429 active->right->key=splay_tree->relinquish_key(
1430 active->right->key);
1431 active->right->key=(
void *) pend;
1436 node=(
NodeInfo *) RelinquishMagickMemory(node);
1440 splay_tree->root=(
NodeInfo *) NULL;
1441 splay_tree->key=(
void *) NULL;
1442 splay_tree->next=(
void *) NULL;
1443 splay_tree->nodes=0;
1444 splay_tree->balance=MagickFalse;
1445 UnlockSemaphoreInfo(splay_tree->semaphore);
1472MagickExport
void ResetSplayTreeIterator(
SplayTreeInfo *splay_tree)
1475 assert(splay_tree->signature == MagickCoreSignature);
1476 if (IsEventLogging() != MagickFalse)
1477 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1478 LockSemaphoreInfo(splay_tree->semaphore);
1479 splay_tree->next=GetFirstSplayTreeNode(splay_tree);
1480 UnlockSemaphoreInfo(splay_tree->semaphore);
1536 if (splay_tree->compare != (int (*)(
const void *,
const void *)) NULL)
1537 compare=splay_tree->compare(n->key,key);
1539 compare=(n->key > key) ? 1 : ((n->key < key) ? -1 : 0);
1548 if (depth >= MaxSplayTreeDepth)
1550 splay_tree->balance=MagickTrue;
1553 n=Splay(splay_tree,depth+1,key,next,node,parent);
1554 if ((n != *node) || (splay_tree->balance != MagickFalse))
1559 if (grandparent == (
NodeInfo **) NULL)
1561 if (n == (*parent)->left)
1574 if ((n == (*parent)->left) && (*parent == (*grandparent)->left))
1577 (*grandparent)->left=p->right;
1578 p->right=(*grandparent);
1584 if ((n == (*parent)->right) && (*parent == (*grandparent)->right))
1587 (*grandparent)->right=p->left;
1588 p->left=(*grandparent);
1594 if (n == (*parent)->left)
1596 (*parent)->left=n->right;
1598 (*grandparent)->right=n->left;
1599 n->left=(*grandparent);
1603 (*parent)->right=n->left;
1605 (*grandparent)->left=n->right;
1606 n->right=(*grandparent);
1611static void SplaySplayTree(
SplayTreeInfo *splay_tree,
const void *key)
1613 if (splay_tree->root == (
NodeInfo *) NULL)
1615 if (splay_tree->key != (
void *) NULL)
1620 if (splay_tree->compare != (int (*)(
const void *,
const void *)) NULL)
1621 compare=splay_tree->compare(splay_tree->root->key,key);
1623 compare=(splay_tree->key > key) ? 1 :
1624 ((splay_tree->key < key) ? -1 : 0);
1628 (void) Splay(splay_tree,0UL,key,&splay_tree->root,(
NodeInfo **) NULL,
1630 if (splay_tree->balance != MagickFalse)
1632 BalanceSplayTree(splay_tree);
1633 (void) Splay(splay_tree,0UL,key,&splay_tree->root,(
NodeInfo **) NULL,
1635 if (splay_tree->balance != MagickFalse)
1636 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1638 splay_tree->key=(
void *) key;