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