ready to test
This commit is contained in:
parent
3ef1bc3ab9
commit
b1646952a5
85
2Q_cache.cpp
85
2Q_cache.cpp
@ -1,85 +0,0 @@
|
||||
|
||||
#include <unordered_map>
|
||||
#include <list>
|
||||
#include "2Q_cache.h"
|
||||
|
||||
class TwoCache {
|
||||
private:
|
||||
//2 LRU caches
|
||||
|
||||
size_t cpct_q1;
|
||||
std::unordered_map<int, std::list<int>::iterator> umap1;
|
||||
std::list <int> q1; //fast queue - the most popular pages
|
||||
|
||||
size_t cpct_q2;
|
||||
std::unordered_map<int, std::list<int>::iterator> umap2;
|
||||
std::list <int> 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(<int id>) - returns <int id> 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;
|
||||
}
|
||||
};
|
66
2Q_cache.h
66
2Q_cache.h
@ -4,11 +4,67 @@
|
||||
#include <unordered_map>
|
||||
#include <list>
|
||||
|
||||
class TwoCache {
|
||||
|
||||
class TwoCache {
|
||||
private:
|
||||
//2 LRU caches
|
||||
|
||||
size_t cpct_q1;
|
||||
std::unordered_map<int, std::list<int>::iterator> umap1;
|
||||
std::list <int> q1; //fast queue - the most popular pages
|
||||
|
||||
size_t cpct_q2;
|
||||
std::unordered_map<int, std::list<int>::iterator> umap2;
|
||||
std::list <int> 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
|
3
git.ignore
Normal file
3
git.ignore
Normal file
@ -0,0 +1,3 @@
|
||||
!*.*
|
||||
!*o*
|
||||
!**
|
0
ideal_cache.cpp
Normal file
0
ideal_cache.cpp
Normal file
10
main.cpp
10
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;
|
||||
}
|
2
makefile
2
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)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user