testing debug
This commit is contained in:
parent
21ba17c7b6
commit
c742d6b58d
59
2Q_cache.h
59
2Q_cache.h
@ -21,31 +21,61 @@ public:
|
||||
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;
|
||||
|
||||
if (umap1.find(id) != umap1.end()) { //page is found, nothing to do (fast case)
|
||||
std::cout << "id ="<<id<<" TIC!\n";
|
||||
|
||||
print_info();
|
||||
|
||||
return;
|
||||
} else if (umap2.find(id) != umap2.end()) {
|
||||
q2.erase(umap2[id]); //if page is in slow put it in fast
|
||||
std::cout << "id ="<<id<<" TIC!\n";
|
||||
|
||||
print_info();
|
||||
|
||||
q2.erase(umap2[id]);
|
||||
//if page is in slow put it in fast
|
||||
std::cout <<__LINE__ << " " << __FILE__<< std::endl;
|
||||
if (q1.size() < cpct_q1) {
|
||||
|
||||
print_info();
|
||||
|
||||
q1.push_front(id);
|
||||
umap1[id] = q1.begin();
|
||||
|
||||
} else {
|
||||
print_info();
|
||||
|
||||
int lru_page = q1.back(); //if fast is overflow - delete
|
||||
q1.pop_back();
|
||||
umap1.erase(lru_page);
|
||||
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();
|
||||
}
|
||||
|
||||
return;
|
||||
} else if (q2.size() < cpct_q2) { //if not in cache put it in slow
|
||||
|
||||
print_info();
|
||||
|
||||
q2.push_front(id);
|
||||
umap2[id] = q2.begin();
|
||||
|
||||
|
||||
return;
|
||||
} else { //if slow is overflow - pop
|
||||
int lru_page = q2.back();
|
||||
|
||||
print_info();
|
||||
int lru_page = q2.back(); //if fast is overflow - delete
|
||||
q2.pop_back();
|
||||
umap2.erase(lru_page);
|
||||
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();
|
||||
|
||||
@ -54,7 +84,7 @@ public:
|
||||
}
|
||||
|
||||
void print_info() {
|
||||
|
||||
std::cout <<__LINE__ << " " << __FILE__<< std::endl;
|
||||
std::cout << "\nq1 (the most usable): ";
|
||||
for (auto i = q1.begin(); i != q1.end(); i++) {
|
||||
std::cout << *i << " ";
|
||||
@ -63,7 +93,24 @@ public:
|
||||
for (auto i = q2.begin(); i != q2.end(); i++) {
|
||||
std::cout << *i << " ";
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "Umap1:\n";
|
||||
|
||||
for (auto it = umap1.begin(); it != umap1.end(); ++it) {
|
||||
std::cout << "Key: " << (it->first) << ", Val: " << *(it->second) << " ";
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "Umap2:\n";
|
||||
|
||||
for (auto it = umap2.begin(); it != umap2.end(); ++it) {
|
||||
std::cout << "Key: " << it->first << ", Val: " << *(it->second) << " ";
|
||||
}
|
||||
|
||||
std::cout << std::endl<<std::endl;
|
||||
}
|
||||
|
||||
std::string string_info () { //to easy compare with tests;
|
||||
|
@ -3,6 +3,8 @@
|
||||
#include "2Q_cache.h"
|
||||
#include <sstream>
|
||||
|
||||
const char* default_test_file = "tests.txt";
|
||||
|
||||
|
||||
int cache_test(std::string file_name) {
|
||||
std::ifstream file(file_name);
|
||||
@ -47,7 +49,10 @@ int cache_test(std::string file_name) {
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
cache_test(argv[1]);
|
||||
|
||||
if(argc == 0) {
|
||||
cache_test(default_test_file);
|
||||
} else {
|
||||
cache_test(argv[1]);
|
||||
}
|
||||
}
|
||||
|
||||
|
26
makefile
26
makefile
@ -1,31 +1,33 @@
|
||||
|
||||
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
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
$(CXX) -o $@ $^
|
||||
$(CXX) -o $@ $^
|
||||
|
||||
%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||
$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||
|
||||
test:
|
||||
$(CXX) $(CXXFLAGS) $(TEST) -o cache_test
|
||||
|
||||
$(CXX) $(CXXFLAGS) $(TEST) -o cache_test
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET) $(TEST)
|
||||
$(RM) $(OBJS) $(TARGET) cache_test
|
||||
|
||||
run: $(TARGET)
|
||||
./$(TARGET)
|
||||
$(RUN)
|
||||
|
||||
.PHONY: all clean run
|
||||
.PHONY: all clean run test
|
Loading…
x
Reference in New Issue
Block a user