SkipListSet

This commit is contained in:
2023-10-21 19:02:54 +02:00
parent 802d2e4227
commit a2c05c4b7b
2 changed files with 56 additions and 17 deletions

View File

@@ -4,7 +4,6 @@
template<typename K>
class SkipListSet {
static constexpr size_t maxL{31};
friend SkipListSetTester;
public:
struct Node {
@@ -56,10 +55,12 @@ private:
};
static int randomL() {
return ffs(rand()) - 1;// NOLINT
int ret = __builtin_ffs(rand());
assert(ret >= 0);
return ret;// NOLINT
}
static inline NodeAllocator nodeAllocator;
NodeAllocator nodeAllocator;
Node *root;
Node *endnode;
@@ -301,20 +302,20 @@ public:
return true;
}
void print() const {
std::cout << "LIST STATUS" << std::endl;
for (size_t i = 0; i <= curL; i++) {
Node *n = root->next[i];
std::cout << "L " << i << ": ";
while (n != nullptr && n->next[0]) {
std::cout << "GUARD: " << n->end << " KEY: " << n->key << " RANGE: " << n->data
<< " <<<<<";
n = n->next[i];
}
std::cout << std::endl;
}
};
// void print() const {
// std::cout << "LIST STATUS" << std::endl;
//
// for (size_t i = 0; i <= curL; i++) {
// Node *n = root->next[i];
// std::cout << "L " << i << ": ";
// while (n != nullptr && n->next[0]) {
// std::cout << "GUARD: " << n->end << " KEY: " << n->key << " RANGE: " << n->data
// << " <<<<<";
// n = n->next[i];
// }
// std::cout << std::endl;
// }
// };
};

View File

@@ -6,10 +6,13 @@
#include "SkipList.hpp"
#include "SkipListSet.hpp"
#include "String.hpp"
#include "Vector.hpp"
#include "serial.hpp"
#include "tty.hpp"
//#include "String.hpp"
//#include "String.hpp"
@@ -122,6 +125,7 @@ public:
assert(testv1[2] == "H6");
assert(testv2[2] == "H5");
all_tty_putstr("Vector tests ok!\n");
return true;
}
};
@@ -141,6 +145,7 @@ public:
str2 = "abcd";
assert(str1 <= str2);
all_tty_putstr("String tests ok!\n");
return true;
}
};
@@ -170,6 +175,37 @@ public:
test1.add(78, "test78", true);
assert(test1.find(78)->data == "test78");
all_tty_putstr("SkipList tests ok!\n");
return true;
}
};
class SkipListSetTester {
public:
bool test() {
SkipListSet<int> test1;
test1.add(5, false);
test1.add(999, false);
test1.add(5, false);
test1.add(1, false);
test1.add(999, false);
assert(test1.find(5)->key == 5);
assert(test1.find(1)->key == 1);
assert(test1.find(999)->key == 999);
test1.erase(1);
assert(test1.find(1)->key != 1);
test1.add(87, false);
assert(test1.find(87)->key == 87);
auto p2 = test1.lower_bound_update(78);
assert(p2->key == 87);
test1.add(78, true);
assert(test1.find(78)->key == 78);
all_tty_putstr("SkipListSet tests ok!\n");
return true;
}
};
@@ -178,6 +214,8 @@ int test_templates() {
SkipListTester SLTester;
SLTester.test();
SkipListSetTester SLSTester;
SLSTester.test();
StringTester stringTester;
stringTester.test();
VectorTester vectorTester;