Advertisement
dvulakh

concurrent_trie_node.cpp

Mar 4th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1.  
  2. #include "concurrent_trie_node.h"
  3.  
  4. typedef list<vertex>::iterator lvi;
  5. typedef concurrent_trie_node trie;
  6.  
  7. // Construct node
  8. trie::concurrent_trie_node(trie* p, vertex val)
  9. {
  10.     this->d = p->d + 1;
  11.     this->mxd = p->mxd;
  12.     this->mxb = p->mxb;
  13.     this->ch = new A(trie*)[mxb + 1]();
  14.     this->dinf = done_info(d == mxd, d == mxd);
  15.     this->val = val;
  16.     this->p = p;
  17. }
  18. trie::concurrent_trie_node(int mxd, int mxb)
  19. {
  20.     this->ch = new A(trie*)[mxb + 1]();
  21.     this->dinf = done_info(0, 0);
  22.     this->mxb = mxb;
  23.     this->mxd = mxd;
  24.     this->p = NULL;
  25.     this->val = 0;
  26.     this->d = 0;
  27. }
  28.  
  29. // Delete node
  30. trie::~concurrent_trie_node()
  31. {
  32.     for (int i = 0; i < mxb; i++)
  33.         if (ch[i] != NULL)
  34.             delete ch[i];
  35.     delete[] ch;
  36. }
  37.  
  38. // Set done_info
  39. void set_dinf(trie* t, done_info d)
  40. {
  41.     done_info nd = d, od = d;
  42.     do {
  43.         nd = d;
  44.         od = t->dinf.load();
  45.     } while (!t->dinf.compare_exchange_weak(od, nd));
  46. }
  47.  
  48. // Insert path
  49. void insert_path(lvi walk, lvi end, trie* t, bool kill)
  50. {
  51.     if (t->d == t->mxd)
  52.         return;
  53.     vertex v = *walk;
  54.     if (t->ch[v].load() == NULL) {
  55.         trie* nt = new trie(t, v), * ot = NULL;
  56.         t->ch[v].compare_exchange_weak(ot, nt);
  57.     }
  58.     walk++;
  59.     if (walk == end) {
  60.         if (kill)
  61.             set_dinf(t, done_info(1, 1));
  62.         return;
  63.     }
  64.     insert_path(walk, end, t->ch[v], kill);
  65.     int dch = 0; double comp = 0;
  66.     for (int i = 0; i <= t->mxb; i++)
  67.         if (t->ch[i].load() != NULL)
  68.             comp += t->ch[i].load()->dinf.load().comp, dch += t->ch[i].load()->dinf.load().done;
  69.     done_info od = t->dinf, nd = t->dinf;
  70.     do {
  71.         od = t->dinf;
  72.         nd = done_info(max(0 + od.comp, comp / (t->mxb - t->d)), od.done || dch == t->mxb - t->d);
  73.     } while (!t->dinf.compare_exchange_weak(od, nd));
  74. }
  75.  
  76. // Follow path
  77. trie* find(lvi walk, lvi end, trie* t)
  78. {
  79.     if (walk == end)
  80.         return t;
  81.     vertex v = *walk++;
  82.     return t->ch[v].load() == NULL ? NULL : find(walk, end, t->ch[v].load());
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement