Exemple #1
0
 def __init__(self, sourceCollection = None):
     # Now tracks occupied cells for rehashing if necessary.
     self._array = Array(HashSet.DEFAULT_CAPACITY)
     self._foundNode = self._priorNode = None
     self._index = -1
     self._occupiedCells = 0
     AbstractCollection.__init__(self, sourceCollection)
Exemple #2
0
 def __init__(self, sourceDictionary=None):
     """Will copy items to the collection from sourceDictionary
     if it's present."""
     self._array = Array(HashDict.DEFAULT_CAPACITY)
     self._foundNode = self._priorNode = None
     self._index = -1
     AbstractDict.__init__(self, sourceDictionary)
Exemple #3
0
 def pop(self):
     """Removes and returns the item at the front of the queue.
     Precondition: the queue is not empty.
     Raises: KeyError if queue is empty.
     Postcondition: the front item is removed from the queue."""
     if self.isEmpty():
         raise KeyError("Queue is empty")
     data = self._items[self._front]
     self._size -= 1
     if self.isEmpty(): self._front = self._rear = -1
     elif self._front == len(self._items) - 1:
         self._front = 0
     else:
         self._front += 1
     if len(self) <= .25 * len(self._items) and \
        ArrayQueue.DEFAULT_CAPACITY <= len(self._items) // 2:
         tempArray = Array(len(self._items) // 2)
         i = 0
         for item in self:
             tempArray[i] = item
             i += 1
         self._items = tempArray
         if not self.isEmpty():
             self._front = 0
             self._rear = len(self) - 1
     return data
Exemple #4
0
 def add(self, item):
     """Inserts item at rear of the queue."""
     # Resize array if full
     if len(self) == len(self._items):
         temp = Array(len(self._items) * 2)
         # Copy so that front is at 0 and rear is at len(self) - 1
         i = 0
         # Copy data from position front through the end of the array
         for j in range(self._front, len(self)):
             temp[i] = self._items[j]
             i += 1
         if self._rear < len(self) - 1:
             # Copy data from position 0 through the rear
             for j in range(0, self._rear + 1):
                 temp[i] = self._items[j]
                 i += 1
         self._items = temp
         # Normalize front and rear
         self._front = 0
         self._rear = len(self) - 1
     if self.isEmpty():
         self._front = self._rear = 0
     elif self._rear == len(self._items) - 1:
         self._rear = 0
     else:
         self._rear += 1
     self._items[self._rear] = item
     self._size += 1
Exemple #5
0
 def remove(self, item):
     """Precondition: item is in self.
     Raises: KeyError if item in not in self.
     Postcondition: item is removed from self."""
     # Check precondition and raise if necessary
     if not item in self:
         raise KeyError(str(item) + " not in bag")
     # Search for the index of the target item
     targetIndex = 0
     for targetItem in self:
         if targetItem == item:
             break
         targetIndex += 1
     # Shift items to the left of target up by one position
     for i in range(targetIndex, len(self) - 1):
         self._items[i] = self._items[i + 1]
     # Decrement logical size
     self._size -= 1
     # Check array memory here and decrease it if necessary
     if len(self) < len(self._items) // 3 and \
        2 * len(self) >= ArrayBag.DEFAULT_CAPACITY:
         temp = Array(len(self._items) // 2)
         for i in range(len(self)):
             temp[i] = self[i]
         self._items = temp
Exemple #6
0
 def rehash(self):
     """Resizes the dictionary and rehashes its keys."""
     if self.loadFactor() > 0.5:
         items = self.items()
         self._array = Array(len(self._array) * 2)
         self._size = 0
         for item in items:
             self[item.key] = item.value
Exemple #7
0
 def __init__(self, sourceCollection=None):
     """Sets the initial state of self, which includes the
     contents of sourceCollection, if it's present."""
     self._items = Array(ArrayBag.DEFAULT_CAPACITY)
     self._size = 0
     if sourceCollection:
         for item in sourceCollection:
             self.add(item)
Exemple #8
0
 def _resize(self, array, logicalSize):
     """If the array needs resizing, resizes and returns
     the new array.  Otherwise, returns the olf array."""
     temp = None
     # If array is full
     if len(array) == logicalSize:
         temp = Array(2 * len(array))
     # If array is wasting space
     elif logicalSize <= .25 * len(array) and \
          len(array) >= ArrayList.DEFAULT_CAPACITY:
         temp = Array(round(.5 * len(array)))
     # None of the above
     else:
         return array
     # Copy items to new array
     for i in range(logicalSize):
         temp[i] = array[i]
     return temp
Exemple #9
0
 def push(self, item):
     """Inserts item at top of the stack."""
     # Resize array here if necessary
     if len(self) == len(self._items):
         temp = Array(2 * len(self))
         for i in range(len(self)):
             temp[i] = self._items[i]
         self._items = temp
     self._items[len(self)] = item
     self._size += 1
Exemple #10
0
 def add(self, item):
     """Adds item to self."""
     # Check array memory here and increase it if necessary
     if len(self) == len(self._items):
         temp = Array(2 * len(self))
         for i in range(len(self)):
             temp[i] = self[i]
         self._items = temp
     self._items[len(self)] = item
     self._size += 1
Exemple #11
0
 def pop(self):
     """Removes and returns the item at the top of the stack.
     Precondition: the stack is not empty.
     Raises: KeyError if stack is empty.
     Postcondition: the top item is removed from the stack."""
     if self.isEmpty():
         raise KeyError("The stack is empty")
     oldItem = self._items[len(self) - 1]
     self._size -= 1
     # Resize the array here if necessary
     if len(self) <= len(self._items) // 4 and \
        len(self._items) >= 2 * ArrayStack.DEFAULT_CAPACITY:
         temp = Array(len(self._items) // 2)
         for i in range(len(self)):
             temp[i] = self._items[i]
         self._items = temp
     return oldItem
Exemple #12
0
 def __init__(self, sourceCollection=None):
     """Sets the initial state of self, which includes the
     contents of sourceCollection, if it's present."""
     self._items = Array(ArrayList.DEFAULT_CAPACITY)
     AbstractList.__init__(self, sourceCollection)
Exemple #13
0
 def _rehash(self):
     items = list(self)
     self._array = Array(len(self._array) * 2)
     self._size = 0
     for item in items:
         self.add(item)
Exemple #14
0
 def clear(self):
     """Makes self become empty."""
     self._size = 0
     self._array = Array(HashSet.DEFAULT_CAPACITY)
Exemple #15
0
 def clear(self):
     """Makes self become empty."""
     self._size = 0
     self._front = self._rear = -1
     self._items = Array(ArrayQueue.DEFAULT_CAPACITY)
Exemple #16
0
 def clear(self):
     """Makes self become empty."""
     self._size = 0
     self._items = Array(ArrayList.DEFAULT_CAPACITY)
Exemple #17
0
 def __init__(self, rows, columns, fillValue=None):
     self._data = Array(rows)
     for row in range(rows):
         self._data[row] = Array(columns, fillValue)