Skip to content

Commit

Permalink
add operators
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed May 22, 2024
1 parent 5697573 commit 2c73fc4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
7 changes: 3 additions & 4 deletions bench/bench_mimalloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct S {
int main() {
auto start = std::chrono::high_resolution_clock::now();
MimallocMemoryResource mi;
std::pmr::set_default_resource(&mi);

// Sequence.
{
Expand All @@ -57,11 +58,10 @@ int main() {
SieveCache<unsigned long, S> cache(68, &mi);
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<unsigned long> dist(0, 99);
std::pmr::polymorphic_allocator<unsigned char> pa{&mi};

for (int i = 1; i < 1000; ++i) {
const auto n = dist(rng);
cache[n] = S(std::pmr::vector<unsigned char>(12, pa), n);
cache[n] = S(std::pmr::vector<unsigned char>{12}, n);
}

for (int i = 1; i < 1000; ++i) {
Expand All @@ -80,11 +80,10 @@ int main() {
SieveCache<unsigned long, S> cache(static_cast<size_t>(SIGMA), &mi);
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<unsigned long> dist(0, 99);
std::pmr::polymorphic_allocator<unsigned char> pa{&mi};

for (int i = 1; i < 1000; ++i) {
const auto n = dist(rng);
cache[n] = S(std::pmr::vector<unsigned char>(12, pa), n);
cache[n] = S(std::pmr::vector<unsigned char>{12}, n);
}

for (int i = 1; i < 1000; ++i) {
Expand Down
39 changes: 38 additions & 1 deletion include/sieve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@

#include <cstddef>
#include <memory_resource>
#if __cplusplus >= 202002L
#include <compare>
#endif

template <typename K, typename V>
class SieveCache {
public:
SieveCache(size_t capacity, std::pmr::memory_resource* mem_resource = std::pmr::get_default_resource())
SieveCache(size_t capacity)
: capacity_(capacity), size_(0), mem_resource_(std::pmr::get_default_resource()) {
keys_ = static_cast<K*>(mem_resource_->allocate(capacity_ * sizeof(K)));
values_ = static_cast<V*>(mem_resource_->allocate(capacity_ * sizeof(V)));
}

SieveCache(size_t capacity, std::pmr::memory_resource* mem_resource)
: capacity_(capacity), size_(0), mem_resource_(mem_resource) {
keys_ = static_cast<K*>(mem_resource_->allocate(capacity_ * sizeof(K)));
values_ = static_cast<V*>(mem_resource_->allocate(capacity_ * sizeof(V)));
Expand All @@ -30,6 +39,34 @@ class SieveCache {
mem_resource_->deallocate(values_, capacity_ * sizeof(V));
}

#if __cplusplus >= 202002L && __has_include(<compare>)
bool operator<=>(const SieveCache<K, V>& other) const = default;
#else
bool operator<(const SieveCache& other) const {
return size_ < other.size_;
}

bool operator>(const SieveCache& other) const {
return size_ > other.size_;
}

bool operator==(const SieveCache& other) const {
if (size_ != other.size_ || capacity_ != other.capacity_) {
return false;
}
for (size_t i = 0; i < size_; ++i) {
if (!(keys_[i] == other.keys_[i] && values_[i] == other.values_[i])) {
return false;
}
}
return true;
}

bool operator!=(const SieveCache& other) const {
return !(*this == other);
}
#endif

V& operator[](const K& key) {
for (size_t i = 0; i < size_; ++i) {
if (keys_[i] == key) {
Expand Down
25 changes: 21 additions & 4 deletions tests/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ TEST_CASE("Testing SieveCache functionality") {
cache.insert("key3", "value3");
CHECK(cache.contains("key1"));
}

SUBCASE("Operator[] test") {
cache["key1"] = "value1";
CHECK(cache.contains("key1"));
Expand All @@ -65,10 +66,26 @@ TEST_CASE("Testing SieveCache functionality") {
// Insert a new key and check for evictions
cache["key4"] = "value4";
CHECK(cache.length() == 3);
}

SUBCASE("Equality and inequality operators") {
SieveCache<std::string, std::string> cache2(3);
cache.insert("key1", "value1");
cache2.insert("key1", "value1");

CHECK(cache == cache2);

cache2.insert("key2", "value2");
CHECK(cache != cache2);
}

SUBCASE("Comparison operators") {
SieveCache<std::string, std::string> cache2(3);
cache.insert("key1", "value1");
cache2.insert("key1", "value1");
cache2.insert("key2", "value2");

// Verify that one of the initial keys was evicted
if (!cache.contains("key3")) {
CHECK(*cache.get("key3") == "value3");
}
CHECK(cache < cache2);
CHECK(cache2 > cache);
}
}

0 comments on commit 2c73fc4

Please sign in to comment.