The Array-Backed List

Agenda

  1. The List Abstract Data Type (ADT)
  2. The List API
  3. Getting started: how to store our data?
  4. Built-in list as array
  5. The ArrayList data structure

1. The List Abstract Data Type (ADT)

An abstract data type (ADT) defines a conceptual model for how data may be stored and manipulated.

A list ADT is simply a container for values which are ordered in a sequence, where each value has at most one preceding and one succeeding value. A given value may appear more than once in a list.

A list data structure is a concrete implementation of the list ADT in some programming language, which, in addition to adhering to the basic premise of the ADT, will also typically support operations that:

  • access values in the list by their position (index)
  • append and insert new values into the list
  • remove values from the list

The implementation of any data structure will generally rely on simpler, constituent data types (e.g., "primitive" types offered by the language), the choice of which may affect the runtime complexities of said operations.

2. The List API

The operations we'll be building into our list data structures will be based on the common and mutable sequence operations defined by the Python library.

In [1]:
class List:        
    ### subscript-based access ###
    
    def __getitem__(self, idx):
        """Implements `x = self[idx]`"""
        pass

    def __setitem__(self, idx, value):
        """Implements `self[idx] = x`"""
        pass

    def __delitem__(self, idx):
        """Implements `del self[idx]`"""
        pass
    
    ### stringification ###
            
    def __repr__(self):
        """Supports inspection"""
        return '[]'
    
    def __str__(self):
        """Implements `str(self)`"""
        return '[]'

    ### single-element manipulation ###
    
    def append(self, value):
        pass
    
    def insert(self, idx, value):
        pass
    
    def pop(self, idx=-1):
        pass
    
    def remove(self, value):
        pass
    
    ### predicates (T/F queries) ###
    
    def __eq__(self, other):
        """Implements `self == other`"""
        return True

    def __contains__(self, value):
        """Implements `val in self`"""
        return True
    
    ### queries ###
    
    def __len__(self):
        """Implements `len(self)`"""
        return len(self.data)
    
    def min(self):
        pass
    
    def max(self):
        pass
    
    def index(self, value, i, j):
        pass
    
    def count(self, value):
        pass

    ### bulk operations ###

    def __add__(self, other):
        """Implements `self + other_array_list`"""
        return self
    
    def clear(self):
        pass
    
    def copy(self):
        pass

    def extend(self, other):
        pass

    ### iteration ###
    
    def __iter__(self):
        """Supports iteration (via `iter(self)`)"""
        pass

3. Getting started: how to store our data?

In [2]:
class List:
    def append(self, value):
        self.data = value
    
    def __getitem__(self, idx):
        """Implements `x = self[idx]`"""
        return self.data

    def __setitem__(self, idx, value):
        """Implements `self[idx] = x`"""
        self.data = value
    
    def __repr__(self):
        """Supports inspection"""
        return str(self.data)
In [3]:
l = List()
In [4]:
l.append('hello')
In [5]:
l[0]
Out[5]:
'hello'
In [6]:
l[0] = 'bye'
In [7]:
l[0]
Out[7]:
'bye'
In [8]:
l[10] = 'yo'
In [9]:
l[5]
Out[9]:
'yo'

We need a storage mechanism that allows us to store and keep track of multiple values. In most languages, an appropriate primitive data container would be an array.

But Python doesn't give us primitive arrays!

4. Built-in list as array

To use the built-in list as though it were a primitive array, we will constrain ourselves to just the following APIs on a given list lst:

  1. lst[i] for getting and setting values at an existing, positive index i
  2. len(lst) to obtain the number of slots
  3. lst.append(None) to grow the list by one slot at a time
  4. del lst[len(lst)-1] to delete the last slot in a list

We can assume operation (1) has time complexity O(1), based on our previous analysis of list indexing.

Operation (2) is trivially O(1), since we are merely asking the list for its size (which it keeps track of and just returns).

Operation (3) is a bit more complicated, but, generally speaking, the built-in list implementation does a good job ensuring that it can be incrementally increased in size in O(1) time. Occasionally, expanding a list may require that it be moved in memory to a new location with sufficient space to accommodate all the contiguous values, but this is rare, and the cost is amortized over the many fast operations.

Operation (4) is trivially O(1), since shrinking a list doesn't require any additional memory and doesn't affect any of the values other than the last one.

5. The ArrayList data structure

In [10]:
class ArrayList:
    def __init__(self):
        self.data = []

    def append(self, value):
        self.data.append(value)

    def __getitem__(self, idx):
        """Implements `x = self[idx]`"""
        assert(isinstance(idx, int))
        return self.data[idx]

    def __setitem__(self, idx, value):
        """Implements `self[idx] = x`"""
        assert(isinstance(idx, int))
        self.data[idx] = value

    def __delitem__(self, idx):
        """Implements `del self[idx]`"""
        assert(isinstance(idx, int))
        for i in range(idx+1, len(self.data)):
            self.data[i-1] = self.data[i]
        del self.data[len(self.data)-1]
    
    def __len__(self):
        """Implements `len(self)`"""
        return len(self.data)
    
    def __repr__(self):
        """Supports inspection"""
        return str(self.data)
In [11]:
lst = ArrayList()
In [12]:
lst
Out[12]:
[]
In [13]:
for x in range(10):
    lst.append(x)
In [14]:
lst[5]
Out[14]:
5
In [15]:
len(lst)
Out[15]:
10
In [16]:
lst
Out[16]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [17]:
lst['a'] # fails due to assertion
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-17-f21f5ea0e82e> in <module>()
----> 1 lst['a'] # fails due to assertion

<ipython-input-10-6a7051b7deae> in __getitem__(self, idx)
      8     def __getitem__(self, idx):
      9         """Implements `x = self[idx]`"""
---> 10         assert(isinstance(idx, int))
     11         return self.data[idx]
     12 

AssertionError: 
In [18]:
del lst[9]
In [19]:
lst
Out[19]:
[0, 1, 2, 3, 4, 5, 6, 7, 8]
In [20]:
del lst[0]
In [21]:
lst
Out[21]:
[1, 2, 3, 4, 5, 6, 7, 8]
In [22]:
lst[-1]
Out[22]:
8
In [23]:
class ArrayList(ArrayList):
    def __init__(self):
        self.data = []

    def append(self, value):
        self.data.append(value)
        
    def _normalize_idx(self, idx):
        nidx = idx
        if nidx < 0:
            nidx += len(self.data)
            if nidx < 0:
                nidx = 0
        return nidx
    
    def __getitem__(self, idx):
        """Implements `x = self[idx]`"""
        assert(isinstance(idx, int))
        idx = self._normalize_idx(idx)
        if idx > len(self.data):
            raise IndexError()
        return self.data[idx]
In [24]:
lst = ArrayList()
In [25]:
for x in range(10):
    lst.append(x)
In [26]:
lst[-1]
Out[26]:
9
In [27]:
lst[100]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-27-ee69601865a9> in <module>()
----> 1 lst[100]

<ipython-input-23-e262052fbdce> in __getitem__(self, idx)
     19         idx = self._normalize_idx(idx)
     20         if idx > len(self.data):
---> 21             raise IndexError()
     22         return self.data[idx]

IndexError: