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