ideal_cache

This commit is contained in:
ag 2024-09-22 03:25:10 +03:00
parent 55345b7c76
commit 552953591e
4 changed files with 115 additions and 91 deletions

69
Ideal_cache.h Normal file
View File

@ -0,0 +1,69 @@
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
class Ideal_Cache {
private:
int capacity;
std::vector<int> cache;
std::unordered_map<int, int> cacheMap; // Для быстрого доступа к индексам
public:
Ideal_Cache(size_t capacity) : capacity(capacity) {}
int tic_number = 0;
void get_request_vector(const std::vector<int>& requests) {
// смотрим есть ли айди в кэше
// Есть - тик
// Нет - смотрим размер, если размер не полный добавляем в кэш
// если размер полный, то уберем тот, который позже всех встретится
for (size_t i = 0; i < requests.size(); ++i) {
int item = requests[i];
if (cacheMap.find(item) != cacheMap.end()) {
tic_number++; //tic
continue;
}
if (cache.size() < capacity) {
cache.push_back(item);
cacheMap[item] = cache.size() - 1;
} else {
int farthest = -1;
int indexToRemove = -1;
for (size_t j = 0; j < cache.size(); ++j) {
auto nextIndex = std::find(requests.begin() + i, requests.end(), cache[j]); //находим следующее вхождение (начиная с i ого тк предыдущие обработались)
if (nextIndex == requests.end()) {
indexToRemove = j; //В конце удалим последний
break;
} else {
int nextIdx = std::distance(requests.begin(), nextIndex);
if (nextIdx > farthest) {
farthest = nextIdx;
indexToRemove = j;
}
}
}
cacheMap.erase(cache[indexToRemove]);
cache[indexToRemove] = item;
cacheMap[item] = indexToRemove;
}
}
}
void info() const {
std::cout << "ideal cache: ";
for (int item : cache) {
std::cout << item << " ";
}
std::cout << std::endl;
}
};

Binary file not shown.

View File

@ -1,97 +1,13 @@
#include <iostream>
#include <fstream>
#include "2Q_cache.h"
#include "Ideal_cache.h"
#include <sstream>
#include <vector>
#include <algorithm>
const char* default_test_file = "tests.txt";
class IdealCache {
private:
size_t cpst;
std::vector <int> cache;
std::vector <int> vec;
int findLastIndex(const std::vector<int>& vec1, const std::vector<int>& vec2) { //
std::unordered_map<int, int> lastIndexMap;
for (size_t i = 0; i < vec2.size(); ++i) {
lastIndexMap[vec2[i]] = i;
}
int lastIndex = -1;
int lastValueIndex = -1;
for (size_t i = 0; i < vec1.size(); ++i) {
if (lastIndexMap.find(vec1[i]) != lastIndexMap.end()) {
if (lastIndexMap[vec1[i]] > lastIndex) {
lastIndex = lastIndexMap[vec1[i]];
lastValueIndex = i;
}
}
}
return lastValueIndex;
}
public:
IdealCache (std::string test_line) {
int number;
std::stringstream ss(test_line);
while(ss >> number)
vec.push_back(number);
cpst = vec[0] + vec[1];
}
int tic_number = 0;
void put_page(int id, int number_of_request) {
if (cache.size() < cpst) {
cache.push_back(id);
//info();
}
else if (std::find(cache.begin(), cache.end(), id) != cache.end()) {
tic_number++;
cache.push_back(id);
//info();
int last_index = findLastIndex(cache, vec.begin() + number_of_request);
//std::cout << "Last index:" << last_index << "\n";
if (last_index != -1){
cache.erase(cache.begin() + last_index);
//info();
} else {
cache.pop_back();
//info();
}
}
}
void info() {
std::cout<<"ideal cache:\n";
for (auto i = cache.begin(); i != cache.end(); i++) {
std::cout << *i << " ";
}
std::cout << "\n";
std::cout<<"request vec:\n";
for (auto i = vec.begin(); i != vec.end(); i++) {
std::cout << *i << " ";
}
std::cout << "\n";
}
};
int cache_test(std::string file_name) {
std::ifstream file(file_name);
@ -112,19 +28,23 @@ int cache_test(std::string file_name) {
ss >> fast_q_sz >> slow_q_sz>>num_of_calls; //read input
TwoCache cache(fast_q_sz, slow_q_sz); //initialising
IdealCache ideal_cache(test_line);
Ideal_Cache ideal_cache(fast_q_sz + slow_q_sz);
std::vector <int> requests;
for (int i = 0; i < num_of_calls; i++) { //executing
ss >> page_id;
cache.put_page(page_id);
ideal_cache.put_page(page_id, i);
requests.push_back(page_id);
}
ideal_cache.get_request_vector(requests);
std::getline(file, answer_line);
if (cache.string_info() == answer_line) { //compare answers
std::cout << "test - " << test_number << " passed\n";
std::cout << "ideal tics / your tics " << ideal_cache.tic_number << "/" << cache.tic_number << std::endl;
std::cout << "ideal tics/your tics " << ideal_cache.tic_number << "/" << cache.tic_number << std::endl;
test_passed++;
} else {
std::cout << "test - " << test_number << " failed\n";
@ -148,5 +68,4 @@ int main(int argc, char* argv[]) {
} else {
cache_test(argv[1]);
}
}
}

View File

@ -26,4 +26,40 @@ clean:
run: $(TARGET)
./$(TARGET)
.PHONY: all clean run
.PHONY: all clean run
TARGET = Q2
CXX = g++
CXXFLAGS = -Wall -Wextra -std=c++11
SRCS = main.cpp
TEST = cache_test.cpp
OBJS = $(SRCS:.cpp=.o)
all: $(TARGET)
$(TARGET): $(OBJS)
$(CXX) -o $@ $^
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
test:
$(CXX) $(CXXFLAGS) $(TEST) -o cache_test
clean:
rm -f $(OBJS) $(TARGET) cache_test
run: $(TARGET)
./$(TARGET)
# Windows compatibility for cleaning
ifeq ($(OS), Windows_NT)
clean:
del /Q $(OBJS) $(TARGET) cache_test
run: $(TARGET)
$(TARGET).exe
endif
.PHONY: all clean run test