Пример #1
0
 def insert(self, index, item):
     """ Adds an item to the Vector at the specified index thus moving all later
         entries down one index.
         Allows negative indexing so a[-1] is the last item
         Raises an IndexError if the index is >= abs(self.size)
         Raises a TypeError if the type of item does not match the Vector type """
     if abs(index) >= self.size:
         raise IndexError("Index out of bounds")
     elif self.size == 0:
         if index != 0:
             raise IndexError(
                 "The vector is empty and only the 0 index is currently active"
             )
         else:
             self.insert_front(item)
     elif ((not self.item_matches_vector_type(item))
           and (self.typesafe == True)):
         raise TypeError(
             "An item was added to the vector with an incompatible type")
     else:
         node = self.__getnode(index)
         new_node = Node(item)
         new_node.prev = node.prev
         new_node.next = node
         node.prev.next = new_node
         node.prev = new_node
         self.sze += 1
Пример #2
0
 def insert_front(self, item):
     """ Prepends an item to the Vector
         Raises a TypeError if the type of item does not match the type of the Vector. """
     if ((not self.item_matches_vector_type(item)) and (self.typesafe == True)):
         raise TypeError("An item was added to the vector with an incompatible type")
     else:
         temp = Node(item)
         temp.next = self.sentinel.next
         temp.prev = self.sentinel
         self.sentinel.next.prev = temp
         self.sentinel.next = temp
         self.size += 1
Пример #3
0
 def insert_rear(self, item):
     """ Appends an item to the vector. 
         Raises a TypeError if the type of `item` does not match the type of the Vector
             AND typesafe==True """
     if ((not self.item_matches_vector_type(item)) and (self.typesafe == True)):
         raise TypeError("An item was added to the vector with an incompatible type")
     else:
         temp = Node(item)
         temp.prev = self.sentinel.prev
         temp.next = self.sentinel
         self.sentinel.prev.next = temp
         self.sentinel.prev = temp
         self.size += 1
Пример #4
0
 def insert_front(self, item):
     """ Prepends an item to the Vector
         Raises a TypeError if the type of item does not match the type of the Vector. """
     if ((not self.item_matches_vector_type(item))
             and (self.typesafe == True)):
         raise TypeError(
             "An item was added to the vector with an incompatible type")
     else:
         temp = Node(item)
         temp.next = self.sentinel.next
         temp.prev = self.sentinel
         self.sentinel.next.prev = temp
         self.sentinel.next = temp
         self.size += 1
Пример #5
0
 def insert_rear(self, item):
     """ Appends an item to the vector. 
         Raises a TypeError if the type of `item` does not match the type of the Vector
             AND typesafe==True """
     if ((not self.item_matches_vector_type(item))
             and (self.typesafe == True)):
         raise TypeError(
             "An item was added to the vector with an incompatible type")
     else:
         temp = Node(item)
         temp.prev = self.sentinel.prev
         temp.next = self.sentinel
         self.sentinel.prev.next = temp
         self.sentinel.prev = temp
         self.size += 1
Пример #6
0
 def insert(self, index, item):
     """ Adds an item to the Vector at the specified index thus moving all later
         entries down one index.
         Allows negative indexing so a[-1] is the last item
         Raises an IndexError if the index is >= abs(self.size)
         Raises a TypeError if the type of item does not match the Vector type """
     if abs(index) >= self.size:
         raise IndexError("Index out of bounds")
     elif self.size == 0:
         if index != 0:
             raise IndexError("The vector is empty and only the 0 index is currently active")
         else:
             self.insert_front(item)
     elif ((not self.item_matches_vector_type(item)) and (self.typesafe == True)):
         raise TypeError("An item was added to the vector with an incompatible type")
     else:
         node = self.__getnode(index)
         new_node = Node(item)
         new_node.prev = node.prev
         new_node.next = node
         node.prev.next = new_node
         node.prev = new_node
         self.sze += 1