def put(self, k, v):
     """Associate value, v, with the key, k."""
     hc = hash(k) % self.M
     entry = self.table[hc]
     if entry:
         if entry.key == k:
             entry.value = v
         else:
             raise RuntimeError('Key Collision between {} and {}'.format(
                 k, entry.key))
     else:
         self.table[hc] = Entry(k, v)
Exemple #2
0
    def put(self, k, v):
        """Associate value, v, with the key, k."""
        hc = hash(k) % self.M  # First place it could be
        while self.table[hc]:
            if self.table[hc].key == k:  # Overwrite if already here
                self.table[hc].value = v
                return
            hc = (hc + 1) % self.M

        if self.N >= self.M - 1:
            raise RuntimeError('Table is Full: cannot store {} -> {}'.format(
                k, v))

        self.table[hc] = Entry(k, v)
        self.N += 1
Exemple #3
0
    def test_entry(self):
        from ch03.entry import Entry, LinkedEntry, MarkedEntry
        e = Entry('key1', 'val1')
        self.assertEqual('key1 -> val1', str(e))

        le = LinkedEntry('key1', 'val1')
        self.assertEqual('key1 -> val1', str(le))

        me = MarkedEntry('key1', 'val1')
        self.assertEqual('key1 -> val1', str(me))
        me.mark()
        self.assertEqual('key1 -> val1 [Marked]', str(me))
        self.assertTrue(me.is_marked())
        me.unmark()
        self.assertFalse(me.is_marked())
        self.assertEqual('key1 -> val1', str(me))
Exemple #4
0
    def put(self, k, v):
        """Associate value, v, with the key, k."""
        hc = hash(k) % self.M  # First place it could be
        while self.table[hc]:
            if self.table[hc].key == k:  # Overwrite if already here
                self.table[hc].value = v
                return
            hc = (hc + 1) % self.M

        # With Open Addressing, you HAVE to insert first into the
        # empty bucket before checking whether you have hit
        # the threshold, otherwise you have to search again to
        # find an empty space. The impact is that this last entry
        # is "inserted twice" on resize; small price to pay. Note
        # That this last entry COULD be the last empty bucket, but
        # the forced resize below will resolve that issue
        self.table[hc] = Entry(k, v)
        self.N += 1

        if self.N >= self.threshold:
            self.resize(2 * self.M + 1)
 def put(self, k, v):
     """Associate value, v, with the key, k."""
     hc = perfect_hash(k)
     self.table[hc] = Entry(k, v)
     self.N += 1