paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Sequence operations. |
paul@6 | 5 | |
paul@519 | 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@459 | 22 | from __builtins__.int import maxint |
paul@777 | 23 | from native import isinstance as _isinstance, is_int |
paul@6 | 24 | |
paul@292 | 25 | class itemaccess: |
paul@224 | 26 | |
paul@292 | 27 | "An abstract class providing item access." |
paul@6 | 28 | |
paul@287 | 29 | def _check_index(self, index): |
paul@287 | 30 | |
paul@287 | 31 | """ |
paul@287 | 32 | Check the given absolute 'index', raising an IndexError if out of |
paul@287 | 33 | bounds. |
paul@287 | 34 | """ |
paul@287 | 35 | |
paul@402 | 36 | if index < 0 or index >= self.__len__(): |
paul@287 | 37 | raise IndexError(index) |
paul@287 | 38 | |
paul@224 | 39 | def __getitem__(self, index): |
paul@224 | 40 | |
paul@224 | 41 | "Return the item or slice specified by 'index'." |
paul@224 | 42 | |
paul@224 | 43 | # Normalise any integer indexes, converting negative indexes to positive |
paul@224 | 44 | # ones. |
paul@6 | 45 | |
paul@777 | 46 | if is_int(index): |
paul@265 | 47 | index = _get_absolute_index(index, self.__len__()) |
paul@224 | 48 | return self.__get_single_item__(index) |
paul@224 | 49 | |
paul@224 | 50 | # Handle slices separately. |
paul@6 | 51 | |
paul@356 | 52 | elif _isinstance(index, slice): |
paul@295 | 53 | return self.__getslice__(index.start, index.end, index.step) |
paul@224 | 54 | |
paul@224 | 55 | # No other kinds of objects are supported as indexes. |
paul@6 | 56 | |
paul@224 | 57 | else: |
paul@282 | 58 | raise TypeError() |
paul@224 | 59 | |
paul@227 | 60 | def __setitem__(self, index, value): |
paul@227 | 61 | |
paul@227 | 62 | "Set at 'index' the given 'value'." |
paul@227 | 63 | |
paul@227 | 64 | # Normalise any integer indexes, converting negative indexes to positive |
paul@227 | 65 | # ones. |
paul@227 | 66 | |
paul@777 | 67 | if is_int(index): |
paul@265 | 68 | index = _get_absolute_index(index, self.__len__()) |
paul@227 | 69 | return self.__set_single_item__(index, value) |
paul@227 | 70 | |
paul@227 | 71 | # Handle slices separately. |
paul@227 | 72 | |
paul@356 | 73 | elif _isinstance(index, slice): |
paul@227 | 74 | return self.__setslice__(index.start, index.end, value) |
paul@227 | 75 | |
paul@227 | 76 | # No other kinds of objects are supported as indexes. |
paul@227 | 77 | |
paul@227 | 78 | else: |
paul@282 | 79 | raise TypeError() |
paul@227 | 80 | |
paul@295 | 81 | def __getslice__(self, start, end=None, step=1): |
paul@6 | 82 | |
paul@295 | 83 | """ |
paul@295 | 84 | Return a slice of the sequence starting from the 'start' index, ending |
paul@295 | 85 | before the optional 'end' (or at the end of the sequence), and providing |
paul@295 | 86 | items at the frequency given by 'step' (with a default step of 1). |
paul@295 | 87 | """ |
paul@295 | 88 | |
paul@295 | 89 | if step == 0: |
paul@295 | 90 | raise ValueError(step) |
paul@224 | 91 | |
paul@224 | 92 | length = self.__len__() |
paul@6 | 93 | |
paul@224 | 94 | # Handle a null start as the first position, otherwise normalising any |
paul@224 | 95 | # start index. |
paul@224 | 96 | |
paul@224 | 97 | if start is None: |
paul@367 | 98 | if step > 0: |
paul@367 | 99 | start = 0 |
paul@367 | 100 | else: |
paul@367 | 101 | start = length - 1 |
paul@224 | 102 | else: |
paul@265 | 103 | start = _get_absolute_index(start, length) |
paul@6 | 104 | |
paul@224 | 105 | # Handle a null end as the first position after the end of the sequence, |
paul@224 | 106 | # otherwise normalising any end index. |
paul@224 | 107 | |
paul@224 | 108 | if end is None: |
paul@367 | 109 | if step > 0: |
paul@367 | 110 | end = length |
paul@367 | 111 | else: |
paul@367 | 112 | end = -1 |
paul@224 | 113 | else: |
paul@265 | 114 | end = _get_absolute_index(end, length) |
paul@6 | 115 | |
paul@384 | 116 | return self.__get_multiple_items__(start, end, step) |
paul@6 | 117 | |
paul@351 | 118 | # Methods implemented by subclasses. |
paul@351 | 119 | |
paul@351 | 120 | def __setslice__(self, start, end, value): |
paul@351 | 121 | |
paul@351 | 122 | "Method to be overridden by subclasses." |
paul@351 | 123 | |
paul@351 | 124 | pass |
paul@351 | 125 | |
paul@351 | 126 | def __get_single_item__(self, index): |
paul@351 | 127 | |
paul@351 | 128 | "Method to be overridden by subclasses." |
paul@351 | 129 | |
paul@351 | 130 | return None |
paul@351 | 131 | |
paul@351 | 132 | def __set_single_item__(self, index, value): |
paul@351 | 133 | |
paul@351 | 134 | "Method to be overridden by subclasses." |
paul@351 | 135 | |
paul@351 | 136 | pass |
paul@351 | 137 | |
paul@384 | 138 | def __get_multiple_items__(self, start, end, step): |
paul@384 | 139 | |
paul@520 | 140 | """ |
paul@520 | 141 | Return items from 'start' until (but excluding) 'end', at 'step' |
paul@520 | 142 | intervals. |
paul@520 | 143 | """ |
paul@384 | 144 | |
paul@520 | 145 | result = [] |
paul@520 | 146 | |
paul@520 | 147 | while step > 0 and start < end or step < 0 and start > end: |
paul@520 | 148 | result.append(self.__get_single_item__(start)) |
paul@520 | 149 | start += step |
paul@520 | 150 | |
paul@520 | 151 | return result |
paul@384 | 152 | |
paul@351 | 153 | def __len__(self): |
paul@351 | 154 | |
paul@351 | 155 | "Method to be overridden by subclasses." |
paul@351 | 156 | |
paul@351 | 157 | return 0 |
paul@351 | 158 | |
paul@459 | 159 | class hashable(itemaccess): |
paul@459 | 160 | |
paul@459 | 161 | "An abstract class providing support for hashable sequences." |
paul@459 | 162 | |
paul@459 | 163 | _p = maxint / 32 |
paul@459 | 164 | _a = 31 |
paul@459 | 165 | |
paul@459 | 166 | def _hashvalue(self, fn): |
paul@459 | 167 | |
paul@459 | 168 | """ |
paul@459 | 169 | Return a value for hashing purposes for the sequence using the given |
paul@459 | 170 | 'fn' on each item. |
paul@459 | 171 | """ |
paul@459 | 172 | |
paul@459 | 173 | result = 0 |
paul@459 | 174 | l = self.__len__() |
paul@459 | 175 | i = 0 |
paul@459 | 176 | |
paul@459 | 177 | while i < l: |
paul@459 | 178 | result = (result * self._a + fn(self.__get_single_item__(i))) % self._p |
paul@459 | 179 | i += 1 |
paul@459 | 180 | |
paul@459 | 181 | return result |
paul@459 | 182 | |
paul@292 | 183 | class sequence(itemaccess): |
paul@292 | 184 | |
paul@292 | 185 | "A common base class for sequence types." |
paul@292 | 186 | |
paul@292 | 187 | def _str(self, opening, closing): |
paul@292 | 188 | |
paul@292 | 189 | "Serialise this object with the given 'opening' and 'closing' strings." |
paul@292 | 190 | |
paul@292 | 191 | b = buffer() |
paul@292 | 192 | i = 0 |
paul@292 | 193 | l = self.__len__() |
paul@292 | 194 | first = True |
paul@292 | 195 | |
paul@292 | 196 | b.append(opening) |
paul@292 | 197 | while i < l: |
paul@292 | 198 | if first: |
paul@292 | 199 | first = False |
paul@292 | 200 | else: |
paul@292 | 201 | b.append(", ") |
paul@292 | 202 | b.append(repr(self.__get_single_item__(i))) |
paul@292 | 203 | i += 1 |
paul@292 | 204 | b.append(closing) |
paul@292 | 205 | |
paul@292 | 206 | return str(b) |
paul@292 | 207 | |
paul@292 | 208 | def __contains__(self, value): |
paul@292 | 209 | |
paul@292 | 210 | "Return whether the list contains 'value'." |
paul@292 | 211 | |
paul@292 | 212 | # Perform a linear search of the sequence contents. |
paul@292 | 213 | |
paul@292 | 214 | for v in self: |
paul@292 | 215 | |
paul@292 | 216 | # Return True if the current value is equal to the specified one. |
paul@292 | 217 | # Note that this is not an identity test, but an equality test. |
paul@292 | 218 | |
paul@292 | 219 | if v == value: |
paul@292 | 220 | return True |
paul@292 | 221 | |
paul@292 | 222 | return False |
paul@292 | 223 | |
paul@292 | 224 | def index(self, value): |
paul@292 | 225 | |
paul@292 | 226 | "Return the index of 'value' or raise ValueError." |
paul@292 | 227 | |
paul@292 | 228 | i = 0 |
paul@402 | 229 | l = self.__len__() |
paul@292 | 230 | while i < l: |
paul@292 | 231 | if self[i] == value: |
paul@292 | 232 | return i |
paul@292 | 233 | i += 1 |
paul@292 | 234 | |
paul@292 | 235 | raise ValueError(value) |
paul@292 | 236 | |
paul@287 | 237 | def __eq__(self, other): |
paul@287 | 238 | |
paul@287 | 239 | "Return whether this sequence is equal to 'other'." |
paul@287 | 240 | |
paul@459 | 241 | try: |
paul@459 | 242 | return self._eq(other) |
paul@459 | 243 | except TypeError: |
paul@459 | 244 | return NotImplemented |
paul@459 | 245 | |
paul@459 | 246 | def _eq(self, other): |
paul@459 | 247 | |
paul@459 | 248 | """ |
paul@459 | 249 | Return whether this sequence is equal to 'other' sequence. Note that |
paul@459 | 250 | this method will raise a TypeError if 'other' is not a sequence. |
paul@459 | 251 | """ |
paul@459 | 252 | |
paul@287 | 253 | # Sequences must have equal lengths to be equal. |
paul@287 | 254 | |
paul@287 | 255 | n = self.__len__() |
paul@402 | 256 | if other.__len__() != n: |
paul@287 | 257 | return False |
paul@265 | 258 | |
paul@287 | 259 | i = 0 |
paul@287 | 260 | while i < n: |
paul@287 | 261 | if self.__getitem__(i) != other.__getitem__(i): |
paul@287 | 262 | return False |
paul@287 | 263 | i += 1 |
paul@265 | 264 | |
paul@287 | 265 | return True |
paul@287 | 266 | |
paul@287 | 267 | def __ne__(self, other): |
paul@287 | 268 | |
paul@287 | 269 | "Return whether this sequence is not equal to 'other'." |
paul@287 | 270 | |
paul@287 | 271 | return not self.__eq__(other) |
paul@265 | 272 | |
paul@351 | 273 | def __iter__(self): |
paul@351 | 274 | |
paul@351 | 275 | "Method to be overridden by subclasses." |
paul@351 | 276 | |
paul@351 | 277 | raise StopIteration() |
paul@351 | 278 | |
paul@6 | 279 | def _get_absolute_index(index, length): |
paul@6 | 280 | |
paul@6 | 281 | """ |
paul@6 | 282 | Return the absolute index for 'index' given a collection having the |
paul@6 | 283 | specified 'length'. |
paul@6 | 284 | """ |
paul@6 | 285 | |
paul@6 | 286 | if index < 0: |
paul@6 | 287 | return length + index |
paul@6 | 288 | else: |
paul@6 | 289 | return index |
paul@6 | 290 | |
paul@6 | 291 | # vim: tabstop=4 expandtab shiftwidth=4 |