1 #!/usr/bin/env python 2 3 """ 4 Sequence operations. 5 6 Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from native import _isinstance 23 24 class sequence: 25 26 "A common base class for sequence types." 27 28 def _str(self, opening, closing): 29 30 "Serialise this object with the given 'opening' and 'closing' strings." 31 32 b = buffer() 33 i = 0 34 l = self.__len__() 35 first = True 36 37 b.append(opening) 38 while i < l: 39 if first: 40 first = False 41 else: 42 b.append(", ") 43 b.append(repr(self.__get_single_item__(i))) 44 i += 1 45 b.append(closing) 46 47 return str(b) 48 49 def __contains__(self, value): 50 51 "Return whether the list contains 'value'." 52 53 # Perform a linear search of the sequence contents. 54 55 for v in self: 56 57 # Return True if the current value is equal to the specified one. 58 # Note that this is not an identity test, but an equality test. 59 60 if v == value: 61 return True 62 63 return False 64 65 def index(self, value): 66 67 "Return the index of 'value' or raise ValueError." 68 69 i = 0 70 l = len(self) 71 while i < l: 72 if self[i] == value: 73 return i 74 i += 1 75 76 raise ValueError(value) 77 78 def __getitem__(self, index): 79 80 "Return the item or slice specified by 'index'." 81 82 # Normalise any integer indexes, converting negative indexes to positive 83 # ones. 84 85 if _isinstance(index, int): 86 index = _get_absolute_index(index, self.__len__()) 87 return self.__get_single_item__(index) 88 89 # Handle slices separately. 90 91 elif _isinstance(index, slice): 92 return self.__getslice__(index.start, index.end) 93 94 # No other kinds of objects are supported as indexes. 95 96 else: 97 raise TypeError() 98 99 def __setitem__(self, index, value): 100 101 "Set at 'index' the given 'value'." 102 103 # Normalise any integer indexes, converting negative indexes to positive 104 # ones. 105 106 if _isinstance(index, int): 107 index = _get_absolute_index(index, self.__len__()) 108 return self.__set_single_item__(index, value) 109 110 # Handle slices separately. 111 112 elif _isinstance(index, slice): 113 return self.__setslice__(index.start, index.end, value) 114 115 # No other kinds of objects are supported as indexes. 116 117 else: 118 raise TypeError() 119 120 def __getslice__(self, start, end=None): 121 122 "Return a slice starting from 'start', with the optional 'end'." 123 124 length = self.__len__() 125 126 # Handle a null start as the first position, otherwise normalising any 127 # start index. 128 129 if start is None: 130 start = 0 131 else: 132 start = _get_absolute_index(start, length) 133 134 # Handle a null end as the first position after the end of the sequence, 135 # otherwise normalising any end index. 136 137 if end is None: 138 end = length 139 else: 140 end = _get_absolute_index(end, length) 141 142 result = [] 143 144 while start < end: 145 result.append(self.__get_single_item__(start)) 146 start += 1 147 148 return result 149 150 def _check_index(self, index): 151 152 """ 153 Check the given absolute 'index', raising an IndexError if out of 154 bounds. 155 """ 156 157 if index < 0 or index >= len(self): 158 raise IndexError(index) 159 160 def _get_absolute_index(index, length): 161 162 """ 163 Return the absolute index for 'index' given a collection having the 164 specified 'length'. 165 """ 166 167 if index < 0: 168 return length + index 169 else: 170 return index 171 172 def _max(x, y): 173 174 "Return the maximum of 'x' and 'y'." 175 176 if x >= y: 177 return x 178 else: 179 return y 180 181 def _min(x, y): 182 183 "Return the minimum of 'x' and 'y'." 184 185 if x <= y: 186 return x 187 else: 188 return y 189 190 def _tuple(l): pass 191 192 # vim: tabstop=4 expandtab shiftwidth=4