Skip to content

Commit

Permalink
P0 Starter Code (#729)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
prashanthduvvada committed Aug 26, 2024
1 parent 89c7a5f commit 0d4e3f8
Show file tree
Hide file tree
Showing 7 changed files with 517 additions and 4 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,10 @@ add_custom_target(fix-clang-tidy-diff
# hardcode some files to check here for each project.
# ##########################################################
set(P0_FILES
"src/planner/plan_func_call.cpp"
"src/include/execution/expressions/string_expression.h"
"src/include/primer/orset.h"
"src/primer/orset.cpp"
"src/include/primer/hyperloglog.h"
"src/include/primer/hyperloglog_presto.h"
"src/primer/hyperloglog.cpp"
"src/primer/hyperloglog_presto.cpp"
)

add_custom_target(check-clang-tidy-p0
Expand Down
79 changes: 79 additions & 0 deletions src/include/primer/hyperloglog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#pragma once

#include <bitset>
#include <functional>
#include <memory>
#include <mutex> // NOLINT
#include <string>
#include <vector>

#define MAX_BITS 64
#define DEFAULT_CARDINALITY 0

namespace bustub {

/** @brief Hash value type. */
using hash_t = uint64_t;

/** @brief Constant for HLL. */
static const double CONSTANT = 0.79402;

template <typename T>
class HyperLogLog {
public:
HyperLogLog() = delete;

explicit HyperLogLog(int16_t n_bits) : cardinality_(0) {}

/**
* @brief Getter value for cardinality.
*
* @returns cardinality value
*/
auto GetCardinality() { return cardinality_; }

/**
* @brief Ddds a value into the HyperLogLog.
*
* @param[in] val - value that's added into hyperloglog
*/
auto AddElem(T val) -> void;

/**
* @brief Function that computes cardinality.
*/
auto ComputeCardinality() -> void;

private:
/**
* @brief Calculates Hash of a given value.
*
* @param[in] val - value
* @returns hash integer of given input value
*/
inline auto CalculateHash(T val) -> hash_t;

/**
* @brief Function that computes binary.
*
*
* @param[in] hash
* @returns binary of a given hash
*/
auto ComputeBinary(const hash_t &hash) const -> std::bitset<MAX_BITS>;

/**
* @brief Function that computes leading zeros.
*
* @param[in] bset - binary values of a given bitset
* @returns leading zeros of given binary set
*/
auto PositionOfLeftmostOne(const std::bitset<MAX_BITS> &bset) const -> uint64_t;

/** @brief Cardinality value. */
size_t cardinality_;

/** @todo (student) can add their data structures that support HyperLogLog */
};

} // namespace bustub
71 changes: 71 additions & 0 deletions src/include/primer/hyperloglog_presto.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

#include <bitset>
#include <functional>
#include <memory>
#include <mutex> // NOLINT
#include <string>
#include <unordered_map>
#include <vector>

#define BUCKET_SIZE 4UL
#define CONSTANT 0.79402
#define MAX_SIZE 7UL

namespace bustub {

template <typename T>
class HyperLogLogPresto {
/**
* INSTRUCTIONS: Testing framework will use the GetDenseBucket and GetOverflow function,
* hence SHOULD NOT be deleted. It's essential to use the dense_bucket_
* data structure.
*/

/** @brief Hash type. */
using hash_t = uint64_t;

public:
/** @brief Disabling default constructor. */
HyperLogLogPresto() = delete;

/** @brief Parameterized constructor. */
explicit HyperLogLogPresto(int16_t n_leading_bits) : cardinality_(0) {}

/** @brief Returns the dense_bucket_ data structure. */
auto GetDenseBucket() const -> std::vector<std::bitset<BUCKET_SIZE>> { return dense_bucket_; }

/** @brief Returns overflow bucket of a specific given index. */
auto GetOverflowBucketofIndex(uint16_t idx) { return overflow_bucket_[idx]; }

/** @brief Retusn the cardinality of the set. */
auto GetCardinality() const -> uint64_t { return cardinality_; }

/** @brief Element is added for HLL calculation. */
auto AddElem(T val) -> void;

/** @brief Function to compute cardinality. */
auto ComputeCardinality() -> void;

private:
/** @brief Calculate Hash.
*
* @param[in] val
*
* @returns hash value
*/
inline auto CalculateHash(T val) -> hash_t { return std::hash<T>{}(val); }

/** @brief Structure holding dense buckets (or also known as registers). */
std::vector<std::bitset<BUCKET_SIZE>> dense_bucket_;

/** @brief Structure holding overflow buckets. */
std::unordered_map<uint16_t, std::bitset<MAX_SIZE - BUCKET_SIZE>> overflow_bucket_;

/** @brief Storing cardinality value */
uint64_t cardinality_;

// TODO(student) - can add more data structures as required
};

} // namespace bustub
2 changes: 2 additions & 0 deletions src/primer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
add_library(
bustub_primer
OBJECT
hyperloglog.cpp
hyperloglog_presto.cpp
orset.cpp
orset_driver.cpp
trie.cpp
Expand Down
36 changes: 36 additions & 0 deletions src/primer/hyperloglog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "primer/hyperloglog.h"

namespace bustub {

template <typename T>
auto inline HyperLogLog<T>::CalculateHash(T val) -> hash_t {
/** @todo student - implement the function */
return 0;
}

template <typename T>
auto HyperLogLog<T>::ComputeBinary(const hash_t &hash) const -> std::bitset<MAX_BITS> {
/** @todo student - implement the function */
return {0};
}

template <typename T>
auto HyperLogLog<T>::PositionOfLeftmostOne(const std::bitset<MAX_BITS> &bset) const -> uint64_t {
/** @todo student - implement the function*/
return 0;
}

template <typename T>
auto HyperLogLog<T>::AddElem(T val) -> void {
/** @todo implement the function */
}

template <typename T>
auto HyperLogLog<T>::ComputeCardinality() -> void {
/** @todo - student implement the function */
}

template class HyperLogLog<int64_t>;
template class HyperLogLog<std::string>;

} // namespace bustub
17 changes: 17 additions & 0 deletions src/primer/hyperloglog_presto.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "primer/hyperloglog_presto.h"

namespace bustub {

template <typename T>
auto HyperLogLogPresto<T>::AddElem(T val) -> void {
/** @todo (student) has to fill the function */
}

template <typename T>
auto HyperLogLogPresto<T>::ComputeCardinality() -> void {
// TODO(student) - implement the function
}

template class HyperLogLogPresto<int64_t>;
template class HyperLogLogPresto<std::string>;
} // namespace bustub
Loading

0 comments on commit 0d4e3f8

Please sign in to comment.