paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Dictionary objects. |
paul@6 | 5 | |
paul@542 | 6 | Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@6 | 7 | |
paul@6 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@6 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@6 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@6 | 11 | version. |
paul@6 | 12 | |
paul@6 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@6 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@6 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@6 | 16 | details. |
paul@6 | 17 | |
paul@6 | 18 | You should have received a copy of the GNU General Public License along with |
paul@6 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@6 | 20 | """ |
paul@6 | 21 | |
paul@542 | 22 | from __builtins__.mapping import hashtable |
paul@528 | 23 | from __builtins__.iteration.iterator import itemiterator |
paul@6 | 24 | |
paul@542 | 25 | class dict(hashtable): |
paul@283 | 26 | |
paul@283 | 27 | "A dictionary representation mapping keys to values." |
paul@283 | 28 | |
paul@283 | 29 | MISSING = object() |
paul@283 | 30 | |
paul@542 | 31 | def __init__(self, iterable=None): |
paul@283 | 32 | |
paul@542 | 33 | "Initialise the dictionary with any given 'iterable'." |
paul@283 | 34 | |
paul@475 | 35 | self.clear() |
paul@283 | 36 | |
paul@542 | 37 | if iterable is not None: |
paul@542 | 38 | for key, value in iterable: |
paul@283 | 39 | self.__setitem__(key, value) |
paul@283 | 40 | |
paul@542 | 41 | # Implementation methods. |
paul@283 | 42 | |
paul@544 | 43 | def _find_entry(self, buckets, key, index): |
paul@283 | 44 | |
paul@544 | 45 | """ |
paul@544 | 46 | Search in 'buckets' for 'key', using an 'index' identifying the bucket |
paul@544 | 47 | involved. |
paul@544 | 48 | """ |
paul@283 | 49 | |
paul@283 | 50 | i = 0 |
paul@283 | 51 | |
paul@544 | 52 | for found, value in buckets[index]: |
paul@283 | 53 | if found == key: |
paul@283 | 54 | return i |
paul@283 | 55 | i += 1 |
paul@283 | 56 | |
paul@283 | 57 | return None |
paul@283 | 58 | |
paul@302 | 59 | def _resize(self, capacity): |
paul@302 | 60 | |
paul@302 | 61 | "Resize the hashtable to have the given 'capacity'." |
paul@302 | 62 | |
paul@304 | 63 | buckets = self._get_buckets(capacity) |
paul@283 | 64 | |
paul@542 | 65 | for key, value in self._items(): |
paul@304 | 66 | self._setitem(buckets, key, value) |
paul@302 | 67 | |
paul@304 | 68 | self.buckets = buckets |
paul@302 | 69 | |
paul@304 | 70 | def _setitem(self, buckets, key, value): |
paul@302 | 71 | |
paul@304 | 72 | "Set in the 'buckets' an item having the given 'key' and 'value'." |
paul@283 | 73 | |
paul@544 | 74 | index, i = self._get_entry(buckets, key) |
paul@283 | 75 | |
paul@283 | 76 | # With no existing entry, append to the bucket. |
paul@283 | 77 | |
paul@283 | 78 | if i is None: |
paul@304 | 79 | buckets[index].append((key, value)) |
paul@304 | 80 | self.size += 1 |
paul@283 | 81 | |
paul@283 | 82 | # With an existing entry, replace the item. |
paul@283 | 83 | |
paul@283 | 84 | else: |
paul@304 | 85 | buckets[index][i] = key, value |
paul@302 | 86 | |
paul@542 | 87 | # String representations. |
paul@542 | 88 | |
paul@542 | 89 | def __str__(self): |
paul@542 | 90 | |
paul@542 | 91 | "Return a string representation." |
paul@542 | 92 | |
paul@542 | 93 | b = buffer() |
paul@542 | 94 | b.append("{") |
paul@542 | 95 | |
paul@542 | 96 | first = True |
paul@542 | 97 | |
paul@542 | 98 | for key, value in self._items(): |
paul@542 | 99 | if first: |
paul@542 | 100 | first = False |
paul@542 | 101 | else: |
paul@542 | 102 | b.append(", ") |
paul@542 | 103 | b.append(key.__repr__()) |
paul@542 | 104 | b.append(" : ") |
paul@542 | 105 | b.append(value.__repr__()) |
paul@542 | 106 | |
paul@542 | 107 | b.append("}") |
paul@542 | 108 | return str(b) |
paul@542 | 109 | |
paul@542 | 110 | __repr__ = __str__ |
paul@542 | 111 | |
paul@475 | 112 | # Public special methods. |
paul@475 | 113 | |
paul@475 | 114 | def __delitem__(self, key): |
paul@475 | 115 | |
paul@475 | 116 | "Remove the entry associated with the given 'key' from this dictionary." |
paul@475 | 117 | |
paul@542 | 118 | self._remove_entry(key) |
paul@475 | 119 | |
paul@475 | 120 | def __getitem__(self, key): |
paul@475 | 121 | |
paul@475 | 122 | "Return the value associated with 'key' from the dictionary." |
paul@475 | 123 | |
paul@475 | 124 | return self.get(key, self.MISSING) |
paul@475 | 125 | |
paul@475 | 126 | def __iter__(self): |
paul@475 | 127 | |
paul@475 | 128 | "Return an iterator." |
paul@475 | 129 | |
paul@475 | 130 | return itemiterator(self.keys()) |
paul@475 | 131 | |
paul@302 | 132 | def __setitem__(self, key, value): |
paul@302 | 133 | |
paul@302 | 134 | "Set a mapping from 'key' to 'value' in the dictionary." |
paul@302 | 135 | |
paul@304 | 136 | capacity = len(self.buckets) |
paul@302 | 137 | |
paul@304 | 138 | if self.size > capacity: |
paul@302 | 139 | self._resize(capacity * 2) |
paul@302 | 140 | |
paul@304 | 141 | self._setitem(self.buckets, key, value) |
paul@283 | 142 | |
paul@475 | 143 | # Public conventional methods. |
paul@6 | 144 | |
paul@473 | 145 | def get(self, key, default=None): |
paul@283 | 146 | |
paul@283 | 147 | """ |
paul@283 | 148 | Return the value stored for 'key'. If 'key' does not have an entry in |
paul@473 | 149 | the dictionary, 'default' will be returned instead. |
paul@283 | 150 | """ |
paul@283 | 151 | |
paul@544 | 152 | index, i = self._get_entry(self.buckets, key) |
paul@283 | 153 | |
paul@283 | 154 | # With no entry index, either raise an exception or return the default. |
paul@283 | 155 | |
paul@283 | 156 | if i is None: |
paul@283 | 157 | if default is self.MISSING: |
paul@544 | 158 | raise KeyError, key |
paul@283 | 159 | else: |
paul@283 | 160 | return default |
paul@283 | 161 | |
paul@283 | 162 | # With a valid entry index, obtain the corresponding value. |
paul@283 | 163 | |
paul@283 | 164 | else: |
paul@304 | 165 | return self.buckets[index][i][1] |
paul@6 | 166 | |
paul@475 | 167 | def has_key(self, key): |
paul@473 | 168 | |
paul@475 | 169 | "Return whether the given 'key' is used with this dictionary." |
paul@475 | 170 | |
paul@475 | 171 | return self.get(key) and True or False |
paul@283 | 172 | |
paul@283 | 173 | def keys(self): |
paul@283 | 174 | |
paul@283 | 175 | "Return the keys for this dictionary." |
paul@283 | 176 | |
paul@304 | 177 | l = [] |
paul@542 | 178 | for key, value in self._items(): |
paul@304 | 179 | l.append(key) |
paul@304 | 180 | return l |
paul@283 | 181 | |
paul@288 | 182 | def items(self): |
paul@288 | 183 | |
paul@288 | 184 | "Return the items, each being a (key, value) tuple, in this dictionary." |
paul@288 | 185 | |
paul@542 | 186 | return self._items() |
paul@288 | 187 | |
paul@544 | 188 | def setdefault(self, key, value): |
paul@544 | 189 | |
paul@544 | 190 | """ |
paul@544 | 191 | Set for 'key' the given 'value' only if no entry for 'key' is already |
paul@544 | 192 | present. Return the associated value for 'key', which will either be the |
paul@544 | 193 | existing value already present in the dictionary or the supplied 'value' |
paul@544 | 194 | that was given to establish an entry in the dictionary. |
paul@544 | 195 | """ |
paul@475 | 196 | |
paul@544 | 197 | if not self.has_key(key): |
paul@544 | 198 | self[key] = value |
paul@544 | 199 | return self.get(key) |
paul@544 | 200 | |
paul@544 | 201 | def update(self, other): |
paul@544 | 202 | |
paul@544 | 203 | "Update this dictionary with the items from 'other'." |
paul@544 | 204 | |
paul@544 | 205 | for key, value in other.items(): |
paul@544 | 206 | self[key] = value |
paul@6 | 207 | |
paul@475 | 208 | def values(self): |
paul@475 | 209 | |
paul@475 | 210 | "Return the values in this dictionary." |
paul@6 | 211 | |
paul@475 | 212 | l = [] |
paul@542 | 213 | for key, value in self._items(): |
paul@475 | 214 | l.append(value) |
paul@475 | 215 | return l |
paul@6 | 216 | |
paul@6 | 217 | # vim: tabstop=4 expandtab shiftwidth=4 |