mirror of
https://github.com/usatiuk/ficus.git
synced 2025-10-29 00:27:52 +01:00
small stuff
This commit is contained in:
@@ -23,19 +23,25 @@ public:
|
||||
}
|
||||
|
||||
UniquePtr(UniquePtr const &other) = delete;
|
||||
UniquePtr &operator=(UniquePtr other) = delete;
|
||||
UniquePtr &operator=(UniquePtr const &other) = delete;
|
||||
|
||||
UniquePtr(UniquePtr &&other) {
|
||||
ptr = other.ptr;
|
||||
other.ptr = nullptr;
|
||||
}
|
||||
|
||||
UniquePtr &operator=(UniquePtr &&other) {
|
||||
ptr = other.ptr;
|
||||
other.ptr = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
T *operator->() const { return ptr; }
|
||||
|
||||
T &operator*() const { return *ptr; }
|
||||
|
||||
T *get() const noexcept { return ptr; }
|
||||
|
||||
|
||||
T *release() noexcept {
|
||||
auto b = ptr;
|
||||
ptr = nullptr;
|
||||
|
||||
@@ -142,40 +142,40 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
void add(V *p, size_t n, bool reuseUpdate = false) {
|
||||
if (!reuseUpdate) {
|
||||
Node *cur = root;
|
||||
for (int i = curL; i >= 0; i--) {
|
||||
while (cur->next[i]->key < p->l && !cur->next[i]->end)
|
||||
cur = cur->next[i];
|
||||
toUpdate[i] = cur;
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < n; i++, p++) {
|
||||
size_t newLevel = randomL();
|
||||
|
||||
if (newLevel > curL) {
|
||||
for (size_t j = curL + 1; j <= newLevel; j++)
|
||||
toUpdate[j] = root;
|
||||
|
||||
curL = newLevel;
|
||||
}
|
||||
|
||||
auto newNode = (Node *) nodeAllocator.get();
|
||||
newNode->key = p->l;
|
||||
newNode->data = *p;
|
||||
|
||||
newNode->before = toUpdate[0];
|
||||
if (toUpdate[0]->next[0] != nullptr) toUpdate[0]->next[0]->before = newNode;
|
||||
|
||||
for (size_t j = 0; j <= newLevel; j++) {
|
||||
newNode->next[j] = toUpdate[j]->next[j];
|
||||
toUpdate[j]->next[j] = newNode;
|
||||
toUpdate[j] = newNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
// void add(V *p, size_t n, bool reuseUpdate = false) {
|
||||
// if (!reuseUpdate) {
|
||||
// Node *cur = root;
|
||||
// for (int i = curL; i >= 0; i--) {
|
||||
// while (cur->next[i]->key < p->l && !cur->next[i]->end)
|
||||
// cur = cur->next[i];
|
||||
// toUpdate[i] = cur;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// for (size_t i = 0; i < n; i++, p++) {
|
||||
// size_t newLevel = randomL();
|
||||
//
|
||||
// if (newLevel > curL) {
|
||||
// for (size_t j = curL + 1; j <= newLevel; j++)
|
||||
// toUpdate[j] = root;
|
||||
//
|
||||
// curL = newLevel;
|
||||
// }
|
||||
//
|
||||
// auto newNode = (Node *) nodeAllocator.get();
|
||||
// newNode->key = p->l;
|
||||
// newNode->data = *p;
|
||||
//
|
||||
// newNode->before = toUpdate[0];
|
||||
// if (toUpdate[0]->next[0] != nullptr) toUpdate[0]->next[0]->before = newNode;
|
||||
//
|
||||
// for (size_t j = 0; j <= newLevel; j++) {
|
||||
// newNode->next[j] = toUpdate[j]->next[j];
|
||||
// toUpdate[j]->next[j] = newNode;
|
||||
// toUpdate[j] = newNode;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
bool erase(Node *begin, Node *end, bool reuseUpdate = false) {
|
||||
if (begin == end) return false;
|
||||
@@ -214,7 +214,7 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
Node *add(K const &k, V const &v, bool reuseUpdate = false) {
|
||||
Node *add(K const &k, V v, bool reuseUpdate = false) {
|
||||
if (!reuseUpdate) {
|
||||
Node *cur = root;
|
||||
|
||||
@@ -239,7 +239,7 @@ public:
|
||||
|
||||
auto newNode = (Node *) nodeAllocator.get();
|
||||
newNode->key = k;
|
||||
newNode->data = v;
|
||||
newNode->data = std::move(v);
|
||||
|
||||
newNode->before = toUpdate[0];
|
||||
if (toUpdate[0]->next[0] != nullptr) toUpdate[0]->next[0]->before = newNode;
|
||||
|
||||
@@ -12,11 +12,12 @@ class String {
|
||||
public:
|
||||
String() noexcept {
|
||||
data = static_cast<char *>(kmalloc(1 * sizeof(char)));
|
||||
curLen = 0;
|
||||
data[0] = '\0';
|
||||
}
|
||||
|
||||
String(const char *in) noexcept {
|
||||
unsigned int curLen = strlen(in);
|
||||
curLen = strlen(in);
|
||||
|
||||
data = static_cast<char *>(kmalloc((curLen + 1) * sizeof(char)));
|
||||
data[0] = '\0';
|
||||
@@ -25,7 +26,7 @@ public:
|
||||
}
|
||||
|
||||
String(String const &str) noexcept {
|
||||
unsigned int curLen = strlen(str.data);
|
||||
curLen = str.curLen;
|
||||
|
||||
data = static_cast<char *>(kmalloc((curLen + 1) * sizeof(char)));
|
||||
data[0] = '\0';
|
||||
@@ -35,11 +36,13 @@ public:
|
||||
|
||||
String(String &&str) noexcept {
|
||||
data = str.data;
|
||||
curLen = str.curLen;
|
||||
str.data = nullptr;
|
||||
}
|
||||
|
||||
String &operator=(String str) noexcept {
|
||||
std::swap(data, str.data);
|
||||
std::swap(curLen, str.curLen);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -47,13 +50,15 @@ public:
|
||||
if (data == nullptr) return;
|
||||
kfree(data);
|
||||
data = nullptr;
|
||||
curLen = 0;
|
||||
}
|
||||
|
||||
String &operator+=(String const &rhs) {
|
||||
data = static_cast<char *>(krealloc(data, sizeof(char) * (strlen(data) + strlen(rhs.data) + 1)));
|
||||
data = static_cast<char *>(krealloc(data, sizeof(char) * (curLen + rhs.curLen + 1)));
|
||||
assert(data != nullptr);
|
||||
|
||||
strcat(data, rhs.data);
|
||||
curLen += rhs.curLen;
|
||||
|
||||
return *this;
|
||||
}
|
||||
@@ -67,10 +72,18 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
const char *c_str() {
|
||||
const char *c_str() const {
|
||||
return data;
|
||||
}
|
||||
|
||||
size_t length() const {
|
||||
return curLen;
|
||||
}
|
||||
|
||||
bool empty() const {
|
||||
return curLen == 0;
|
||||
}
|
||||
|
||||
bool operator==(String const &rhs) const {
|
||||
return strcmp(data, rhs.data) == 0;
|
||||
}
|
||||
@@ -92,7 +105,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
// unsigned int curLen = 0;
|
||||
size_t curLen = 0;
|
||||
char *data;
|
||||
};
|
||||
|
||||
|
||||
@@ -130,14 +130,19 @@ public:
|
||||
bool test() {
|
||||
String str1("hello");
|
||||
assert(str1 == "hello");
|
||||
assert(str1.length() == 5);
|
||||
str1 += "Hello!";
|
||||
assert(str1 == "helloHello!");
|
||||
assert(str1.length() == 11);
|
||||
str1 = String("abcd");
|
||||
assert(str1.length() == 4);
|
||||
String str2("dcba");
|
||||
assert(str2.length() == 4);
|
||||
assert(str2 > str1);
|
||||
assert(str1 < str2);
|
||||
assert(str1 <= str2);
|
||||
str2 = "abcd";
|
||||
assert(str2.length() == 4);
|
||||
assert(str1 <= str2);
|
||||
|
||||
// GlobalTtyManager.all_tty_putstr("String tests ok!\n");
|
||||
@@ -208,6 +213,16 @@ public:
|
||||
void test_unique_ptr() {
|
||||
UniquePtr<String> ptr("Hello");
|
||||
assert(*ptr == "Hello");
|
||||
|
||||
UniquePtr<String> ptr2(std::move(ptr));
|
||||
assert(*ptr2 == "Hello");
|
||||
assert(ptr.get() == nullptr);
|
||||
|
||||
UniquePtr<String> ptr3;
|
||||
ptr3 = std::move(ptr2);
|
||||
assert(ptr2.get() == nullptr);
|
||||
|
||||
assert(*ptr3 == "Hello");
|
||||
}
|
||||
|
||||
void test_list() {
|
||||
|
||||
@@ -116,6 +116,10 @@ public:
|
||||
return curSize;
|
||||
}
|
||||
|
||||
bool empty() const {
|
||||
return curSize == 0;
|
||||
}
|
||||
|
||||
private:
|
||||
size_t capacity = 2;
|
||||
size_t curSize = 0;
|
||||
|
||||
Reference in New Issue
Block a user