Ejemplo n.º 1
0
    def read_from_file(self, file):
        # lead_str = file[1]
        # parse manually:
        ignored = [
            '\n',
            'node [shape=record];\n',
            '}\n',
        ]

        edgestrings = []
        for line in file:
            if self.lead_str is '':
                self.lead_str = line
                continue
            if line in ignored:
                continue
            if '->' in line:
                edgestrings.append(line)
            else:
                node = Node.new_from_string(line)
                if node.ID in self.nodes:
                    print(node.ID)
                    print(self.nodes[node.ID].ID)
                    print('ERROR: Node ID\'s are not unique!:', str(node),
                          str(self.nodes[node.ID]))
                    exit(-1)
                self.nodes[node.ID] = node

        for string in edgestrings:
            edge = Edge.new_from_string(string, self.nodes)
            self.edges.append(edge)

        return
Ejemplo n.º 2
0
 def create_node(self, type_string):
     node = Node(type_string, ID=None)
     if node.ID in self.nodes:
         print('ERROR: Node ID\'s are not unique!:', str(node),
               str(self.nodes[node.ID]))
         exit(-1)
     self.nodes[node.ID] = node
     return node
Ejemplo n.º 3
0
 def sortlist(self, data):
     newnode = Node(data)
     if self.head is None:
         self.head = newnode
     else:
         tempnode = self.head
         if tempnode.data > data:
             newnode.next = tempnode
             self.head = newnode
         else:
             prevnode = self.head
             while tempnode is not None:
                 if tempnode.data > data:
                     newnode.next = prevnode.next
                     prevnode.next = newnode
                     return
                 prevnode = tempnode
                 tempnode = tempnode.next
             prevnode.next = newnode
Ejemplo n.º 4
0
    def addnode(self, data):

        newnode = Node(data)  # Create NewNode Of Type Class Node
        if self.head is None:  # List Is Empty Then Add To Start
            self.head = newnode
        else:  # Traverse Till The End
            tempnode = self.head
            while tempnode.next is not None:
                tempnode = tempnode.next
            tempnode.next = newnode  # Add Newnode At The End Of The List
Ejemplo n.º 5
0
    def add(self, obj):
        newNode = Node(obj)

        if self.head is None:
            self.head = newNode
        else:
            tempNode = self.head
            while tempNode.next is not None:
                tempNode = tempNode.next
            tempNode.next = newNode
Ejemplo n.º 6
0
 def sort(self, obj):
     newNode = Node(obj)
     if self.head is None:
         self.head = newNode
     else:
         tempNode = self.head
         if tempNode.object > obj:
             newNode.next = tempNode
             self.head = newNode
         else:
             previous = self.head
             while tempNode is not None:
                 if tempNode.object > obj:
                     newNode.next = previous.next
                     previous.next = newNode
                     return
                 previous = tempNode
                 tempNode = tempNode.next
             previous.next = newNode
Ejemplo n.º 7
0
 def insert_FF(self, edge):
     info = {
         "type_string": "ff",
         "width": edge.width,
         "ID": None,
     }
     FF = Node(**info)
     self.nodes[FF.ID] = FF
     self.create_edge(edge.tail, FF, edge.width, edge.tail_pos, None, None)
     self.create_edge(FF, edge.head, edge.width, None, edge.head_pos, None)
     self.remove_edge(edge)
     return FF
Ejemplo n.º 8
0
    def rebuild_tree(self, root):
        if root.left is None:  # root does not have left child, so to find one.
            if self.end:
                return
            if root.code is None:
                root.left = Node(code='0')
            else:
                root.left = Node(code=root.code + '0')

            if self.code[0] == '1':  # leaf
                leaf = chr(int(''.join(self.code[1:9]), 2))
                self.code = self.code[9:]
                root.left.key = leaf

                self.cnt_leafs += 1
                if self.cnt_leafs == self.sum_leafs:
                    self.end = True
                self.rebuild_tree(root)
            else:  # inner node
                self.code = self.code[1:]
                self.rebuild_tree(root.left)
        if root.right is None:
            if self.end:
                return
            if root.code is None:
                root.right = Node(code='1')
            else:
                root.right = Node(code=root.code + '1')
            if self.code[0] == '1':
                leaf = chr(int(''.join(self.code[1:9]), 2))
                self.code = self.code[9:]
                root.right.key = leaf
                self.cnt_leafs += 1
                if self.cnt_leafs == self.sum_leafs:
                    self.end = True
                return
            else:
                self.code = self.code[1:]
                self.rebuild_tree(root.right)
Ejemplo n.º 9
0
 def __init__(self, src, dst):
     self.code = []
     self.nodes = {}
     self.src = src
     self.dst = dst
     self.output = []
     self.root = Node()
     self.sum_leafs = None
     self.cnt_leafs = 0
     self.end = False
     self.idx = 0
     # begin
     self.process()
Ejemplo n.º 10
0
    def create_copy(self):
        graph = Graph()
        for ID, node in self.nodes.items():
            info = node.info
            # create a new node with same information
            graph.nodes[ID] = Node(node.type_string, ID, **info)

        # print(self.nodes, graph.nodes, self.nodes is graph.nodes)
        # return
        for edge in self.edges:
            tail = graph.nodes[edge.tail.ID]
            head = graph.nodes[edge.head.ID]
            new_edge = Edge(tail, head, edge.width, edge.tail_pos,
                            edge.head_pos)
            new_edge.reference_edge = edge
            graph.edges.append(new_edge)

        graph.lead_str = self.lead_str

        return graph
