Commit 7279ac1b authored by Dmitry Vasiliev's avatar Dmitry Vasiliev

Optimization: use iterators where possible

parent 399c6ba3
...@@ -48,7 +48,7 @@ def num2str(n): ...@@ -48,7 +48,7 @@ def num2str(n):
def str2num(s): def str2num(s):
return struct.unpack(">Q", "\000\000" + s)[0] return struct.unpack(">Q", "\000\000" + s)[0]
class fsIndex: class fsIndex(object):
def __init__(self): def __init__(self):
self._data = {} self._data = {}
...@@ -76,7 +76,7 @@ class fsIndex: ...@@ -76,7 +76,7 @@ class fsIndex:
def __len__(self): def __len__(self):
r = 0 r = 0
for tree in self._data.values(): for tree in self._data.itervalues():
r += len(tree) r += len(tree)
return r return r
...@@ -85,7 +85,7 @@ class fsIndex: ...@@ -85,7 +85,7 @@ class fsIndex:
self[k] = v self[k] = v
def has_key(self, key): def has_key(self, key):
v=self.get(key, self) v = self.get(key, self)
return v is not self return v is not self
def __contains__(self, key): def __contains__(self, key):
...@@ -101,27 +101,27 @@ class fsIndex: ...@@ -101,27 +101,27 @@ class fsIndex:
self._data.clear() self._data.clear()
def __iter__(self): def __iter__(self):
for prefix, tree in self._data.items(): for prefix, tree in self._data.iteritems():
for suffix in tree: for suffix in tree:
yield prefix + suffix yield prefix + suffix
iterkeys = __iter__
def keys(self): def keys(self):
r = [] return list(self.iterkeys())
for prefix, tree in self._data.items():
for suffix in tree.keys(): def iteritems(self):
r.append(prefix + suffix) for prefix, tree in self._data.iteritems():
return r for suffix, value in tree.iteritems():
yield (prefix + suffix, str2num(value))
def items(self): def items(self):
r = [] return list(self.iteritems())
for prefix, tree in self._data.items():
for suffix, v in tree.items(): def itervalues(self):
r.append(((prefix + suffix), str2num(v))) for tree in self._data.itervalues():
return r for value in tree.itervalues():
yield str2num(value)
def values(self): def values(self):
r = [] return list(self.itervalues())
for prefix, tree in self._data.items():
for v in tree.values():
r.append(str2num(v))
return r
...@@ -18,11 +18,14 @@ from ZODB.utils import p64 ...@@ -18,11 +18,14 @@ from ZODB.utils import p64
class Test(unittest.TestCase): class Test(unittest.TestCase):
def testInserts(self): def setUp(self):
index=fsIndex() self.index = fsIndex()
for i in range(200): for i in range(200):
index[p64(i*1000)]=(i*1000L+1) self.index[p64(i * 1000)] = (i * 1000L + 1)
def testInserts(self):
index = self.index
for i in range(0,200): for i in range(0,200):
self.assertEqual((i,index[p64(i*1000)]), (i,(i*1000L+1))) self.assertEqual((i,index[p64(i*1000)]), (i,(i*1000L+1)))
...@@ -40,7 +43,7 @@ class Test(unittest.TestCase): ...@@ -40,7 +43,7 @@ class Test(unittest.TestCase):
# self.failUnless(len(index._data) > 1) # self.failUnless(len(index._data) > 1)
def testUpdate(self): def testUpdate(self):
index=fsIndex() index = self.index
d={} d={}
for i in range(200): for i in range(200):
...@@ -63,6 +66,51 @@ class Test(unittest.TestCase): ...@@ -63,6 +66,51 @@ class Test(unittest.TestCase):
self.assertEqual(index.get(p64(399000)), 399002) self.assertEqual(index.get(p64(399000)), 399002)
self.assertEqual(len(index), 600) self.assertEqual(len(index), 600)
def testKeys(self):
keys = list(iter(self.index))
keys.sort()
for i, k in enumerate(keys):
self.assertEqual(k, p64(i * 1000))
keys = list(self.index.iterkeys())
keys.sort()
for i, k in enumerate(keys):
self.assertEqual(k, p64(i * 1000))
keys = self.index.keys()
keys.sort()
for i, k in enumerate(keys):
self.assertEqual(k, p64(i * 1000))
def testValues(self):
values = list(self.index.itervalues())
values.sort()
for i, v in enumerate(values):
self.assertEqual(v, (i * 1000L + 1))
values = self.index.values()
values.sort()
for i, v in enumerate(values):
self.assertEqual(v, (i * 1000L + 1))
def testItems(self):
items = list(self.index.iteritems())
items.sort()
for i, item in enumerate(items):
self.assertEqual(item, (p64(i * 1000), (i * 1000L + 1)))
items = self.index.items()
items.sort()
for i, item in enumerate(items):
self.assertEqual(item, (p64(i * 1000), (i * 1000L + 1)))
def test_suite(): def test_suite():
loader=unittest.TestLoader() loader=unittest.TestLoader()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment