Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <cassert>
  4.  
  5. namespace containers {
  6.    
  7. template<typename ValueT, typename KeyT>
  8. class NTree {
  9. public:
  10.     enum {
  11.         Dimensions = KeyT::Dimensions,
  12.         Branches = 1<<Dimensions,
  13.     };
  14.     typedef ValueT ValueType;
  15.     typedef KeyT KeyType;
  16.  
  17.     struct Node {
  18.         Node **nodes;
  19.         KeyType key;
  20.         ValueType value;
  21.        
  22.         Node() : nodes(NULL) {
  23.         }
  24.  
  25.         Node(const KeyType &key, const ValueType &value)
  26.             : key(key), value(value), nodes(NULL) {
  27.         }
  28.  
  29.         ~Node() {
  30.             for (int i = 0; i < Branches; ++i) {
  31.                 delete nodes[i];
  32.             }
  33.             delete[] nodes;
  34.         }
  35.        
  36.         const Node *get(const KeyType &key) const {
  37.             if (this->key == key)
  38.                 return this;
  39.            
  40.             if (!nodes)
  41.                 return NULL;
  42.            
  43.             int index = key > this->key;
  44.             assert((index >= 0)&&(index < Branches));
  45.            
  46.             return nodes[index]->get(key);
  47.         }
  48.        
  49.         Node &insert(const KeyType &key, const ValueType &value) {
  50.             if (this->key == key) {
  51.                 this->value = value;
  52.                 return *this;
  53.             }
  54.            
  55.             int index = key > this->key;
  56.             assert((index >= 0)&&(index < Branches));
  57.            
  58.             if (!nodes) {
  59.                 nodes = new Node*[Branches];
  60.                 for (int i = 0; i < Branches; ++i) {
  61.                     nodes[i] = NULL;
  62.                 }
  63.             }
  64.            
  65.             if (!nodes[index]) {
  66.                 Node *node = new Node(key, value);
  67.                 nodes[index] = node;
  68.                 return *node;
  69.             }
  70.            
  71.             return nodes[index]->insert(key, value);
  72.         }
  73.     };
  74.    
  75.     Node *root;
  76.  
  77.     NTree() : root(NULL) {
  78.     }
  79.    
  80.     const ValueType *get(const KeyType &key) const {
  81.         if (!root)
  82.             return NULL;
  83.         const Node *node = root->get(key);
  84.         if (!node)
  85.             return NULL;
  86.         return &node->value;
  87.     }
  88.    
  89.     void insert(const KeyType &key, const ValueType &value) {
  90.         if (!root) {
  91.             root = new Node(key, value);
  92.             return;
  93.         }
  94.         root->insert(key, value);
  95.     }
  96. };
  97.  
  98. namespace test {
  99.  
  100. struct CharV3 {
  101.     enum {
  102.         Dimensions = 3,
  103.     };
  104.     char x;
  105.     char y;
  106.     char z;
  107.    
  108.     bool operator ==(const CharV3 &other) const {
  109.         return (x == other.x) && (y == other.y) && (z == other.z);
  110.     }
  111.    
  112.     int operator >(const CharV3 &other) const {
  113.         return ((x > other.x)?1:0) | ((y > other.y)?1:0)<<1 | ((z > other.z)?1:0)<<2;
  114.     }
  115. };
  116.    
  117. inline void test_ntree() {
  118.     NTree<int, CharV3> tree;
  119.    
  120.     CharV3 c;
  121.     c.x = 0; c.y = 1; c.z = 25;
  122.     tree.insert(c, 10);
  123.     c.x = 2; c.y = 0; c.z = 24;
  124.     tree.insert(c, 11);
  125.     c.x = -5; c.y = -1; c.z = 100;
  126.     tree.insert(c, 12);
  127.  
  128.     c.x = 0; c.y = 1; c.z = 25;
  129.     assert(*tree.get(c) == 10);
  130.     c.x = 2; c.y = 0; c.z = 24;
  131.     assert(*tree.get(c) == 11);
  132.     c.x = -5; c.y = -1; c.z = 100;
  133.     assert(*tree.get(c) == 12);
  134.     c.x = -6; c.y = -1; c.z = 100;
  135.     assert(tree.get(c) == NULL);
  136.  
  137.    
  138. }
  139.  
  140. } // namespace test
  141.  
  142. } // namespace containers
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement