1 #!/usr/bin/env python 2 3 """ 4 Tuple objects. 5 6 Copyright (C) 2015, 2016, 2017, 2018 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 __builtins__.iteration.iterator import itemiterator 23 from __builtins__.sequence import hashable, unpackable 24 from native import tuple_init, \ 25 list_element, list_len, list_setsize, list_setelement, \ 26 isinstance as _isinstance 27 28 class tuple(unpackable, hashable): 29 30 "Implementation of tuple." 31 32 def __init__(self, args=None): 33 34 "Initialise the tuple." 35 36 # Reserve an attribute for a fragment reference along with some space 37 # for elements. 38 39 if args is None: 40 size = 0 41 else: 42 size = args.__len__() 43 44 self.__data__ = tuple_init(size) 45 46 if size: 47 list_setsize(self.__data__, size) 48 49 # Populate the tuple. 50 51 i = 0 52 while i < size: 53 list_setelement(self.__data__, i, args[i]) 54 i += 1 55 56 def __hash__(self): 57 58 "Return a hashable value for the tuple." 59 60 return self._hashvalue(hash) 61 62 def __getslice__(self, start, end=None, step=1): 63 64 """ 65 Return a slice starting from 'start', with the optional 'end' and 66 'step'. 67 """ 68 69 return tuple(get_using(unpackable.__getslice__, self)(start, end, step)) 70 71 def __len__(self): 72 73 "Return the length of the tuple." 74 75 return list_len(self.__data__) 76 77 def __add__(self, other): 78 79 "Add this tuple to 'other'." 80 81 if not _isinstance(other, tuple): 82 raise TypeError 83 return tuple(tuplepair(self, other)) 84 85 def __str__(self): 86 87 "Return a string representation." 88 89 return self._str("(", ")") 90 91 __repr__ = __str__ 92 93 def __bool__(self): 94 95 "Tuples are true if non-empty." 96 97 return self.__len__() != 0 98 99 def __iter__(self): 100 101 "Return an iterator." 102 103 return itemiterator(self) 104 105 # Special implementation methods. 106 107 def __get_single_item__(self, index): 108 109 "Return the item at the normalised (positive) 'index'." 110 111 self._check_index(index) 112 return list_element(self.__data__, index) 113 114 def __set_single_item__(self, index, value): 115 116 "Set at the normalised (positive) 'index' the given 'value'." 117 118 raise TypeError 119 120 class tuplepair: 121 122 "A combination of tuples." 123 124 def __init__(self, a, b): 125 self.a = a 126 self.b = b 127 128 def __len__(self): 129 130 "Return the combined length of the tuples." 131 132 return len(self.a) + len(self.b) 133 134 def __getitem__(self, index): 135 136 "Return the value from 'index' in the combined tuple." 137 138 asize = len(self.a) 139 if index < asize: 140 return self.a.__get_single_item__(index) 141 else: 142 return self.b.__get_single_item__(index - asize) 143 144 # vim: tabstop=4 expandtab shiftwidth=4