Esempio n. 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
Esempio n. 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
Esempio n. 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
Esempio n. 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
Esempio n. 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
Esempio n. 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
Esempio n. 7
0
 def __init__(self, *args, **kwargs):
     """ Takes an unlimited number of arguments which become the contents of the Vector
         Takes an optional kwarg `typesafe` which can be `True` or `False` """
     # Type safety operations (optional)
     if "typesafe" in kwargs:
         self.typesafe = kwargs["typesafe"]
         if self.typesafe != True and self.typesafe != False:
             raise TypeError(
                 "The `typesafe` argument to Vector was not `True` or `False`"
             )
     else:
         self.typesafe = False
     self.sentinel = Node("__SENTINEL__")
     self.sentinel.next = self.sentinel
     self.sentinel.prev = self.sentinel
     self.size = 0
     if ((not Vector.set_is_same_type(args)) and (self.typesafe == True)):
         raise TypeError("Arguments to the list are not the same type")
     else:
         # Build the list
         for item in args:
             self.insert_rear(item)
         self.size = len(args)