47 : type(type), data(data), prev(
nullptr), next(
nullptr), up(
nullptr), down(
nullptr) {}
57 static constexpr int MAX_LEVEL = 16;
58 static constexpr double P = 0.5;
60 std::uniform_real_distribution<> dis;
67 SkipList() : gen(std::random_device()()), dis(0, 1), current_level(0), totalSize(0) {
86 while (cur->next->type != SentinelType::POSITIVE_INFINITY &&
87 cur->next->data.
getKey() <= key) {
90 if (cur->down !=
nullptr) {
106 std::pair<bool, std::string>
get(
const std::string &key) {
108 if (cur->data.
getKey() == key) {
109 return {
true, cur->data.
getValue()};
123 void put(
const std::string &key,
const std::string &value) {
125 if (current->data.
getKey() == key) {
130 totalSize += kv.
size();
131 Node *newNode =
new Node(SentinelType::NORMAL, kv);
132 newNode->prev = current;
133 newNode->next = current->next;
134 current->next->prev = newNode;
135 current->next = newNode;
138 while (i < MAX_LEVEL && dis(gen) < P) {
139 if (i >= current_level) {
143 newHead->next = newTail;
144 newTail->prev = newHead;
145 newHead->down = head;
146 newTail->down = tail;
152 while (current->up ==
nullptr) {
153 current = current->prev;
155 current = current->up;
157 newNodeUp->prev = current;
158 newNodeUp->next = current->next;
159 current->next->prev = newNodeUp;
160 current->next = newNodeUp;
161 newNode->up = newNodeUp;
162 newNodeUp->down = newNode;
186 using iterator_category = std::forward_iterator_tag;
188 using difference_type = std::ptrdiff_t;
192 Iterator(
Node *node =
nullptr) : current(node) {}
194 reference operator*()
const {
195 return current->data;
197 pointer operator->()
const {
198 return &(current->data);
201 Iterator &operator++() {
203 current = current->next;
204 if (current && current->type == SentinelType::POSITIVE_INFINITY) {
210 Iterator operator++(
int) {
211 Iterator temp = *
this;
215 bool operator==(
const Iterator &other)
const {
216 return current == other.current;
218 bool operator!=(
const Iterator &other)
const {
219 return !(*
this == other);
224 Node *current = head;
225 while (current->down !=
nullptr) {
226 current = current->down;
228 current = current->next;
230 if (current->type == SentinelType::POSITIVE_INFINITY) {
241 Iterator find(
const std::string &key) {
243 if (node->data.
getKey() == key && node->type == SentinelType::NORMAL) {
251 while (cur !=
nullptr) {
252 Node *next = cur->down;
254 while (tmp !=
nullptr) {
255 Node *tmpNext = tmp->next;
Key-Value Pair Class.
Definition key_value.hpp:19
void setValue(const std::string &newValue)
Set the Value object.
Definition key_value.hpp:44
const std::string & getValue() const
Get the Value object.
Definition key_value.hpp:58
size_t size() const
Get the Size of the Key Value Pair object.
Definition key_value.hpp:66
const std::string & getKey() const
Get the Key object.
Definition key_value.hpp:51
Skip List Node Class.
Definition skiplist.hpp:37
A Iterator class for the skip list.
Definition skiplist.hpp:181
Node * search(const std::string &key)
Search for a key in the skip list.
Definition skiplist.hpp:83
void put(const std::string &key, const std::string &value)
This method inserts a key-value pair into the skip list.
Definition skiplist.hpp:123
size_t getSize()
Get the size of the skip list.
Definition skiplist.hpp:172
std::pair< bool, std::string > get(const std::string &key)
This method retrieves the value associated with a given key in the skip list.
Definition skiplist.hpp:106
SentinelType
To represent the type of node in the skip list.
Definition skiplist.hpp:23