paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Dictionary objects. |
paul@6 | 5 | |
paul@283 | 6 | Copyright (C) 2015, 2016 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@290 | 22 | from __builtins__.iterator import itemiterator |
paul@302 | 23 | from __builtins__.sequence import _max |
paul@283 | 24 | import native |
paul@6 | 25 | |
paul@283 | 26 | class dict: |
paul@283 | 27 | |
paul@283 | 28 | "A dictionary representation mapping keys to values." |
paul@283 | 29 | |
paul@283 | 30 | MISSING = object() |
paul@283 | 31 | |
paul@283 | 32 | def __init__(self, args=None): |
paul@283 | 33 | |
paul@283 | 34 | "Initialise the dictionary." |
paul@283 | 35 | |
paul@304 | 36 | self.size = 0 |
paul@304 | 37 | self.buckets = self._get_buckets(args is not None and len(args) / 2 or 0) |
paul@283 | 38 | |
paul@283 | 39 | if args is not None: |
paul@283 | 40 | for key, value in args: |
paul@283 | 41 | self.__setitem__(key, value) |
paul@283 | 42 | |
paul@298 | 43 | def __str__(self): |
paul@298 | 44 | |
paul@298 | 45 | "Return a string representation." |
paul@298 | 46 | |
paul@298 | 47 | b = buffer() |
paul@298 | 48 | b.append("{") |
paul@298 | 49 | |
paul@298 | 50 | first = True |
paul@298 | 51 | |
paul@298 | 52 | for key, value in self.items(): |
paul@298 | 53 | if first: |
paul@298 | 54 | first = False |
paul@298 | 55 | else: |
paul@298 | 56 | b.append(", ") |
paul@298 | 57 | b.append(key.__repr__()) |
paul@298 | 58 | b.append(" : ") |
paul@298 | 59 | b.append(value.__repr__()) |
paul@298 | 60 | |
paul@298 | 61 | b.append("}") |
paul@298 | 62 | return str(b) |
paul@298 | 63 | |
paul@298 | 64 | __repr__ = __str__ |
paul@298 | 65 | |
paul@304 | 66 | def _get_buckets(self, capacity): |
paul@302 | 67 | |
paul@302 | 68 | """ |
paul@302 | 69 | Reserve an attribute for a hashtable reference along with some space |
paul@302 | 70 | for elements. |
paul@302 | 71 | """ |
paul@302 | 72 | |
paul@304 | 73 | buckets = [] |
paul@304 | 74 | capacity = _max(capacity, 5) |
paul@304 | 75 | i = 0 |
paul@304 | 76 | |
paul@304 | 77 | while i < capacity: |
paul@304 | 78 | buckets.append([]) |
paul@304 | 79 | i += 1 |
paul@304 | 80 | |
paul@304 | 81 | return buckets |
paul@302 | 82 | |
paul@283 | 83 | def _get_index(self, key): |
paul@283 | 84 | |
paul@283 | 85 | "Check 'key' and return an index or raise TypeError." |
paul@283 | 86 | |
paul@283 | 87 | index = key.__hash__() |
paul@304 | 88 | |
paul@283 | 89 | if not native._isinstance(index, int): |
paul@283 | 90 | raise TypeError |
paul@283 | 91 | |
paul@304 | 92 | return index % len(self.buckets) |
paul@283 | 93 | |
paul@283 | 94 | def _find_entry(self, key, index): |
paul@283 | 95 | |
paul@283 | 96 | "Search for 'key', using an 'index' identifying the bucket involved." |
paul@283 | 97 | |
paul@283 | 98 | i = 0 |
paul@283 | 99 | |
paul@304 | 100 | for found, value in self.buckets[index]: |
paul@283 | 101 | if found == key: |
paul@283 | 102 | return i |
paul@283 | 103 | i += 1 |
paul@283 | 104 | |
paul@283 | 105 | return None |
paul@283 | 106 | |
paul@302 | 107 | def _resize(self, capacity): |
paul@302 | 108 | |
paul@302 | 109 | "Resize the hashtable to have the given 'capacity'." |
paul@302 | 110 | |
paul@304 | 111 | buckets = self._get_buckets(capacity) |
paul@283 | 112 | |
paul@302 | 113 | for key, value in self.items(): |
paul@304 | 114 | self._setitem(buckets, key, value) |
paul@302 | 115 | |
paul@304 | 116 | self.buckets = buckets |
paul@302 | 117 | |
paul@304 | 118 | def _setitem(self, buckets, key, value): |
paul@302 | 119 | |
paul@304 | 120 | "Set in the 'buckets' an item having the given 'key' and 'value'." |
paul@283 | 121 | |
paul@283 | 122 | # Find an index identifying the bucket involved. |
paul@283 | 123 | |
paul@283 | 124 | index = self._get_index(key) |
paul@283 | 125 | |
paul@283 | 126 | # Find the entry index within the bucket of the key. |
paul@283 | 127 | |
paul@283 | 128 | i = self._find_entry(key, index) |
paul@283 | 129 | |
paul@283 | 130 | # With no existing entry, append to the bucket. |
paul@283 | 131 | |
paul@283 | 132 | if i is None: |
paul@304 | 133 | buckets[index].append((key, value)) |
paul@304 | 134 | self.size += 1 |
paul@283 | 135 | |
paul@283 | 136 | # With an existing entry, replace the item. |
paul@283 | 137 | |
paul@283 | 138 | else: |
paul@304 | 139 | buckets[index][i] = key, value |
paul@302 | 140 | |
paul@302 | 141 | def __setitem__(self, key, value): |
paul@302 | 142 | |
paul@302 | 143 | "Set a mapping from 'key' to 'value' in the dictionary." |
paul@302 | 144 | |
paul@304 | 145 | capacity = len(self.buckets) |
paul@302 | 146 | |
paul@304 | 147 | if self.size > capacity: |
paul@302 | 148 | self._resize(capacity * 2) |
paul@302 | 149 | |
paul@304 | 150 | self._setitem(self.buckets, key, value) |
paul@283 | 151 | |
paul@6 | 152 | def __delitem__(self, key, value): pass |
paul@6 | 153 | |
paul@283 | 154 | def __getitem__(self, key, default=MISSING): |
paul@283 | 155 | |
paul@283 | 156 | """ |
paul@283 | 157 | Return the value stored for 'key'. If 'key' does not have an entry in |
paul@283 | 158 | the dictionary, a KeyError will be raised unless 'default' is specified. |
paul@283 | 159 | In which case, 'default' will be returned instead. |
paul@283 | 160 | """ |
paul@283 | 161 | |
paul@283 | 162 | # Find an index identifying the bucket involved. |
paul@283 | 163 | |
paul@283 | 164 | index = self._get_index(key) |
paul@283 | 165 | |
paul@283 | 166 | # Find the entry index within the bucket of the key. |
paul@283 | 167 | |
paul@283 | 168 | i = self._find_entry(key, index) |
paul@283 | 169 | |
paul@283 | 170 | # With no entry index, either raise an exception or return the default. |
paul@283 | 171 | |
paul@283 | 172 | if i is None: |
paul@283 | 173 | if default is self.MISSING: |
paul@288 | 174 | raise KeyError(key) |
paul@283 | 175 | else: |
paul@283 | 176 | return default |
paul@283 | 177 | |
paul@283 | 178 | # With a valid entry index, obtain the corresponding value. |
paul@283 | 179 | |
paul@283 | 180 | else: |
paul@304 | 181 | return self.buckets[index][i][1] |
paul@6 | 182 | |
paul@6 | 183 | def clear(self): pass |
paul@6 | 184 | def has_key(self): pass |
paul@283 | 185 | |
paul@283 | 186 | def keys(self): |
paul@283 | 187 | |
paul@283 | 188 | "Return the keys for this dictionary." |
paul@283 | 189 | |
paul@304 | 190 | l = [] |
paul@304 | 191 | for key, value in self.items(): |
paul@304 | 192 | l.append(key) |
paul@304 | 193 | return l |
paul@283 | 194 | |
paul@283 | 195 | def values(self): |
paul@283 | 196 | |
paul@283 | 197 | "Return the values in this dictionary." |
paul@283 | 198 | |
paul@304 | 199 | l = [] |
paul@304 | 200 | for key, value in self.items(): |
paul@304 | 201 | l.append(value) |
paul@304 | 202 | return l |
paul@283 | 203 | |
paul@288 | 204 | def items(self): |
paul@288 | 205 | |
paul@288 | 206 | "Return the items, each being a (key, value) tuple, in this dictionary." |
paul@288 | 207 | |
paul@304 | 208 | l = [] |
paul@304 | 209 | for bucket in self.buckets: |
paul@304 | 210 | l += bucket |
paul@304 | 211 | return l |
paul@288 | 212 | |
paul@6 | 213 | def get(self, key): pass |
paul@6 | 214 | def setdefault(self, key, value): pass |
paul@6 | 215 | def update(self, other): pass |
paul@6 | 216 | |
paul@6 | 217 | def __iter__(self): |
paul@6 | 218 | |
paul@6 | 219 | "Return an iterator." |
paul@6 | 220 | |
paul@290 | 221 | return itemiterator(self.keys()) |
paul@6 | 222 | |
paul@6 | 223 | # vim: tabstop=4 expandtab shiftwidth=4 |