ideal_cache_1_ver
This commit is contained in:
parent
a21fc9e08d
commit
55345b7c76
BIN
cache_test
BIN
cache_test
Binary file not shown.
@ -2,9 +2,96 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include "2Q_cache.h"
|
#include "2Q_cache.h"
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
const char* default_test_file = "tests.txt";
|
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) {
|
int cache_test(std::string file_name) {
|
||||||
std::ifstream file(file_name);
|
std::ifstream file(file_name);
|
||||||
@ -20,21 +107,24 @@ 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, test_passed = 0, test_failed = 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);
|
||||||
ss >> fast_q_sz >> slow_q_sz>>num_of_calls; //read input
|
ss >> fast_q_sz >> slow_q_sz>>num_of_calls; //read input
|
||||||
|
|
||||||
TwoCache cache(fast_q_sz, slow_q_sz); //initialising
|
TwoCache cache(fast_q_sz, slow_q_sz); //initialising
|
||||||
|
IdealCache ideal_cache(test_line);
|
||||||
|
|
||||||
for(int i = 0; i < num_of_calls; i++) { //executing
|
for (int i = 0; i < num_of_calls; i++) { //executing
|
||||||
ss >> page_id;
|
ss >> page_id;
|
||||||
cache.put_page(page_id);
|
cache.put_page(page_id);
|
||||||
|
ideal_cache.put_page(page_id, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::getline(file, answer_line);
|
std::getline(file, answer_line);
|
||||||
|
|
||||||
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";
|
||||||
|
std::cout << "ideal tics / your tics " << ideal_cache.tic_number << "/" << cache.tic_number << std::endl;
|
||||||
test_passed++;
|
test_passed++;
|
||||||
} else {
|
} else {
|
||||||
std::cout << "test - " << test_number << " failed\n";
|
std::cout << "test - " << test_number << " failed\n";
|
||||||
@ -53,7 +143,7 @@ int cache_test(std::string file_name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
if(argc == 0) {
|
if (argc == 0) {
|
||||||
cache_test(default_test_file);
|
cache_test(default_test_file);
|
||||||
} else {
|
} else {
|
||||||
cache_test(argv[1]);
|
cache_test(argv[1]);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user