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