Lichen

lib/__builtins__/list.py

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