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