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