// This may look like C code, but it is really -*- C++ -*-
Copyright (C) 1988 Free Software Foundation
written by Doug Lea (dl@rocky.oswego.edu)
This file is part of the GNU C++ Library. This library is free
software; you can redistribute it and/or modify it under the terms of
the GNU Library General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version. This library is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
#include "<T>.<C>.SplayMap.h"
struct to simulate the special `null' node in the Sleater & Tarjan JACM 1985
All routines use a version of their `simple top-down' splay alg. (p 669)
<T><C>SplayNode* <T><C>SplayMap::leftmost()
<T><C>SplayNode* t = root;
if (t != 0) while (t->lt != 0) t = t->lt;
<T><C>SplayNode* <T><C>SplayMap::rightmost()
<T><C>SplayNode* t = root;
if (t != 0) while (t->rt != 0) t = t->rt;
<T><C>SplayNode* <T><C>SplayMap::succ(<T><C>SplayNode* t)
while (t->lt != 0) t = t->lt;
if (t->par == 0 || t == t->par->lt)
<T><C>SplayNode* <T><C>SplayMap::pred(<T><C>SplayNode* t)
while (t->rt != 0) t = t->rt;
if (t->par == 0 || t == t->par->rt)
Pix <T><C>SplayMap::seek(<T&> key)
<T><C>SplayNode* t = root;
int comp = <T>CMP(key, t->item);
<T><C>SplayNode* dummy = (<T><C>SplayNode*)(&_dummy_null);
<T><C>SplayNode* l = dummy;
<T><C>SplayNode* r = dummy;
dummy->rt = dummy->lt = dummy->par = 0;
<T><C>SplayNode* tr = t->rt;
comp = <T>CMP(key, tr->item);
if (comp <= 0 || tr->rt == 0)
if ((t->rt = tr->lt) != 0) t->rt->par = t;
comp = <T>CMP(key, t->item);
<T><C>SplayNode* tl = t->lt;
comp = <T>CMP(key, tl->item);
if (comp >= 0 || tl->lt == 0)
if ((t->lt = tl->rt) != 0) t->lt->par = t;
comp = <T>CMP(key, t->item);
if ((r->lt = t->rt) != 0) r->lt->par = r;
if ((l->rt = t->lt) != 0) l->rt->par = l;
if ((t->lt = dummy->rt) != 0) t->lt->par = t;
if ((t->rt = dummy->lt) != 0) t->rt->par = t;
return (comp == 0) ? Pix(t) : 0;
<C>& <T><C>SplayMap::operator [] (<T&> item)
<T><C>SplayNode* t = root;
root = new <T><C>SplayNode(item, def);
int comp = <T>CMP(item, t->item);
<T><C>SplayNode* dummy = (<T><C>SplayNode*)(&_dummy_null);
<T><C>SplayNode* l = dummy;
<T><C>SplayNode* r = dummy;
dummy->rt = dummy->lt = dummy->par = 0;
<T><C>SplayNode* tr = t->rt;
tr = new <T><C>SplayNode(item, def);
comp = <T>CMP(item, tr->item);
<T><C>SplayNode* trr = tr->rt;
trr = new <T><C>SplayNode(item, def);
comp = <T>CMP(item, trr->item);
if ((t->rt = tr->lt) != 0) t->rt->par = t;
<T><C>SplayNode* tl = t->lt;
tl = new <T><C>SplayNode(item, def);
comp = <T>CMP(item, tl->item);
<T><C>SplayNode* tll = tl->lt;
tll = new <T><C>SplayNode(item, def);
comp = <T>CMP(item, tll->item);
if ((t->lt = tl->rt) != 0) t->lt->par = t;
if ((r->lt = t->rt) != 0) r->lt->par = r;
if ((l->rt = t->lt) != 0) l->rt->par = l;
if ((t->lt = dummy->rt) != 0) t->lt->par = t;
if ((t->rt = dummy->lt) != 0) t->rt->par = t;
void <T><C>SplayMap::del(<T&> key)
<T><C>SplayNode* t = (<T><C>SplayNode*)(seek(key));
<T><C>SplayNode* p = t->par;
if ((root = t->lt) != 0) root->par = 0;
if ((p->lt = t->lt) != 0) p->lt->par = p;
if ((p->rt = t->lt) != 0) p->rt->par = p;
<T><C>SplayNode* r = t->rt;
<T><C>SplayNode* l = r->lt;
if ((r->lt = t->lt) != 0) r->lt->par = r;
if ((r->lt = l->rt) != 0) r->lt->par = r;
void <T><C>SplayMap::_kill(<T><C>SplayNode* t)
<T><C>SplayNode* <T><C>SplayMap::_copy(<T><C>SplayNode* t)
<T><C>SplayNode* l = _copy(t->lt);
<T><C>SplayNode* r = _copy(t->rt);
<T><C>SplayNode* x = new <T><C>SplayNode(t->item, t->cont, l, r);
<T><C>SplayNode* trail = leftmost();
<T><C>SplayNode* t = succ(trail);
v &= <T>CMP(trail->item, t->item) < 0;
if (!v) error("invariant failure");