paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Tuple objects. |
paul@6 | 5 | |
paul@665 | 6 | Copyright (C) 2015, 2016, 2017 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@528 | 22 | from __builtins__.iteration.iterator import itemiterator |
paul@459 | 23 | from __builtins__.sequence import hashable, sequence |
paul@773 | 24 | from native import tuple_init, \ |
paul@773 | 25 | list_element, list_len, list_setsize, list_setelement |
paul@6 | 26 | |
paul@459 | 27 | class tuple(sequence, hashable): |
paul@6 | 28 | |
paul@6 | 29 | "Implementation of tuple." |
paul@6 | 30 | |
paul@159 | 31 | def __init__(self, args=None): |
paul@159 | 32 | |
paul@159 | 33 | "Initialise the tuple." |
paul@159 | 34 | |
paul@227 | 35 | # Reserve an attribute for a fragment reference along with some space |
paul@227 | 36 | # for elements. |
paul@227 | 37 | |
paul@773 | 38 | self.__data__ = tuple_init(size) |
paul@227 | 39 | |
paul@773 | 40 | if args is None: |
paul@773 | 41 | size = 0 |
paul@773 | 42 | else: |
paul@773 | 43 | size = args.__len__() |
paul@773 | 44 | list_setsize(self.__data__, size) |
paul@227 | 45 | |
paul@773 | 46 | # Populate the tuple. |
paul@773 | 47 | |
paul@227 | 48 | i = 0 |
paul@773 | 49 | while i < size: |
paul@773 | 50 | list_setelement(self.__data__, i, args[i]) |
paul@227 | 51 | i += 1 |
paul@6 | 52 | |
paul@459 | 53 | def __hash__(self): |
paul@459 | 54 | |
paul@459 | 55 | "Return a hashable value for the tuple." |
paul@459 | 56 | |
paul@459 | 57 | return self._hashvalue(hash) |
paul@459 | 58 | |
paul@551 | 59 | def __getslice__(self, start, end=None, step=1): |
paul@6 | 60 | |
paul@551 | 61 | """ |
paul@551 | 62 | Return a slice starting from 'start', with the optional 'end' and |
paul@551 | 63 | 'step'. |
paul@551 | 64 | """ |
paul@6 | 65 | |
paul@551 | 66 | return tuple(get_using(sequence.__getslice__, self)(start, end, step)) |
paul@159 | 67 | |
paul@159 | 68 | def __len__(self): |
paul@6 | 69 | |
paul@159 | 70 | "Return the length of the tuple." |
paul@159 | 71 | |
paul@356 | 72 | return list_len(self.__data__) |
paul@159 | 73 | |
paul@6 | 74 | def __add__(self, other): pass |
paul@159 | 75 | |
paul@227 | 76 | def __str__(self): |
paul@227 | 77 | |
paul@227 | 78 | "Return a string representation." |
paul@227 | 79 | |
paul@227 | 80 | return self._str("(", ")") |
paul@6 | 81 | |
paul@224 | 82 | __repr__ = __str__ |
paul@224 | 83 | |
paul@6 | 84 | def __bool__(self): |
paul@6 | 85 | |
paul@6 | 86 | "Tuples are true if non-empty." |
paul@6 | 87 | |
paul@6 | 88 | return self.__len__() != 0 |
paul@6 | 89 | |
paul@6 | 90 | def __iter__(self): |
paul@6 | 91 | |
paul@6 | 92 | "Return an iterator." |
paul@6 | 93 | |
paul@290 | 94 | return itemiterator(self) |
paul@6 | 95 | |
paul@6 | 96 | # Special implementation methods. |
paul@6 | 97 | |
paul@159 | 98 | def __get_single_item__(self, index): |
paul@227 | 99 | |
paul@227 | 100 | "Return the item at the normalised (positive) 'index'." |
paul@227 | 101 | |
paul@265 | 102 | self._check_index(index) |
paul@356 | 103 | return list_element(self.__data__, index) |
paul@227 | 104 | |
paul@227 | 105 | def __set_single_item__(self, index, value): |
paul@227 | 106 | |
paul@227 | 107 | "Set at the normalised (positive) 'index' the given 'value'." |
paul@227 | 108 | |
paul@665 | 109 | raise TypeError |
paul@6 | 110 | |
paul@6 | 111 | # vim: tabstop=4 expandtab shiftwidth=4 |