1 #!/usr/bin/env python 2 3 """ 4 List objects. 5 6 Copyright (C) 2015, 2016, 2017 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__.iterator import itemiterator 23 from __builtins__.sequence import sequence 24 from native import list_append, list_concat, list_element, list_init, \ 25 list_len, list_nonempty, list_setelement, list_setsize 26 27 class list(sequence): 28 29 "Implementation of list." 30 31 def __init__(self, args=None): 32 33 "Initialise the list." 34 35 # Reserve an attribute for a fragment reference along with some space 36 # for elements. 37 38 self.__data__ = list_init(args is not None and len(args) or 0) 39 40 if args is not None: 41 self.extend(args) 42 43 def __delitem__(self, index): pass 44 def __setslice__(self, start, end, slice): pass 45 def __delslice__(self, start, end): pass 46 47 def append(self, value): 48 49 "Append 'value' to the list." 50 51 list_append(self, value) 52 53 def insert(self, i, value): pass 54 55 def extend(self, iterable): 56 57 "Extend the list with the contents of 'iterable'." 58 59 for i in iterable: 60 self.append(i) 61 62 def pop(self): 63 64 "Remove the last item from the list, returning the item." 65 66 i = self[-1] 67 68 # NOTE: Should truncate the allocated list after several pops. 69 70 list_setsize(self.__data__, self.__len__() - 1) 71 return i 72 73 def reverse(self): pass 74 75 def sort(self, cmp=None, key=None, reverse=0): pass 76 77 def __len__(self): 78 79 "Return the length of the list." 80 81 return list_len(self.__data__) 82 83 def __add__(self, other): 84 85 "Add this list to 'other', producing a new list." 86 87 l = list(self) 88 l.extend(other) 89 return l 90 91 def __iadd__(self, other): 92 93 "Concatenate 'other' to the list." 94 95 if isinstance(other, list): 96 list_concat(self, other.__data__) 97 else: 98 self.extend(other) 99 return self 100 101 def __str__(self): 102 103 "Return a string representation." 104 105 return self._str("[", "]") 106 107 __repr__ = __str__ 108 109 def __bool__(self): 110 111 "Lists are true if non-empty." 112 113 return list_nonempty(self.__data__) 114 115 def __iter__(self): 116 117 "Return an iterator." 118 119 return itemiterator(self) 120 121 # Special implementation methods. 122 123 def __get_single_item__(self, index): 124 125 "Return the item at the normalised (positive) 'index'." 126 127 self._check_index(index) 128 return list_element(self.__data__, index) 129 130 def __set_single_item__(self, index, value): 131 132 "Set at the normalised (positive) 'index' the given 'value'." 133 134 self._check_index(index) 135 return list_setelement(self.__data__, index, value) 136 137 # vim: tabstop=4 expandtab shiftwidth=4