コード例 #1
0
 def makeEmpty(self):
     tempIndex = 0
     while tempIndex < len(self.buckets):
         self.buckets[tempIndex] = MapEntry()
         tempIndex += 1
     self.currentSize = 0
     return
コード例 #2
0
 def __init__(self,
              keyComparator: Comparator = None,
              valueComparator: Comparator = None,
              size: int = 10):
     """
     :type keyComparator: object
     :type valueComparator: object
     :type size: int
     """
     self.buckets = [MapEntry()] * size
     self.currentSize = 0
     self.keyComparator = keyComparator
     self.valueComparator = valueComparator
コード例 #3
0
 def put(self, key, value) -> bool:
     # We first create the object to be added into the table and initialize final index position
     newEntry = MapEntry(key, value, True)
     # We first check if it exists on the the table
     # OPTIONAL - IF DESIRED WE CAN RESTRICT THE LIST TO ONLY
     # ACCEPT ELEMENTS THAT ARE NOT ALREADY IN HASH TABLE
     if self.containsKey(key):
         self.remove(key)
     # Check if percentage of elements is below 50% of capacity
     if (self.currentSize / len(self.buckets)) >= 0.5:
         # if its over half capacity, extend array (WORST CASE SCENARIO)
         self.__reallocate()
     # ---------------------------------------
     # We begin by first locating first  hashing position
     firstFreePosition = self.linearHashFunction(key, self.buckets)
     entryIndex = firstFreePosition
     if self.buckets[entryIndex] is not None and self.buckets[
             entryIndex].isStatus() is not False:
         # ---------------------------------------
         # If first hashing position fails we proceed to second hash
         secondFreePosition = self.squareHashFunction(key, self.buckets)
         entryIndex = secondFreePosition
         if self.buckets[entryIndex] is not None and self.buckets[
                 entryIndex].isStatus() is not False:
             # ---------------------------------------
             # If second hashing position fails we proceed to second hash
             thirdFreePosition = self.cubicHashFunction(key, self.buckets)
             entryIndex = thirdFreePosition
             if self.buckets[entryIndex] is not None and self.buckets[
                     entryIndex].isStatus() is not False:
                 # ---------------------------------------
                 # If third hash fails we proceed to linear probing
                 tempIndex = (self.linearHashFunction(key, self.buckets) +
                              1) % len(self.buckets)
                 while tempIndex != self.linearHashFunction(
                         key, self.buckets):
                     # If empty space is found then set index to such spot
                     if self.buckets[tempIndex] is not None and self.buckets[
                             tempIndex].isStatus() is False:
                         entryIndex = tempIndex
                         break
                     tempIndex = (tempIndex + 1) % len(self.buckets)
     self.buckets[entryIndex] = newEntry
     self.currentSize += 1
     return True
コード例 #4
0
 def put(self, key, value):
     # Creation of new element to be included in Hash Table
     newEntry = MapEntry(key, value, True)
     # Check if element doesn't exist
     if self.containsKey(key) is True:
         # if it does exist delete
         self.remove(key)
     # We check if the existing ratio between elements and spaces hasn't surpassed 70%
     if self.size() / len(self.buckets) >= 0.7:
         # if surpassed reAllocate the hash table
         self.__reAllocate()
     # To know corresponding location, we hash it first
     firstElementPosition = self.linearHashFunction(key, self.buckets)
     firstList = self.buckets[firstElementPosition]
     # We add it to the list
     firstList.add(newEntry)
     self.currentSize += 1
     return True
コード例 #5
0
 def remove(self, key) -> object:
     # We begin search of the element with first hash functions
     firstPosition = self.linearHashFunction(key, self.buckets)
     eliminateIndex = firstPosition
     firstElement = self.buckets[firstPosition]
     if firstElement.isStatus() is False or firstElement.getKey(
     ) is not key:
         # If first hashing fails proceed to second hashing
         secondPosition = self.squareHashFunction(key, self.buckets)
         secondElement = self.buckets[secondPosition]
         eliminateIndex = secondPosition
         if secondElement.isStatus() is False or secondElement.getKey(
         ) is not key:
             # If first hashing fails proceed to second hashing
             thirdPosition = self.cubicHashFunction(key, self.buckets)
             thirdElement = self.buckets[thirdPosition]
             eliminateIndex = thirdPosition
             if thirdElement.isStatus() is False or thirdElement.getKey(
             ) is not key:
                 # If third hashing fails we proceed into linear probing
                 tempIndex = (self.linearHashFunction(key, self.buckets) +
                              1) % len(self.buckets)
                 while tempIndex != self.linearHashFunction(
                         key, self.buckets):
                     # If specified loc
                     if self.buckets[tempIndex].isStatus(
                     ) is True and self.buckets[tempIndex].getKey() is key:
                         eliminateIndex = tempIndex
                         break
                     tempIndex = (tempIndex + 1) % len(self.buckets)
             # if linear probe fails the object doesn't exist
             if self.linearHashFunction(key, self.buckets) == tempIndex:
                 return None
     result = self.buckets[eliminateIndex].getValue()
     self.buckets[eliminateIndex] = MapEntry()
     self.currentSize -= 1
     return result