Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

Skiplist.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/basic.h>
35 #include <ogdf/basic/memory.h>
36 
37 namespace ogdf {
38 
39 template<class X> class SkiplistIterator;
40 
42 
50 template<class X> class Skiplist {
51  friend class SkiplistIterator<X>;
52  friend class Element;
53 
55  class Element {
56  friend class Skiplist<X>;
57  friend class SkiplistIterator<X>;
58 
59  X entry; // content
60  Element** next; // successor elements
61 
62  // construction
63  Element(const X &item, int height) :
64  entry(item) {
65  next = (Element**)malloc(height*sizeof(Element*));
66  }
67 
69  free(next);
70  }
71 
73  };
74 
75 public:
76 
78  Skiplist() : m_lSize(0) {
79  srand((unsigned int)time(nullptr));
80  m_realheight = 5;
81  m_height = 1;
82  m_start = (Element**)malloc(m_realheight*sizeof(Element*));
83  m_start[0] = nullptr;
84  }
85 
87  clear();
88  free(m_start);
89  }
90 
92  bool isElement(X item) const {
93  int h = m_height - 1;
94  Element** cur = m_start; // wheeha!
95  while(true) {
96  if( cur[h] && *(cur[h]->entry) < *item ) //nxt != nullptr
97  cur = cur[h]->next;
98  else if(--h < 0)
99  return cur[0] && *(cur[0]->entry) == *item;
100  }
101  }
102 
104  void add(X item) {
105  m_lSize++;
106 
107  int nh = random_height();
108  Element* n = new Element(item, nh);
109  if(nh > m_height)
110  grow(nh);
111 
112  int h = m_height - 1;
113  Element** cur = m_start; // wheeha!
114  while(true) {
115  if( cur[h] && *(cur[h]->entry) < *item ) //nxt != nullptr
116  cur = cur[h]->next;
117  else {
118  if(h < nh) { // add only if new element is high enough
119  n->next[h] = cur[h];
120  cur[h] = n;
121  }
122  if(--h < 0)
123  return;
124  }
125  }
126  }
127 
129  int size() const { return m_lSize; }
130 
132  int empty() const { return m_lSize==0; }
133 
135 
139  void clear(bool killData = false) {
140  Element* item = m_start[0];
141  while(item) {
142  Element* old = item;
143  item = item->next[0];
144  if(killData)
145  delete old->entry;
146  delete old;
147  }
148  m_lSize = 0;
149  m_height = 1;
150  m_start[0] = nullptr;
151  }
152 
154  const SkiplistIterator<X> begin() const { return m_start[0]; }
155 
156 private:
157  int m_lSize;
159  int m_height;
161 
163  int h = 1;
164  while(rand() > RAND_MAX/2) h++;
165  return h;
166  }
167 
168  void grow(int newheight) {
169  if(newheight > m_realheight) {
170  m_realheight = newheight;
171  Element** newStart = static_cast<Element**>(realloc(m_start, m_realheight*sizeof(Element*)));
172  if (newStart == nullptr) {
173  free(m_start);
174  } else {
175  m_start = newStart;
176  }
177  }
178  for(int i = newheight; i-- > m_height;) {
179  m_start[i] = nullptr;
180  }
181  m_height = newheight;
182  }
183 
184 };
185 
187 template<class X> class SkiplistIterator {
188  friend class Skiplist<X>;
189 
190  const typename Skiplist<X>::Element *el;
191 
192  SkiplistIterator(const typename Skiplist<X>::Element *e) { el = e; }
193 
194 public:
195 
197  const X &operator*() const { return el->entry; }
198 
199  bool valid() const { return el != nullptr; }
200 
203  el = el->next[0];
204  return *this;
205  }
208  SkiplistIterator<X> it = *this;
209  el = el->next[0];
210  return it;
211  }
212 
215  el = it.el;
216  return *this;
217  }
218 };
219 
220 }
void add(X item)
Adds the item item into the skiplist [O&#39;(log n)].
Definition: Skiplist.h:104
Declaration of memory manager for allocating small pieces of memory.
Forward-Iterator for Skiplists.
Definition: Skiplist.h:39
int empty() const
Returns true if the skiplist contains no elements.
Definition: Skiplist.h:132
bool valid() const
Definition: Skiplist.h:199
SkiplistIterator< X > & operator++()
Move the iterator one item forward (prefix notation)
Definition: Skiplist.h:202
bool isElement(X item) const
Returns true if the item item is contained in the skiplist [O&#39;(log n)].
Definition: Skiplist.h:92
SkiplistIterator(const typename Skiplist< X >::Element *e)
Definition: Skiplist.h:192
int random_height()
Definition: Skiplist.h:162
Internal structure to hold the items and internal forward pointers of the skiplist.
Definition: Skiplist.h:55
const SkiplistIterator< X > begin() const
returns an (forward) iterator for the skiplist
Definition: Skiplist.h:154
int size() const
Returns the current size of the skiplist, i.e., the number of elements.
Definition: Skiplist.h:129
SkiplistIterator< X > operator++(int)
Move the iterator one item forward (prefix notation)
Definition: Skiplist.h:207
void clear(bool killData=false)
Clears the current skiplist.
Definition: Skiplist.h:139
Skiplist()
Construct an initially empty skiplist.
Definition: Skiplist.h:78
Element(const X &item, int height)
Definition: Skiplist.h:63
#define OGDF_NEW_DELETE
Makes the class use OGDF&#39;s memory allocator.
Definition: memory.h:84
const Skiplist< X >::Element * el
Definition: Skiplist.h:190
const X & operator*() const
Returns the item to which the iterator points.
Definition: Skiplist.h:197
void grow(int newheight)
Definition: Skiplist.h:168
Basic declarations, included by all source files.
static const int X
Definition: GmlParser.h:65
The namespace for all OGDF objects.
int m_realheight
Definition: Skiplist.h:160
A randomized skiplist.
Definition: Skiplist.h:50
Element ** m_start
Definition: Skiplist.h:158
SkiplistIterator< X > & operator=(const SkiplistIterator< X > &it)
Assignment operator.
Definition: Skiplist.h:214