鱼C论坛

 找回密码
 立即注册
查看: 2384|回复: 0

[技术交流] 跳跃表的C++实现

[复制链接]
发表于 2014-4-9 23:38:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
参考资料:Algorithms in C++ Parts 1-4, Robert Sedgewick
代码如下:
  1. #ifndef SKIPLIST_H
  2. #define SKIPLIST_H
  3. #include "Random.h"

  4. static const int max_level = 30;
  5.         //for small lists, max_level can be set 5

  6. template<typename Key, typename Value>
  7. class SkipList
  8. {
  9.         struct Node
  10.         {
  11.                 Key   key;
  12.                 Value val;
  13.                 int   sz;
  14.                 Node ** next;

  15.                 Node (Key key, Value value, int n)
  16.                         : key(key), val(value), sz(n)
  17.                 {
  18.                         next = new Node* [n];
  19.                         for(int i = 0; i < n; ++i)
  20.                                 next[i] = 0;
  21.                 }
  22.         };
  23. public:
  24.         SkipList ()
  25.                 : lgN(0)
  26.         {
  27.                 head = new Node(0, 0, max_level);
  28.         }

  29.         int  count () { return count(head, 0); }

  30.         void insert (const Key& k, const Value& v) { rootInsert(head, new Node(k, v, rand_gen()), lgN); }

  31.         Value search (const Key& k) { return search(head, k, lgN); }

  32.         void remove (const Key& k) { remove(head, k, lgN); }
  33. private:
  34.         Node * head;
  35.         int    lgN;
  36.         Random rnd;

  37.         int rand_gen ()
  38.         {
  39.                 int i, t = rnd.randomInt();
  40.                 for (i = 1; i < max_level; ++i)
  41.                 {
  42.                         int res = t >> (31-i);
  43.                         if (res) break;
  44.                 }
  45.                 if (i > lgN) lgN = i;
  46.                 return i;
  47.         }

  48.         int count (Node * t, int c)
  49.         {
  50.                 Node * t0 = t->next[0];
  51.                 if (!t0) return c;
  52.                 return count(t0, c+1);
  53.         }

  54.         void rootInsert (Node * t, Node * x, int n)
  55.         {
  56.                 Key v = x->key;
  57.                 Node * pos = t->next[n];
  58.                 if (!pos || v < pos->key)
  59.                 {
  60.                         if (n < x->sz)
  61.                         {
  62.                                 x->next[n] = pos;
  63.                                 t->next[n] = x;
  64.                         }
  65.                         if (!n) return;
  66.                         rootInsert(t, x, n-1);
  67.                         return;
  68.                 }
  69.                 rootInsert(pos, x, n);
  70.         }

  71.         Value search (Node * t, Key k, int n)
  72.         {
  73.                 if (!t) return 0;
  74.                 if (k == t->key) return t->val;
  75.                 Node * x = t->next[n];
  76.                 if(!x || k < x->key)
  77.                 {
  78.                         if (!n) return 0;
  79.                         return search(t, k, n-1);
  80.                 }
  81.                 return search(x, k, n);
  82.         }

  83.         void remove (Node * t, Key k, int n)
  84.         {
  85.                 if (!t) return;
  86.                 Node * x = t->next[n];
  87.                 if (!x || x->key >= k)
  88.                 {
  89.                         if (x)
  90.                         {
  91.                                 if (k == x->key)
  92.                                         t->next[n] = x->next[n];
  93.                                 if (!k)
  94.                                 {
  95.                                         delete x;
  96.                                         return;
  97.                                 }
  98.                         }
  99.                         if (!n) return;
  100.                         remove(t, k, n-1);
  101.                 }
  102.                 remove(t->next[n], k, n);
  103.         }
  104. };

  105. #endif
复制代码
由于原来我发布过的代码里面有Random.h,这里就不重读列出了。参见http://bbs.fishc.com/thread-44631-1-1.html的附件。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 19:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表