// This may look like C code, but it is really -*- C++ -*- /* Copyright (C) 1991 Free Software Foundation 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. */ /* * Bags implemented via William Pugh SkipList algorithms. * CACM, June 1990, p 668-676. * */ #ifndef _SkipBag_h #ifdef __GNUG__ #pragma interface #endif #define _SkipBag_h 1 #include ".Bag.h" #include #include class SkipBag; class RealSkipBagNode; class SkipBagNode { friend class SkipBag; private: RealSkipBagNode * * forward; SkipBagNode(int size); }; class RealSkipBagNode : public SkipBagNode { friend class SkipBag; private: item; RealSkipBagNode( h, int size); }; typedef RealSkipBagNode* SkipBagNodePtr; inline SkipBagNode::SkipBagNode(int size) : forward(new SkipBagNodePtr[size+1]) { } inline RealSkipBagNode::RealSkipBagNode( h, int size) : item(h), SkipBagNode(size) { } class SkipBag : public Bag { friend class SkipBaginit; protected: SkipBagNode* header; int level; int max_levels; int randoms_left; long random_bits; static MLCG* gen; int random_level(void); SkipBagNodePtr leftmost(void); SkipBagNodePtr rightmost(void); SkipBagNodePtr pred(SkipBagNodePtr t); SkipBagNodePtr succ(SkipBagNodePtr t); void _kill(void); private: enum { BITS_IN_RANDOM = LONGBITS-1 }; public: SkipBag(long size=DEFAULT_INITIAL_CAPACITY); SkipBag(SkipBag& a); ~SkipBag(void); Pix add( i); void del( i); void remove(i); int nof( i); int contains( i); void clear(void); Pix first(void); void next(Pix& i); & operator () (Pix i); Pix seek( i, Pix from = 0); Pix last(void); void prev(Pix& i); int OK(void); }; inline SkipBagNodePtr SkipBag::leftmost(void) { return header->forward[0]; } inline SkipBagNodePtr SkipBag::succ(SkipBagNodePtr t) { SkipBagNodePtr result = 0; if (t->forward[0]!=header) result = t->forward[0]; return result; } inline Pix SkipBag::first(void) { return Pix(leftmost()); } inline Pix SkipBag::last(void) { return Pix(rightmost()); } inline void SkipBag::next(Pix& i) { if (i != 0) i = Pix(succ((SkipBagNodePtr)i)); } inline & SkipBag::operator () (Pix i) { if (i == 0) error("null Pix"); return ((SkipBagNodePtr)i)->item; } inline void SkipBag::prev(Pix& i) { if (i != 0) i = Pix(pred((SkipBagNodePtr)i)); } inline int SkipBag::contains( key) { return seek(key) != 0; } inline SkipBag::~SkipBag() { _kill(); delete header; } static class SkipBaginit { public: SkipBaginit(); ~SkipBaginit(); private: static int count; } skipBaginit; #endif