def __init__(self, data): """ Initialize the iterator before the first entry in linkedhashtable :param data: the linkedhashtable class """ self.i = ChainNode("") self.i.setLink(data._front)
class Iterator: """ The internal iterator class """ __slots__ = "i" def __init__(self, data): """ Initialize the iterator before the first entry in linkedhashtable :param data: the linkedhashtable class """ self.i = ChainNode("") self.i.setLink(data._front) def __iter__(self): """ Define the iterator itself :return: the iterator itself """ return self def __next__(self): """ Get the next entry in the linkedhashtable class till the last entry :return: the next entry """ if not self.i.hasLink(): raise StopIteration() self.i = self.i.getLink() return self.i
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
def test_add(self): """ Test the add and _add function """ e1 = ChainNode("we") e2 = ChainNode("are") e3 = ChainNode("the") e4 = ChainNode("light") e5 = ChainNode("miwa") table = Linkedhashtable() self.assertEqual(str(table), "front -> <- back") table.add(e1) table.add(e2) table.add(e3) table.add(e4) table.add(e5) self.assertEqual(str(table), "front -> we are the light miwa <- back")
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
def test_contains(self): """ Test the contains function """ e1 = ChainNode("we") e2 = ChainNode("are") e3 = ChainNode("the") e4 = ChainNode("light") e5 = ChainNode("miwa") table = Linkedhashtable() table.add(e1) table.add(e2) table.add(e3) table.add(e4) table.add(e5) self.assertTrue(table.contains("we")) self.assertTrue(table.contains("the")) self.assertTrue(table.contains("miwa")) self.assertFalse(table.contains("you")) self.assertFalse(table.contains("wee"))
def test_iterator(self): """ Test the iterator class """ e1 = ChainNode("we") e2 = ChainNode("are") e3 = ChainNode("the") e4 = ChainNode("light") e5 = ChainNode("miwa") table = Linkedhashtable() table.add(e1) table.add(e2) table.add(e3) table.add(e4) table.add(e5) temp = table.__iter__() self.assertEqual(str(temp.__next__()), "we") self.assertEqual(str(temp.__next__()), "are") self.assertEqual(str(temp.__next__()), "the") self.assertEqual(str(temp.__next__()), "light") self.assertEqual(str(temp.__next__()), "miwa")
def test_rehash(self): """ Test the rehash, sizeOver, sizeOff, and _remove functions """ e1 = ChainNode("rise") e2 = ChainNode("on") e3 = ChainNode("up") e4 = ChainNode("till") e5 = ChainNode("ya") e6 = ChainNode("touching") e7 = ChainNode("moon") e8 = ChainNode("we") e9 = ChainNode("are") e10 = ChainNode("the") e11 = ChainNode("light") e12 = ChainNode("miwa") table = Linkedhashtable(6, 0.8) self.assertEqual(table._size, 0) self.assertEqual(table._capacity, 6) self.assertEqual(len(table._list), 6) table.add(e1) table.add(e2) table.add(e3) table.add(e4) table.add(e5) table.add(e6) table.add(e7) table.add(e8) table.add(e9) table.add(e10) table.add(e11) table.add(e12) self.assertEqual(table._size, 12) self.assertEqual(table._capacity, 24) self.assertEqual(len(table._list), 24) self.assertEqual( str(table), "front -> rise on up till ya touching moon" " we are the light miwa <- back") table.remove("ya") table.remove("up") table.remove("are") table.remove("on") table.remove("miwa") table.remove("moon") table.remove("rise") table.remove("the") self.assertEqual(table._size, 4) self.assertEqual(table._capacity, 12) self.assertEqual(len(table._list), 12) self.assertEqual(str(table), "front -> till touching we light <- back") table.remove("we") self.assertEqual(table._size, 3) self.assertEqual(table._capacity, 12) self.assertEqual(len(table._list), 12) self.assertEqual(str(table), "front -> till touching light <- back") table.remove("till") self.assertEqual(table._size, 2) self.assertEqual(table._capacity, 6) self.assertEqual(len(table._list), 6) self.assertEqual(str(table), "front -> touching light <- back") table.remove("light") self.assertEqual(table._size, 1) self.assertEqual(table._capacity, 3) self.assertEqual(len(table._list), 3) self.assertEqual(str(table), "front -> touching <- back") table.remove("touching") self.assertEqual(table._size, 0) self.assertEqual(table._capacity, 3) self.assertEqual(len(table._list), 3) self.assertEqual(str(table), "front -> <- back")