def test_contains(self):
     bucket = b.Bucket(5)
     for entry in entry_list:
         bucket.add_hash_entry(entry)
     self.assertTrue(bucket.contains(entry6))
     entry7 = he.HashEntry(np.array([6, 7, 8]), None, v.Voxel(None, None, None))
     self.assertFalse(bucket.contains(entry7))
    def add_hash_entry(self, hash_entry):
        """
        add the hash_entry to the hash map
        resolve collisions
        :return: tuple (ith_bucket, ith_entry), which represents the place it's stored; 0 if add failed
        """
        if hash_entry is None:
            return -1, -1

        if self.needs_resize():
            self.double_table_size()

        hash_value = self.hash_function(hash_entry.get_position())
        bucket = self.get_ith_bucket(hash_value)

        if bucket is None:
            bucket = b.Bucket(self._bucket_size)
            bucket.add_hash_entry(hash_entry)
            self.set_ith_bucket(bucket, hash_value)
            self._num_non_empty_bucket += 1
            return hash_value, 0
        else:
            if bucket.is_full():
                return self._add_to_linked_list(bucket, hash_value, hash_entry)
            else:
                return hash_value, bucket.add_hash_entry(hash_entry)
 def test_add(self):
     bucket = b.Bucket(5)
     self.assertEqual(bucket.get_num_entry_stored(), 0, "test count method")
     for i in range(5):
         self.assertEqual(bucket.add_hash_entry(entry_list[i]), i, "return index where entry is added")
     self.assertEqual(bucket.add_hash_entry(entry6), -1, "return -1 when add failed")
     bucket.remove_hash_entry(np.array([1,2,3]))
     bucket.remove_hash_entry(np.array([3,9,4]))
     self.assertEqual(bucket.add_hash_entry(entry6), 0)
 def test_is_overflow(self):
     bucket = b.Bucket(5)
     self.assertFalse(bucket.is_overflow())
     for entry in entry_list:
         bucket.add_hash_entry(entry)
     self.assertEqual(bucket.get_num_entry_stored(), 5, "current number of entries is 5")
     self.assertFalse(bucket.is_overflow())
     entry = bucket.get_ith_entry(-1)
     entry.set_offset((2,3))
     self.assertTrue(bucket.is_overflow(), "last entry has non-empty offset = overflow")
     bucket.remove_hash_entry(np.array([4, 4, 3]))
     self.assertEqual(bucket.get_num_entry_stored(), 4, "current number of entries is 4")
     self.assertTrue(bucket.is_overflow(), "overflow can occur even if the bucket is not full")
 def _find_next_free_space_and_add_entry(self, begin_b_idx, begin_e_idx,
                                         prev_entry, add_entry):
     """
     find the next available bucket to insert the hash entry
     :return: (idx_bucket, idx_entry) if inserted, else (-1,-1)
     """
     scanned_entire_map = False
     b_idx = begin_b_idx
     e_idx = begin_e_idx
     if e_idx < self._bucket_size - 1:
         bucket = self.get_ith_bucket(b_idx)
         # find next available space of current bucket to insert alien entry
         for i in range(e_idx, self._bucket_size - 1):
             if bucket.is_empty_entry(i):
                 bucket.set_ith_entry(add_entry, i)
                 prev_entry.set_offset((begin_b_idx, i))
                 return begin_b_idx, i
     # find the next available bucket to add entry
     while not scanned_entire_map:
         # set index to next bucket's index
         b_idx = 0 if b_idx >= self._table_size - 1 else b_idx + 1
         bucket = self.get_ith_bucket(b_idx)
         if bucket is None:
             bucket = b.Bucket(self._bucket_size)
             bucket.add_hash_entry(add_entry)
             self.set_ith_bucket(bucket, b_idx)
             self._num_non_empty_bucket += 1
             prev_entry.set_offset((b_idx, 0))
             return b_idx, 0
         if bucket.is_full_for_alien_entries():
             scanned_entire_map = b_idx == begin_b_idx
         # bucket is neither none nor full
         else:
             ith_entry = bucket.add_hash_entry(add_entry)
             prev_entry.set_offset((b_idx, ith_entry))
             return b_idx, ith_entry
     return -1, -1
 def test_remove(self):
     bucket = b.Bucket(5)
     for entry in entry_list:
         bucket.add_hash_entry(entry)
     self.assertEqual(bucket.remove_hash_entry(np.array([0,0,1])), -1)
     self.assertEqual(bucket.remove_hash_entry(np.array([3,9,4])), 2)
 def test_get(self):
     bucket = b.Bucket(5)
     for entry in entry_list:
         bucket.add_hash_entry(entry)
     self.assertTrue(entry2.equals(bucket.get_hash_entry(np.array([2,0,5]))))
     self.assertTrue(bucket.get_hash_entry(np.array([1,5,2])) is None)
 def test_is_full(self):
     bucket = b.Bucket(5)
     self.assertFalse(bucket.is_full())
     for entry in entry_list:
         bucket.add_hash_entry(entry)
     self.assertTrue(bucket.is_full())