153 lines
4.3 KiB
C++
153 lines
4.3 KiB
C++
#include <iostream>
|
|
#include <fstream>
|
|
#include "2Q_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);
|
|
|
|
if(!file.is_open()) {
|
|
std::cerr << "File open error" << std::endl; //open test file
|
|
|
|
return -1;
|
|
}
|
|
|
|
|
|
|
|
std::string test_line, answer_line;
|
|
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);
|
|
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);
|
|
|
|
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);
|
|
}
|
|
|
|
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;
|
|
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++;
|
|
}
|
|
|
|
file.close();
|
|
|
|
std::cout << "\nNumber of tests = "<< test_number << "\nTests passed = " << test_passed << "\nTests failed = " << test_failed << "\n";
|
|
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char* argv[]) {
|
|
if (argc == 0) {
|
|
cache_test(default_test_file);
|
|
} else {
|
|
cache_test(argv[1]);
|
|
}
|
|
}
|
|
|