[cpp][17_skiplist] get random level.

This commit is contained in:
Liam Huang 2018-10-30 11:06:35 +08:00
parent 2c6e0bce70
commit 435b76b903
2 changed files with 26 additions and 5 deletions

View File

@ -10,6 +10,9 @@
#include <memory>
#include <initializer_list>
#include <utility>
#include <random>
#include <chrono>
#include <algorithm>
template <typename T,
typename Allocator = std::allocator<T>>
@ -30,11 +33,18 @@ class skiplist : public std::list<T, Allocator> {
using const_reverse_iterator = typename container_type::const_reverse_iterator;
private:
template <typename T, typename Iter>
static const size_type MAX_LEVEL = 16;
mutable size_type level_count = 1;
const unsigned int seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator = std::default_random_engine(seed);
std::binomial_distribution<size_type> distribution = std::binomial_distribution<size_type>(MAX_LEVEL, 0.5);
private:
template <typename Value, typename Iter>
struct IndexNode {
IndexNode(const T& data_, Iter down_) : data(data_), down(down_) {}
const T data;
const Iter down;
IndexNode(const Value& data_, Iter down_) : data(data_), down(down_) {}
const Value data;
const Iter down;
};
public:
@ -56,11 +66,19 @@ class skiplist : public std::list<T, Allocator> {
skiplist(std::initializer_list<value_type> init,
const allocator_type& alloc = allocator_type()) :
container_type(std::forward<std::initializer_list<value_type>>(init), alloc) {}
~skiplist();
~skiplist() {}
public:
skiplist& operator=(const skiplist& other);
skiplist& operator=(skiplist&& other);
private:
inline size_type get_random_level() { return distribution(generator); }
public:
iterator find(const value_type& target);
iterator insert(const value_type& value);
iterator erase(const value_type& value);
};
#endif // SKIPLIST_SKIPLIST_HPP_

View File

@ -8,5 +8,8 @@ int main() {
}
std::cout << std::endl;
for (size_t i = 0; i != 20; ++i)
std::cout << test.get_random_level() << std::endl;
return 0;
}