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@283 | 43 | def _find_entry(self, key, index): |
paul@283 | 44 | |
paul@283 | 45 | "Search for 'key', using an 'index' identifying the bucket involved." |
paul@283 | 46 | |
paul@283 | 47 | i = 0 |
paul@283 | 48 | |
paul@304 | 49 | for found, value in self.buckets[index]: |
paul@283 | 50 | if found == key: |
paul@283 | 51 | return i |
paul@283 | 52 | i += 1 |
paul@283 | 53 | |
paul@283 | 54 | return None |
paul@283 | 55 | |
paul@302 | 56 | def _resize(self, capacity): |
paul@302 | 57 | |
paul@302 | 58 | "Resize the hashtable to have the given 'capacity'." |
paul@302 | 59 | |
paul@304 | 60 | buckets = self._get_buckets(capacity) |
paul@283 | 61 | |
paul@542 | 62 | for key, value in self._items(): |
paul@304 | 63 | self._setitem(buckets, key, value) |
paul@302 | 64 | |
paul@304 | 65 | self.buckets = buckets |
paul@302 | 66 | |
paul@304 | 67 | def _setitem(self, buckets, key, value): |
paul@302 | 68 | |
paul@304 | 69 | "Set in the 'buckets' an item having the given 'key' and 'value'." |
paul@283 | 70 | |
paul@475 | 71 | index, i = self._get_entry(key) |
paul@283 | 72 | |
paul@283 | 73 | # With no existing entry, append to the bucket. |
paul@283 | 74 | |
paul@283 | 75 | if i is None: |
paul@304 | 76 | buckets[index].append((key, value)) |
paul@304 | 77 | self.size += 1 |
paul@283 | 78 | |
paul@283 | 79 | # With an existing entry, replace the item. |
paul@283 | 80 | |
paul@283 | 81 | else: |
paul@304 | 82 | buckets[index][i] = key, value |
paul@302 | 83 | |
paul@542 | 84 | # String representations. |
paul@542 | 85 | |
paul@542 | 86 | def __str__(self): |
paul@542 | 87 | |
paul@542 | 88 | "Return a string representation." |
paul@542 | 89 | |
paul@542 | 90 | b = buffer() |
paul@542 | 91 | b.append("{") |
paul@542 | 92 | |
paul@542 | 93 | first = True |
paul@542 | 94 | |
paul@542 | 95 | for key, value in self._items(): |
paul@542 | 96 | if first: |
paul@542 | 97 | first = False |
paul@542 | 98 | else: |
paul@542 | 99 | b.append(", ") |
paul@542 | 100 | b.append(key.__repr__()) |
paul@542 | 101 | b.append(" : ") |
paul@542 | 102 | b.append(value.__repr__()) |
paul@542 | 103 | |
paul@542 | 104 | b.append("}") |
paul@542 | 105 | return str(b) |
paul@542 | 106 | |
paul@542 | 107 | __repr__ = __str__ |
paul@542 | 108 | |
paul@475 | 109 | # Public special methods. |
paul@475 | 110 | |
paul@475 | 111 | def __delitem__(self, key): |
paul@475 | 112 | |
paul@475 | 113 | "Remove the entry associated with the given 'key' from this dictionary." |
paul@475 | 114 | |
paul@542 | 115 | self._remove_entry(key) |
paul@475 | 116 | |
paul@475 | 117 | def __getitem__(self, key): |
paul@475 | 118 | |
paul@475 | 119 | "Return the value associated with 'key' from the dictionary." |
paul@475 | 120 | |
paul@475 | 121 | return self.get(key, self.MISSING) |
paul@475 | 122 | |
paul@475 | 123 | def __iter__(self): |
paul@475 | 124 | |
paul@475 | 125 | "Return an iterator." |
paul@475 | 126 | |
paul@475 | 127 | return itemiterator(self.keys()) |
paul@475 | 128 | |
paul@302 | 129 | def __setitem__(self, key, value): |
paul@302 | 130 | |
paul@302 | 131 | "Set a mapping from 'key' to 'value' in the dictionary." |
paul@302 | 132 | |
paul@304 | 133 | capacity = len(self.buckets) |
paul@302 | 134 | |
paul@304 | 135 | if self.size > capacity: |
paul@302 | 136 | self._resize(capacity * 2) |
paul@302 | 137 | |
paul@304 | 138 | self._setitem(self.buckets, key, value) |
paul@283 | 139 | |
paul@475 | 140 | # Public conventional methods. |
paul@6 | 141 | |
paul@473 | 142 | def get(self, key, default=None): |
paul@283 | 143 | |
paul@283 | 144 | """ |
paul@283 | 145 | Return the value stored for 'key'. If 'key' does not have an entry in |
paul@473 | 146 | the dictionary, 'default' will be returned instead. |
paul@283 | 147 | """ |
paul@283 | 148 | |
paul@475 | 149 | index, i = self._get_entry(key) |
paul@283 | 150 | |
paul@283 | 151 | # With no entry index, either raise an exception or return the default. |
paul@283 | 152 | |
paul@283 | 153 | if i is None: |
paul@283 | 154 | if default is self.MISSING: |
paul@288 | 155 | raise KeyError(key) |
paul@283 | 156 | else: |
paul@283 | 157 | return default |
paul@283 | 158 | |
paul@283 | 159 | # With a valid entry index, obtain the corresponding value. |
paul@283 | 160 | |
paul@283 | 161 | else: |
paul@304 | 162 | return self.buckets[index][i][1] |
paul@6 | 163 | |
paul@475 | 164 | def has_key(self, key): |
paul@473 | 165 | |
paul@475 | 166 | "Return whether the given 'key' is used with this dictionary." |
paul@475 | 167 | |
paul@475 | 168 | return self.get(key) and True or False |
paul@283 | 169 | |
paul@283 | 170 | def keys(self): |
paul@283 | 171 | |
paul@283 | 172 | "Return the keys for this dictionary." |
paul@283 | 173 | |
paul@304 | 174 | l = [] |
paul@542 | 175 | for key, value in self._items(): |
paul@304 | 176 | l.append(key) |
paul@304 | 177 | return l |
paul@283 | 178 | |
paul@288 | 179 | def items(self): |
paul@288 | 180 | |
paul@288 | 181 | "Return the items, each being a (key, value) tuple, in this dictionary." |
paul@288 | 182 | |
paul@542 | 183 | return self._items() |
paul@288 | 184 | |
paul@6 | 185 | def setdefault(self, key, value): pass |
paul@475 | 186 | |
paul@6 | 187 | def update(self, other): pass |
paul@6 | 188 | |
paul@475 | 189 | def values(self): |
paul@475 | 190 | |
paul@475 | 191 | "Return the values in this dictionary." |
paul@6 | 192 | |
paul@475 | 193 | l = [] |
paul@542 | 194 | for key, value in self._items(): |
paul@475 | 195 | l.append(value) |
paul@475 | 196 | return l |
paul@6 | 197 | |
paul@6 | 198 | # vim: tabstop=4 expandtab shiftwidth=4 |