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@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@224 | 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@224 | 51 | elif _isinstance(index, slice): |
paul@224 | 52 | return self.__getslice__(index.start, index.end) |
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@227 | 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@227 | 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@224 | 80 | def __getslice__(self, start, end=None): |
paul@6 | 81 | |
paul@224 | 82 | "Return a slice starting from 'start', with the optional 'end'." |
paul@224 | 83 | |
paul@224 | 84 | length = self.__len__() |
paul@6 | 85 | |
paul@224 | 86 | # Handle a null start as the first position, otherwise normalising any |
paul@224 | 87 | # start index. |
paul@224 | 88 | |
paul@224 | 89 | if start is None: |
paul@224 | 90 | start = 0 |
paul@224 | 91 | else: |
paul@265 | 92 | start = _get_absolute_index(start, length) |
paul@6 | 93 | |
paul@224 | 94 | # Handle a null end as the first position after the end of the sequence, |
paul@224 | 95 | # otherwise normalising any end index. |
paul@224 | 96 | |
paul@224 | 97 | if end is None: |
paul@224 | 98 | end = length |
paul@224 | 99 | else: |
paul@265 | 100 | end = _get_absolute_index(end, length) |
paul@6 | 101 | |
paul@224 | 102 | result = [] |
paul@224 | 103 | |
paul@224 | 104 | while start < end: |
paul@224 | 105 | result.append(self.__get_single_item__(start)) |
paul@224 | 106 | start += 1 |
paul@224 | 107 | |
paul@224 | 108 | return result |
paul@6 | 109 | |
paul@292 | 110 | class sequence(itemaccess): |
paul@292 | 111 | |
paul@292 | 112 | "A common base class for sequence types." |
paul@292 | 113 | |
paul@292 | 114 | def _str(self, opening, closing): |
paul@292 | 115 | |
paul@292 | 116 | "Serialise this object with the given 'opening' and 'closing' strings." |
paul@292 | 117 | |
paul@292 | 118 | b = buffer() |
paul@292 | 119 | i = 0 |
paul@292 | 120 | l = self.__len__() |
paul@292 | 121 | first = True |
paul@292 | 122 | |
paul@292 | 123 | b.append(opening) |
paul@292 | 124 | while i < l: |
paul@292 | 125 | if first: |
paul@292 | 126 | first = False |
paul@292 | 127 | else: |
paul@292 | 128 | b.append(", ") |
paul@292 | 129 | b.append(repr(self.__get_single_item__(i))) |
paul@292 | 130 | i += 1 |
paul@292 | 131 | b.append(closing) |
paul@292 | 132 | |
paul@292 | 133 | return str(b) |
paul@292 | 134 | |
paul@292 | 135 | def __contains__(self, value): |
paul@292 | 136 | |
paul@292 | 137 | "Return whether the list contains 'value'." |
paul@292 | 138 | |
paul@292 | 139 | # Perform a linear search of the sequence contents. |
paul@292 | 140 | |
paul@292 | 141 | for v in self: |
paul@292 | 142 | |
paul@292 | 143 | # Return True if the current value is equal to the specified one. |
paul@292 | 144 | # Note that this is not an identity test, but an equality test. |
paul@292 | 145 | |
paul@292 | 146 | if v == value: |
paul@292 | 147 | return True |
paul@292 | 148 | |
paul@292 | 149 | return False |
paul@292 | 150 | |
paul@292 | 151 | def index(self, value): |
paul@292 | 152 | |
paul@292 | 153 | "Return the index of 'value' or raise ValueError." |
paul@292 | 154 | |
paul@292 | 155 | i = 0 |
paul@292 | 156 | l = len(self) |
paul@292 | 157 | while i < l: |
paul@292 | 158 | if self[i] == value: |
paul@292 | 159 | return i |
paul@292 | 160 | i += 1 |
paul@292 | 161 | |
paul@292 | 162 | raise ValueError(value) |
paul@292 | 163 | |
paul@287 | 164 | def __eq__(self, other): |
paul@287 | 165 | |
paul@287 | 166 | "Return whether this sequence is equal to 'other'." |
paul@287 | 167 | |
paul@287 | 168 | # Sequences must have equal lengths to be equal. |
paul@287 | 169 | |
paul@287 | 170 | n = self.__len__() |
paul@287 | 171 | if len(other) != n: |
paul@287 | 172 | return False |
paul@265 | 173 | |
paul@287 | 174 | i = 0 |
paul@287 | 175 | while i < n: |
paul@287 | 176 | if self.__getitem__(i) != other.__getitem__(i): |
paul@287 | 177 | return False |
paul@287 | 178 | i += 1 |
paul@265 | 179 | |
paul@287 | 180 | return True |
paul@287 | 181 | |
paul@287 | 182 | def __ne__(self, other): |
paul@287 | 183 | |
paul@287 | 184 | "Return whether this sequence is not equal to 'other'." |
paul@287 | 185 | |
paul@287 | 186 | return not self.__eq__(other) |
paul@265 | 187 | |
paul@6 | 188 | def _get_absolute_index(index, length): |
paul@6 | 189 | |
paul@6 | 190 | """ |
paul@6 | 191 | Return the absolute index for 'index' given a collection having the |
paul@6 | 192 | specified 'length'. |
paul@6 | 193 | """ |
paul@6 | 194 | |
paul@6 | 195 | if index < 0: |
paul@6 | 196 | return length + index |
paul@6 | 197 | else: |
paul@6 | 198 | return index |
paul@6 | 199 | |
paul@6 | 200 | def _max(x, y): |
paul@6 | 201 | |
paul@6 | 202 | "Return the maximum of 'x' and 'y'." |
paul@6 | 203 | |
paul@6 | 204 | if x >= y: |
paul@6 | 205 | return x |
paul@6 | 206 | else: |
paul@6 | 207 | return y |
paul@6 | 208 | |
paul@6 | 209 | def _min(x, y): |
paul@6 | 210 | |
paul@6 | 211 | "Return the minimum of 'x' and 'y'." |
paul@6 | 212 | |
paul@6 | 213 | if x <= y: |
paul@6 | 214 | return x |
paul@6 | 215 | else: |
paul@6 | 216 | return y |
paul@6 | 217 | |
paul@6 | 218 | # vim: tabstop=4 expandtab shiftwidth=4 |