Advertisement
999ms

Untitled

Jul 17th, 2020
1,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.78 KB | None | 0 0
  1. class BinaryTrie:
  2.     def __init__(self):
  3.         self.nodes = [0 for _ in range(64)]
  4.         self.empty_nodes = [63 - i for i in range(63)]
  5.         self.MASK = (1 << 31) - 1
  6.  
  7.     def __sizeof__(self):
  8.         return self.nodes.__sizeof__() + self.empty_nodes.__sizeof__() + self.MASK.__sizeof__()
  9.  
  10.     def get_left(self, value):
  11.         return (value >> 1) & self.MASK
  12.  
  13.     def get_right(self, value):
  14.         return (value >> 32) & self.MASK
  15.  
  16.     @staticmethod
  17.     def get_type(value):
  18.         return value & 1
  19.  
  20.     def _create_node(self):
  21.         if len(self.empty_nodes) == 0:
  22.             self.empty_nodes = [2 * len(self.nodes) - 1 - i for i in range(len(self.nodes))]
  23.             self.nodes += [0 for _ in range(len(self.nodes))]
  24.         index = self.empty_nodes.pop()
  25.         return index
  26.  
  27.     def _remove_nodes(self, node):
  28.         if node == 0:
  29.             return
  30.         self.empty_nodes.append(node)
  31.         left_node = self.get_left(self.nodes[node])
  32.         if left_node:
  33.             self._remove_nodes(left_node)
  34.         right_node = self.get_right(self.nodes[node])
  35.         if right_node:
  36.             self._remove_nodes(right_node)
  37.         self.nodes[node] = 0
  38.  
  39.     def add_address(self, value, size):
  40.         current_node = 0
  41.         for i in range(size):
  42.             bit = 31 - i
  43.             if self.get_type(self.nodes[current_node]) == 1:
  44.                 return
  45.             if (value >> bit) & 1 == 0:
  46.                 current_left = self.get_left(self.nodes[current_node])
  47.                 if current_left == 0:
  48.                     current_left = self._create_node()
  49.                     self.nodes[current_node] |= current_left << 1
  50.                 current_node = current_left
  51.             else:
  52.                 current_right = self.get_right(self.nodes[current_node])
  53.                 if current_right == 0:
  54.                     current_right = self._create_node()
  55.                     self.nodes[current_node] |= current_right << 32
  56.                 current_node = current_right
  57.  
  58.         self.nodes[current_node] |= 1
  59.         current_left = self.get_left(self.nodes[current_node])
  60.         current_right = self.get_right(self.nodes[current_node])
  61.         self._remove_nodes(current_left)
  62.         self._remove_nodes(current_right)
  63.  
  64.     def __contains__(self, value, size):
  65.         current_node = 0
  66.         for i in range(size):
  67.             bit = 31 - i
  68.             if self.get_type(self.nodes[current_node]) == 1:
  69.                 return True
  70.             if (value >> bit) & 1 == 0:
  71.                 current_left = self.get_left(self.nodes[current_node])
  72.                 if current_left == 0:
  73.                     return False
  74.                 current_node = current_left
  75.             else:
  76.                 current_right = self.get_right(self.nodes[current_node])
  77.                 if current_right == 0:
  78.                     return False
  79.                 current_node = current_right
  80.         return self.get_type(self.nodes[current_node]) == 1
  81.  
  82.  
  83. def get_data(address):
  84.     if '/' in address:
  85.         address, routing_prefix = address.split('/')
  86.         size = 32 - int(routing_prefix)
  87.     else:
  88.         size = 0
  89.  
  90.     address = list(map(int, address.split('.')))
  91.  
  92.     value = 0
  93.     for item in address:
  94.         value <<= 8
  95.         value ^= item
  96.  
  97.     value >>= size
  98.     value <<= size
  99.     return value, 32 - size
  100.  
  101.  
  102. arr = ['132.43.43.4', '132.132.123.123/16', '12.3.3.3', '123.123.132.123']
  103. brr = ['9.9.9.9', '123.123.123.123/1', '12.3.3.4', '123.0.0.0', '0.0.0.0', '312.32.32.32/13']
  104.  
  105. binaryTrie = BinaryTrie()
  106.  
  107. for address in arr:
  108.     value, size = get_data(address)
  109.     binaryTrie.add_address(value, size)
  110.  
  111. arr += brr
  112. for address in arr:
  113.     value, size = get_data(address)
  114.     print(binaryTrie.__contains__(value, size))
  115.  
  116. print(binaryTrie.__sizeof__())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement