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