1.1 --- a/lib/__builtins__/dict.py Sun Jan 15 18:59:16 2017 +0100
1.2 +++ b/lib/__builtins__/dict.py Sun Jan 15 22:17:34 2017 +0100
1.3 @@ -33,8 +33,7 @@
1.4
1.5 "Initialise the dictionary."
1.6
1.7 - self.size = 0
1.8 - self.buckets = self._get_buckets(args is not None and len(args) / 2 or 0)
1.9 + self.clear()
1.10
1.11 if args is not None:
1.12 for key, value in args:
1.13 @@ -80,6 +79,20 @@
1.14
1.15 return buckets
1.16
1.17 + def _get_entry(self, key):
1.18 +
1.19 + "Return the index and entry index as a tuple for 'key'."
1.20 +
1.21 + # Find an index identifying the bucket involved.
1.22 +
1.23 + index = self._get_index(key)
1.24 +
1.25 + # Find the entry index within the bucket of the key.
1.26 +
1.27 + i = self._find_entry(key, index)
1.28 +
1.29 + return index, i
1.30 +
1.31 def _get_index(self, key):
1.32
1.33 "Check 'key' and return an index or raise TypeError."
1.34 @@ -119,13 +132,7 @@
1.35
1.36 "Set in the 'buckets' an item having the given 'key' and 'value'."
1.37
1.38 - # Find an index identifying the bucket involved.
1.39 -
1.40 - index = self._get_index(key)
1.41 -
1.42 - # Find the entry index within the bucket of the key.
1.43 -
1.44 - i = self._find_entry(key, index)
1.45 + index, i = self._get_entry(key)
1.46
1.47 # With no existing entry, append to the bucket.
1.48
1.49 @@ -138,6 +145,32 @@
1.50 else:
1.51 buckets[index][i] = key, value
1.52
1.53 + # Public special methods.
1.54 +
1.55 + def __delitem__(self, key):
1.56 +
1.57 + "Remove the entry associated with the given 'key' from this dictionary."
1.58 +
1.59 + index, i = self._get_entry(key)
1.60 +
1.61 + if index is None or i is None:
1.62 + raise KeyError, key
1.63 +
1.64 + del self.buckets[index][i]
1.65 + self.size -= 1
1.66 +
1.67 + def __getitem__(self, key):
1.68 +
1.69 + "Return the value associated with 'key' from the dictionary."
1.70 +
1.71 + return self.get(key, self.MISSING)
1.72 +
1.73 + def __iter__(self):
1.74 +
1.75 + "Return an iterator."
1.76 +
1.77 + return itemiterator(self.keys())
1.78 +
1.79 def __setitem__(self, key, value):
1.80
1.81 "Set a mapping from 'key' to 'value' in the dictionary."
1.82 @@ -149,13 +182,14 @@
1.83
1.84 self._setitem(self.buckets, key, value)
1.85
1.86 - def __delitem__(self, key, value): pass
1.87 + # Public conventional methods.
1.88
1.89 - def __getitem__(self, key):
1.90 + def clear(self):
1.91
1.92 - "Return the value associated with 'key' from the dictionary."
1.93 + "Reset the dictionary to an empty state."
1.94
1.95 - return self.get(key, self.MISSING)
1.96 + self.size = 0
1.97 + self.buckets = self._get_buckets(0)
1.98
1.99 def get(self, key, default=None):
1.100
1.101 @@ -164,13 +198,7 @@
1.102 the dictionary, 'default' will be returned instead.
1.103 """
1.104
1.105 - # Find an index identifying the bucket involved.
1.106 -
1.107 - index = self._get_index(key)
1.108 -
1.109 - # Find the entry index within the bucket of the key.
1.110 -
1.111 - i = self._find_entry(key, index)
1.112 + index, i = self._get_entry(key)
1.113
1.114 # With no entry index, either raise an exception or return the default.
1.115
1.116 @@ -185,9 +213,11 @@
1.117 else:
1.118 return self.buckets[index][i][1]
1.119
1.120 - def clear(self): pass
1.121 + def has_key(self, key):
1.122
1.123 - def has_key(self): pass
1.124 + "Return whether the given 'key' is used with this dictionary."
1.125 +
1.126 + return self.get(key) and True or False
1.127
1.128 def keys(self):
1.129
1.130 @@ -198,15 +228,6 @@
1.131 l.append(key)
1.132 return l
1.133
1.134 - def values(self):
1.135 -
1.136 - "Return the values in this dictionary."
1.137 -
1.138 - l = []
1.139 - for key, value in self.items():
1.140 - l.append(value)
1.141 - return l
1.142 -
1.143 def items(self):
1.144
1.145 "Return the items, each being a (key, value) tuple, in this dictionary."
1.146 @@ -217,12 +238,16 @@
1.147 return l
1.148
1.149 def setdefault(self, key, value): pass
1.150 +
1.151 def update(self, other): pass
1.152
1.153 - def __iter__(self):
1.154 + def values(self):
1.155 +
1.156 + "Return the values in this dictionary."
1.157
1.158 - "Return an iterator."
1.159 -
1.160 - return itemiterator(self.keys())
1.161 + l = []
1.162 + for key, value in self.items():
1.163 + l.append(value)
1.164 + return l
1.165
1.166 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/tests/dict.py Sun Jan 15 18:59:16 2017 +0100
2.2 +++ b/tests/dict.py Sun Jan 15 22:17:34 2017 +0100
2.3 @@ -18,6 +18,8 @@
2.4 print "d[30]: key not found", exc.key
2.5 print d.get(30) # None
2.6 print d.get(30, "c") # c
2.7 +print d.has_key(20) # True
2.8 +print d.has_key(30) # False
2.9
2.10 l = f(d)
2.11 print "# l: ",
2.12 @@ -60,3 +62,17 @@
2.13 print d[[1, 2]]
2.14 except TypeError:
2.15 print "d[[1, 2]]: key not appropriate"
2.16 +
2.17 +# Attempt to remove items.
2.18 +
2.19 +del d[20]
2.20 +print d.has_key(20) # False
2.21 +try:
2.22 + del d[30] # should fail with an exception
2.23 +except KeyError, exc:
2.24 + print "del d[30]: key not found", exc.key
2.25 +
2.26 +# Clear the dictionary.
2.27 +
2.28 +d.clear()
2.29 +print d # {}