-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add observe remove set stubs for p0
Signed-off-by: Yuchen Liang <yuchenl3@andrew.cmu.edu>
- Loading branch information
Showing
8 changed files
with
553 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
namespace bustub { | ||
|
||
/** @brief Unique ID type. */ | ||
using uid_t = int64_t; | ||
|
||
/** @brief The observed remove set datatype. */ | ||
template <typename T> | ||
class ORSet { | ||
public: | ||
ORSet() = default; | ||
|
||
/** | ||
* @brief Checks if an element is in the set. | ||
* | ||
* @param elem the element to check | ||
* @return true if the element is in the set, and false otherwise. | ||
*/ | ||
auto Contains(const T &elem) const -> bool; | ||
|
||
/** | ||
* @brief Adds an element to the set. | ||
* | ||
* @param elem the element to add | ||
* @param uid unique token associated with the add operation. | ||
*/ | ||
void Add(const T &elem, uid_t uid); | ||
|
||
/** | ||
* @brief Removes an element from the set if it exists. | ||
* | ||
* @param elem the element to remove. | ||
*/ | ||
void Remove(const T &elem); | ||
|
||
/** | ||
* @brief Merge changes from another ORSet. | ||
* | ||
* @param other another ORSet | ||
*/ | ||
void Merge(const ORSet<T> &other); | ||
|
||
/** | ||
* @brief Gets all the elements in the set. | ||
* | ||
* @return all the elements in the set. | ||
*/ | ||
auto Elements() const -> std::vector<T>; | ||
|
||
/** | ||
* @brief Gets a string representation of the set. | ||
* | ||
* @return a string representation of the set. | ||
*/ | ||
auto ToString() const -> std::string; | ||
|
||
private: | ||
// TODO(student): Add your private memeber variables to represent ORSet. | ||
}; | ||
|
||
} // namespace bustub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#pragma once | ||
|
||
#include <atomic> | ||
#include <memory> | ||
#include <vector> | ||
#include "primer/orset.h" | ||
|
||
namespace bustub { | ||
|
||
/** @brief Unique ID type. */ | ||
using uid_t = int64_t; | ||
|
||
template <typename T> | ||
class ORSetDriver; | ||
|
||
template <typename T> | ||
class ORSetNode { | ||
public: | ||
explicit ORSetNode(ORSetDriver<T> *driver, size_t node_id) : driver_(driver), node_id_(node_id) {} | ||
|
||
/** | ||
* @brief Adds an element to the local ORSet. | ||
* | ||
* @param elem the element to add | ||
*/ | ||
inline void Add(const T &elem) { orset_.Add(elem, driver_->GenerateUid()); } | ||
|
||
/** | ||
* @brief Removes an element from the local ORSet. | ||
* | ||
* @param elem the element to remove. | ||
*/ | ||
inline void Remove(const T &elem) { orset_.Remove(elem); } | ||
|
||
/** | ||
* @brief Checks if an element is in the local ORSet. | ||
* | ||
* @param elem the element to check | ||
* @return true if the element is in the set, and false otherwise. | ||
*/ | ||
inline auto Contains(const T &elem) -> bool { return orset_.Contains(elem); } | ||
|
||
/** | ||
* @brief Merges another ORSet to the local ORSet. | ||
* | ||
* @param to_be_merged the ORSet to be merged. | ||
*/ | ||
inline void Merge(const ORSet<T> to_be_merged) { orset_.Merge(to_be_merged); } | ||
|
||
/** | ||
* @brief Saves all local changes to the driver. | ||
*/ | ||
void Save(); | ||
|
||
/** | ||
* @brief Loads all the remote changes to the local ORSet. | ||
*/ | ||
void Load(); | ||
|
||
/** | ||
* @brief Gets a copy of the local ORSet. | ||
* | ||
* @return the local ORSet. | ||
*/ | ||
inline auto GetORSet() -> ORSet<T> { return orset_; } | ||
|
||
private: | ||
/** @brief The local ORSet. */ | ||
ORSet<T> orset_; | ||
|
||
/** @brief ORSet Driver. */ | ||
ORSetDriver<T> *driver_; | ||
|
||
/** @brief node id */ | ||
size_t node_id_; | ||
}; | ||
|
||
/** @brief A driver class for managing ORSets. */ | ||
template <typename T> | ||
class ORSetDriver { | ||
friend class ORSetNode<T>; | ||
|
||
public: | ||
explicit ORSetDriver(size_t num_orset_node); | ||
|
||
/** | ||
* @brief Gets the ORSetNode at index. | ||
*/ | ||
inline auto operator[](size_t index) -> std::unique_ptr<ORSetNode<T>> & { return orset_nodes_[index]; } | ||
auto operator[](size_t index) const -> const std::unique_ptr<ORSetNode<T>> & { return orset_nodes_[index]; } | ||
|
||
/** | ||
* @brief Gets the ORSet node at index. | ||
* | ||
* @param index index of the ORSet node. | ||
* @return the ORSet node associated with the index. | ||
*/ | ||
inline auto At(size_t index) -> std::unique_ptr<ORSetNode<T>> & { return orset_nodes_[index]; } | ||
|
||
/** | ||
* @brief Saves changes in all nodes and then load all the changes. | ||
*/ | ||
void Sync(); | ||
|
||
private: | ||
/** | ||
* @brief Generates a unique id. | ||
* | ||
* @return a unique id. | ||
*/ | ||
inline auto GenerateUid() -> uid_t { return next_uid_++; } | ||
|
||
/** @brief A list of ORSet nodes. */ | ||
std::vector<std::unique_ptr<ORSetNode<T>>> orset_nodes_; | ||
|
||
/** @brief List of saved copies of ORSet. */ | ||
std::vector<ORSet<T>> saved_copies_; | ||
|
||
/** @brief Monotonically increasing unique id for the elements. */ | ||
std::atomic<uid_t> next_uid_ = 0; | ||
}; | ||
|
||
} // namespace bustub |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
add_library( | ||
bustub_primer | ||
OBJECT | ||
orset.cpp | ||
orset_driver.cpp | ||
trie.cpp | ||
trie_store.cpp) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "primer/orset.h" | ||
#include <algorithm> | ||
#include <string> | ||
#include <vector> | ||
#include "common/exception.h" | ||
#include "fmt/format.h" | ||
|
||
namespace bustub { | ||
|
||
template <typename T> | ||
auto ORSet<T>::Contains(const T &elem) const -> bool { | ||
// TODO(student): Implement this | ||
throw NotImplementedException("ORSet<T>::Contains is not implemented"); | ||
} | ||
|
||
template <typename T> | ||
void ORSet<T>::Add(const T &elem, uid_t uid) { | ||
// TODO(student): Implement this | ||
throw NotImplementedException("ORSet<T>::Add is not implemented"); | ||
} | ||
|
||
template <typename T> | ||
void ORSet<T>::Remove(const T &elem) { | ||
// TODO(student): Implement this | ||
throw NotImplementedException("ORSet<T>::Remove is not implemented"); | ||
} | ||
|
||
template <typename T> | ||
void ORSet<T>::Merge(const ORSet<T> &other) { | ||
// TODO(student): Implement this | ||
throw NotImplementedException("ORSet<T>::Merge is not implemented"); | ||
} | ||
|
||
template <typename T> | ||
auto ORSet<T>::Elements() const -> std::vector<T> { | ||
// TODO(student): Implement this | ||
throw NotImplementedException("ORSet<T>::Elements is not implemented"); | ||
} | ||
|
||
template <typename T> | ||
auto ORSet<T>::ToString() const -> std::string { | ||
auto elements = Elements(); | ||
std::sort(elements.begin(), elements.end()); | ||
return fmt::format("{{{}}}", fmt::join(elements, ", ")); | ||
} | ||
|
||
template class ORSet<int>; | ||
template class ORSet<std::string>; | ||
|
||
} // namespace bustub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include "primer/orset_driver.h" | ||
#include <string> | ||
|
||
namespace bustub { | ||
|
||
template <typename T> | ||
ORSetDriver<T>::ORSetDriver(size_t num_orset_node) { | ||
orset_nodes_.reserve(num_orset_node); | ||
for (size_t i = 0; i < num_orset_node; ++i) { | ||
orset_nodes_.emplace_back(std::make_unique<ORSetNode<T>>(this, i)); | ||
} | ||
saved_copies_.resize(num_orset_node); | ||
} | ||
|
||
template <typename T> | ||
void ORSetNode<T>::Load() { | ||
for (size_t i = 0; i < driver_->saved_copies_.size(); ++i) { | ||
if (i == node_id_) { | ||
continue; | ||
} | ||
Merge(driver_->saved_copies_[i]); | ||
} | ||
} | ||
|
||
template <typename T> | ||
void ORSetNode<T>::Save() { | ||
driver_->saved_copies_[node_id_] = orset_; | ||
} | ||
|
||
template <typename T> | ||
void ORSetDriver<T>::Sync() { | ||
for (const auto &node : orset_nodes_) { | ||
node->Save(); | ||
} | ||
for (const auto &node : orset_nodes_) { | ||
node->Load(); | ||
} | ||
} | ||
|
||
template class ORSetDriver<int>; | ||
template class ORSetDriver<std::string>; | ||
|
||
} // namespace bustub |
Oops, something went wrong.