diff --git a/c-cpp/17_skiplist/skiplist_tr.hpp b/c-cpp/17_skiplist/skiplist_tr.hpp index 027f5db2..4c7c71cb 100644 --- a/c-cpp/17_skiplist/skiplist_tr.hpp +++ b/c-cpp/17_skiplist/skiplist_tr.hpp @@ -10,15 +10,28 @@ #include #include #include +#include namespace skiplist_detail { template struct InternalNode { - const Key key; - std::multiset values; using iterator = typename std::list::iterator; + const Key key; + std::multiset values; std::vector forwards; }; + +template +class random_level { + private: + mutable std::random_device rd; + mutable std::mt19937 gen = std::mt19937(rd()); + mutable std::binomial_distribution dist; + + public: + random_level(IntType max_level, double prob) : dist(max_level - 1, prob) {} + inline IntType operator()() const { return dist(gen); } +}; } // namespace skiplist_detail template ::value, "STATIC ASSERT FAILED! iterator type differs."); + + private: + size_type max_lv_ = 16; + double prob_ = 0.5; + mutable random_level rl_; + + public: + skiplist() : rl_(max_lv_, prob_) {} }; #endif // SKIPLIST_SKIPLIST_TR_HPP_ diff --git a/c-cpp/17_skiplist/skiplist_tr_test.cc b/c-cpp/17_skiplist/skiplist_tr_test.cc index 511829ef..e654fc7d 100644 --- a/c-cpp/17_skiplist/skiplist_tr_test.cc +++ b/c-cpp/17_skiplist/skiplist_tr_test.cc @@ -3,11 +3,22 @@ */ #include +#include +#include #include "skiplist_tr.hpp" int main() { - std::cout << "hello world" << std::endl; + // 1. UT for skiplist_detail::random_level + skiplist_detail::random_level rl(5, 0.5); + std::map hist; + for (size_t i = 0; i != 10000; ++i) { + ++hist[rl()]; + } + for (auto p : hist) { + std::cout << p.first << ' ' << std::string(p.second / 100, '*') << '\n'; + } + return 0; }