Skip to content

Commit 0d4e3f8

Browse files
P0 Starter Code (#729)
* P0 Starter Code * Minor Change to fix in GCC * More changes to fix * Fixing format erros * Fixing Clang-tidy errors * Tidy fix 2 * Improving Grammar in comments
1 parent 89c7a5f commit 0d4e3f8

File tree

7 files changed

+517
-4
lines changed

7 files changed

+517
-4
lines changed

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,10 @@ add_custom_target(fix-clang-tidy-diff
250250
# hardcode some files to check here for each project.
251251
# ##########################################################
252252
set(P0_FILES
253-
"src/planner/plan_func_call.cpp"
254-
"src/include/execution/expressions/string_expression.h"
255-
"src/include/primer/orset.h"
256-
"src/primer/orset.cpp"
253+
"src/include/primer/hyperloglog.h"
254+
"src/include/primer/hyperloglog_presto.h"
255+
"src/primer/hyperloglog.cpp"
256+
"src/primer/hyperloglog_presto.cpp"
257257
)
258258

259259
add_custom_target(check-clang-tidy-p0

src/include/primer/hyperloglog.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#pragma once
2+
3+
#include <bitset>
4+
#include <functional>
5+
#include <memory>
6+
#include <mutex> // NOLINT
7+
#include <string>
8+
#include <vector>
9+
10+
#define MAX_BITS 64
11+
#define DEFAULT_CARDINALITY 0
12+
13+
namespace bustub {
14+
15+
/** @brief Hash value type. */
16+
using hash_t = uint64_t;
17+
18+
/** @brief Constant for HLL. */
19+
static const double CONSTANT = 0.79402;
20+
21+
template <typename T>
22+
class HyperLogLog {
23+
public:
24+
HyperLogLog() = delete;
25+
26+
explicit HyperLogLog(int16_t n_bits) : cardinality_(0) {}
27+
28+
/**
29+
* @brief Getter value for cardinality.
30+
*
31+
* @returns cardinality value
32+
*/
33+
auto GetCardinality() { return cardinality_; }
34+
35+
/**
36+
* @brief Ddds a value into the HyperLogLog.
37+
*
38+
* @param[in] val - value that's added into hyperloglog
39+
*/
40+
auto AddElem(T val) -> void;
41+
42+
/**
43+
* @brief Function that computes cardinality.
44+
*/
45+
auto ComputeCardinality() -> void;
46+
47+
private:
48+
/**
49+
* @brief Calculates Hash of a given value.
50+
*
51+
* @param[in] val - value
52+
* @returns hash integer of given input value
53+
*/
54+
inline auto CalculateHash(T val) -> hash_t;
55+
56+
/**
57+
* @brief Function that computes binary.
58+
*
59+
*
60+
* @param[in] hash
61+
* @returns binary of a given hash
62+
*/
63+
auto ComputeBinary(const hash_t &hash) const -> std::bitset<MAX_BITS>;
64+
65+
/**
66+
* @brief Function that computes leading zeros.
67+
*
68+
* @param[in] bset - binary values of a given bitset
69+
* @returns leading zeros of given binary set
70+
*/
71+
auto PositionOfLeftmostOne(const std::bitset<MAX_BITS> &bset) const -> uint64_t;
72+
73+
/** @brief Cardinality value. */
74+
size_t cardinality_;
75+
76+
/** @todo (student) can add their data structures that support HyperLogLog */
77+
};
78+
79+
} // namespace bustub
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#pragma once
2+
3+
#include <bitset>
4+
#include <functional>
5+
#include <memory>
6+
#include <mutex> // NOLINT
7+
#include <string>
8+
#include <unordered_map>
9+
#include <vector>
10+
11+
#define BUCKET_SIZE 4UL
12+
#define CONSTANT 0.79402
13+
#define MAX_SIZE 7UL
14+
15+
namespace bustub {
16+
17+
template <typename T>
18+
class HyperLogLogPresto {
19+
/**
20+
* INSTRUCTIONS: Testing framework will use the GetDenseBucket and GetOverflow function,
21+
* hence SHOULD NOT be deleted. It's essential to use the dense_bucket_
22+
* data structure.
23+
*/
24+
25+
/** @brief Hash type. */
26+
using hash_t = uint64_t;
27+
28+
public:
29+
/** @brief Disabling default constructor. */
30+
HyperLogLogPresto() = delete;
31+
32+
/** @brief Parameterized constructor. */
33+
explicit HyperLogLogPresto(int16_t n_leading_bits) : cardinality_(0) {}
34+
35+
/** @brief Returns the dense_bucket_ data structure. */
36+
auto GetDenseBucket() const -> std::vector<std::bitset<BUCKET_SIZE>> { return dense_bucket_; }
37+
38+
/** @brief Returns overflow bucket of a specific given index. */
39+
auto GetOverflowBucketofIndex(uint16_t idx) { return overflow_bucket_[idx]; }
40+
41+
/** @brief Retusn the cardinality of the set. */
42+
auto GetCardinality() const -> uint64_t { return cardinality_; }
43+
44+
/** @brief Element is added for HLL calculation. */
45+
auto AddElem(T val) -> void;
46+
47+
/** @brief Function to compute cardinality. */
48+
auto ComputeCardinality() -> void;
49+
50+
private:
51+
/** @brief Calculate Hash.
52+
*
53+
* @param[in] val
54+
*
55+
* @returns hash value
56+
*/
57+
inline auto CalculateHash(T val) -> hash_t { return std::hash<T>{}(val); }
58+
59+
/** @brief Structure holding dense buckets (or also known as registers). */
60+
std::vector<std::bitset<BUCKET_SIZE>> dense_bucket_;
61+
62+
/** @brief Structure holding overflow buckets. */
63+
std::unordered_map<uint16_t, std::bitset<MAX_SIZE - BUCKET_SIZE>> overflow_bucket_;
64+
65+
/** @brief Storing cardinality value */
66+
uint64_t cardinality_;
67+
68+
// TODO(student) - can add more data structures as required
69+
};
70+
71+
} // namespace bustub

src/primer/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
add_library(
22
bustub_primer
33
OBJECT
4+
hyperloglog.cpp
5+
hyperloglog_presto.cpp
46
orset.cpp
57
orset_driver.cpp
68
trie.cpp

src/primer/hyperloglog.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "primer/hyperloglog.h"
2+
3+
namespace bustub {
4+
5+
template <typename T>
6+
auto inline HyperLogLog<T>::CalculateHash(T val) -> hash_t {
7+
/** @todo student - implement the function */
8+
return 0;
9+
}
10+
11+
template <typename T>
12+
auto HyperLogLog<T>::ComputeBinary(const hash_t &hash) const -> std::bitset<MAX_BITS> {
13+
/** @todo student - implement the function */
14+
return {0};
15+
}
16+
17+
template <typename T>
18+
auto HyperLogLog<T>::PositionOfLeftmostOne(const std::bitset<MAX_BITS> &bset) const -> uint64_t {
19+
/** @todo student - implement the function*/
20+
return 0;
21+
}
22+
23+
template <typename T>
24+
auto HyperLogLog<T>::AddElem(T val) -> void {
25+
/** @todo implement the function */
26+
}
27+
28+
template <typename T>
29+
auto HyperLogLog<T>::ComputeCardinality() -> void {
30+
/** @todo - student implement the function */
31+
}
32+
33+
template class HyperLogLog<int64_t>;
34+
template class HyperLogLog<std::string>;
35+
36+
} // namespace bustub

src/primer/hyperloglog_presto.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "primer/hyperloglog_presto.h"
2+
3+
namespace bustub {
4+
5+
template <typename T>
6+
auto HyperLogLogPresto<T>::AddElem(T val) -> void {
7+
/** @todo (student) has to fill the function */
8+
}
9+
10+
template <typename T>
11+
auto HyperLogLogPresto<T>::ComputeCardinality() -> void {
12+
// TODO(student) - implement the function
13+
}
14+
15+
template class HyperLogLogPresto<int64_t>;
16+
template class HyperLogLogPresto<std::string>;
17+
} // namespace bustub

0 commit comments

Comments
 (0)