skiplist.h 14 KB

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