| top | | | ^ | | | | section top | | 
pSplay c = X->gt;
Y->lt = c;
X->gt = Y;
T = X;
| ^ | | | | | | section top | | | ^ | | | | | | section top | | 
pSplay c = X->lt;
Y->gt = c;
X->lt = Y;
T = X;
| ^ | | | | | | section top | | | ^ | | | | | | section top | | 
pSplay b = Y->lt;
pSplay c = X->lt;
X->lt = Y;
Y->lt = Z;
Y->gt = c;
Z->gt = b;
T = X;
| ^ | | | | | | section top | | 
pSplay b = X->lt;
pSplay c = X->gt;
X->lt = Z;
X->gt = Y;
Z->gt = b;
Y->lt = c;
T = X;
| ^ | | | | | | section top | | 
pSplay b = X->lt;
pSplay c = X->gt;
X->lt = Y;
X->gt = Z;
Y->gt = b;
Z->lt = c;
T = X;
| ^ | | | | | | section top | | 
pSplay c = Y->gt;
pSplay b = X->gt;
X->gt = Y;
Y->gt = Z;
Y->lt = b;
Z->lt = c;
T = X;
| ^ | | | | | | section top | | - proc Seek—Find a key in a tree; returns the new top.
| - io node—The top node, the found node (or somewhere near it) is brought to the top.
| - input key—The sought key if not NULL.
| - input int pos—Priority or key.
| - output exact—If the key matches exactly.
|
|
|
| ^ | | | | | | section top | |
typedef struct Stack Stack,*pStack;
struct Stack {
pSplay elem;
int cc;
pStack under;
};
pStack t = 0,w;
| ^ | | | | | | section top | |
while (node) {
w = heap(Stack); w->elem = node; w->under = t; t = w;
t->cc = keycmp(key,pos,node);
node = t->cc>0 ? node->gt : t->cc<0 ? node->lt : 0;
}
| ^ | | | | | | section top | | | ^ | | | | | | section top | |
if (exact) *exact = t->cc==0;
node = t->elem; retreat = t->cc<0; dispose(t);
| ^ | | | | | | section top | | | ^ | | | | | | section top | |
static pSplay seek(pSplay node,Obj key,int pos,bool *exact);
| ^ | | | | | | section top | |
static bool advance(Obj mapping) {
if (top(mapping)->gt) {
pSplay T = top(mapping)->gt,X,Y;
<Raise the least node of a subtree>
top(mapping)->gt = T;
T = top(mapping);
Y = T;
X = Y->gt;
{<Raise the greater node of a subtree>}
top(mapping) = T;
return true;
}else {
return false;
}
}
static Obj firstSplayScan(Intr intr,Obj mapping) {
pSplay T = top(mapping);
if (T) {
<Raise the least node of a subtree>
top(mapping) = T;
while (top(mapping)->priorityQueue) {
if (!advance(mapping)) {
rprintf(intr,"empty mapping");
return 0;
}
}
return incr(top(mapping)->key);
}else {
rprintf(intr,"empty mapping");
return 0;
}
}
static Obj lastSplayScan(Intr intr,Obj mapping) {
pSplay T = top(mapping);
if (T) {
<Raise the greatest node of a subtree>
top(mapping) = T;
return incr(T->key);
}else
rprintf(intr,"empty mapping");
return 0;
}
static Obj nextSplayScan(Intr intr,Obj mapping,Obj key) {
if (!top(mapping)) {
rprintf(intr,"empty mapping");
return 0;
}else {
top(mapping) = seek(top(mapping),key,mapkey,0);
if (strcmp(Tcl_GetString(key),Tcl_GetString(top(mapping)->key))<0) {
return incr(top(mapping)->key);
}else if (!top(mapping)->gt) {
rprintf(intr,"no next key");
return 0;
}else {
pSplay T = top(mapping)->gt,X,Y;
<Raise the least node of a subtree>
top(mapping)->gt = T;
T = top(mapping);
Y = T;
X = Y->gt;
{<Raise the greater node of a subtree>}
top(mapping) = T;
return incr(T->key);
}
}
}
static Obj previousSplayScan(Intr intr,Obj mapping,Obj key) {
if (!top(mapping)) {
rprintf(intr,"empty mapping");
return 0;
}else {
top(mapping) = seek(top(mapping),key,mapkey,0);
if (strcmp(Tcl_GetString(key),Tcl_GetString(top(mapping)->key))>0) {
return incr(top(mapping)->key);
}else if (!top(mapping)->lt) {
rprintf(intr,"no previous key");
return 0;
}else {
pSplay T = top(mapping)->lt,X,Y;
<Raise the greatest node of a subtree>
top(mapping)->lt = T;
T = top(mapping);
Y = T;
X = Y->lt;
{<Raise the lesser node of a subtree>}
top(mapping) = T;
if (top(mapping)->priorityQueue) {
rprintf(intr,"no previous key");
}else {
return incr(T->key);
}
}
}
}
static ptr beginScan(Intr intr,Obj mapping,Obj firstHint,bool *sorted) {
bool *more = heap(bool); *more = top(mapping)!=0;
if (*more) {
pSplay T = top(mapping);
if (firstHint) {
T = seek(T,firstHint,mapkey,0);
}else {
<Raise the least node of a subtree>
}
top(mapping) = T;
while (top(mapping)->priorityQueue) {
if (!advance(mapping)) {
*more = false;
break;
}
}
}
*sorted = true;
return more;
}
static int continueScan(Intr intr,Obj mapping,ptr cookie,Obj *key,Obj *data,int *flags) {
bool *more = cookie;
if (!top(mapping) || !*more) {
return TCL_BREAK;
}else {
top(mapping);
*key = incr(top(mapping)->key);
*data = incr(top(mapping)->data);
*flags = top(mapping)->flags;
*more = advance(mapping);
return TCL_OK;
}
}
static int endScan(Intr intr,Obj mapping,ptr cookie) {
dispose(cookie); return TCL_OK;
}
| ^ | | | | | | section top | |
static Obj firstSplayScan(Intr intr,Obj mapping);
static Obj lastSplayScan(Intr intr,Obj mapping);
static Obj nextSplayScan(Intr intr,Obj mapping,Obj key);
static Obj previousSplayScan(Intr intr,Obj mapping,Obj key);
static ptr beginScan(Intr intr,Obj mapping,Obj firstHint,bool *sorted);
static int continueScan(Intr intr,Obj mapping,ptr cookie,Obj *key,Obj *data,int *flags);
static int endScan(Intr intr,Obj mapping,ptr cookie);
| ^ | | | | | | section top | | 
static pSplay addSplay(pSplay tree,Obj key,int pos,Obj data,int flags) {
bool exact;
flags &= ~(wyrm_assocFlagCompressKey|wyrm_assocFlagCompressData);
tree = seek(tree,key,pos,&exact);
if (exact) {
if (flags>=0) tree->flags = flags;
if (data) {
incr(data);
decr(tree->data); tree->data = data;
}
return tree;
}else {
pSplay X = tree;
pSplay S = heap(Splay); zero(1,Splay,S);
S->key = incr(key?key:Tcl_NewObj());
S->data = incr(data?data:Tcl_NewObj());
S->priorityQueue = pos!=mapkey;
S->flags = flags<0 ? 0 : flags;
if (X) {
if (keycmp(key,pos,X)<0) {
S->lt = X->lt; S->gt = X; X->lt = 0;
} else {
S->gt = X->gt; S->lt = X; X->gt = 0;
}
}
return S;
}
}
| ^ | | | | | | section top | |
static int getSplay(Intr intr,Obj mapping,Obj seeking,Obj *key,Obj *data,int *flags) {
if (top(mapping)) {
pSplay T = seek(top(mapping),seeking,mapkey,0);
if (key) *key = incr(T->key);
if (data) *data = incr(T->data);
if (flags) *flags = T->flags;
top(mapping) = T;
return TCL_OK;
}else {
if (key) *key = 0;
if (data) *data = 0;
if (flags) *flags = -1;
rprintf(intr,"empty mapping");
return TCL_ERROR;
}
}
static int putSplay(Intr intr,Obj mapping,Obj key,Obj data,int flags) {
top(mapping) = addSplay(top(mapping),key,mapkey,data,flags);
Tcl_InvalidateStringRep(mapping);
return TCL_OK;
}
| ^ | Definition continued at: 30. | | | | | section top | | 
static int deleteSplay(Intr intr,Obj mapping,Obj key) {
if (top(mapping)) {
bool exact; pSplay X = seek(top(mapping),key,mapkey,&exact);
if (!exact) {
rprintf(intr,"missing: %{y}s",key);
return TCL_ERROR;
}
if (X->lt==0) {
top(mapping) = X->gt;
}else if (X->gt==0) {
top(mapping) = X->lt;
}else {
pSplay T = X->gt;
<Raise the least node of a subtree>
T->lt = X->lt; top(mapping) = T;
}
Tcl_InvalidateStringRep(mapping);
deletenode(X);
return TCL_OK;
}else {
rprintf(intr,"empty mapping");
return 0;
}
}
| ^ | | | | | | section top | |
static int getSplay(Intr intr,Obj mapping,Obj seeking,Obj *key,Obj *data,int *flags);
static pSplay addSplay(pSplay tree,Obj key,int pos,Obj data,int flags);
static int putSplay(Intr intr,Obj mapping,Obj key,Obj data,int flags);
static int deleteSplay(Intr intr,Obj mapping,Obj key);
| ^ | | |
| |