Ejemplo n.º 11
0
def merge(graph, nodepair, nodepair_list):
    a = nodepair.node_a
    b = nodepair.node_b
    # print('merging', str(a), str(b), len(nodepair.edges), nodepair.edge_weight(), nodepair.combined_size())
    nodes = graph.nodes.values()

    # TODO improve execution time by removing
    # node_pairs from worklist when merging
    if a not in nodes:
        # print("a does not exist, returning")
        return
    if b not in nodes:
        # print("b does not exist, returning")
        return

    if not a.in_edges and not b.out_edges:
        # do not merge input and output nodes
        return
    info = {
        'label': 'merge' + a.ID + b.ID,
        'operator_delay': nodepair.combined_size(),
    }
    type_string = 'mergednode'
    node = Node(type_string, ID=None, **info)

    out_nodes = []
    in_nodes = []

    for edge in a.in_edges[:]:
        if edge.tail is b:
            continue
        new_edge = graph.create_edge(edge.tail, node, edge.width,
                                     edge.tail_pos, None, None)
        new_edge.reference_edge = edge.reference_edge
        if edge.tail not in in_nodes:
            in_nodes.append(edge.tail)
        graph.remove_edge(edge)
    for edge in b.in_edges[:]:
        if edge.tail is a:
            continue
        new_edge = graph.create_edge(edge.tail, node, edge.width,
                                     edge.tail_pos, None, None)
        new_edge.reference_edge = edge.reference_edge
        if edge.tail not in in_nodes:
            in_nodes.append(edge.tail)
        graph.remove_edge(edge)

    for edge in a.out_edges[:]:
        if edge.head is b:
            continue
        new_edge = graph.create_edge(node, edge.head, edge.width, None,
                                     edge.head_pos, None)
        new_edge.reference_edge = edge.reference_edge
        if edge.head not in out_nodes:
            out_nodes.append(edge.head)
        graph.remove_edge(edge)

    for edge in b.out_edges[:]:
        if edge.head == a:
            continue
        new_edge = graph.create_edge(node, edge.head, edge.width, None,
                                     edge.head_pos, None)
        new_edge.reference_edge = edge.reference_edge
        if edge.head not in out_nodes:
            out_nodes.append(edge.head)
        graph.remove_edge(edge)

    for p in nodepair_list[:]:
        if p.node_a in [a, b] or p.node_b in [a, b]:
            nodepair_list.remove(p)

    for inp in in_nodes:
        if inp != node:
            nodepair_list.append(Node_Pair(inp, node))

    for out in out_nodes:
        if out != node:
            nodepair_list.append(Node_Pair(out, node))

    graph.remove_node(a)
    graph.remove_node(b)

    graph.nodes[node.ID] = node
Ejemplo n.º 12
0
 def addtostart(self, data):
     newnode = Node(data)
     newnode.next = self.head  # Point Newnode To Head Node
     self.head = newnode  # Assign Newnode As Head Node
Ejemplo n.º 13
0
def compress(src, dst):
    nodes = {}
    with open(src, 'r') as fr:
        text = fr.read()
        for c in text:
            if c in nodes:
                nodes[c].value += 1
            else:
                nodes[c] = Node(c, 1)
    print('nodes complete')
    # Add in psuedo-EOF marker symbol
    # EOF = chr(255) + chr(255)
    # nodes[EOF] = Node(EOF, 1)
    num = len(nodes)  # maximum is 255, so 8 bits is enough.
    print('num: {}'.format(num))
    num_code = bin(num)[2:]
    while len(num_code) != 8:
        num_code = '0' + num_code
    q = BinaryHeap()
    for node in nodes.values():
        q.insert(node)
    min1 = q.delete()
    min2 = q.delete()
    while min2:
        q.insert(min1 + min2)
        min1 = q.delete()
        min2 = q.delete()
    root = min1
    # print(root)
    result = ''
    for char in text:
        if char in nodes:
            result += nodes[char].code
        else:
            print('Char {} not found'.format(char))

            # result += node
    header = tree_serialization(root)
    result = num_code + header + result
    cnt = 0
    while len(result) % 8 != 0:
        cnt += 1
        result += '0'
    print('cnt: {}'.format(cnt))
    cnt_code = bin(cnt)[2:]
    while len(cnt_code) != 8:
        cnt_code = '0' + cnt_code
    result = cnt_code + result

    # with open('debug.txt', 'w') as f:
    #    f.write(result)
    hexGrp = []
    # while result:
    #     hexGrp.append(result[:8])
    #     result = result[8:]
    # Cost too much time.
    i = 0
    while i < len(result):
        hexGrp.append(result[i:i + 8])
        i += 8
    print('Generate hex_group done.')
    # print(hexGrp)
    output = bytearray([int(group, 2) for group in hexGrp])
    with open(dst, 'wb') as wbf:
        wbf.write(output)
Ejemplo n.º 14
0
def test_node():
    return Node()
Ejemplo n.º 15
0

def func_preorder(func, node):
    if node:
        func(node.value)
        func_preorder(func, node.left)
        func_preorder(func, node.right)


if __name__ == "__main__":
    import random

    def generate():
        return random.randint(1, 9)

    root = Node()
    root.add(4)
    root.add(2)
    root.add(6)
    root.add(1)
    root.add(3)
    root.add(5)
    root.add(7)

    # Good for sorting by value
    inorder = []
    print(func_inorder)
    func_inorder(inorder.append, root)
    print(inorder)

    # Good for removing leaves first
Ejemplo n.º 16
0
 def add_first(self, obj):
     newNode = Node()
     newNode.object = obj
     newNode.next = self.head
     self.head = newNode