diff --git a/bench/bench_mimalloc.cc b/bench/bench_mimalloc.cc index 595d4f6..21bafb2 100644 --- a/bench/bench_mimalloc.cc +++ b/bench/bench_mimalloc.cc @@ -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. { @@ -57,11 +58,10 @@ int main() { SieveCache cache(68, &mi); std::mt19937 rng(std::random_device{}()); std::uniform_int_distribution dist(0, 99); - std::pmr::polymorphic_allocator pa{&mi}; for (int i = 1; i < 1000; ++i) { const auto n = dist(rng); - cache[n] = S(std::pmr::vector(12, pa), n); + cache[n] = S(std::pmr::vector{12}, n); } for (int i = 1; i < 1000; ++i) { @@ -80,11 +80,10 @@ int main() { SieveCache cache(static_cast(SIGMA), &mi); std::mt19937 rng(std::random_device{}()); std::uniform_int_distribution dist(0, 99); - std::pmr::polymorphic_allocator pa{&mi}; for (int i = 1; i < 1000; ++i) { const auto n = dist(rng); - cache[n] = S(std::pmr::vector(12, pa), n); + cache[n] = S(std::pmr::vector{12}, n); } for (int i = 1; i < 1000; ++i) { diff --git a/include/sieve.hpp b/include/sieve.hpp index 5d504ae..9e4d306 100644 --- a/include/sieve.hpp +++ b/include/sieve.hpp @@ -11,11 +11,20 @@ #include #include +#if __cplusplus >= 202002L +#include +#endif template 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(mem_resource_->allocate(capacity_ * sizeof(K))); + values_ = static_cast(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(mem_resource_->allocate(capacity_ * sizeof(K))); values_ = static_cast(mem_resource_->allocate(capacity_ * sizeof(V))); @@ -30,6 +39,34 @@ class SieveCache { mem_resource_->deallocate(values_, capacity_ * sizeof(V)); } +#if __cplusplus >= 202002L && __has_include() + bool operator<=>(const SieveCache& 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) { diff --git a/tests/test.cc b/tests/test.cc index 92d568e..fe22964 100644 --- a/tests/test.cc +++ b/tests/test.cc @@ -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")); @@ -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 cache2(3); + cache.insert("key1", "value1"); + cache2.insert("key1", "value1"); + + CHECK(cache == cache2); + + cache2.insert("key2", "value2"); + CHECK(cache != cache2); + } + + SUBCASE("Comparison operators") { + SieveCache 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); } }