diff --git a/2Q_cache.cpp b/2Q_cache.cpp deleted file mode 100644 index 55dc869..0000000 --- a/2Q_cache.cpp +++ /dev/null @@ -1,85 +0,0 @@ - -#include -#include -#include "2Q_cache.h" - -class TwoCache { -private: - //2 LRU caches - - size_t cpct_q1; - std::unordered_map::iterator> umap1; - std::list q1; //fast queue - the most popular pages - - size_t cpct_q2; - std::unordered_map::iterator> umap2; - std::list q2; //slow queue - if page asked at the first time put it to q2, if itasked from q2 it put it to q1, - -public: - TwoCache(size_t q1sz, size_t q2sz): cpct_q1(q1sz), cpct_q2(q2sz) {} //ctor - - int get_page(int id) { //get_page() - returns if page is found, and -1 if not - if(umap1.find(id) != umap1.end()) { - q1.splice(q1.begin(), q1, umap1[id]); //if page did not find in the end of fast(q1) queue - put it in the begin - - return id; - } - - if (umap2.find(id) != umap2.end()) { //if page was not found in the end of slow queue - put is from slow(q2) to begin fast(q1) - auto it = umap2[id]; - - q2.splice(q2.begin(), q2, it); //put this page from queue to the slow - umap2.erase(id); //delete it from slow queue - - if (q1.size() == cpct_q1) { //if q1 is overflowed delete oldest element from fast(q1) - umap1.erase(q1.back()); - q1.pop_back(); - } - q1.push_front(id); //if it is not overflowed put it at the begin - umap2[id] = q1.begin(); - - return id; - } else return -1; //page was not found - return PAGE_DID_NOT_FOUND = -1 - } - - void put_page(int id) { //puts page in cache - if (umap1.find(id) != umap1.end()) { //if page is q1 - return; - } - - if (umap2.find(id) != umap2.end()) { //I know that in this case there will be 2 loops in get_page() i will think about it... - get_page(id); - return; - } - - if (q1.size() == cpct_q1) { //if overflow - umap1.erase(q1.back()); - q1.pop_back(); - } - - if (q2.size() == cpct_q2) { - umap2.erase(q2.back()); - q2.pop_back(); - } - - q1.push_front(id); //in the end push page in cache - umap1[id] = q1.begin(); - } - - - - - - void info() { - std::cout << "q1 (the most usable): "; - for (auto i = q1.begin(); i != q1.end(); i++) { - std::cout << *i << " "; - } - std::cout << "\nq2(Less usable): "; - for (auto i = q2.begin(); i != q2.end(); i++) { - std::cout << *i << " "; - } - std::cout << "\ncapacity q1: " << cpct_q1 - << "\ncapacity q2: " << cpct_q2 << std::endl; - } -}; \ No newline at end of file diff --git a/2Q_cache.h b/2Q_cache.h index 5dd716a..b4b4552 100644 --- a/2Q_cache.h +++ b/2Q_cache.h @@ -4,11 +4,67 @@ #include #include -class TwoCache { + +class TwoCache { +private: + //2 LRU caches + + size_t cpct_q1; + std::unordered_map::iterator> umap1; + std::list q1; //fast queue - the most popular pages + + size_t cpct_q2; + std::unordered_map::iterator> umap2; + std::list q2; //slow queue - if page asked at the first time put it to q2, if itasked from q2 it put it to q1, + public: - TwoCache(size_t q1sz, size_t q2sz); - int get_page(int id); - void put_page(int id); - void info(); + TwoCache(size_t q1sz, size_t q2sz): cpct_q1(q1sz), cpct_q2(q2sz) {} //ctor + + void put_page(int id) { + if (umap1.find(id) != umap1.end()) { //page is in fast queue in the end, nothing to do (fast case) + std::cout<<"fast case\n"; + + return; + } else if (umap2.find(id) != umap2.end()) { + q2.erase(umap2[id]); //if page is in slow put it in fast + if (q1.size() < cpct_q1) { + q1.push_front(id); + umap1[id] = q1.begin(); + } else { + int lru_page = q1.back(); //if fast is overflow - delete + q1.pop_back(); + umap1.erase(lru_page); + q1.push_front(id); + umap1[id] = q1.begin(); + } + + return; + } else if (q2.size() < cpct_q2) { //if not in cache put it in slow + q2.push_front(id); + umap2[id] = q2.begin(); + + return; + } else { //if slow is overflow - pop + int lru_page = q2.back(); + q2.pop_back(); + umap2.erase(lru_page); + q2.push_front(id); + umap2[id] = q2.begin(); + + return; + } + } + + void info() { + std::cout << "q1 (the most usable): "; + for (auto i = q1.begin(); i != q1.end(); i++) { + std::cout << *i << " "; + } + std::cout << "\nq2 (Less usable): "; + for (auto i = q2.begin(); i != q2.end(); i++) { + std::cout << *i << " "; + } + } }; + #endif \ No newline at end of file diff --git a/Q2 b/Q2 new file mode 100755 index 0000000..88097aa Binary files /dev/null and b/Q2 differ diff --git a/git.ignore b/git.ignore new file mode 100644 index 0000000..796ce64 --- /dev/null +++ b/git.ignore @@ -0,0 +1,3 @@ +!*.* +!*o* +!** \ No newline at end of file diff --git a/ideal_cache.cpp b/ideal_cache.cpp new file mode 100644 index 0000000..e69de29 diff --git a/main.cpp b/main.cpp index d6466b5..676cb03 100644 --- a/main.cpp +++ b/main.cpp @@ -4,20 +4,20 @@ int main() { - TwoCache cache(2, 4); + TwoCache cache(4, 4); cache.put_page(1); cache.put_page(2); - std::cout << cache.get_page(1) << std::endl; // Вывод: 1 + cache.info(); cache.put_page(1); cache.put_page(2); + cache.info(); cache.put_page(1); cache.put_page(2); cache.put_page(1); cache.put_page(2); cache.put_page(3); - std::cout << cache.get_page(2) << std::endl; // Вывод: 2 - cache.put_page(4); // Теперь 4 будет в Q1 - std::cout << cache.get_page(3) << std::endl; // Вывод: -1 (3 нет в кэше) + cache.info(); + cache.put_page(4); return 0; } \ No newline at end of file diff --git a/main.o b/main.o new file mode 100644 index 0000000..f20fff4 Binary files /dev/null and b/main.o differ diff --git a/makefile b/makefile index 08ef223..cc00984 100644 --- a/makefile +++ b/makefile @@ -5,7 +5,7 @@ CXX = g++ CXXFLAGS = -Wall -Wextra -std=c++11 -SRCS = 2Q_cache.cpp main.cpp +SRCS = main.cpp OBJS = $(SRCS:.cpp=.o) diff --git a/tests b/tests new file mode 100644 index 0000000..e69de29