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