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@224 | 23 | from __builtins__.sequence import sequence |
paul@140 | 24 | import native |
paul@6 | 25 | |
paul@224 | 26 | class list(sequence): |
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 __contains__(self, value): pass |
paul@6 | 43 | def __setitem__(self, index, value): pass |
paul@6 | 44 | def __delitem__(self, index): pass |
paul@6 | 45 | def __setslice__(self, start, end, slice): pass |
paul@6 | 46 | def __delslice__(self, start, end): pass |
paul@206 | 47 | |
paul@206 | 48 | def append(self, value): |
paul@206 | 49 | |
paul@206 | 50 | "Append 'value' to the list." |
paul@206 | 51 | |
paul@206 | 52 | native._list_append(self, value) |
paul@206 | 53 | |
paul@6 | 54 | def insert(self, i, value): pass |
paul@6 | 55 | |
paul@6 | 56 | def extend(self, iterable): |
paul@6 | 57 | |
paul@6 | 58 | "Extend the list with the contents of 'iterable'." |
paul@6 | 59 | |
paul@6 | 60 | for i in iterable: |
paul@6 | 61 | self.append(i) |
paul@6 | 62 | |
paul@6 | 63 | def pop(self): pass |
paul@6 | 64 | def reverse(self): pass |
paul@6 | 65 | def sort(self, cmp=None, key=None, reverse=0): pass |
paul@140 | 66 | |
paul@140 | 67 | def __len__(self): |
paul@140 | 68 | |
paul@140 | 69 | "Return the length of the list." |
paul@140 | 70 | |
paul@140 | 71 | return native._list_len(self) |
paul@140 | 72 | |
paul@6 | 73 | def __add__(self, other): pass |
paul@206 | 74 | |
paul@206 | 75 | def __iadd__(self, other): |
paul@206 | 76 | |
paul@206 | 77 | "Concatenate 'other' to the list." |
paul@206 | 78 | |
paul@206 | 79 | if isinstance(other, list): |
paul@206 | 80 | native._list_concat(self, other) |
paul@206 | 81 | else: |
paul@206 | 82 | self.extend(other) |
paul@206 | 83 | return self |
paul@198 | 84 | |
paul@198 | 85 | def __str__(self): |
paul@198 | 86 | |
paul@198 | 87 | "Return a string representation of the list." |
paul@198 | 88 | |
paul@206 | 89 | b = buffer() |
paul@206 | 90 | i = 0 |
paul@206 | 91 | l = self.__len__() |
paul@206 | 92 | first = True |
paul@206 | 93 | |
paul@206 | 94 | # NOTE: Should really show quoted forms of the items. |
paul@198 | 95 | |
paul@206 | 96 | b.append("[") |
paul@206 | 97 | while i < l: |
paul@206 | 98 | if first: |
paul@206 | 99 | first = False |
paul@206 | 100 | else: |
paul@206 | 101 | b.append(", ") |
paul@222 | 102 | b.append(repr(self.__get_single_item__(i))) |
paul@218 | 103 | i += 1 |
paul@206 | 104 | b.append("]") |
paul@206 | 105 | |
paul@206 | 106 | return str(b) |
paul@6 | 107 | |
paul@222 | 108 | __repr__ = __str__ |
paul@222 | 109 | |
paul@6 | 110 | def __bool__(self): |
paul@6 | 111 | |
paul@6 | 112 | "Lists are true if non-empty." |
paul@6 | 113 | |
paul@140 | 114 | return native._list_nonempty(self) |
paul@6 | 115 | |
paul@6 | 116 | def __iter__(self): |
paul@6 | 117 | |
paul@6 | 118 | "Return an iterator." |
paul@6 | 119 | |
paul@6 | 120 | return listiterator(self) |
paul@6 | 121 | |
paul@6 | 122 | # Special implementation methods. |
paul@6 | 123 | |
paul@140 | 124 | def __get_single_item__(self, index): |
paul@140 | 125 | |
paul@194 | 126 | "Return the item at the normalised (positive) 'index'." |
paul@194 | 127 | |
paul@194 | 128 | if index >= len(self): |
paul@194 | 129 | raise IndexError(index) |
paul@140 | 130 | |
paul@140 | 131 | return native._list_element(self, index) |
paul@6 | 132 | |
paul@6 | 133 | # vim: tabstop=4 expandtab shiftwidth=4 |