From e948d3c81a2291c4843dfb83af583de339fe770d Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 20 Jun 2026 19:18:21 +0200 Subject: [PATCH 01/11] LRU empty files --- CMakeLists.txt | 2 +- LRU/CMakeLists.txt | 8 ++++++++ LRU/LRU.hpp | 5 +++++ hello_world_lib/CMakeLists.txt | 11 ----------- hello_world_lib/hello.cpp | 3 --- hello_world_lib/hello.hpp | 3 --- main/CMakeLists.txt | 2 +- main/main.cpp | 2 +- tests/CMakeLists.txt | 10 +++++----- tests/{hello_test.cc => LRU_test.cpp} | 4 ++-- 10 files changed, 23 insertions(+), 27 deletions(-) create mode 100644 LRU/CMakeLists.txt create mode 100644 LRU/LRU.hpp delete mode 100644 hello_world_lib/CMakeLists.txt delete mode 100644 hello_world_lib/hello.cpp delete mode 100644 hello_world_lib/hello.hpp rename tests/{hello_test.cc => LRU_test.cpp} (62%) diff --git a/CMakeLists.txt b/CMakeLists.txt index ed582c8..4c3131a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ set(CMAKE_CXX_EXTENSIONS OFF) add_subdirectory(main) -add_subdirectory(hello_world_lib) +add_subdirectory(LRU) include(CTest) diff --git a/LRU/CMakeLists.txt b/LRU/CMakeLists.txt new file mode 100644 index 0000000..b460d1f --- /dev/null +++ b/LRU/CMakeLists.txt @@ -0,0 +1,8 @@ +add_library(LRU INTERFACE) + +target_sources(LRU + INTERFACE + FILE_SET HEADERS + FILES + LRU.hpp +) diff --git a/LRU/LRU.hpp b/LRU/LRU.hpp new file mode 100644 index 0000000..f6927e6 --- /dev/null +++ b/LRU/LRU.hpp @@ -0,0 +1,5 @@ +#pragma once + +#include + +std::string hello() { return std::string("hello"); } diff --git a/hello_world_lib/CMakeLists.txt b/hello_world_lib/CMakeLists.txt deleted file mode 100644 index 2326389..0000000 --- a/hello_world_lib/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -add_library(hello_world_lib) - -target_sources(hello_world_lib - PRIVATE - hello.cpp - - PUBLIC - FILE_SET HEADERS - FILES - hello.hpp -) diff --git a/hello_world_lib/hello.cpp b/hello_world_lib/hello.cpp deleted file mode 100644 index 9c6e92a..0000000 --- a/hello_world_lib/hello.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include "hello.hpp" - -std::string hello() { return std::string("Hello, world!"); } diff --git a/hello_world_lib/hello.hpp b/hello_world_lib/hello.hpp deleted file mode 100644 index 60c1132..0000000 --- a/hello_world_lib/hello.hpp +++ /dev/null @@ -1,3 +0,0 @@ -#include - -std::string hello(); diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 31a6e4d..6fd3185 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -7,5 +7,5 @@ target_sources(main target_link_libraries(main PRIVATE - hello_world_lib + LRU ) diff --git a/main/main.cpp b/main/main.cpp index b278a92..f06ddc2 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,4 +1,4 @@ -#include "hello.hpp" +#include "LRU.hpp" #include int main() { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cd0e7f2..24688e8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,14 +9,14 @@ set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) FetchContent_MakeAvailable(googletest) -add_executable(hello_test - hello_test.cc +add_executable(caches_test + LRU_test.cpp ) -target_link_libraries(hello_test +target_link_libraries(caches_test PRIVATE - hello_world_lib + LRU GTest::gtest_main ) include(GoogleTest) -gtest_discover_tests(hello_test) +gtest_discover_tests(caches_test) diff --git a/tests/hello_test.cc b/tests/LRU_test.cpp similarity index 62% rename from tests/hello_test.cc rename to tests/LRU_test.cpp index b6e2278..422005b 100644 --- a/tests/hello_test.cc +++ b/tests/LRU_test.cpp @@ -1,8 +1,8 @@ -#include "hello.hpp" +#include "LRU.hpp" #include // Demonstrate some basic assertions. TEST(HelloTest, BasicAssertions) { // Expect equality. - EXPECT_EQ(hello(), std::string("Hello, world!")); + EXPECT_EQ(hello(), std::string("hello")); } From 1216fdf185669ea4edd1d360bac03e64b86133aa Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 21 Jun 2026 11:58:28 +0200 Subject: [PATCH 02/11] lru --- LRU/LRU.hpp | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/LRU/LRU.hpp b/LRU/LRU.hpp index f6927e6..8d38696 100644 --- a/LRU/LRU.hpp +++ b/LRU/LRU.hpp @@ -1,5 +1,39 @@ #pragma once -#include +#include +#include -std::string hello() { return std::string("hello"); } +namespace cache { + +template class LRU_t { + std::list cache_; + + using ListIt = typename std::list::iterator; + std::unordered_map hash_; + +public: + LRU_t(size_t sz) : hash_{sz} {} + + bool full() const { return cache_.size() == hash_.size(); }; + + template bool lookup_update(KeyT key, F slow_get_page) { + auto hit = hash_.find(key); + if (hit == hash_.end()) { // not found + if (full()) { + hash_.erase(cache_.back().id); + cache_.pop_back(); + } + cache_push_front(slow_get_page(key)); + hash_[key] = cache_.begin(); + return false; + } + + auto eltit = hit->second; + if (eltit != cache_.begin()) { + cache_.splice(cache_.begin(), cache_, eltit); + } + return true; + } +}; + +} // namespace cache From 8a99c4a3bac22ad9fc3c91783ae1279c2311a92b Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 21 Jun 2026 12:47:53 +0200 Subject: [PATCH 03/11] lru from lectures --- LRU/LRU.hpp | 9 +++++---- main/main.cpp | 23 +++++++++++++++++++++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/LRU/LRU.hpp b/LRU/LRU.hpp index 8d38696..051d737 100644 --- a/LRU/LRU.hpp +++ b/LRU/LRU.hpp @@ -6,24 +6,25 @@ namespace cache { template class LRU_t { + size_t sz_; std::list cache_; using ListIt = typename std::list::iterator; std::unordered_map hash_; public: - LRU_t(size_t sz) : hash_{sz} {} + LRU_t(size_t sz) : sz_{sz} {} - bool full() const { return cache_.size() == hash_.size(); }; + bool full() const { return cache_.size() == sz_; }; template bool lookup_update(KeyT key, F slow_get_page) { auto hit = hash_.find(key); if (hit == hash_.end()) { // not found if (full()) { - hash_.erase(cache_.back().id); + hash_.erase(cache_.back()); cache_.pop_back(); } - cache_push_front(slow_get_page(key)); + cache_.push_front(slow_get_page(key)); hash_[key] = cache_.begin(); return false; } diff --git a/main/main.cpp b/main/main.cpp index f06ddc2..350bc4d 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,8 +1,27 @@ #include "LRU.hpp" +#include #include +int slow_get_page(int key) { return key; } + + int main() { - std::string str = hello(); + size_t hits{}; + size_t sz; + int nelts; + + std::cin >> sz >> nelts; + + cache::LRU_t lru{sz}; + + for (int i = 0; i < nelts; ++i) { + int p; + std::cin >> p; + assert(std::cin.good()); + if (lru.lookup_update(p, slow_get_page)) + hits += 1; + } - std::cout << str << std::endl; + std::cout << "hits: " << hits << std::endl; + return 0; } From e0dec30bb1d26ee5cc3d5326026424e6c0e3c76d Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 21 Jun 2026 13:02:06 +0200 Subject: [PATCH 04/11] formatted output --- main/main.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index 350bc4d..c7a109c 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,12 +1,14 @@ #include "LRU.hpp" #include #include +#include +#include int slow_get_page(int key) { return key; } int main() { - size_t hits{}; + std::map cache_hits; size_t sz; int nelts; @@ -19,9 +21,12 @@ int main() { std::cin >> p; assert(std::cin.good()); if (lru.lookup_update(p, slow_get_page)) - hits += 1; + cache_hits["LRU"] += 1; } - std::cout << "hits: " << hits << std::endl; + std::cout << "Cache type\tN hits\n"; + for(auto [cache_type, nhits]: cache_hits){ + std::cout << cache_type << "\t" << nhits << std::endl; + } return 0; } From 19a5f2f84f63595d3e75f0acfe7ef13549abd233 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 21 Jun 2026 13:27:39 +0200 Subject: [PATCH 05/11] empty lru --- LRU/LRU.hpp | 2 +- tests/LRU_test.cpp | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/LRU/LRU.hpp b/LRU/LRU.hpp index 051d737..ba80ce3 100644 --- a/LRU/LRU.hpp +++ b/LRU/LRU.hpp @@ -13,7 +13,7 @@ template class LRU_t { std::unordered_map hash_; public: - LRU_t(size_t sz) : sz_{sz} {} + explicit LRU_t(size_t sz) : sz_{sz} {} bool full() const { return cache_.size() == sz_; }; diff --git a/tests/LRU_test.cpp b/tests/LRU_test.cpp index 422005b..038583a 100644 --- a/tests/LRU_test.cpp +++ b/tests/LRU_test.cpp @@ -1,8 +1,17 @@ #include "LRU.hpp" #include + + +int slow_get_page(int key) {return key;} + + // Demonstrate some basic assertions. -TEST(HelloTest, BasicAssertions) { - // Expect equality. - EXPECT_EQ(hello(), std::string("hello")); +TEST(LRU, ctr) { + cache::LRU_t lru{3}; + + // cache is empty, no hits + EXPECT_FALSE(lru.lookup_update(1, slow_get_page)); + EXPECT_FALSE(lru.lookup_update(2, slow_get_page)); + EXPECT_FALSE(lru.lookup_update(3, slow_get_page)); } From 8b06ad7303556811b57f8e5f623a25ef125e355c Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 21 Jun 2026 14:59:49 +0200 Subject: [PATCH 06/11] new tests --- tests/LRU_test.cpp | 77 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/tests/LRU_test.cpp b/tests/LRU_test.cpp index 038583a..ce6e215 100644 --- a/tests/LRU_test.cpp +++ b/tests/LRU_test.cpp @@ -1,17 +1,80 @@ #include "LRU.hpp" #include +#include +struct page_t{ + int id; +}; -int slow_get_page(int key) {return key;} +page_t slow_get_page(int key) { return page_t{key}; } - -// Demonstrate some basic assertions. -TEST(LRU, ctr) { - cache::LRU_t lru{3}; +TEST(LRU, no_hit) { + cache::LRU_t lru{3}; // cache is empty, no hits + EXPECT_FALSE(lru.lookup_update(1, slow_get_page )); +} + +TEST(LRU, one_hit) { + cache::LRU_t lru{3}; + EXPECT_FALSE(lru.lookup_update(1, slow_get_page)); - EXPECT_FALSE(lru.lookup_update(2, slow_get_page)); - EXPECT_FALSE(lru.lookup_update(3, slow_get_page)); + EXPECT_TRUE(lru.lookup_update(1, slow_get_page)); // cache hit +} + +TEST(LRU, is_full) { + cache::LRU_t lru{2}; + + lru.lookup_update(1, slow_get_page); // [1] + lru.lookup_update(1, slow_get_page); // [1] + EXPECT_FALSE(lru.full()); + + lru.lookup_update(2, slow_get_page); // [2, 1] + EXPECT_TRUE(lru.full()); +} + +TEST(LRU, no_eviction) { + cache::LRU_t lru{3}; + + EXPECT_FALSE(lru.lookup_update(1, slow_get_page)); // [1] + EXPECT_FALSE(lru.lookup_update(2, slow_get_page)); // [2, 1] + EXPECT_TRUE(lru.lookup_update(1, slow_get_page)); // [1, 2] + EXPECT_FALSE(lru.lookup_update(3, slow_get_page)); // [3, 1, 2] + EXPECT_TRUE(lru.lookup_update(1, slow_get_page)); // [1, 3, 2] + EXPECT_TRUE(lru.lookup_update(2, slow_get_page)); // [2, 1, 3] +} + +TEST(LRU, eviction) { + cache::LRU_t lru{2}; + + lru.lookup_update(1, slow_get_page); // [1] + lru.lookup_update(2, slow_get_page); // [2, 1] + lru.lookup_update(3, slow_get_page); // [3, 2] + + EXPECT_FALSE(lru.lookup_update(1, slow_get_page)); // 1 is not present before lookup } + +TEST(LRU, example_from_lecture) { + cache::LRU_t lru{2}; + + std::vector data_id = {1, 2, 1, 2, 1, 2}; + size_t hits{}; + for (auto el : data_id) { + if (lru.lookup_update(el, slow_get_page)) + hits += 1; + } + + EXPECT_EQ(4, hits); +} + +TEST(LRU, non_default_key) { + + +} + + + + + + From 1865e4635421809dfb3d75ffe4562e225a891708 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 21 Jun 2026 15:13:06 +0200 Subject: [PATCH 07/11] updated LRU with stoking key --- LRU/LRU.hpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/LRU/LRU.hpp b/LRU/LRU.hpp index ba80ce3..16f64cd 100644 --- a/LRU/LRU.hpp +++ b/LRU/LRU.hpp @@ -7,10 +7,10 @@ namespace cache { template class LRU_t { size_t sz_; - std::list cache_; + std::list> cache_; - using ListIt = typename std::list::iterator; - std::unordered_map hash_; + using ListIt = typename decltype(cache_)::iterator; + std::unordered_map hash_; public: explicit LRU_t(size_t sz) : sz_{sz} {} @@ -21,18 +21,16 @@ template class LRU_t { auto hit = hash_.find(key); if (hit == hash_.end()) { // not found if (full()) { - hash_.erase(cache_.back()); + hash_.erase(cache_.back().first); cache_.pop_back(); } - cache_.push_front(slow_get_page(key)); - hash_[key] = cache_.begin(); + cache_.emplace_front(key, slow_get_page(key)); + hash_.emplace(key, cache_.begin()); return false; } auto eltit = hit->second; - if (eltit != cache_.begin()) { - cache_.splice(cache_.begin(), cache_, eltit); - } + cache_.splice(cache_.begin(), cache_, eltit); return true; } }; From 452479e9f7a92881dc748f10c1ec3f804e5638aa Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 21 Jun 2026 15:26:47 +0200 Subject: [PATCH 08/11] lru test removed vector --- tests/LRU_test.cpp | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/tests/LRU_test.cpp b/tests/LRU_test.cpp index ce6e215..342bd6a 100644 --- a/tests/LRU_test.cpp +++ b/tests/LRU_test.cpp @@ -1,19 +1,19 @@ #include "LRU.hpp" #include -#include - -struct page_t{ +namespace { +struct page_t { int id; }; -page_t slow_get_page(int key) { return page_t{key}; } +page_t slow_get_page(int page_id) { return page_t{page_id}; } +} // namespace TEST(LRU, no_hit) { cache::LRU_t lru{3}; // cache is empty, no hits - EXPECT_FALSE(lru.lookup_update(1, slow_get_page )); + EXPECT_FALSE(lru.lookup_update(1, slow_get_page)); } TEST(LRU, one_hit) { @@ -52,29 +52,18 @@ TEST(LRU, eviction) { lru.lookup_update(2, slow_get_page); // [2, 1] lru.lookup_update(3, slow_get_page); // [3, 2] - EXPECT_FALSE(lru.lookup_update(1, slow_get_page)); // 1 is not present before lookup + EXPECT_FALSE( + lru.lookup_update(1, slow_get_page)); // 1 is not present before lookup } TEST(LRU, example_from_lecture) { cache::LRU_t lru{2}; - std::vector data_id = {1, 2, 1, 2, 1, 2}; size_t hits{}; - for (auto el : data_id) { - if (lru.lookup_update(el, slow_get_page)) + for (int page_id : {1, 2, 1, 2, 1, 2}) { + if (lru.lookup_update(page_id, slow_get_page)) hits += 1; } EXPECT_EQ(4, hits); } - -TEST(LRU, non_default_key) { - - -} - - - - - - From 01bfb551dcb8a569084cec30453626067eda24d2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 21 Jun 2026 15:29:31 +0200 Subject: [PATCH 09/11] gitworkflow fails fast --- .github/workflows/cmake-multi-platform.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index 7ab1b4d..d2840a2 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -14,7 +14,7 @@ jobs: strategy: # Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable. - fail-fast: false + fail-fast: true # Set up a matrix to run the following 3 configurations: # 1. @@ -31,7 +31,7 @@ jobs: c_compiler: cl cpp_compiler: cl - os: ubuntu-latest - c_compiler: gcc + c_compilel: gcc cpp_compiler: g++ - os: ubuntu-latest c_compiler: clang From 81fd3c548e3379065c47b645a5e7d61ad47612af Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 21 Jun 2026 15:37:36 +0200 Subject: [PATCH 10/11] Wall, Wextra, Werror --- LRU/CMakeLists.txt | 8 ++++++++ main/CMakeLists.txt | 6 ++++++ tests/CMakeLists.txt | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/LRU/CMakeLists.txt b/LRU/CMakeLists.txt index b460d1f..c2d9f8a 100644 --- a/LRU/CMakeLists.txt +++ b/LRU/CMakeLists.txt @@ -6,3 +6,11 @@ target_sources(LRU FILES LRU.hpp ) + + +target_compile_options(LRU + INTERFACE + -Wall + -Wextra + -Werror +) diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 6fd3185..f1ef904 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -4,6 +4,12 @@ target_sources(main main.cpp ) +target_compile_options(main PRIVATE + -Wall + -Wextra + -Werror +) + target_link_libraries(main PRIVATE diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 24688e8..d5f13bd 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -12,6 +12,14 @@ FetchContent_MakeAvailable(googletest) add_executable(caches_test LRU_test.cpp ) + +target_compile_options(caches_test PRIVATE + -Wall + -Wextra + -Werror +) + + target_link_libraries(caches_test PRIVATE LRU From e76bfafd3ad978539f6ef79ba075805f2d4ec478 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 21 Jun 2026 15:45:40 +0200 Subject: [PATCH 11/11] windows doesnt support Wextra --- LRU/CMakeLists.txt | 8 -------- main/CMakeLists.txt | 9 ++++----- tests/CMakeLists.txt | 9 ++++----- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/LRU/CMakeLists.txt b/LRU/CMakeLists.txt index c2d9f8a..b460d1f 100644 --- a/LRU/CMakeLists.txt +++ b/LRU/CMakeLists.txt @@ -6,11 +6,3 @@ target_sources(LRU FILES LRU.hpp ) - - -target_compile_options(LRU - INTERFACE - -Wall - -Wextra - -Werror -) diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index f1ef904..c0efbd5 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -4,13 +4,12 @@ target_sources(main main.cpp ) -target_compile_options(main PRIVATE - -Wall - -Wextra - -Werror +target_compile_options(main + PRIVATE + $<$:/W4 /WX> + $<$:-Wall -Wextra -Werror> ) - target_link_libraries(main PRIVATE LRU diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d5f13bd..eb26a4e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -13,13 +13,12 @@ add_executable(caches_test LRU_test.cpp ) -target_compile_options(caches_test PRIVATE - -Wall - -Wextra - -Werror +target_compile_options(caches_test + PRIVATE + $<$:/W4 /WX> + $<$:-Wall -Wextra -Werror> ) - target_link_libraries(caches_test PRIVATE LRU