|
int | level |
| Maximum level of the skiplist.
|
|
std::shared_ptr< Node > | header |
| Pointer to the header node.
|
|
SkipList class implementation with basic methods
Definition at line 55 of file skip_list.cpp.
◆ SkipList()
data_structures::SkipList::SkipList |
( |
| ) |
|
|
inline |
Skip List constructor. Initializes header, start Node for searching in the list
Definition at line 64 of file skip_list.cpp.
64 {
66
68 }
int level
Maximum level of the skiplist.
std::shared_ptr< Node > header
Pointer to the header node.
constexpr int MAX_LEVEL
Maximum level of skip list.
◆ deleteElement()
void data_structures::SkipList::deleteElement |
( |
int | key | ) |
|
|
inline |
Deletes an element by key and prints if has been removed successfully
- Parameters
-
key | is number that is used for comparision. |
Definition at line 133 of file skip_list.cpp.
133 {
134 std::shared_ptr<Node> x =
header;
135
138
139 for (
int i =
level; i >= 0; i--) {
140 while (x->forward[i] != nullptr && x->forward[i]->key < key) {
141 x = x->forward[i];
142 }
144 }
145
146 x = x->forward[0];
147
148 bool doesnt_exist = (x == nullptr || x->key != key);
149
150 if (!doesnt_exist) {
151 for (
int i = 0; i <=
level; i++) {
152 if (
update[i]->forward[i] != x) {
153 break;
154 }
155 update[i]->forward[i] = x->forward[i];
156 }
157
159 std::cout << "Deleted" << std::endl;
160 } else {
161 std::cout << "Doesn't exist" << std::endl;
162 }
163 }
void update(std::vector< int64_t > *segtree, std::vector< int64_t > *lazy, int64_t start, int64_t end, int64_t delta, uint64_t low, uint64_t high, uint64_t pos)
Updates a range of the segment tree.
◆ displayList()
void data_structures::SkipList::displayList |
( |
| ) |
|
|
inline |
Display skip list level
Definition at line 191 of file skip_list.cpp.
191 {
192 std::cout << "Displaying list:\n";
193 for (
int i = 0; i <=
level; i++) {
194 std::shared_ptr<Node>
node =
header->forward[i];
195 std::cout << "Level " << (i) << ": ";
196 while (
node !=
nullptr) {
197 std::cout <<
node->key <<
" ";
199 }
200 std::cout << std::endl;
201 }
202 }
◆ insertElement()
void data_structures::SkipList::insertElement |
( |
int | key, |
|
|
void * | value ) |
|
inline |
Inserts elements with given key and value; It's level is computed by randomLevel() function.
- Parameters
-
key | is number that is used for comparision |
value | pointer to a value, that can be any type |
Definition at line 90 of file skip_list.cpp.
90 {
91 std::cout << "Inserting" << key << "...";
92 std::shared_ptr<Node> x =
header;
95
96 for (
int i =
level; i >= 0; i--) {
97 while (x->forward[i] != nullptr && x->forward[i]->key < key) {
98 x = x->forward[i];
99 }
101 }
102
103 x = x->forward[0];
104
105 bool doesnt_exist = (x == nullptr || x->key != key);
106 if (doesnt_exist) {
108
109 if (rlevel >
level) {
111
112
114 }
115
116 std::shared_ptr<Node> n =
117 std::make_shared<Node>(key, rlevel, value);
118 for (int i = 0; i <= rlevel; i++) {
119 n->forward[i] =
update[i]->forward[i];
120 update[i]->forward[i] = n;
121 }
122 std::cout << "Inserted" << std::endl;
123
124 } else {
125 std::cout << "Exists" << std::endl;
126 }
127 }
◆ randomLevel()
int data_structures::SkipList::randomLevel |
( |
| ) |
|
|
inline |
Returns random level of the skip list. Every higher level is 2 times less likely.
- Returns
- random level for skip list
Definition at line 75 of file skip_list.cpp.
75 {
76 int lvl = 0;
77 while (
static_cast<float>(std::rand()) / RAND_MAX <
PROBABILITY &&
79 lvl++;
80 }
81 return lvl;
82 }
constexpr float PROBABILITY
Current probability for "coin toss".
◆ searchElement()
void * data_structures::SkipList::searchElement |
( |
int | key | ) |
|
|
inline |
Searching element in skip list structure
- Parameters
-
key | is number that is used for comparision |
- Returns
- pointer to the value of the node
Definition at line 170 of file skip_list.cpp.
170 {
171 std::shared_ptr<Node> x =
header;
172 std::cout << "Searching for " << key << std::endl;
173
174 for (
int i =
level; i >= 0; i--) {
175 while (x->forward[i] && x->forward[i]->key < key) x = x->forward[i];
176 }
177
178 x = x->forward[0];
179 if (x && x->key == key) {
180 std::cout << "Found" << std::endl;
181 return x->value;
182 } else {
183 std::cout << "Not Found" << std::endl;
184 return nullptr;
185 }
186 }
◆ header
std::shared_ptr<Node> data_structures::SkipList::header |
|
private |
◆ level
int data_structures::SkipList::level |
|
private |
Maximum level of the skiplist.
Definition at line 56 of file skip_list.cpp.
The documentation for this class was generated from the following file: