Skip to content

Commit 13ead9b

Browse files
authored
f25-p0 fix: add back seeded HashFunction util, mod contention ratio test (#830)
f25-p0 fix: add back seeded HashFunction util
1 parent 11ddbdd commit 13ead9b

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/include/primer/count_min_sketch.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
#pragma once
1414

1515
#include <cstdint>
16+
#include <functional>
1617
#include <utility>
1718
#include <vector>
1819

20+
#include "common/util/hash_util.h"
21+
1922
namespace bustub {
2023

2124
template <typename KeyType>
@@ -79,6 +82,25 @@ class CountMinSketch {
7982
/** Dimensions of the count-min sketch matrix */
8083
const uint32_t width_; // Number of buckets for each hash function
8184
const uint32_t depth_; // Number of independent hash functions
85+
/** Pre-computed hash functions for each row */
86+
std::vector<std::function<size_t(const KeyType &)>> hash_functions_;
87+
88+
/** @fall2025 PLEASE DO NOT MODIFY THE FOLLOWING */
89+
constexpr static size_t SEED_BASE = 15445;
90+
91+
/**
92+
* @brief Seeded hash function generator
93+
*
94+
* @param seed Used for creating independent hash functions
95+
* @return A function that maps items to column indices
96+
*/
97+
inline auto HashFunction(size_t seed) -> std::function<size_t(const KeyType &)> {
98+
return [seed, this](const KeyType &item) -> size_t {
99+
auto h1 = std::hash<KeyType>{}(item);
100+
auto h2 = bustub::HashUtil::CombineHashes(seed, SEED_BASE);
101+
return bustub::HashUtil::CombineHashes(h1, h2) % width_;
102+
};
103+
}
82104

83105
/** @todo (student) can add their data structures that support count-min sketch operations */
84106
};

src/primer/count_min_sketch.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ namespace bustub {
2727
template <typename KeyType>
2828
CountMinSketch<KeyType>::CountMinSketch(uint32_t width, uint32_t depth) : width_(width), depth_(depth) {
2929
/** @TODO(student) Implement this function! */
30+
31+
/** @fall2025 PLEASE DO NOT MODIFY THE FOLLOWING */
32+
// Initialize seeded hash functions
33+
hash_functions_.reserve(depth_);
34+
for (size_t i = 0; i < depth_; i++) {
35+
hash_functions_.push_back(this->HashFunction(i));
36+
}
3037
}
3138

3239
template <typename KeyType>

test/primer/count_min_sketch_test.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ TEST(CountMinSketchTest, ContentionRatioTest) {
721721
std::vector<size_t> time_ms_wo_mutex;
722722

723723
// Two Threads inserting 10000 entries each
724-
auto cms = CountMinSketch<int>(500, 15);
724+
auto cms = CountMinSketch<int64_t>(500, 15);
725725
const int insert_iters = 10000;
726726
const int num_threads = 2;
727727

@@ -760,6 +760,11 @@ TEST(CountMinSketchTest, ContentionRatioTest) {
760760
}
761761
}
762762

763+
// Check result correctness
764+
for (int i = 0; i < 10; i++) {
765+
ASSERT_EQ(cms.Count(i), 20000);
766+
}
767+
763768
std::cout << "<<< BEGIN" << std::endl;
764769
std::cout << "Multithreaded Insertion Time: ";
765770
double sum_1 = 0;

0 commit comments

Comments
 (0)