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