20 #define ULL unsigned long long 34 long nth_root = trunc(std::pow(X, 1.0 / n));
38 if (std::pow(nth_root + 1, n) <= X) {
54 vector<int> coordinates(D);
56 while (key > 0 && i >= 0) {
57 coordinates[i--] = key % M;
110 void setChild(
int,
Node*);
112 vector<int> getCoordinates();
128 this->child.assign(D, NULL);
147 return this->mutex.try_lock();
156 this->mutex.unlock();
177 this->val_mutex.lock();
179 this->val_mutex.unlock();
189 this->val_mutex.lock();
191 this->val_mutex.unlock();
203 if (index < 0 && index >= this->child.size())
204 throw "Index out of bounds";
205 this->child_mutex.lock();
206 this->child[index] = childNode;
207 this->child_mutex.
unlock();
218 if (index < 0 && index >= this->child.size())
219 throw "Index out of bounds";
220 this->child_mutex.lock();
221 Node<T>* node = this->child[index];
222 this->child_mutex.
unlock();
233 return this->COORDINATES;
259 pair<Node<T>*,
Node<T>*> locatePredecessor(vector<int>);
281 this->root =
new Node<T>(0, D, N);
298 while (current != NULL && coordinates[d] > current->
getCoordinates()[d]){
299 predecessor = current;
303 if (current == NULL || coordinates[d] < current->
getCoordinates()[d])
308 return make_pair(predecessor, current);
319 if (key < 0 || key >= N)
323 pair<Node<T>*,
Node<T>*> p = locatePredecessor(coordinates);
324 Node<T>* predecessor = p.first;
327 if (predecessor != NULL && !predecessor->
try_lock())
329 if (current != NULL && !current->
try_lock())
331 if (predecessor != NULL)
336 p = locatePredecessor(coordinates);
337 if (predecessor != p.first || current != p.second)
339 if (predecessor != NULL)
346 if (current != NULL && key == current->
getKey())
350 if (predecessor != NULL)
359 while (d < this->D && coordinates[d] <= predecessor->
getCoordinates()[d])
362 throw "Given key is out of key space";
398 if (key < 0 || key >= N)
401 pair<Node<T>*,
Node<T>*> p = locatePredecessor(coordinates);
403 if (current != NULL && current->
getKey() == key)
416 if (key < 0 || key >= N)
423 T val = root->getValue();
424 this->root->setValue(NULL);
429 pair<Node<T>*,
Node<T>*> p = locatePredecessor(coordinates);
430 Node<T>* predecessor = p.first;
433 if (predecessor != NULL && !predecessor->
try_lock())
435 if (current != NULL && !current->
try_lock())
437 if (predecessor != NULL)
441 if (current == NULL || current->
getKey() != key)
452 while (d < this->D && predecessor->
getChild(d) != current)
455 throw "Given key is out of key space";
459 while (_d > 0 && new_current == NULL) {
461 new_current = current->
getChild(_d);
464 if (new_current != NULL && !new_current->
try_lock())
466 if (predecessor != NULL)
473 p = locatePredecessor(coordinates);
474 if (predecessor != p.first || current != p.second ||
475 (current != NULL && current->
getKey() != key))
477 if (predecessor != NULL)
481 if (new_current != NULL)
492 predecessor->
setChild(d, new_current);
511 vector< Node<T>* > stack;
512 stack.push_back(mdList.
root);
513 while(stack.size() > 0)
517 cout<<node->
getKey()<<
" ";
520 for (
int d = 0; d < mdList.
D; d++)
522 cout<<coordinates[d]<<
", ";
529 for (
int d = 0; d < mdList.
D; d++)
537 stack.push_back(child);
540 for (
int d = 0; d < mdList.
D; d++)
542 cout<<coordinates[d]<<
", ";
563 if (node->
getKey() == key)
565 cout<<node->
getKey()<<
" ";
568 for (
int d = 0; d < mdlist.
D; d++)
570 cout<<coordinates[d]<<
", ";
577 for (
int d = 0; d < mdlist.
D; d++)
587 for (
int d = 0; d < mdlist.
D; d++)
589 cout<<coordinates[d]<<
", ";
598 cout << key <<
" Not found!\n";
MDList(int, ULL)
MDList constructor.
Definition: mdlist.h:277
T remove(ULL)
Removes the given key.
Definition: mdlist.h:414
Node class.
Definition: mdlist.h:70
std::mutex val_mutex
Mutex lock for value.
Definition: mdlist.h:100
bool try_lock()
Non blocking lock operation on node mutex.
Definition: mdlist.h:145
void printMDList(MDList< T > mdList)
Prints MDList.
Definition: mdlist.h:509
std::mutex mutex
Mutex lock for this Node.
Definition: mdlist.h:92
void insert(ULL, T)
Insert (key, value) to MDList.
Definition: mdlist.h:317
Node< T > * root
Head or Root of the MDList.
Definition: mdlist.h:258
std::mutex child_mutex
Mutex lock for children.
Definition: mdlist.h:96
int D
The D value of MDList.
Definition: mdlist.h:250
Node(ULL, int, ULL, T val=NULL)
Node Class Constructor.
Definition: mdlist.h:123
void setValue(T)
Setter for value.
Definition: mdlist.h:175
Node< T > * getChild(int)
Getter for Child.
Definition: mdlist.h:216
ULL key
The key.
Definition: mdlist.h:75
vector< int > keyToCoordinates(ULL key, int D, ULL N)
Converts key to node coordinates.
Definition: mdlist.h:51
vector< Node * > child
The vector of children of this Node.
Definition: mdlist.h:88
T find(ULL)
Searches for given key.
Definition: mdlist.h:396
long nthRoot(ULL X, int n)
Finds nth root of given number.
Definition: mdlist.h:32
ULL N
The key space.
Definition: mdlist.h:254
ULL getKey()
Getter for key.
Definition: mdlist.h:164
void unlock()
Unlocks the node mutex.
Definition: mdlist.h:154
T val
The value.
Definition: mdlist.h:79
vector< int > COORDINATES
The Node coordinates.
Definition: mdlist.h:84
void lock()
Locks the node mutex.
Definition: mdlist.h:135
void setChild(int, Node *)
Setter for child Node.
Definition: mdlist.h:201
T getValue()
Getter for Value.
Definition: mdlist.h:187
void findAndPrint(MDList< T > mdlist, ULL key)
Finds and prints node for given key.
Definition: mdlist.h:558
MDList class MDList is a dictionary based datastruture which stores (key, value) pairs.
Definition: mdlist.h:245
vector< int > getCoordinates()
Getter for Node coordinates.
Definition: mdlist.h:231
pair< Node< T > *, Node< T > * > locatePredecessor(vector< int >)
Locate Predecessor/Parent of given coordinates.
Definition: mdlist.h:291
#define ULL
Definition: mdlist.h:20