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@548 | 23 | from __builtins__.sequence import sequence, _get_absolute_index |
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@548 | 43 | def __delitem__(self, index): |
paul@548 | 44 | |
paul@548 | 45 | "Delete the item at 'index'." |
paul@548 | 46 | |
paul@548 | 47 | length = self.__len__() |
paul@548 | 48 | index = _get_absolute_index(index, length) |
paul@548 | 49 | last = length - 1 |
paul@548 | 50 | |
paul@548 | 51 | while index < last: |
paul@548 | 52 | self[index] = self[index + 1] |
paul@548 | 53 | index += 1 |
paul@548 | 54 | |
paul@548 | 55 | # NOTE: Should truncate the allocated list after several pops. |
paul@548 | 56 | |
paul@548 | 57 | list_setsize(self.__data__, last) |
paul@548 | 58 | |
paul@6 | 59 | def __setslice__(self, start, end, slice): pass |
paul@6 | 60 | def __delslice__(self, start, end): pass |
paul@206 | 61 | |
paul@206 | 62 | def append(self, value): |
paul@206 | 63 | |
paul@206 | 64 | "Append 'value' to the list." |
paul@206 | 65 | |
paul@356 | 66 | list_append(self, value) |
paul@206 | 67 | |
paul@6 | 68 | def insert(self, i, value): pass |
paul@6 | 69 | |
paul@6 | 70 | def extend(self, iterable): |
paul@6 | 71 | |
paul@6 | 72 | "Extend the list with the contents of 'iterable'." |
paul@6 | 73 | |
paul@6 | 74 | for i in iterable: |
paul@6 | 75 | self.append(i) |
paul@6 | 76 | |
paul@516 | 77 | def pop(self): |
paul@516 | 78 | |
paul@516 | 79 | "Remove the last item from the list, returning the item." |
paul@516 | 80 | |
paul@516 | 81 | i = self[-1] |
paul@516 | 82 | |
paul@516 | 83 | # NOTE: Should truncate the allocated list after several pops. |
paul@516 | 84 | |
paul@516 | 85 | list_setsize(self.__data__, self.__len__() - 1) |
paul@516 | 86 | return i |
paul@516 | 87 | |
paul@6 | 88 | def reverse(self): pass |
paul@516 | 89 | |
paul@6 | 90 | def sort(self, cmp=None, key=None, reverse=0): pass |
paul@140 | 91 | |
paul@140 | 92 | def __len__(self): |
paul@140 | 93 | |
paul@140 | 94 | "Return the length of the list." |
paul@140 | 95 | |
paul@356 | 96 | return list_len(self.__data__) |
paul@140 | 97 | |
paul@516 | 98 | def __add__(self, other): |
paul@516 | 99 | |
paul@516 | 100 | "Add this list to 'other', producing a new list." |
paul@516 | 101 | |
paul@516 | 102 | l = list(self) |
paul@516 | 103 | l.extend(other) |
paul@516 | 104 | return l |
paul@206 | 105 | |
paul@206 | 106 | def __iadd__(self, other): |
paul@206 | 107 | |
paul@206 | 108 | "Concatenate 'other' to the list." |
paul@206 | 109 | |
paul@206 | 110 | if isinstance(other, list): |
paul@356 | 111 | list_concat(self, other.__data__) |
paul@206 | 112 | else: |
paul@206 | 113 | self.extend(other) |
paul@206 | 114 | return self |
paul@198 | 115 | |
paul@198 | 116 | def __str__(self): |
paul@198 | 117 | |
paul@227 | 118 | "Return a string representation." |
paul@198 | 119 | |
paul@227 | 120 | return self._str("[", "]") |
paul@6 | 121 | |
paul@222 | 122 | __repr__ = __str__ |
paul@222 | 123 | |
paul@6 | 124 | def __bool__(self): |
paul@6 | 125 | |
paul@6 | 126 | "Lists are true if non-empty." |
paul@6 | 127 | |
paul@356 | 128 | return list_nonempty(self.__data__) |
paul@6 | 129 | |
paul@6 | 130 | def __iter__(self): |
paul@6 | 131 | |
paul@6 | 132 | "Return an iterator." |
paul@6 | 133 | |
paul@290 | 134 | return itemiterator(self) |
paul@6 | 135 | |
paul@6 | 136 | # Special implementation methods. |
paul@6 | 137 | |
paul@140 | 138 | def __get_single_item__(self, index): |
paul@140 | 139 | |
paul@194 | 140 | "Return the item at the normalised (positive) 'index'." |
paul@194 | 141 | |
paul@265 | 142 | self._check_index(index) |
paul@356 | 143 | return list_element(self.__data__, index) |
paul@6 | 144 | |
paul@227 | 145 | def __set_single_item__(self, index, value): |
paul@227 | 146 | |
paul@227 | 147 | "Set at the normalised (positive) 'index' the given 'value'." |
paul@227 | 148 | |
paul@265 | 149 | self._check_index(index) |
paul@356 | 150 | return list_setelement(self.__data__, index, value) |
paul@227 | 151 | |
paul@6 | 152 | # vim: tabstop=4 expandtab shiftwidth=4 |