def testins_bad(self): t = avl.new([(5, 6), (2, 8), (7, -1), (-1, 1), (2, -3)], compare=pair_compare) for i in range(5): self.assertRaises(TypeError, t.insert, 66) self.assertRaises(TypeError, t.has_key, 66) self.failIf(66 in t) t.insert((3, 3)) self.assert_(t.verify() == 1)
def testnew_compare_unique(self): random.shuffle(self.list) tuples = [(i, 0) for i in self.list] tuples3 = tuples * 3 for i in range(self._times): t = avl.new(tuples3, compare=pair_compare, unique=1) self.assert_(verify_len(t, self.size)) for u in tuples: self.assert_(u in t)
def testnew_basic_unique(self): for i in range(self._times): random.shuffle(self.list) list3 = self.list * 3 t = avl.new(list3, unique=1) self.assert_(verify_len(t, self.size)) for n in self.list: self.assert_(n in t) t = None
def testnew_compare_dupes(self): random.shuffle(self.list) pairs = [(i, 0) for i in self.list] pairs3 = pairs * 3 size3 = self.size * 3 for i in range(self._times): t = avl.new(pairs3, compare=pair_compare, unique=0) self.assert_(verify_len(t, size3)) for p in pairs: self.assert_(p in t)
def testdump_compare(self): t = avl.new(self.list1, compare=revlowercmp) for proto in [0, 2]: f = StringIO.StringIO() p = pickle.Pickler(f, proto) t.dump(p) f.seek(0) p = pickle.Unpickler(f) u = avl.load(p) self.assert_(u.verify() == 1) self.assert_(equal_tree(t, u)) f.close()
def testseq_spaninsert(self): print 'please wait ...' n = 5000 t = avl.new(compare=lambda x, y: cmp(y, x)) for i in range(2): for i in gen_ints(0, 3 * n): e = random.randint(-n, n) a1, a2 = t.span(e) t.insert(e, a2) self.assert_(t.span(e) == (a1, a2 + 1)) self.assert_(verify_len(t, 3 * n)) t.clear()
def testseq_sliceconcat(self): print 'please wait ...' n = 2000 e = avl.new() t = range_tree(0, n) self.assert_(verify_empty(e + e)) self.assert_(equal_tree(t + e, t)) self.assert_(equal_tree(e + t, t)) for a in gen_ints_perm(50, n): u = t[:a] + t[a:] self.assert_(u.verify() == 1) self.assert_(equal_tree(t, u)) u = None
def testseq_concatinplace(self): self.assertRaises(TypeError, avl.new().concat, []) n = 2000 e = avl.new() t = range_tree(0, n) u = t[:] u.concat(e) self.assert_(equal_tree(t, u)) e.concat(u) self.assert_(equal_tree(e, u)) u += avl.new() self.assert_(equal_tree(t, u)) e.clear() e += t self.assert_(equal_tree(e, t)) e.clear() for a in gen_ints_perm(100, n): u = t[:a] u += t[a:] self.assert_(u.verify() == 1) self.assert_(equal_tree(t, u)) u = None
def testseq_insert(self): n = 5000 t = avl.new() self.assertRaises(IndexError, t.insert, 'out', 1) self.assertRaises(IndexError, t.insert, 'out', -13) for i in gen_ints_perm(373, n): t.insert(2 * i) self.assert_(t.verify() == 1) for i in range(3): u = avl.new(t) for k in gen_ints(0, n): odd = 2 * k + 1 u.insert(odd, odd) self.assert_(u[odd] == odd) self.assert_(u.verify() == 1) self.assert_(len(u) == 2 * n) for i in range(100): u.append(2 * n) for i in range(100): u.insert(0, 0) self.assert_(u.verify() == 1) list = None
def testseq_index(self): t = avl.new() n = 5000 for r in range(3): for i in range(3): for k in gen_ints_perm(11, n): t.insert(k) self.assert_(verify_len(t, n * 3)) for i in gen_ints(0, n): self.assert_(t.index(i) == i * 3) a, b = t.span(i) self.assert_((a == i * 3) and (b - a == 3)) t.clear()
def __init__(self, dimension): self.__dimension = dimension self.__nb_value = 0 self.__virtual = list() # [ (space_part, data)? ] # the items in the AVL are lists with [value, nb_before_value, [ (space_part, data)? ] ]? self.__constrained = avl.new(compare=CompCounter.__comparator ) self.__changed = True self.__cut_value = None self.__data_left = list() self.__data_right = list()
def testdel_basic(self): t = avl.new() t.remove(1) t.remove(-2) self.assert_(verify_empty(t)) t = range_tree(-2000, +2000) self.assert_(t.verify() == 1) n = len(t) # no-op others = range(-2100, -2000) + range(2000, 2100) random.shuffle(others) for i in others: t.remove(i) self.assert_(verify_len(t, n)) others = None # empty trees list = range(-2000, 2000) for i in range(10): random.shuffle(list) u = avl.new(t) for k in list: u.remove(k) self.failIf(k in u) self.assert_(verify_empty(u))
def testseq_basic(self): t = avl.new() for step, modulo in [(491, 2000), (313, 10000)]: for i in gen_ints_perm(step, modulo): t.insert(i) for i in gen_ints(0, modulo): self.assert_(t.index(i) == i) self.assert_(t[i] == i) self.assert_(t[-i - 1] == t[modulo - i - 1]) for i in gen_ints(-100, 0): self.assert_(t.index(i) == -1) for i in gen_ints(modulo, modulo + 100): self.assert_(t.index(i) == -1) t.clear() self.assertRaises(IndexError, self.geti, t, 11) self.assertRaises(IndexError, self.geti, t, -5)
def testiter_remove(self): print 'please wait ...' for start in range(1, self.n + 1): u = avl.new(self.t) self.assert_(u.verify() == 1) j = iter(u) for i in range(start): j.next() index = j.index() self.assert_(index == start - 1) while index < len(u): j.remove() #self.assert_(u.verify() == 1) self.assert_(j.index() == index) self.assertRaises(avl.Error, j.remove) self.assert_(u.verify() == 1)
def testseq_removedupes(self): n = 2500 steps = [501, 7, 1003, 1863] repeats = len(steps) t = avl.new() for step in steps: for k in gen_ints_perm(step, n): t.insert(k) for k in gen_ints_perm(1209, n): a, b = t.span(k) self.assert_(b - a == repeats) self.assert_(a == 0 or t[a - 1] < k) self.assert_(b == len(t) or t[b] > k) for i in xrange(repeats): t.remove_at(a) self.assert_(t.span(k) == (a, a)) self.assert_(verify_empty(t))
def testiter_basic(self): t = avl.new() j = iter(t) # before first k = t.iter(1) # after last self.assertRaises(StopIteration, j.next) self.assertRaises(StopIteration, j.prev) self.assertRaises(StopIteration, k.next) self.assertRaises(StopIteration, k.prev) t.insert('bb') self.assertRaises(StopIteration, j.prev) self.assertRaises(StopIteration, k.next) self.assert_(j.next() == 'bb') self.assert_(k.prev() == 'bb') self.assertRaises(StopIteration, j.next) self.assertRaises(StopIteration, k.prev) self.assert_(j.prev() == 'bb') self.assert_(k.next() == 'bb') self.assert_(j.cur() == 'bb' and k.cur() == 'bb') t.insert('aa') self.assert_(j.prev() == 'aa') t.insert('cc') self.assert_(k.next() == 'cc')
def testdump_basic(self): t = avl.new(self.list1) for proto in [0, 2]: f = StringIO.StringIO() p = pickle.Pickler(f, proto) t.dump(p) f.seek(0) p = pickle.Unpickler(f) a = avl.load(p) self.assert_(a.verify() == 1) self.assert_(equal_tree(a, t)) f.close() t = random_int_tree(0, 3000, 7000) for m in [pickle, cPickle]: for proto in [0, 2]: f = cStringIO.StringIO() p = m.Pickler(f, proto) avl.dump(t, p) f.seek(0) p = pickle.Unpickler(f) a = avl.load(p) self.assert_(a.verify() == 1) self.assert_(equal_tree(a, t)) f.close()
def __init__(self, process): self.process = process self.tree = avl.new()
def __init__(self, dlldir): self.dlldir = dlldir self.tree = avl.new() self.known_libs = {}
start_tree.delete(annotation) if sorted(annotations_list, key=lambda x: x.start) != list(start_tree): print "Expected:".ljust(10), sorted(annotations_list, key=lambda x: x.start) print "Got:".ljust(10), list(start_tree) print "Failed to remove:", annotation import ipdb ipdb.set_trace() # print "New list:", sorted(annotations_list, key=lambda x: x.start) # print "New tree:", list(start_tree) # start_tree.print_colours() ordered_list = [a.start for a in start_tree] avl_start_tree = avl.new(source=annotations_list, compare=compare_start) print "Searching in list", ordered_list # import ipdb; ipdb.set_trace() iteration_counts = [] for i in range(100): left = random.randint(0, 100) right = random.randint(0, 100) list_slice = [v for v in ordered_list if v >= left and v <= right] tree_slice = [ a.start for a in start_tree[Index(start=left):Index(start=right)] ] iteration_counts.append(start_tree.iteration_counter) lower, upper = avl_start_tree.span(Index(start=left),
if not start_tree.sanity_test(): print("Tree is insane prior to copying") old_start_tree = SliceableTree(start_tree, compare_start) start_tree.delete(annotation) if sorted(annotations_list, key=lambda x: x.start) != list(start_tree): print("Expected:".ljust(10), sorted(annotations_list, key=lambda x: x.start)) print("Got:".ljust(10), list(start_tree)) print("Failed to remove:", annotation) import ipdb; ipdb.set_trace() # print "New list:", sorted(annotations_list, key=lambda x: x.start) # print "New tree:", list(start_tree) # start_tree.print_colours() ordered_list = [a.start for a in start_tree] avl_start_tree = avl.new(source = annotations_list, compare = compare_start) print("Searching in list", ordered_list) # import ipdb; ipdb.set_trace() iteration_counts = [] for i in range(100): left = random.randint(0, 100) right = random.randint(0, 100) list_slice = [v for v in ordered_list if v >= left and v <= right] tree_slice = [a.start for a in start_tree[Index(start = left):Index(start = right)]] iteration_counts.append(start_tree.iteration_counter) lower, upper = avl_start_tree.span(Index(start = left), Index(start = right)) avl_slice = [a.start for a in avl_start_tree[lower:upper]] if list_slice != tree_slice and left < right: # My naive slicing here doesn't work with reversed indices
def testiter_forloop(self): list = self.orig[:] for i in range(5): random.shuffle(list) for k in avl.new(list): self.assert_(k == self.orig[k])
def testfrom_iter_compare(self): a = self.list1 + map(lambda s: string.lower(s), self.list1) t = avl.new(a, unique=0, compare=revlowercmp) u = avl.from_iter(iter(t), len(t), compare=revlowercmp) self.assert_(u.verify() == 1)
def random_pair_tree(lo, hi, size=1000, compare=None): t = avl.new(compare=compare) for i in gen_ints(0, size): t.insert((random.randint(lo, hi - 1), 0)) return t
def range_tree(lo, hi, compare=None): t = avl.new(compare=compare) for i in gen_ints(lo, hi): t.insert(i) return t
def __init__(self, values=[], key=None): self._set_key(key) self._avl = avl.new(values, self._cmp)