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);
689 LockSemaphoreInfo(splay_tree->semaphore);
690 if (splay_tree->root != (
NodeInfo *) NULL)
692 if ((splay_tree->relinquish_value != (
void *(*)(
void *)) NULL) &&
693 (splay_tree->root->value != (
void *) NULL))
694 splay_tree->root->value=splay_tree->relinquish_value(
695 splay_tree->root->value);
696 if ((splay_tree->relinquish_key != (
void *(*)(
void *)) NULL) &&
697 (splay_tree->root->key != (
void *) NULL))
698 splay_tree->root->key=splay_tree->relinquish_key(splay_tree->root->key);
699 splay_tree->root->key=(
void *) NULL;
700 for (pend=splay_tree->root; pend != (
NodeInfo *) NULL; )
705 if (active->left != (
NodeInfo *) NULL)
707 if ((splay_tree->relinquish_value != (
void *(*)(
void *)) NULL) &&
708 (active->left->value != (
void *) NULL))
709 active->left->value=splay_tree->relinquish_value(
710 active->left->value);
711 if ((splay_tree->relinquish_key != (
void *(*)(
void *)) NULL) &&
712 (active->left->key != (
void *) NULL))
713 active->left->key=splay_tree->relinquish_key(active->left->key);
714 active->left->key=(
void *) pend;
717 if (active->right != (
NodeInfo *) NULL)
719 if ((splay_tree->relinquish_value != (
void *(*)(
void *)) NULL) &&
720 (active->right->value != (
void *) NULL))
721 active->right->value=splay_tree->relinquish_value(
722 active->right->value);
723 if ((splay_tree->relinquish_key != (
void *(*)(
void *)) NULL) &&
724 (active->right->key != (
void *) NULL))
725 active->right->key=splay_tree->relinquish_key(
727 active->right->key=(
void *) pend;
732 node=(
NodeInfo *) RelinquishMagickMemory(node);
736 splay_tree->signature=(~MagickCoreSignature);
737 UnlockSemaphoreInfo(splay_tree->semaphore);
738 RelinquishSemaphoreInfo(&splay_tree->semaphore);
739 splay_tree=(
SplayTreeInfo *) RelinquishMagickMemory(splay_tree);
767MagickExport
const void *GetNextKeyInSplayTree(
SplayTreeInfo *splay_tree)
776 assert(splay_tree->signature == MagickCoreSignature);
777 if (IsEventLogging() != MagickFalse)
778 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
779 if ((splay_tree->root == (
NodeInfo *) NULL) ||
780 (splay_tree->next == (
void *) NULL))
781 return((
void *) NULL);
782 LockSemaphoreInfo(splay_tree->semaphore);
783 SplaySplayTree(splay_tree,splay_tree->next);
784 splay_tree->next=(
void *) NULL;
785 node=splay_tree->root->right;
788 while (node->left != (
NodeInfo *) NULL)
790 splay_tree->next=node->key;
792 key=splay_tree->root->key;
793 UnlockSemaphoreInfo(splay_tree->semaphore);
821MagickExport
const void *GetNextValueInSplayTree(
SplayTreeInfo *splay_tree)
830 assert(splay_tree->signature == MagickCoreSignature);
831 if (IsEventLogging() != MagickFalse)
832 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
833 if ((splay_tree->root == (
NodeInfo *) NULL) ||
834 (splay_tree->next == (
void *) NULL))
835 return((
void *) NULL);
836 LockSemaphoreInfo(splay_tree->semaphore);
837 SplaySplayTree(splay_tree,splay_tree->next);
838 splay_tree->next=(
void *) NULL;
839 node=splay_tree->root->right;
842 while (node->left != (
NodeInfo *) NULL)
844 splay_tree->next=node->key;
846 value=splay_tree->root->value;
847 UnlockSemaphoreInfo(splay_tree->semaphore);
875MagickExport
const void *GetRootValueFromSplayTree(
SplayTreeInfo *splay_tree)
881 assert(splay_tree->signature == MagickCoreSignature);
882 if (IsEventLogging() != MagickFalse)
883 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
884 value=(
const void *) NULL;
885 LockSemaphoreInfo(splay_tree->semaphore);
886 if (splay_tree->root != (
NodeInfo *) NULL)
887 value=splay_tree->root->value;
888 UnlockSemaphoreInfo(splay_tree->semaphore);
919MagickExport
const void *GetValueFromSplayTree(
SplayTreeInfo *splay_tree,
929 assert(splay_tree->signature == MagickCoreSignature);
930 if (IsEventLogging() != MagickFalse)
931 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
932 if (splay_tree->root == (
NodeInfo *) NULL)
933 return((
void *) NULL);
934 LockSemaphoreInfo(splay_tree->semaphore);
935 SplaySplayTree(splay_tree,key);
936 if (splay_tree->compare != (int (*)(
const void *,
const void *)) NULL)
937 compare=splay_tree->compare(splay_tree->root->key,key);
939 compare=(splay_tree->root->key > key) ? 1 :
940 ((splay_tree->root->key < key) ? -1 : 0);
943 UnlockSemaphoreInfo(splay_tree->semaphore);
944 return((
void *) NULL);
946 value=splay_tree->root->value;
947 UnlockSemaphoreInfo(splay_tree->semaphore);
974MagickExport
size_t GetNumberOfNodesInSplayTree(
978 assert(splay_tree->signature == MagickCoreSignature);
979 if (IsEventLogging() != MagickFalse)
980 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
981 return(splay_tree->nodes);
1012 int (*method)(
NodeInfo *,
const void *),
const void *value)
1041 if (splay_tree->root == (
NodeInfo *) NULL)
1043 nodes=(
NodeInfo **) AcquireQuantumMemory((
size_t) splay_tree->nodes,
1045 transitions=(
unsigned char *) AcquireQuantumMemory((
size_t) splay_tree->nodes,
1046 sizeof(*transitions));
1047 if ((nodes == (
NodeInfo **) NULL) || (transitions == (
unsigned char *) NULL))
1048 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1050 final_transition=MagickFalse;
1051 nodes[0]=splay_tree->root;
1052 transitions[0]=(
unsigned char) LeftTransition;
1053 for (i=0; final_transition == MagickFalse; )
1056 transition=(TransitionType) transitions[i];
1059 case LeftTransition:
1061 transitions[i]=(
unsigned char) DownTransition;
1062 if (node->left == (
NodeInfo *) NULL)
1065 nodes[i]=node->left;
1066 transitions[i]=(
unsigned char) LeftTransition;
1069 case RightTransition:
1071 transitions[i]=(
unsigned char) UpTransition;
1072 if (node->right == (
NodeInfo *) NULL)
1075 nodes[i]=node->right;
1076 transitions[i]=(
unsigned char) LeftTransition;
1079 case DownTransition:
1082 transitions[i]=(
unsigned char) RightTransition;
1083 status=(*method)(node,value);
1085 final_transition=MagickTrue;
1092 final_transition=MagickTrue;
1100 nodes=(
NodeInfo **) RelinquishMagickMemory(nodes);
1101 transitions=(
unsigned char *) RelinquishMagickMemory(transitions);
1138 int (*compare)(
const void *,
const void *),
void *(*relinquish_key)(
void *),
1139 void *(*relinquish_value)(
void *))
1144 splay_tree=(
SplayTreeInfo *) AcquireCriticalMemory(
sizeof(*splay_tree));
1145 (void) memset(splay_tree,0,
sizeof(*splay_tree));
1146 splay_tree->root=(
NodeInfo *) NULL;
1147 splay_tree->compare=compare;
1148 splay_tree->relinquish_key=relinquish_key;
1149 splay_tree->relinquish_value=relinquish_value;
1150 splay_tree->balance=MagickFalse;
1151 splay_tree->key=(
void *) NULL;
1152 splay_tree->next=(
void *) NULL;
1153 splay_tree->nodes=0;
1154 splay_tree->debug=IsEventLogging();
1155 splay_tree->semaphore=AcquireSemaphoreInfo();
1156 splay_tree->signature=MagickCoreSignature;
1186MagickExport
void *RemoveNodeByValueFromSplayTree(
SplayTreeInfo *splay_tree,
1197 assert(splay_tree->signature == MagickCoreSignature);
1198 if (IsEventLogging() != MagickFalse)
1199 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1201 if (splay_tree->root == (
NodeInfo *) NULL)
1203 LockSemaphoreInfo(splay_tree->semaphore);
1204 next=(
NodeInfo *) GetFirstSplayTreeNode(splay_tree);
1207 SplaySplayTree(splay_tree,next);
1209 node=splay_tree->root->right;
1212 while (node->left != (
NodeInfo *) NULL)
1216 if (splay_tree->root->value == value)
1228 key=splay_tree->root->key;
1229 SplaySplayTree(splay_tree,key);
1230 splay_tree->key=(
void *) NULL;
1231 if (splay_tree->compare != (int (*)(
const void *,
const void *)) NULL)
1232 compare=splay_tree->compare(splay_tree->root->key,key);
1234 compare=(splay_tree->root->key > key) ? 1 :
1235 ((splay_tree->root->key < key) ? -1 : 0);
1238 UnlockSemaphoreInfo(splay_tree->semaphore);
1241 left=splay_tree->root->left;
1242 right=splay_tree->root->right;
1243 if ((splay_tree->relinquish_value != (
void *(*)(
void *)) NULL) &&
1244 (splay_tree->root->value != (
void *) NULL))
1245 splay_tree->root->value=splay_tree->relinquish_value(
1246 splay_tree->root->value);
1247 splay_tree->root=(
NodeInfo *) RelinquishMagickMemory(splay_tree->root);
1248 splay_tree->nodes--;
1251 splay_tree->root=right;
1252 UnlockSemaphoreInfo(splay_tree->semaphore);
1255 splay_tree->root=left;
1258 while (left->right != (
NodeInfo *) NULL)
1262 UnlockSemaphoreInfo(splay_tree->semaphore);
1266 UnlockSemaphoreInfo(splay_tree->semaphore);
1295MagickExport
void *RemoveNodeFromSplayTree(
SplayTreeInfo *splay_tree,
1309 assert(splay_tree->signature == MagickCoreSignature);
1310 if (IsEventLogging() != MagickFalse)
1311 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1312 value=(
void *) NULL;
1313 if (splay_tree->root == (
NodeInfo *) NULL)
1315 LockSemaphoreInfo(splay_tree->semaphore);
1316 SplaySplayTree(splay_tree,key);
1317 splay_tree->key=(
void *) NULL;
1318 if (splay_tree->compare != (int (*)(
const void *,
const void *)) NULL)
1319 compare=splay_tree->compare(splay_tree->root->key,key);
1321 compare=(splay_tree->root->key > key) ? 1 :
1322 ((splay_tree->root->key < key) ? -1 : 0);
1325 UnlockSemaphoreInfo(splay_tree->semaphore);
1328 left=splay_tree->root->left;
1329 right=splay_tree->root->right;
1330 value=splay_tree->root->value;
1331 if ((splay_tree->relinquish_key != (
void *(*)(
void *)) NULL) &&
1332 (splay_tree->root->key != (
void *) NULL))
1333 splay_tree->root->key=splay_tree->relinquish_key(splay_tree->root->key);
1334 splay_tree->root=(
NodeInfo *) RelinquishMagickMemory(splay_tree->root);
1335 splay_tree->nodes--;
1338 splay_tree->root=right;
1339 UnlockSemaphoreInfo(splay_tree->semaphore);
1342 splay_tree->root=left;
1345 while (left->right != (
NodeInfo *) NULL)
1349 UnlockSemaphoreInfo(splay_tree->semaphore);
1384 assert(splay_tree->signature == MagickCoreSignature);
1385 if (IsEventLogging() != MagickFalse)
1386 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1387 LockSemaphoreInfo(splay_tree->semaphore);
1388 if (splay_tree->root != (
NodeInfo *) NULL)
1390 if ((splay_tree->relinquish_value != (
void *(*)(
void *)) NULL) &&
1391 (splay_tree->root->value != (
void *) NULL))
1392 splay_tree->root->value=splay_tree->relinquish_value(
1393 splay_tree->root->value);
1394 if ((splay_tree->relinquish_key != (
void *(*)(
void *)) NULL) &&
1395 (splay_tree->root->key != (
void *) NULL))
1396 splay_tree->root->key=splay_tree->relinquish_key(splay_tree->root->key);
1397 splay_tree->root->key=(
void *) NULL;
1398 for (pend=splay_tree->root; pend != (
NodeInfo *) NULL; )
1403 if (active->left != (
NodeInfo *) NULL)
1405 if ((splay_tree->relinquish_value != (
void *(*)(
void *)) NULL) &&
1406 (active->left->value != (
void *) NULL))
1407 active->left->value=splay_tree->relinquish_value(
1408 active->left->value);
1409 if ((splay_tree->relinquish_key != (
void *(*)(
void *)) NULL) &&
1410 (active->left->key != (
void *) NULL))
1411 active->left->key=splay_tree->relinquish_key(active->left->key);
1412 active->left->key=(
void *) pend;
1415 if (active->right != (
NodeInfo *) NULL)
1417 if ((splay_tree->relinquish_value != (
void *(*)(
void *)) NULL) &&
1418 (active->right->value != (
void *) NULL))
1419 active->right->value=splay_tree->relinquish_value(
1420 active->right->value);
1421 if ((splay_tree->relinquish_key != (
void *(*)(
void *)) NULL) &&
1422 (active->right->key != (
void *) NULL))
1423 active->right->key=splay_tree->relinquish_key(
1424 active->right->key);
1425 active->right->key=(
void *) pend;
1430 node=(
NodeInfo *) RelinquishMagickMemory(node);
1434 splay_tree->root=(
NodeInfo *) NULL;
1435 splay_tree->key=(
void *) NULL;
1436 splay_tree->next=(
void *) NULL;
1437 splay_tree->nodes=0;
1438 splay_tree->balance=MagickFalse;
1439 UnlockSemaphoreInfo(splay_tree->semaphore);
1466MagickExport
void ResetSplayTreeIterator(
SplayTreeInfo *splay_tree)
1469 assert(splay_tree->signature == MagickCoreSignature);
1470 if (IsEventLogging() != MagickFalse)
1471 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
1472 LockSemaphoreInfo(splay_tree->semaphore);
1473 splay_tree->next=GetFirstSplayTreeNode(splay_tree);
1474 UnlockSemaphoreInfo(splay_tree->semaphore);
1528 if (splay_tree->compare != (int (*)(
const void *,
const void *)) NULL)
1529 compare=splay_tree->compare(n->key,key);
1531 compare=(n->key > key) ? 1 : ((n->key < key) ? -1 : 0);
1540 if (depth >= MaxSplayTreeDepth)
1542 splay_tree->balance=MagickTrue;
1545 n=Splay(splay_tree,depth+1,key,next,node,parent);
1546 if ((n != *node) || (splay_tree->balance != MagickFalse))
1551 if (grandparent == (
NodeInfo **) NULL)
1553 if (n == (*parent)->left)
1566 if ((n == (*parent)->left) && (*parent == (*grandparent)->left))
1569 (*grandparent)->left=p->right;
1570 p->right=(*grandparent);
1576 if ((n == (*parent)->right) && (*parent == (*grandparent)->right))
1579 (*grandparent)->right=p->left;
1580 p->left=(*grandparent);
1586 if (n == (*parent)->left)
1588 (*parent)->left=n->right;
1590 (*grandparent)->right=n->left;
1591 n->left=(*grandparent);
1595 (*parent)->right=n->left;
1597 (*grandparent)->left=n->right;
1598 n->right=(*grandparent);
1603static void SplaySplayTree(
SplayTreeInfo *splay_tree,
const void *key)
1605 if (splay_tree->root == (
NodeInfo *) NULL)
1607 if (splay_tree->key != (
void *) NULL)
1612 if (splay_tree->compare != (int (*)(
const void *,
const void *)) NULL)
1613 compare=splay_tree->compare(splay_tree->root->key,key);
1615 compare=(splay_tree->key > key) ? 1 :
1616 ((splay_tree->key < key) ? -1 : 0);
1620 (void) Splay(splay_tree,0UL,key,&splay_tree->root,(
NodeInfo **) NULL,
1622 if (splay_tree->balance != MagickFalse)
1624 BalanceSplayTree(splay_tree);
1625 (void) Splay(splay_tree,0UL,key,&splay_tree->root,(
NodeInfo **) NULL,
1627 if (splay_tree->balance != MagickFalse)
1628 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1630 splay_tree->key=(
void *) key;