4#ifdef LINKED_LIST_VISUALIZATION_H
5#include "../../visualization/list_visual/linked_list_visualization.h"
13#include <unordered_set>
31 MAX_LEVEL = MAX_LEVEL;
32 root = std::make_shared<node>(0, MAX_LEVEL);
34 throw std::invalid_argument(
"Max level value is too high");
36 if (PROB > 0.0 && PROB < 1.0) {
38 }
else if (PROB >= 1.0) {
39 throw std::invalid_argument(
40 "Probability value is greater or equal to 1");
41 }
else if (PROB <= 0.0) {
42 throw std::invalid_argument(
43 "Probability value is smaller or equal to 0");
45 }
catch (std::invalid_argument &e) {
46 std::cerr << e.what() <<
'\n';
56 skip_list(
const skip_list &s) : level(s.level), PROB(s.PROB), MAX_LEVEL(s.MAX_LEVEL), root(s.root) {
71 MAX_LEVEL = s.MAX_LEVEL;
86 std::shared_ptr<node> head = root;
87 std::vector<std::shared_ptr<node>> update(MAX_LEVEL + 1,
nullptr);
89 for (
int i = level; i >= 0; i--) {
90 while (head->next[i] !=
nullptr && head->next[i]->key < key) {
97 if (!head || head->key != key) {
100 for (
int i = level + 1; i < lvl + 1; i++) {
106 std::shared_ptr<node> nn = std::make_shared<node>(key, lvl);
107 for (
int i = 0; i <= lvl; i++) {
108 nn->next[i] = update[i]->next[i];
109 update[i]->next[i] = nn;
135 std::shared_ptr<node> x = root;
136 std::vector<std::shared_ptr<node>> update(MAX_LEVEL + 1,
nullptr);
138 for (int64_t i = level; i >= 0; i--) {
139 while (x->next[i] && x->next[i]->key < key) {
146 if (x && x->key == key) {
147 for (int64_t i = 0; i <= level; i++) {
148 if (update[i]->next[i] != x) {
151 update[i]->next[i] = x->next[i];
153 while (level > 0 && root->next[level] ==
nullptr) {
165 std::shared_ptr<node> x = root;
166 for (int64_t i = level; i >= 0; i--) {
167 while (x->next[i] && x->next[i]->key < key) {
172 if (x && x->key == key) {
183 #ifdef LINKED_LIST_VISUALIZATION_H
185 std::string generated = this->generate();
186 linked_list_visualization::visualize(generated);
194 std::shared_ptr<node> root = l.root;
196 for (
int i = 0; i <= l.level; i++) {
198 std::shared_ptr<node> curr = l.root->next[i];
199 while (curr !=
nullptr) {
200 out << curr->key <<
" ";
201 curr = curr->next[i];
220 std::vector<std::shared_ptr<node>> next;
221 node(T key,
int level,
void *value =
nullptr) :
key(
key) {
222 for (
int i = 0; i < level + 1; i++) {
223 next.push_back(
nullptr);
229 float r = (float)rand() / RAND_MAX;
231 while (r < PROB && lvl < MAX_LEVEL) {
233 r = (float)rand() / RAND_MAX;
239 std::shared_ptr<node> root;
241 std:: string generate_node(std::string node_val,
int levs){
244 gen +=
" [label=\"<";
245 gen += std::to_string(levs);
248 for(
int i=levs-1;i>=0;i--){
250 gen += std::to_string(i);
259 std::string generate_edge(std::string prev_val, std::string curr_val,
int lev){
263 gen += std::to_string(lev);
267 gen += std::to_string(lev);
272 std::string generate() {
274 gen +=
"rankdir=LR;";
276 gen +=
"node [shape=record;]";
278 std::unordered_set<std::string> S;
279 int m_level = std::min(level, (
int)root->next.size());
280 gen += generate_node(
"root", m_level+1);
281 gen += generate_node(
"NULL", m_level+1);
282 gen += generate_edge(
"root",
"NULL", m_level+1);
283 for(
int i=m_level;i>=0;i--){
284 std::shared_ptr<node> head = root;
285 head = head->next[i];
286 std::string prev_val =
"root";
287 std::string head_key;
289 if(std::is_same_v<T, std::string>){
290 head_key = head->key;
292 head_key = to_string(head->key);
294 if(S.find(head_key) == S.end()){
296 gen += generate_node(head_key, i);
298 gen += generate_edge(prev_val, head_key, i);
300 head = head->next[i];
302 gen += generate_edge(prev_val,
"NULL", i);
313 std::shared_ptr<node> ptr;
320 explicit Iterator(std::shared_ptr<node> ptr) noexcept : ptr(ptr) {}
339 if (ptr !=
nullptr) {
Iterator class.
Definition skip_list.h:311
Iterator operator++(int)
operator ++ for type Iterator
Definition skip_list.h:350
T operator*()
operator * for type Iterator
Definition skip_list.h:369
bool operator!=(const Iterator &it)
operator != for type Iterator
Definition skip_list.h:362
Iterator & operator=(std::shared_ptr< node > current)
= operator for Iterator type*
Definition skip_list.h:328
Iterator & operator++()
operator ++
Definition skip_list.h:338
Iterator(std::shared_ptr< node > ptr) noexcept
Construct a new Iterator object.
Definition skip_list.h:320
skip_list class.
Definition skip_list.h:20
skip_list(int MAX_LEVEL, float PROB)
skip_list constructor.
Definition skip_list.h:28
Iterator end()
pointer that points to the last element of the list
Definition skip_list.h:128
void remove(T key)
remove function.
Definition skip_list.h:134
~skip_list() noexcept
Destroy the skip list object.
Definition skip_list.h:79
skip_list & operator=(const skip_list &s)
operator = for the skip_list class
Definition skip_list.h:68
skip_list(const skip_list &s)
Copy constructor for the skip_list class.
Definition skip_list.h:56
friend std::ostream & operator<<(std::ostream &out, skip_list< T > &l)
visualize function returns a .dot file that can be previewd with graphviz plugin in vscode
Definition skip_list.h:193
Iterator begin()
pointer that points to the first element of the list
Definition skip_list.h:121
void insert(T key)
insert function.
Definition skip_list.h:85
bool search(T key)
search function.
Definition skip_list.h:164
@ key
the parser read a key of a value in an object