def traverse(self, s): """Returns the auth nodes for leaf s + 1.""" for h in range(H): if not ((s >> h) & 1): tau = h break if tau > 0: tempkeep = self.keep[(tau - 1) >> 1] # prevent overwriting if not ((s >> (tau+1)) & 1) and tau < H - 1: self.keep[tau >> 1] = self.auth[tau] if tau == 0: self.auth[0] = Node(h=0, v=leafcalc(s)) else: self.auth[tau] = Node(h=0, v=g(self.auth[tau - 1].v + tempkeep.v)) for h in range(tau): if h < H - K: self.auth[h] = self.treehash[h].node else: offset = (1 << (H - 1 - h)) + h - H rowidx = ((s >> h) - 1) >> 1 self.auth[h] = self.retain[offset + rowidx] for h in range(tau if (tau < H - K) else H - K): startidx = s + 1 + 3 * (1 << h) if startidx < 1 << H: self.treehash[h].restart(startidx) return self.auth
def test_complexe_distance(self): com = Node("COM") b = Node("B", com) c = Node("C", b) g = Node("G", b) h = Node("H", g) self.assertEqual(h.distance(), 3)
def enqueue(self, val): if not self._bottom: self._bottom = self._top = Node(val) self._size += 1 return node = Node(val) self._bottom.next_node = node self._bottom = self._bottom.next_node self._size += 1
def append(self, val): if not self._first: self._first = Node(val) self._size += 1 return tmp = self._first while tmp.next_node: tmp = tmp.next_node tmp.next_node = Node(val) self._size += 1
def update(self): """Performs one unit of computation on the stack. This can imply either the introduction a new leaf node or the computation of a parent node""" if self.completed: return if len(self.stack) >= 2 and self.stack[-1].h == self.stack[-2].h: node_r = self.stack.pop() node_l = self.stack.pop() self.stack.append(Node(h=node_l.h + 1, v=g(node_l.v + node_r.v))) else: self.stack.append(Node(h=0, v=leafcalc(self.next_idx))) self.next_idx += 1 if self.stack[-1].h == self.h: self.completed = True return
def push_left(self, item): old = self._left self._left = Node(item) self._left.next_node = old if self._right is None: self._right = self._left self._size += 1
def push(self, val): old = self._top self._top = Node(val) self._top.next_node = old if old is None: self._bottom = self._top self._size += 1
def update(self): """Performs one iteration of Treehash, i.e. adds one leaf node. Note that this is different from Treehash.update() in the classic traversal algorithm, where only one computational unit is performed.""" node1 = Node(h=0, v=leafcalc(self.next_idx)) while self.stackusage > 0 and STACK[-1].h == node1.h: node2 = STACK.pop() self.stackusage -= 1 node1 = Node(h=node1.h + 1, v=g(node2.v + node1.v)) STACK.append(node1) self.stackusage += 1 self.next_idx += 1 if self.stackusage == 1 and STACK[-1].h == self.h: self.completed = True self.node = STACK.pop() self.stackusage -= 1
def attr_section_number(self, prv: Node) -> Node: # {{{1 num = self.attr_section_number_text(prv) ret = Node("attribute", dict( NAME="doc", VALUE=num, )) return ret
def traverse(s): """Returns the auth nodes for leaf s + 1.""" for h in range(H): if not ((s >> h) & 1): tau = h break if tau > 0: tempKEEP = KEEP[(tau - 1) >> 1] # prevent overwriting too soon if not ((s >> (tau + 1)) & 1) and tau < H - 1: KEEP[tau >> 1] = AUTH[tau] if tau == 0: AUTH[0] = Node(h=0, v=leafcalc(s)) else: AUTH[tau] = Node(h=0, v=g(AUTH[tau - 1].v + tempKEEP.v)) for h in range(tau): if h < H - K: AUTH[h] = TREEHASH[h].node else: offset = (1 << (H - 1 - h)) + h - H rowidx = ((s >> h) - 1) >> 1 AUTH[h] = RETAIN[offset + rowidx] for h in range(tau if (tau < H - K) else H - K): startidx = s + 1 + 3 * (1 << h) if startidx < 1 << H: TREEHASH[h].__init__(h, startidx) for _ in range((H - K) >> 1): l_min = H h = H - K for j in range(H - K): if TREEHASH[j].completed: low = H elif TREEHASH[j].stackusage == 0: low = j else: low = TREEHASH[j].height() if low < l_min: h = j l_min = low if h != H - K: TREEHASH[h].update() return AUTH
def push_right(self, item): old = self._right self._right = Node(item) if self.is_empty(): self._left = self._right else: old.next_node = self._right self._size += 1
def get_series_node(self, series_name): series_name = series_name.split("-")[0].strip() key = series_name.lower() node = self.series_nodes.get(key, None) if node is None: node = Node(series_name, self) self.series_nodes[key] = node return node
def stack_update(self, idx): node1 = Node(h=0, v=leafcalc(idx)) if node1.h < H - K and idx == 3: self.treehash[0].node = node1 while self.stack and self.stack[-1].h == node1.h: if idx >> node1.h == 1: self.auth[node1.h] = node1 else: # node1 is a right-node with row-index 2idx + 3 if node1.h < H - K and idx >> node1.h == 3: self.treehash[node1.h].node = node1 elif node1.h >= H - K: offset = (1 << (H - 1 - node1.h)) + node1.h - H rowidx = ((idx >> node1.h) - 3) >> 1 self.retain[offset + rowidx] = node1 node2 = self.stack.pop() node1 = Node(h=node1.h + 1, v=g(node2.v + node1.v)) self.stack.append(node1)
def insert(self, item): old = self._last self._last = Node(item) if not self._first: self._first = self._last else: old.next_node = self._last self._size += 1
def keygen_and_setup(): """Sets up TREEHASH and AUTH for the start of classic Merkle traversal.""" for h in range(H): TREEHASH[h] = Treehash(h, completed=True) stack = [] for j in range(2**H): node1 = Node(h=0, v=leafcalc(j)) if j == 0: TREEHASH[0].stack = [node1] while stack and stack[-1].h == node1.h: if not AUTH[node1.h]: AUTH[node1.h] = node1 node2 = stack.pop() node1 = Node(h=node1.h + 1, v=g(node2.v + node1.v)) if node1.h < H and not TREEHASH[node1.h].stack: TREEHASH[node1.h].stack.append(node1) stack.append(node1) return stack.pop()
def enqueue(self, val): old_last = self._last self._last = Node(val) self._last.next_node = None if self.is_empty(): self._first = self._last else: old_last.next_node = self._last self._size += 1
def load_channels(self): by_channel_node = Node("By Channel", self) def channel(name, slug): IviewIndexNode(name, by_channel_node, API_URL + "/channel/" + slug) channel("ABC1", "abc1") channel("ABC2", "abc2") channel("ABC3", "abc3") channel("ABC4Kids", "abc4kids") channel("iView Exclusives", "iview")
def insert_after(self, current_node_item, new_node_item): tmp = self._first while tmp: if tmp.val == current_node_item: old_next_node = tmp.next_node new_node = Node(new_node_item) tmp.next_node = new_node new_node.next_node = old_next_node self._size += 1 break tmp = tmp.next_node
def traverse(s): """Returns the auth nodes for leaf s + 1.""" tau = next(h for h in range(H) if not (s >> h) & 1) if not (s >> (tau + 1)) & 1 and tau < H - 1: KEEP[tau] = AUTH[tau] if tau == 0: AUTH[0] = Node(h=0, v=leafcalc(s)) else: AUTH[tau] = Node(h=tau, v=g(AUTH[tau - 1].v + KEEP[tau - 1].v)) KEEP[tau - 1] = None for h in range(tau): if h < H - K: AUTH[h] = TREEHASH[h].node TREEHASH[h].node = None else: AUTH[h] = RETAIN[h].pop() for h in range(min(tau, H - K)): startidx = s + 1 + 3 * 2**h if startidx < 2**H: TREEHASH[h].__init__(h, startidx) for _ in range((H - K) // 2): l_min = float('inf') h = None for j in range(H - K): if TREEHASH[j].completed: low = float('inf') elif TREEHASH[j].stackusage == 0: low = j else: low = TREEHASH[j].height() if low < l_min: h = j l_min = low if h is not None: TREEHASH[h].update() return AUTH
def keygen_and_setup(): """Sets up TREEHASH, RETAIN and AUTH for the start of BDS traversal.""" for h in range(H - K): TREEHASH[h] = Treehash(h, completed=True) stack = [] for j in range(2**H): node1 = Node(h=0, v=leafcalc(j)) if node1.h < H - K and j == 3: TREEHASH[0].node = node1 while stack and stack[-1].h == node1.h: if not AUTH[node1.h]: AUTH[node1.h] = node1 else: # in this case node1 is a right-node with row-index 2j + 3 if node1.h < H - K and TREEHASH[node1.h].node is None: TREEHASH[node1.h].node = node1 elif node1.h >= H - K: RETAIN[node1.h].appendleft(node1) node2 = stack.pop() node1 = Node(h=node1.h + 1, v=g(node2.v + node1.v)) stack.append(node1) return stack.pop()
def add_episode(self, ep_info): video_key = ep_info["episodeHouseNumber"] series_title = ep_info["seriesTitle"] title = ep_info.get("title", None) episode_title = format_episode_title(series_title, title) series_node = self.series_map.get(series_title, None) if not series_node: series_node = Node(series_title, self) self.series_map[series_title] = series_node IviewEpisodeNode(episode_title, series_node, video_key)
def run(self, path): h = [] count = 0 self.startNode.h = self.getHScoreFromString(str(self.startNode.matrix)) self.startNode.g = 0 self.startNode.f = self.startNode.h heapq.heappush(h, self.startNode) while h: current = heapq.heappop(h) count = count + 1 if (count <= self.maxLength): path.write( str(current.f) + " " + str(current.g) + " " + str(current.h) + " ") path.write(self.matrixToString(current.matrix) + "\n") if str(current.matrix) == self.expectedResult: return current for i in range(0, self.size): for j in range(0, self.size): newMatrix = self.newBoard(i, j, current.matrix) depth = current.depth + 1 serialMatrix = str(newMatrix) n = Node(parent=current, matrix=newMatrix, move=chr(i + 65) + str(j), depth=depth) n.exploratoryDepth = 0 n.h = self.getMinHWithExploratorySearch( n, self.maxExploratoryDepth) n.g = self.getG(n) n.f = self.getF(n) if serialMatrix in self.seen: if self.seen[serialMatrix].f > n.f: self.seen[ serialMatrix].parentNode = n.parentNode self.seen[serialMatrix].f = n.f self.seen[serialMatrix].g = n.g self.seen[serialMatrix].h = n.h self.seen[serialMatrix].depth = depth else: heapq.heappush(h, n) self.seen[serialMatrix] = n
def load_categories(self): by_category_node = Node("By Category", self) data = grab_json(API_URL + "/categories") categories = data["categories"] for category_data in categories: category_title = category_data["title"] category_title = string.capwords(category_title) category_href = category_data["href"] IviewIndexNode(category_title, by_category_node, API_URL + "/" + category_href)
def load_categories(self): by_category_node = Node("By Category", self) def category(name, slug): IviewIndexNode(name, by_category_node, API_URL + "/category/" + slug) category("Arts & Culture", "arts") category("Comedy", "comedy") category("Documentary", "docs") category("Drama", "drama") category("Education", "education") category("Lifestyle", "lifestyle") category("News & Current Affairs", "news") category("Panel & Discussion", "panel") category("Sport", "sport")
def keygen_and_setup(): """Sets up TREEHASH, RETAIN and AUTH for the start of BDS traversal.""" for h in range(H - K): TREEHASH[h] = Treehash(h, completed=True) stack = [] for j in range(1 << H): node1 = Node(h=0, v=leafcalc(j)) if node1.h < H - K and j == 3: TREEHASH[0].node = node1 while stack and stack[-1].h == node1.h: if j >> node1.h == 1: AUTH[node1.h] = node1 else: # in this case node1 is a right-node with row-index 2j + 3 if node1.h < H - K and j >> node1.h == 3: TREEHASH[node1.h].node = node1 elif node1.h >= H - K: offset = (1 << (H - 1 - node1.h)) + node1.h - H rowidx = ((j >> node1.h) - 3) >> 1 RETAIN[offset + rowidx] = node1 node2 = stack.pop() node1 = Node(h=node1.h + 1, v=g(node2.v + node1.v)) stack.append(node1) return stack.pop()
def __findleaf__(self, value, node): self.cts[node] += 1 if (node.lchild and node.rchild): if (self.cts[node.lchild] < self.cts[node.rchild]): self.__findleaf__(value, node.lchild) else: self.__findleaf__(value, node.rchild) else: newnode = Node(value) self.cts[newnode] = 1 if (not node.lchild): node.left(newnode) else: node.right(newnode) newnode.parent = node self.__bubble__(newnode)
def add_element(self, element, index=-1): # 实例化元素node element_node = Node(element) if not self.head: # 空链表添加 # 执行插入逻辑 self.head, self.tail = element_node, element_node else: if -1 == index: # 末尾添加 if self.tail: # 执行插入逻辑 self.tail.next_node, self.tail = element_node, element_node else: self.get_end_node() # 执行插入逻辑 self.tail.next_node, self.tail = element_node, element_node pass pass else: # 指定位置添加 end_index = self.size - 1 if end_index < index: # index位置 > 链表长度, 执行末尾添加 self.add_element(element=element) pass elif end_index == index: # 末尾之前添加node pre_node, end_node = self.get_end_node() # 执行插入逻辑 pre_node.next_node, element_node.next_node = element_node, end_node self.tail = end_node pass else: idx = 0 pre_node, temp_node = self.head, self.head while (idx + 1) != index: # fixme temp_node = temp_node.next_node idx += 1 pass # 执行插入逻辑 element_node.next_node, temp_node.next_node = temp_node.next_node, element_node pass pass pass pass self.size += 1 return self
def convert(self, operation, node_def): """Function to create Node object representing given operation. Creates a new Node object to represent the given operation. Args: operation (GraphDef operation object) : The operation to create a node for, is None in case of constructing nodes for functions. node_def : The node_def of the operation/function. Returns: The created Node object instance representing the operation. """ node = Node.Node(label=node_def.name, operator_type=node_def.op, value=None) node = self._attr_from_node_def(operation, node_def, node) return node
def run(self, path): stack = deque() maxDepth = 0 stack.append(self.startNode) levelSeen = {} while stack: current = stack.pop() if (current.depth <= self.maxDepth): path.write("0 0 0 ") path.write(self.matrixToString(current.matrix) + "\n") if str(current.matrix) == self.expectedResult: return current priority = [] for i in range(0, self.size): for j in range(0, self.size): newMatrix = self.newBoard(i, j, current.matrix) depth = current.depth + 1 serialMatrix = str(newMatrix) n = Node(parent=current, matrix=newMatrix, move=chr(i + 65) + str(j), depth=depth) if serialMatrix in self.seen: if self.seen[serialMatrix] > depth: self.seen[serialMatrix] = depth priority.append(n) else: priority.append(n) self.seen[serialMatrix] = depth priority.sort(key=lambda x: x.matrix, reverse=True) stack.extend(priority)
def load_channels(self): by_channel_node = Node("By Channel", self) data = grab_json(API_URL + "/channel") channels = data["channels"] for channel_data in channels: channel_id = channel_data["categoryID"] channel_title = { "abc1": "ABC1", "abc2": "ABC2", "abc3": "ABC3", "abc4kids": "ABC4Kids", "news": "News", "abcarts": "ABC Arts", }.get(channel_id, channel_data["title"]) channel_href = channel_data["href"] IviewIndexNode(channel_title, by_channel_node, API_URL + "/" + channel_href)