Пример #1
0
 def __init__(self, memory, points):
     self.memory = memory
     self.points = sorted(points, key=x_coord)
     self.nodes = [XLinkedListNode(memory, NodeItem(*p)) \
                   for p in self.points]
     self.link_nodes()
     self.memory.add_array_to_disk(self.nodes)
Пример #2
0
 def __init__(self, memory, points):
     self.memory = memory
     self.points = points
     self.nodes = [
         Node(self.memory, NodeItem(point)) for point in self.points
     ]
     self.memory.add_array_to_disk(self.nodes)
Пример #3
0
 def test_fast_LCA(self, data_at_leaves=True):
     points = [(i, i) for i in range(1, 17)]
     node_items = [NodeItem(key=x, data=y) for x, y in points]
     tree = VEBTree(self.memory, node_items, VEBNode, data_at_leaves)
     self.assertEqual(
         tree.fast_LCA(1, 16).point(),
         tree.predecessor(9).point())
     self.assertEqual(
         tree.fast_LCA(1, 1).point(),
         tree.predecessor(1).point())
Пример #4
0
    def __init__(self, memory, points, y_upper_bound=True):
        assert len(points) > 0
        self.memory = memory
        self.y_upper_bound = y_upper_bound

        self.points = sorted(points, key=x_coord)
        node_items = [NodeItem(x, y) for x, y in points]
        self.xveb = VEB3Sided(memory, node_items)

        self.link_nodes_to_2Sided()
Пример #5
0
    def __init__(self, memory, points):
        assert len(points) > 0

        self.memory = memory

        self.points = sorted(points, key=y_coord)
        node_items = [NodeItem(y, x) for x, y in points]
        self.yveb = VEB4Sided(memory, node_items)

        self.link_nodes_to_3Sided()
Пример #6
0
    def __init__(self, memory, points, veb_order=True):
        self.memory = memory
        self.points = points
        self.veb_order = veb_order

        node_items = [NodeItem(y, x) for x, y in points]
        self.ybst = VEBTree(memory,
                            node_items,
                            RangeTreeNode,
                            data_at_leaves=True,
                            veb_order=veb_order)
        self.link_nodes_to_XBST()
Пример #7
0
    def verify_veb_order(self, points, data_at_leaves, printout):
        node_items = [NodeItem(key=y, data=x) for x, y in points]
        tree = VEBTree(self.memory, node_items, VEBNode, data_at_leaves)
        nodes = list(tree.veb_ordered_nodes)
        for i in range(len(nodes)):
            if printout:
                print(str(nodes[i]) + ', ' + str(nodes[i]._depth))

            # checks local structure besides for root and leaves
            if not nodes[i].is_root() and not nodes[i].is_leaf():
                self.assertTrue(nodes[i - 1] is nodes[i].parent
                                or nodes[i - 2] is nodes[i].parent
                                or nodes[i + 1] is nodes[i].left)
        if printout:
            print("Test Passed!")
Пример #8
0
    def __init__(self, memory, points, veb_order=True):
        # if veb_order is True, store points in veb order
        # else store in sorted order

        self.memory = memory
        self.points = sorted(points, key=x_coord)
        self.veb_order = veb_order
        node_items = [NodeItem(x, y) for x, y in self.points]
        self.tree = VEBTree(self.memory,
                            node_items,
                            XBSTNode,
                            data_at_leaves=False,
                            veb_order=veb_order)
        self.linked_list = XLinkedList(memory, points)
        self.link_tree_to_linked_list()
Пример #9
0
    def verify_successor(self, points, data_at_leaves, printout):
        node_items = [NodeItem(key=y, data=x) for x, y in points]
        tree = VEBTree(self.memory, node_items, VEBNode, data_at_leaves)
        nodes = sorted(tree.veb_ordered_nodes, key=lambda z: z.key)
        epsilon = 1e-5
        if printout:
            for i in range(len(nodes)):
                print(nodes[i])

        for i in range(len(nodes)):
            access_node = tree.successor(nodes[i].key)
            self.assertEqual(nodes[i].key, access_node.key)

            curr_node = tree.successor(nodes[i].key - epsilon)
            self.assertEqual(nodes[i].key, curr_node.key)

        self.assertEqual(tree.successor(nodes[-1].key + epsilon), None)
        if printout:
            print("Test Passed!")
Пример #10
0
    def __init__(self, memory, points, x_upper_bound=True, y_upper_bound=True):
        assert len(points) > 0

        self.memory = memory
        self.x_upper_bound = x_upper_bound
        self.y_upper_bound = y_upper_bound

        # Points are stored in yveb sorted by y coordinate
        self.points = sorted(points, key=y_coord)
        node_items = [NodeItem(y, x) for x, y in points]
        self.yveb = VEB2Sided(self.memory, node_items)

        # Construct the xarray, must pass in pre-sorted (by y) points
        self.alpha = 2
        self.base_case_length = 10
        self.xarray = XArray(self.memory, self.points, self.alpha, \
                self.base_case_length, self.x_upper_bound, self.y_upper_bound)

        # initialize xarray_index field in each Node2Sided object
        self.link_nodes_to_xarray()
