1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/*
* $Revision: 2523 $
*
* last checkin:
* $Author: gutwenger $
* $Date: 2012-07-02 20:59:27 +0200 (Mon, 02 Jul 2012) $
***************************************************************/
/** \file
* \brief Declaration of class Skiplist.
*
* \author Markus Chimani
*
* \par License:
* This file is part of the Open Graph Drawing Framework (OGDF).
*
* \par
* Copyright (C)<br>
* See README.txt in the root directory of the OGDF installation for details.
*
* \par
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* Version 2 or 3 as published by the Free Software Foundation;
* see the file LICENSE.txt included in the packaging of this file
* for details.
*
* \par
* This program 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 General Public License for more details.
*
* \par
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* \see http://www.gnu.org/copyleft/gpl.html
***************************************************************/
#ifndef OGDF_SKIPLIST_H
#define OGDF_SKIPLIST_H
#include <ogdf/basic/basic.h>
namespace ogdf {
template<class X> class SkiplistIterator;
//! A randomized skiplist
/**
* The elements height is computed using the traditional coin-flip method, using a
* 50-50 chance to stop growing. The given running times of the methods below are therefore
* only expected running times.
*
* \warning The code expects the type \a X to be a pointer! If \a X is not a pointer,
* compiler errors will occur!
*/
template<class X> class Skiplist {
friend class SkiplistIterator<X>;
friend class Element;
//! Internal structure to hold the items and internal forward pointers of the skiplist
class Element {
friend class Skiplist<X>;
friend class SkiplistIterator<X>;
X entry; // content
Element** next; // successor elements
// construction
Element(const X &item, int height) :
entry(item) {
next = (Element**)malloc(height*sizeof(Element*));
}
~Element() {
free(next);
}
OGDF_NEW_DELETE
};
public:
//! Construct an initially empty skiplist
Skiplist() : lSize(0) {
srand((unsigned int)time(NULL));
realheight = 5;
height = 1;
start = (Element**)malloc(realheight*sizeof(Element*));
start[0] = NULL;
}
~Skiplist() {
clear();
free(start);
}
//! Returns true if the item \a item is contained in the skiplist [O'(log n)]
bool isElement(X item) const {
int h = height - 1;
Element** cur = start; // wheeha!
while(true) {
if( cur[h] && *(cur[h]->entry) < *item ) //nxt != NULL
cur = cur[h]->next;
else if(--h < 0)
return cur[0] && *(cur[0]->entry) == *item;
}
}
//! Adds the item \a item into the skiplist [O'(log n)]
void add(X item) {
lSize++;
int nh = random_height();
Element* n = OGDF_NEW Element(item, nh);
if(nh > height)
grow(nh);
int h = height - 1;
Element** cur = start; // wheeha!
while(true) {
if( cur[h] && *(cur[h]->entry) < *item ) //nxt != NULL
cur = cur[h]->next;
else {
if(h < nh) { // add only if new element is high enough
n->next[h] = cur[h];
cur[h] = n;
}
if(--h < 0)
return;
}
}
}
//! Returns the current size of the skiplist, i.e., the number of elements
int size() const { return lSize; }
//! Returns true if the skiplist contains no elements
int empty() const { return (lSize==0); }
//! Clears the current skiplist
/**
* If \a killData is true, the items of the Skiplist (which are stored as
* pointers) are automatically deleted.
*/
void clear(bool killData = false) {
Element* item = start[0];
Element* old;
while(item) {
old = item;
item = item->next[0];
if(killData)
delete old->entry;
delete old;
}
lSize = 0;
height = 1;
start[0] = 0;
}
//! returns an (forward) iterator for the skiplist
const SkiplistIterator<X> begin() const { return start[0]; }
private:
int lSize;
Element** start;
int height;
int realheight;
int random_height() {
int h = 1;
while(rand() > RAND_MAX/2) h++;
return h;
}
void grow(int newheight) {
if(newheight > realheight) {
realheight = newheight;
start = (Element**)realloc(start, realheight*sizeof(Element*));
}
for(int i = newheight; i-->height;) {
start[i] = NULL;
}
height = newheight;
}
};
//! Forward-Iterator for Skiplists
template<class X> class SkiplistIterator {
friend class Skiplist<X>;
const typename Skiplist<X>::Element *el;
SkiplistIterator(const typename Skiplist<X>::Element *e) { el = e; }
public:
//! Returns the item to which the iterator points
const X &operator*() const { return el->entry; }
bool valid() const { return (el != 0); }
//! Move the iterator one item forward (prefix notation)
SkiplistIterator<X> &operator++() {
el = el->next[0];
return *this;
}
//! Move the iterator one item forward (prefix notation)
SkiplistIterator<X> operator++(int) {
SkiplistIterator<X> it = *this;
el = el->next[0];
return it;
}
//! Assignment operator
SkiplistIterator<X> &operator=(const SkiplistIterator<X> &it) {
el = it.el;
return *this;
}
};
}
#endif /*OGDF_SKIPLIST_H*/
|