skiplist.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #include"../basical/type_checking.h"
  2. #include<cstdlib>
  3. #include<cstring>
  4. #include<exception>
  5. #include<random>
  6. #include<shared_mutex>
  7. #include <variant>
  8. #include<chrono>
  9. #include<iostream>
  10. namespace nanxing_extend
  11. {
  12. static int count=0;
  13. //错误处理机制
  14. class nextpoint_new:std::exception //skip_node分配空间失败的时候
  15. {
  16. const char* what() const noexcept
  17. {
  18. return "malloc next_node point fault";
  19. }
  20. };
  21. class newNode_error:std::exception //申请新的空间的时候
  22. {
  23. const char* what() const noexcept
  24. {
  25. return "malloc new node error";
  26. }
  27. };
  28. class random_error:std::exception //申请预设随机数空间的时候
  29. {
  30. const char* what() const noexcept
  31. {
  32. return "malloc random space error";
  33. }
  34. };
  35. enum class Skip_result //跳表操作的结果
  36. {
  37. successufl,
  38. #ifdef SKIP_MAX_SIZE
  39. full,
  40. #endif
  41. #ifdef SKIP_MAX_SIZE
  42. too_small,
  43. #endif
  44. fault,
  45. exit
  46. };
  47. //注意这里的V只能是要么是能直接深拷贝的类型,要么是指向堆上数据的指针类型
  48. template<typename K,typename V>
  49. struct skip_node
  50. {
  51. static_assert(nanxing_extend::compare_admit<K>::value,"the type of K is error");
  52. static_assert(nanxing_extend::compare_admit<V>::value,"the type of V is error");
  53. skip_node<K,V>** next_node;
  54. V value;
  55. K key;
  56. private:
  57. int level;
  58. public:
  59. skip_node(){};
  60. skip_node(K _key,V _value,int _level):key(_key),value(_value),level(_level){};
  61. void init_next(int level)
  62. {
  63. try
  64. {
  65. next_node=::new skip_node<K,V>*[level];
  66. }
  67. catch(std::bad_alloc){ //捕获内存分配错误
  68. throw nextpoint_new(); //重新抛出一个定制的更详细的类型用以明确错误的具体位置
  69. }
  70. std::memset(next_node,0,sizeof(skip_node<K,V>*)*level);
  71. }
  72. K get_key(){ return key; }
  73. };
  74. template<typename K,typename V>
  75. class skipList
  76. {
  77. static_assert(nanxing_extend::compare_admit<K>::value,"the type of K is error");
  78. static_assert(nanxing_extend::compare_admit<V>::value,"the type of V is error");
  79. private:
  80. using Node=skip_node<K,V>;
  81. using ptr=Node*;
  82. using Nptr=Node**;
  83. //由于C++的便利性我们考虑使用带头节点的跳表(C++允许对数据不进行初始化(默认构造函数))
  84. #ifdef NANXING_THREAD_
  85. std::shared_mutex RW_lock; //读写锁
  86. #endif
  87. Nptr head; //头节点
  88. int max_level; //最大高度
  89. int* random_level; //如果启用随机数表这个就非空,反之为nullptr
  90. //当不启用随机数表,使用rand()构造随机数,启用的时候用mt19773构造随机数
  91. int current_level; //跳表当前高度
  92. int current_size; //跳表当前尺寸
  93. //这里出于一个考虑,当跳表单纯作为小数据内存数据库,单表大小限制是没有意义的
  94. //但是像level_db这样作为KV数据库的缓存的时候,就需要限制大小进行落盘
  95. #ifdef SKIP_MAX_SIZE
  96. int max_size; //跳表允许的最大尺寸
  97. #endif
  98. public:
  99. #ifndef SKIP_MAX_SIZE
  100. skipList(int _max_level):max_level(_max_level),random_level(nullptr)
  101. {
  102. try
  103. {
  104. Node* middle=::new skip_node<K,V>;
  105. middle->init_next(max_level);
  106. head=::new Node*[max_level];
  107. for(int i=0;i<max_level;i++)
  108. {
  109. head[i]=middle;
  110. }
  111. }
  112. catch(std::bad_alloc)
  113. {
  114. throw newNode_error(); //重新抛出更详细的错误类型
  115. }
  116. if(max_level==0){ //如果将高度设置为0直接调用terminate打断整个程序执行
  117. std::cerr<<"the level of skiplist cannot set zero"<<std::endl;
  118. std::terminate();
  119. }
  120. }
  121. #elif
  122. skipList(int _max_level,int _max_size):max_size(_max_size),max_level(_max_level),random_level(nullptr)
  123. {
  124. try
  125. {
  126. Node* middle=::new skip_node;
  127. middle->init_next(max_level);
  128. head=::new (Node*)[max_level];
  129. for(auto& i in head)
  130. {
  131. i=middle;
  132. }
  133. }
  134. catch(std::bad_alloc)
  135. {
  136. throw newNode_error();
  137. }
  138. }
  139. #endif
  140. [[nodiscard]] //这个返回值最好不要忽略否则很有可能会出现内存泄漏
  141. auto insert(K _key,V _value)->std::variant<Skip_result,V> //如果相同的时候我们考虑将value返回因为value很可能会是一个指针,需要手动清空内存
  142. {
  143. #ifdef NANXING_THREAD_
  144. std::lock_guard<std::shared_mutex> lock(RW_lock);
  145. #endif
  146. #ifdef SKIP_MAX_SIZE
  147. if(current_size==max_size)
  148. {
  149. return sk=Skip_result::full;
  150. }
  151. #endif
  152. int rand_level=0;
  153. ptr updata[max_level]={nullptr}; //用于更新的数组
  154. ptr point=head[max_level-1];
  155. ptr new_node;
  156. std::variant<Skip_result,V> sk;
  157. for(int i=max_level-1;i>=0;i--)
  158. {
  159. for(;;)
  160. {
  161. if(point->next_node[i]==nullptr)
  162. {
  163. updata[i]=point;
  164. break;
  165. }
  166. else if(point->next_node[i]->key>=_key)
  167. {
  168. if(point->next_node[i]->key==_key)
  169. {
  170. sk=std::move(point->next_node[i]->value); //这个值已经不需要了,直接移动
  171. point->next_node[i]->value=_value;
  172. return sk;
  173. }
  174. else
  175. {
  176. updata[i]=point;
  177. break;
  178. }
  179. }
  180. else{
  181. point=point->next_node[i]; //更新point指针
  182. }
  183. }
  184. }
  185. [[likely]]
  186. if(random_level!=nullptr)
  187. {
  188. rand_level=random_level[current_size%1024];
  189. }
  190. else
  191. {
  192. rand_level=rand()%max_level;
  193. }
  194. ptr tmp=nullptr;
  195. new_node=new skip_node(_key,_value,rand_level);
  196. new_node->init_next(rand_level);
  197. for(int i=0;i<rand_level;i++)
  198. {
  199. tmp=updata[i]->next_node[i];
  200. updata[i]->next_node[i]=new_node;
  201. new_node->next_node[i]=tmp;
  202. }
  203. if(rand_level>current_level)
  204. {
  205. current_level=rand_level;
  206. }
  207. current_size++;
  208. sk=Skip_result::successufl;
  209. return sk;
  210. }
  211. [[nodiscard]]
  212. auto search(K _key) noexcept ->std::variant<Skip_result,V>{ //不涉及任何内存分配相关任务,因此是异常安全的
  213. #ifdef NANXING_THREAD_
  214. std::shared_lock<std::shared_mutex> lock(RW_lock);
  215. #endif
  216. std::variant<Skip_result,V> sk;
  217. ptr tmp=head[current_level-1];
  218. int tmp_level=current_level-1;
  219. for(int i=tmp_level;i>=0;i--)
  220. {
  221. while(tmp->next_node[tmp_level]!=nullptr)
  222. {
  223. if(tmp->next_node[tmp_level]->key>=_key)
  224. {
  225. if(tmp->next_node[tmp_level]->key==_key)
  226. {
  227. return sk=Skip_result::exit;
  228. }
  229. else{
  230. break; //跳出开始下一层循环
  231. }
  232. }
  233. else{
  234. tmp=tmp->next_node[tmp_level];
  235. }
  236. }
  237. }
  238. return sk=Skip_result::fault;
  239. }
  240. void init_skip() //直接生成随机数表
  241. {
  242. #ifdef NANXING_THREAD_
  243. std::lock_guard<std::shared_mutex> lock(RW_lock);
  244. #endif
  245. if(random_level!=nullptr)
  246. {
  247. return;
  248. }
  249. try{
  250. random_level=::new int[1024]; //刚好是一页的大小(4KB)
  251. }
  252. catch(std::bad_alloc)
  253. {
  254. throw random_error();
  255. }
  256. std::mt19937 rnd(std::chrono::system_clock::now().time_since_epoch().count());
  257. for(int i=0;i<1024;i++)
  258. {
  259. random_level[i]=(rnd()%max_level)+1;
  260. }
  261. }
  262. void Print()
  263. {
  264. ptr tmp=head[0]->next_node[0];
  265. while(tmp!=nullptr&&tmp->next_node[0]!=nullptr) //这里用了截断的技巧,即第一个条件不成立就不会触发第二个条件运行
  266. {
  267. std::cout<<"("<<tmp->get_key()<<","<<tmp->value<<")"<<"->";
  268. tmp=tmp->next_node[0];
  269. count++;
  270. }
  271. if(tmp!=nullptr)
  272. {
  273. std::cout<<"("<<tmp->get_key()<<","<<tmp->value<<")"<<std::endl;
  274. count++;
  275. }
  276. std::cout<<"count ="<<count<<std::endl;
  277. }
  278. #ifdef SKIP_MAX_SIZE
  279. [[nodicard]]
  280. inline auto change_size(int _max_size)->std::variant<Skip_result,V> noexcept
  281. {
  282. std::variant<
  283. if(_max_size>this->max_size)
  284. {
  285. this->max_size=_max_size;
  286. tmp=Skip_result::successufl;
  287. return tmp;
  288. }
  289. else
  290. {
  291. tmp=Skip_result::too_samll;
  292. return tmp;
  293. }
  294. }
  295. #endif
  296. #ifdef _NANXING_TEST_
  297. inline void insert_check()
  298. {
  299. ptr tmp=head[0]->next_node[0];
  300. K tmp_key;
  301. if(tmp==nullptr)
  302. {
  303. std::cerr<<"the skiplist is empty"<<std::endl;
  304. std::terminate();
  305. }
  306. else
  307. {
  308. tmp_key=head[0]->next_node[0]->key;
  309. }
  310. tmp=tmp->next_node[0];
  311. while(tmp->next_node[0]!=nullptr)
  312. {
  313. if(tmp->next_node[0]->key<tmp_key)
  314. {
  315. std::cerr<<"THE skiplist insert error"<<std::endl;
  316. std::terminate();
  317. }
  318. tmp_key=tmp->key;
  319. tmp=tmp->next_node[0];
  320. }
  321. std::cout<<"insert successful"<<std::endl;
  322. }
  323. #endif
  324. };
  325. }