Skip to content

Commit

Permalink
refactor and clean
Browse files Browse the repository at this point in the history
  • Loading branch information
Amadeus-cyf committed Dec 29, 2023
1 parent 9bdf21b commit 1a5dd1d
Show file tree
Hide file tree
Showing 4 changed files with 442 additions and 455 deletions.
49 changes: 18 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ skiplist::Skiplist<std::string, decltype(compare)> skiplist(4, compare);
Insert a key.
```C++
/* return true if success */
skiplist.insert("key0");
skiplist.Insert("key0");
```

Check whether a key exists.
```C++
if(skiplist.contains("key1")) {
if(skiplist.Contains("key1")) {
/* do something */
}
```

Get a key by rank
```C++
/* get first element */
const std::string& first_key = skiplist.getElementByRank(0);
const std::string& first_key = skiplist.GetElementByRank(0);
/* get last element */
const std::string& last_key = skiplist.getElementByRank(-1);
const std::string& last_key = skiplist.GetElementByRank(-1);

/* access via index, 0-index based */
const std::string& first_key = skiplist[0];
Expand All @@ -60,77 +60,77 @@ const std::string& last_key = skiplist[skiplist.size()-1];
Get rank of a key
```C++
/* get the rank(0-index based) of a key */
const size_t rank = skiplist.getRankofElement("key");
const size_t rank = skiplist.GetRankofElement("key");
```

Get keys by range
```C++
/* get keys between [0, 4] */
const std::vector<std::string>& keys = skiplist.getElementsByRange(0, 4);
const std::vector<std::string>& keys = skiplist.GetElementsByRange(0, 4);
/* get the second last and the last key */
const std::vector<std::string>& last_keys = skiplist.getElementsByRange(-2, -1);
const std::vector<std::string>& last_keys = skiplist.GetElementsByRange(-2, -1);
```

Get keys by reverse range
```C++
/* get keys reversely between [0, 4] */
const std::vector<std::string>& keys = skiplist.getElementsByRevRange(0, 4);
const std::vector<std::string>& keys = skiplist.GetElementsByRevRange(0, 4);
/* get reversely the second last and the last key */
const std::vector<std::string>& last_keys = skiplist.getElementsByRevRange(-2, -1);
const std::vector<std::string>& last_keys = skiplist.GetElementsByRevRange(-2, -1);
```

Get keys greater than a value
```C++
/* return all keys greater than "key_to_compare" */
const std::vector<std::string>& keys = skiplist.getElementsGt("key_to_compare");
const std::vector<std::string>& keys = skiplist.GetElementsGt("key_to_compare");
```

Get keys greater than or equal to a value
```C++
/* return all keys greater than or equal to "key_to_compare" */
const std::vector<std::string>& keys = skiplist.getElementsGte("key_to_compare");
const std::vector<std::string>& keys = skiplist.GetElementsGte("key_to_compare");
```

Get keys less than a value
```C++
/* return all keys less than "key_to_compare" */
const std::vector<std::string>& keys = skiplist.getElementsLt("key_to_compare");
const std::vector<std::string>& keys = skiplist.GetElementsLt("key_to_compare");
```

Get keys less than or equal to a value
```C++
/* return all keys less than or equal to "key_to_compare" */
const std::vector<std::string>& keys = skiplist.getElementsLte("key_to_compare");
const std::vector<std::string>& keys = skiplist.GetElementsLte("key_to_compare");
```

Get keys within a range
```C++
/* return all keys within the range [key_start, key_end) */
const std::vector<std::string>& keys = skiplist.getElementsInRange("key_start", "key_end");
const std::vector<std::string>& keys = skiplist.GetElementsInRange("key_start", "key_end");
```

Delete a key.
```C++
/* return true if success */
skiplist.del("key1");
skiplist.Delete("key1");
```

Update a key.
```C++
/* return true if success */
skiplist.update("key2", "key5");
skiplist.Update("key2", "key5");
```

Iterate over the skiplist.
```C++
for (auto it = skiplist.begin(); it != skiplist.end(); ++it) {
for (auto it = skiplist.Begin(); it != skiplist.End(); ++it) {
/* do something */
}
```

Print the skiplist.
```C++
skiplist.print();
skiplist.Print();
```

## Running Unit Tests
Expand All @@ -139,19 +139,6 @@ cd build && ./skiplist_tests
```

## Benchmarks
### Setup
Use a skiplist with initial depth = 16. All keys in the skiplist are random generated strings with length = 10.
```
2023-02-05T15:38:21+08:00
Running ./skiplist_benchmark
Run on (10 X 24.0411 MHz CPU s)
CPU Caches:
L1 Data 64 KiB
L1 Instruction 128 KiB
L2 Unified 4096 KiB (x10)
Load Average: 2.43, 2.12, 1.88
```

### Running Benchmark
```sh
cd build && ./skiplist_benchmark
Expand Down
30 changes: 15 additions & 15 deletions benchmarks/skiplist_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static void Insert(benchmark::State& state) {
for (auto _ : state) {
const std::string& key = randString(10);
keys.push_back(key);
skiplist.insert(key);
skiplist.Insert(key);
}
}

Expand All @@ -37,10 +37,10 @@ static void Search(benchmark::State& state) {
if (exist) {
/* search for an existing key */
const std::string& key = keys[rand() % keys.size()];
skiplist.contains(key);
skiplist.Contains(key);
} else {
/* search for a non-existing key */
skiplist.contains(randString(10));
skiplist.Contains(randString(10));
}
}
}
Expand All @@ -52,10 +52,10 @@ static void Update(benchmark::State& state) {
if (exist) {
/* update an existing key */
const std::string& key = keys[rand() % keys.size()];
skiplist.update(key, randString(10));
skiplist.Update(key, randString(10));
} else {
/* update a non-existing key */
skiplist.update(randString(10), randString(10));
skiplist.Update(randString(10), randString(10));
}
}
}
Expand All @@ -67,17 +67,17 @@ static void Delete(benchmark::State& state) {
if (exist) {
/* delete an existing key */
const std::string& key = keys[rand() % keys.size()];
skiplist.del(key);
skiplist.Delete(key);
} else {
/* delete a non-existing key */
skiplist.del(randString(10));
skiplist.Delete(randString(10));
}
}
}

static void GetElementByRank(benchmark::State& state) {
for (auto _ : state) {
skiplist.getElementByRank(rand() % skiplist.size());
skiplist.GetElementByRank(rand() % skiplist.Size());
}
}

Expand All @@ -86,41 +86,41 @@ static void GetRankOfElement(benchmark::State& state) {
bool exist = rand() % 2 == 1;

if (exist) {
skiplist.getRankofElement(keys[rand() % keys.size()]);
skiplist.GetRankofElement(keys[rand() % keys.size()]);
} else {
skiplist.getRankofElement(randString(10));
skiplist.GetRankofElement(randString(10));
}
}
}

static void GetElementsByRange(benchmark::State& state) {
for (auto _ : state) {
skiplist.getElementsByRange(rand() % keys.size(), rand() % (keys.size() + 1));
skiplist.GetElementsByRange(rand() % keys.size(), rand() % (keys.size() + 1));
}
}

static void GetElementsByRevRange(benchmark::State& state) {
for (auto _ : state) {
skiplist.getElementsByRevRange(rand() % keys.size(), rand() % (keys.size() + 1));
skiplist.GetElementsByRevRange(rand() % keys.size(), rand() % (keys.size() + 1));
}
}

static void GetElementsGt(benchmark::State& state) {
for (auto _ : state) {
skiplist.getElementsGt(randString(10));
skiplist.GetElementsGt(randString(10));
}
}

static void GetElementsLt(benchmark::State& state) {
for (auto _ : state) {
skiplist.getElementsLt(randString(10));
skiplist.GetElementsLt(randString(10));
}
}

static void GetElementsInRange(benchmark::State& state) {
for (auto _ : state) {
const std::string &s1 = randString(10), &s2 = randString(10);
skiplist.getElementsInRange(s1 < s2 ? s1 : s2, s1 < s2 ? s2 : s1);
skiplist.GetElementsInRange(s1 < s2 ? s1 : s2, s1 < s2 ? s2 : s1);
}
}

Expand Down
Loading

0 comments on commit 1a5dd1d

Please sign in to comment.