paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | List objects. |
paul@6 | 5 | |
paul@9 | 6 | Copyright (C) 2015, 2016 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@9 | 22 | from __builtins__.iterator import listiterator |
paul@9 | 23 | from __builtins__.sequence import _getitem, _getslice |
paul@140 | 24 | import native |
paul@6 | 25 | |
paul@6 | 26 | class list(object): |
paul@6 | 27 | |
paul@6 | 28 | "Implementation of list." |
paul@6 | 29 | |
paul@6 | 30 | def __init__(self, args=None): |
paul@6 | 31 | |
paul@6 | 32 | "Initialise the list." |
paul@6 | 33 | |
paul@206 | 34 | # Reserve an attribute for a fragment reference along with some space |
paul@206 | 35 | # for elements. |
paul@159 | 36 | |
paul@206 | 37 | self.__data__ = native._list_init(len(args)) |
paul@159 | 38 | |
paul@6 | 39 | if args is not None: |
paul@6 | 40 | self.extend(args) |
paul@6 | 41 | |
paul@6 | 42 | def __getitem__(self, index): |
paul@6 | 43 | |
paul@6 | 44 | "Return the item or slice specified by 'index'." |
paul@6 | 45 | |
paul@6 | 46 | return _getitem(self, index) |
paul@6 | 47 | |
paul@6 | 48 | def __contains__(self, value): pass |
paul@6 | 49 | def __setitem__(self, index, value): pass |
paul@6 | 50 | def __delitem__(self, index): pass |
paul@6 | 51 | |
paul@6 | 52 | def __getslice__(self, start, end=None): |
paul@6 | 53 | |
paul@6 | 54 | "Return a slice starting from 'start', with the optional 'end'." |
paul@6 | 55 | |
paul@6 | 56 | return _getslice(self, start, end) |
paul@6 | 57 | |
paul@6 | 58 | def __setslice__(self, start, end, slice): pass |
paul@6 | 59 | def __delslice__(self, start, end): pass |
paul@206 | 60 | |
paul@206 | 61 | def append(self, value): |
paul@206 | 62 | |
paul@206 | 63 | "Append 'value' to the list." |
paul@206 | 64 | |
paul@206 | 65 | native._list_append(self, value) |
paul@206 | 66 | |
paul@6 | 67 | def insert(self, i, value): pass |
paul@6 | 68 | |
paul@6 | 69 | def extend(self, iterable): |
paul@6 | 70 | |
paul@6 | 71 | "Extend the list with the contents of 'iterable'." |
paul@6 | 72 | |
paul@6 | 73 | for i in iterable: |
paul@6 | 74 | self.append(i) |
paul@6 | 75 | |
paul@6 | 76 | def pop(self): pass |
paul@6 | 77 | def reverse(self): pass |
paul@6 | 78 | def sort(self, cmp=None, key=None, reverse=0): pass |
paul@140 | 79 | |
paul@140 | 80 | def __len__(self): |
paul@140 | 81 | |
paul@140 | 82 | "Return the length of the list." |
paul@140 | 83 | |
paul@140 | 84 | return native._list_len(self) |
paul@140 | 85 | |
paul@6 | 86 | def __add__(self, other): pass |
paul@206 | 87 | |
paul@206 | 88 | def __iadd__(self, other): |
paul@206 | 89 | |
paul@206 | 90 | "Concatenate 'other' to the list." |
paul@206 | 91 | |
paul@206 | 92 | if isinstance(other, list): |
paul@206 | 93 | native._list_concat(self, other) |
paul@206 | 94 | else: |
paul@206 | 95 | self.extend(other) |
paul@206 | 96 | return self |
paul@198 | 97 | |
paul@198 | 98 | def __str__(self): |
paul@198 | 99 | |
paul@198 | 100 | "Return a string representation of the list." |
paul@198 | 101 | |
paul@206 | 102 | b = buffer() |
paul@206 | 103 | i = 0 |
paul@206 | 104 | l = self.__len__() |
paul@206 | 105 | first = True |
paul@206 | 106 | |
paul@206 | 107 | # NOTE: Should really show quoted forms of the items. |
paul@198 | 108 | |
paul@206 | 109 | b.append("[") |
paul@206 | 110 | while i < l: |
paul@206 | 111 | if first: |
paul@206 | 112 | first = False |
paul@206 | 113 | else: |
paul@206 | 114 | b.append(", ") |
paul@206 | 115 | b.append(self.__get_single_item__(i)) |
paul@206 | 116 | b.append("]") |
paul@206 | 117 | |
paul@206 | 118 | return str(b) |
paul@6 | 119 | |
paul@6 | 120 | def __bool__(self): |
paul@6 | 121 | |
paul@6 | 122 | "Lists are true if non-empty." |
paul@6 | 123 | |
paul@140 | 124 | return native._list_nonempty(self) |
paul@6 | 125 | |
paul@6 | 126 | def __iter__(self): |
paul@6 | 127 | |
paul@6 | 128 | "Return an iterator." |
paul@6 | 129 | |
paul@6 | 130 | return listiterator(self) |
paul@6 | 131 | |
paul@6 | 132 | # Special implementation methods. |
paul@6 | 133 | |
paul@140 | 134 | def __get_single_item__(self, index): |
paul@140 | 135 | |
paul@194 | 136 | "Return the item at the normalised (positive) 'index'." |
paul@194 | 137 | |
paul@194 | 138 | if index >= len(self): |
paul@194 | 139 | raise IndexError(index) |
paul@140 | 140 | |
paul@140 | 141 | return native._list_element(self, index) |
paul@6 | 142 | |
paul@6 | 143 | # vim: tabstop=4 expandtab shiftwidth=4 |