def test_persistent_hashtrie(): space = Space() t = PersistentHashTrie(space) t1 = t.assoc(String("a"), String("b")) assert len(t1.keys()) == 1 assert space.eq(t1.keys()[0], String("a")) assert space.eq(t1.get_item(String("a")), String("b")) t2 = t1.assoc(String("b"), String("c")) assert len(t2.keys()) == 2
def test_persistent_hashtrie_with_many(): space = Space() t = PersistentHashTrie(space) items = [String("foo%d" % i) for i in xrange(200)] for item in items: t = t.assoc(item, item) assert len(t.keys()) == len(items) for item in items: assert space.eq(t.get_item(item), item) for item in items: t = t.assoc(item, item) assert space.eq(t.get_item(item), item) for item in items: assert space.eq(t.get_item(item), item) assert len(t.keys()) == len(items)
def skip_test_persistent_hashtrie_collisions(): space = Space() t0 = PersistentHashTrie(space) # two different objects with same hash a = HashAndEqSimulator(100, 1) b = HashAndEqSimulator(100, 2) t1 = t0.assoc(a, String("a")) t = t1.assoc(b, String("b")) assert space.eq(t.get_item(a), String("a")) assert space.eq(t.get_item(b), String("b")) assert len(t.keys()) == 2
class MutableDict(Root): ''' Mutable dictionary that should be based on PyPy's implementation, but I can't get it accepted by translator. So, for now, it's based on persistent hash trie. ''' def __init__(self, space): self.container = PersistentHashTrie(space) def repr(self): return 'MutableDict{%s}' % ', '.join([ '%s: %s' % (k.repr(), self.container.get_item(k).repr()) for k in self.container.keys() ]) def set_item(self, key, val): self.container = self.container.assoc(key, val) def get_item(self, key): return self.container.get_item(key)