Author: Stephan <[email protected]>
Branch:
Changeset: r115:316e192fad33
Date: 2011-09-01 19:55 +0200
http://bitbucket.org/pypy/lang-js/changeset/316e192fad33/
Log: added Map delname method
diff --git a/js/test/test_map.py b/js/test/test_map.py
--- a/js/test/test_map.py
+++ b/js/test/test_map.py
@@ -15,15 +15,43 @@
assert m.addname('foo') == 0
assert m.addname('bar') == 1
-
def test_indexof(self):
m = Map()
- m.indexes['foo'] = 0
- m.indexes['bar'] = 1
+ m.addname('foo')
+ m.addname('bar')
assert m.indexof('foo') == 0
assert m.indexof('bar') == 1
assert m.indexof('baz') == Map.NOT_FOUND
+ def test_delname(self):
+ m = Map()
+ m.addname('foo')
+ assert m.indexof('foo') is not None
+ m.delname('foo')
+ assert m.indexof('foo') == Map.NOT_FOUND
+
+ def test_delname_monotone(self):
+ m = Map()
+ m.addname('foo')
+ index_of_foo = m.indexof('foo')
+ assert index_of_foo is not None
+ m.delname('foo')
+ assert m.indexof('foo') == Map.NOT_FOUND
+ m.addname('foo')
+ assert index_of_foo != m.indexof('foo')
+
+ def test_delname_monotone2(self):
+ m = Map()
+ m.addname('foo')
+ m.addname('bar')
+ index_of_foo = m.indexof('foo')
+ assert index_of_foo is not None
+ m.delname('foo')
+ assert m.indexof('foo') == Map.NOT_FOUND
+ m.addname('foo')
+ assert index_of_foo != m.indexof('foo')
+ assert m.indexof('bar') != m.indexof('foo')
+
class TestMapDict(object):
def test_set(self):
m = MapDict(2)
diff --git a/js/utils.py b/js/utils.py
--- a/js/utils.py
+++ b/js/utils.py
@@ -45,6 +45,12 @@
NOT_FOUND = -1
def __init__(self):
self.indexes = {}
+ self.next_index = 0
+
+ def _get_next_index(self):
+ index = self.next_index
+ self.next_index += 1
+ return index
def __repr__(self):
return "%s:\n %s" %(object.__repr__(self), repr(self.indexes))
@@ -53,10 +59,13 @@
return self.indexes.get(name, self.NOT_FOUND)
def addname(self, name):
- if name not in self.indexes:
- self.indexes[name] = len(self.indexes)
+ if self.indexof(name) == self.NOT_FOUND:
+ self.indexes[name] = self._get_next_index()
return self.indexof(name)
+ def delname(self, name):
+ self.indexes[name] = self.NOT_FOUND
+
class MapDict(Map):
def __init__(self, size = 99):
Map.__init__(self)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit