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 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)
def test_hashtrie_like_mutable_with_many(): space = Space() t = PersistentHashTrie(space) items = [String("foo%d" % i) for i in xrange(200)] for item in items: t._set_item(item, item) for item in items: assert space.eq(t.get_item(item), item) for item in items: t._set_item(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 __init__(self, space): self.container = PersistentHashTrie(space)
def test_hashtrie_like_mutable(): space = Space() t = PersistentHashTrie(space) t._set_item(String("foobar"), String("foobarval")) t._set_item(String("foobar2"), String("foobarval2")) assert space.eq(t.get_item(String("foobar")), String("foobarval")) assert space.eq(t.get_item(String("foobar2")), String("foobarval2")) assert len(t.keys()) == 2 assert space.eq(t.keys()[0], String("foobar")) or space.eq(t.keys()[1], String("foobar")) assert space.eq(t.keys()[0], String("foobar2")) or space.eq(t.keys()[1], String("foobar2"))