Compare commits
2 Commits
b577a5fe02
...
a21fc9e08d
Author | SHA1 | Date | |
---|---|---|---|
a21fc9e08d | |||
1860adceff |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
*.o
|
||||
*
|
||||
*.
|
||||
*.txt
|
||||
*.vscode
|
54
2Q_cache.h
54
2Q_cache.h
@ -18,68 +18,78 @@ private:
|
||||
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:
|
||||
int tic_number = 0;
|
||||
|
||||
TwoCache(size_t q1sz, size_t q2sz): cpct_q1(q1sz), cpct_q2(q2sz) {} //ctor
|
||||
|
||||
void put_page(int id) {
|
||||
std::cout << "id: " << id << std::endl;
|
||||
//std::cout <<__LINE__ << " " << __FILE__<< std::endl;
|
||||
//std::cout << "id: " << id << std::endl;
|
||||
|
||||
if (umap1.find(id) != umap1.end()) { //page is found, nothing to do (fast case)
|
||||
std::cout << "id ="<<id<<" TIC!\n";
|
||||
//std::cout << "id ="<<id<<" TIC!\n";
|
||||
|
||||
print_info();
|
||||
tic_number++;
|
||||
|
||||
//print_info();
|
||||
|
||||
return;
|
||||
} else if (umap2.find(id) != umap2.end()) {
|
||||
std::cout << "id ="<<id<<" TIC!\n";
|
||||
//std::cout << "id ="<<id<<" TIC!\n";
|
||||
|
||||
print_info();
|
||||
tic_number++;
|
||||
|
||||
q2.erase(umap2[id]);
|
||||
//if page is in slow put it in fast
|
||||
std::cout <<__LINE__ << " " << __FILE__<< std::endl;
|
||||
|
||||
umap2.erase(id);
|
||||
|
||||
|
||||
if (q1.size() < cpct_q1) {
|
||||
|
||||
print_info();
|
||||
//print_info();
|
||||
|
||||
q1.push_front(id);
|
||||
|
||||
umap1[id] = q1.begin();
|
||||
|
||||
//print_info();
|
||||
|
||||
} else {
|
||||
print_info();
|
||||
//print_info();
|
||||
|
||||
int lru_page = q1.back(); //if fast is overflow - delete
|
||||
auto lru_page = q1.back(); //if fast is overflow - delete
|
||||
umap1.erase(lru_page);
|
||||
q1.pop_back();
|
||||
auto p = umap1.find(lru_page);
|
||||
umap1.erase(p);
|
||||
// std::cout << "ERASING " << lru_page << "\n";
|
||||
// umap1.erase(lru_page);
|
||||
// std::cout << "KAAAAl"`<<*(umap1[lru_page]) << "\n";
|
||||
q1.push_front(id);
|
||||
umap1[id] = q1.begin();
|
||||
|
||||
//print_info();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
} else if (q2.size() < cpct_q2) { //if not in cache put it in slow
|
||||
|
||||
print_info();
|
||||
//print_info();
|
||||
|
||||
q2.push_front(id);
|
||||
umap2[id] = q2.begin();
|
||||
|
||||
//print_info();
|
||||
|
||||
|
||||
return;
|
||||
} else { //if slow is overflow - pop
|
||||
|
||||
print_info();
|
||||
//print_info();
|
||||
|
||||
int lru_page = q2.back(); //if fast is overflow - delete
|
||||
umap2.erase(lru_page);
|
||||
q2.pop_back();
|
||||
auto p = umap2.find(lru_page);
|
||||
std::cout<< "ERASING " << *(umap2[lru_page]) <<std::endl;
|
||||
umap2.erase(p);
|
||||
q2.push_front(id);
|
||||
umap2[id] = q2.begin();
|
||||
|
||||
return;
|
||||
|
||||
//print_info();
|
||||
}
|
||||
}
|
||||
|
||||
|
BIN
cache_test
Executable file
BIN
cache_test
Executable file
Binary file not shown.
@ -18,7 +18,7 @@ int cache_test(std::string file_name) {
|
||||
|
||||
|
||||
std::string test_line, answer_line;
|
||||
int fast_q_sz, slow_q_sz, num_of_calls, page_id, test_number = 0;
|
||||
int fast_q_sz, slow_q_sz, num_of_calls, page_id, test_number = 0, test_passed = 0, test_failed = 0;
|
||||
|
||||
while(std::getline(file, test_line)) {
|
||||
std::stringstream ss(test_line);
|
||||
@ -35,9 +35,11 @@ int cache_test(std::string file_name) {
|
||||
|
||||
if(cache.string_info() == answer_line) { //compare answers
|
||||
std::cout << "test - " << test_number << " passed\n";
|
||||
test_passed++;
|
||||
} else {
|
||||
std::cout << "test - " << test_number << " failed\n";
|
||||
std::cout << "right answer - " << answer_line << "your answer - " << cache.string_info() << std::endl;
|
||||
test_failed++;
|
||||
}
|
||||
|
||||
test_number++;
|
||||
@ -45,6 +47,8 @@ int cache_test(std::string file_name) {
|
||||
|
||||
file.close();
|
||||
|
||||
std::cout << "\nNumber of tests = "<< test_number << "\nTests passed = " << test_passed << "\nTests failed = " << test_failed << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
20
makefile
20
makefile
@ -1,17 +1,13 @@
|
||||
TARGET = Q2
|
||||
|
||||
CXX = g++
|
||||
|
||||
CXXFLAGS = -Wall -Wextra -std=c++11
|
||||
|
||||
SRCS = main.cpp
|
||||
TEST = cache_test.cpp
|
||||
OBJS = $(SRCS:.cpp=.o)
|
||||
|
||||
ifeq ($(OS),Windows_NT)
|
||||
RM = del
|
||||
RUN = $(TARGET).exe
|
||||
else
|
||||
RM = rm -f
|
||||
RUN = ./$(TARGET)
|
||||
endif
|
||||
OBJS = $(SRCS:.cpp=.o)
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
@ -24,10 +20,10 @@ $(TARGET): $(OBJS)
|
||||
test:
|
||||
$(CXX) $(CXXFLAGS) $(TEST) -o cache_test
|
||||
|
||||
|
||||
clean:
|
||||
$(RM) $(OBJS) $(TARGET) cache_test
|
||||
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
run: $(TARGET)
|
||||
$(RUN)
|
||||
./$(TARGET)
|
||||
|
||||
.PHONY: all clean run test
|
||||
.PHONY: all clean run
|
Loading…
x
Reference in New Issue
Block a user