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