Example #1
0
    def remove(self, obj):
        """
        Remove a spcific entry(obj) form the hashtable
        :param obj: the entry being removed
        :return: the removed entry
        """
        if self.contains(obj):
            if self.sizeOff():
                self.rehash()
            toRemoved = ChainNode(obj)
            key = toRemoved.getKey(self._capacity)
            temp = self._list[key]
            temp_back = temp
            # if the entry to be removed is the first item in this linked list
            if str(temp) == obj:
                self._list[key] = self._list[key].getChain()
                temp.setChain(None)
            # else the entry to be removed is in other position than the first
            else:
                while not temp.getChain() is None:
                    temp = temp.getChain()
                    if str(temp) == obj:
                        temp_back.setChain(temp.getChain())
                        temp.setChain(None)
                        break
                    temp_back = temp_back.getChain()

            # if the entry to be removed is the only one left in hashtable
            if temp.getPrevious() is None and temp.getLink() is None:
                self._front = None
                self._back = None
            # if the entry to be removed is front
            elif temp.getPrevious() is None:
                self._front = temp.getLink()
                temp.getLink().setPrevious(None)
                temp.setLink(None)
            # if the entry to be removed is back
            elif temp.getLink() is None:
                self._back = temp.getPrevious()
                temp.getPrevious().setLink(None)
                temp.setPrevious(None)
            # if the entry to be removed is in the middle
            else:
                temp.getPrevious().setLink(temp.getLink())
                temp.getLink().setPrevious(temp.getPrevious())
                temp.setPrevious(None)
                temp.setLink(None)
            self._size -= 1
            return temp
Example #2
0
 def contains(self, obj):
     """
     Check if an entry(obj) is in hashtable
     :param obj: the entry being checked
     :return: true if this entry is found
     """
     toSearch = ChainNode(obj)
     key = toSearch.getKey(self._capacity)
     temp = self._list[key]
     if self._list[key] is None:
         return False
     elif str(temp) == obj:
         return True
     else:
         while not temp.getChain() is None:
             temp = temp.getChain()
             if str(temp) == obj:
                 return True
     return False