Пример #11
0
    def verify_BST(self, points, data_at_leaves, printout):
        node_items = [NodeItem(key=y, data=x) for x, y in points]
        tree = VEBTree(self.memory, node_items, VEBNode, data_at_leaves)
        frontier = [tree.root]
        max_depth = 0
        # runs BFS and check invariant holds at every node
        for node in frontier:
            if printout:
                print(str(node) + ', ' + str(node._depth))

            if node.left is not None:
                frontier.append(node.left)
                self.assertTrue(node.left.key <= node.key)
                self.assertEqual(node.left._depth, node._depth + 1)
            if node.right is not None:
                frontier.append(node.right)
                self.assertTrue(node.right.key >= node.key)
                self.assertEqual(node.right._depth, node._depth + 1)
            max_depth = max(max_depth, node._depth)
        if printout:
            print("Test Passed!")
Пример #12
0
 def copy(self):
     return type(self)(self.memory, NodeItem(self.key, self.data))
Пример #13
0
    def __init__(self,
                 memory,
                 points,
                 alpha=2,
                 base_case_length=10,
                 x_upper_bound=True,
                 y_upper_bound=True):
        # print("inside: x_upper_bound: ", x_upper_bound)
        # print("inside: y_upper_bound: ", y_upper_bound)

        xarray_points = []
        self.y_to_xarray_chunk_map = dict()
        self.alpha = alpha
        self.memory = memory
        self.x_upper_bound = x_upper_bound
        self.y_upper_bound = y_upper_bound

        # Sort yvals from largest to smallest
        all_yvals = [p[1] for p in points]
        if y_upper_bound:
            all_yvals.reverse()

        y_sorted_points = points
        # this only needs to be done for x_upper_bound = False
        # and y_upper_bound = True
        if not x_upper_bound and y_upper_bound:
            y_sorted_points.reverse()

        # Intialize S_0
        i = 0
        S_i = sorted(y_sorted_points, key=xcoord)
        if not x_upper_bound:
            S_i.reverse()
        start_i = 0
        xarr_start_i = 0
        base_case_termination = False
        bad_points_thrown_out = 0

        for j in range(len(all_yvals)):
            # if j % 500 == 0:
            # print('preprocessed %s points' % (j))
            y = all_yvals[j]
            # The following line only works if S_i is sorted in x in the correct direction
            # It does not take x_upper_bound as a value
            x = self.is_sparse_x_value(y, S_i, y_upper_bound,
                                       j - bad_points_thrown_out)

            if x:
                i += 1
                x_i, y_i = x, y

                # map all y values strictly greater than y_i to xarr_start_i
                # the index of the first element of P_i
                for k in range(start_i, j):
                    self.y_to_xarray_chunk_map[all_yvals[k]] = xarr_start_i
                    start_i = j

                # In next line, S_i refers to S_{i-1} because it has not been updated yet
                if x_upper_bound:
                    p_i_minus_1 = [s for s in S_i if s[0] <= x_i]
                else:
                    p_i_minus_1 = [s for s in S_i if s[0] >= x_i]
                xarray_points = xarray_points + p_i_minus_1
                xarr_start_i += len(p_i_minus_1)

                prev_size = len(S_i)
                # Update S_i from S_{i-1}
                if y_upper_bound:
                    if x_upper_bound:
                        S_i = [s for s in S_i if s[0] > x_i or s[1] <= y_i]
                    else:
                        S_i = [s for s in S_i if s[0] < x_i or s[1] <= y_i]
                else:
                    if x_upper_bound:
                        S_i = [s for s in S_i if s[0] > x_i or s[1] >= y_i]
                    else:
                        S_i = [s for s in S_i if s[0] < x_i or s[1] >= y_i]
                bad_points_thrown_out += prev_size - len(S_i)

            # Base case. Map all remaining elements to the last chunk, S_i
            if len(S_i) < base_case_length:
                # print('BASE CASE!')
                base_case_termination = True
                for k in range(start_i, len(all_yvals)):
                    self.y_to_xarray_chunk_map[all_yvals[k]] = xarr_start_i
                xarray_points = xarray_points + S_i
                break

        # It is also possible that the final block is larger than the
        # base case and is also dense.
        if not base_case_termination:
            # print("Final block is dense!")
            for k in range(start_i, len(all_yvals)):
                self.y_to_xarray_chunk_map[all_yvals[k]] = xarr_start_i
            xarray_points = xarray_points + S_i

        # Add to memory
        self.xarray = [
            Node(self.memory, NodeItem(xarr_point))
            for xarr_point in xarray_points
        ]
        self.memory.add_array_to_disk(self.xarray)