-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLFU.cpp
More file actions
127 lines (118 loc) · 3.31 KB
/
LFU.cpp
File metadata and controls
127 lines (118 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "LFU.hpp"
equivalence_class_num_type LFU::get_the_number_of_equivalence_classes() const
{
cout << "cache_manager_LRU::get_the_number_of_equivalence_classes()const not need to acomplish" << endl;
exit(0);
return 0;
}
data_cell_num_type LFU::get_the_number_of_data_units() const
{
return LFU_Container.size();
}
bool LFU::query_in_cache(queryItem &queryItem)
{
if (LFU_Container.empty())
{
return false;
}
bool flag = false;
for (auto &a : LFU_Container)
{
if (a.getDims() == queryItem.getDims())
{
queryItem.setMeasure(a.getMeasure());
a.add_LFU_count();
flag = true;
break;
}
}
if (flag)
{
return true;
}
else
{
return false;
}
}
void LFU::refresh_cache(const queryItem &queryItem)
{
LFU_Container.push_back(queryItem);
while (get_the_number_of_data_units() > data_cells_limit_size)
{
cache_replacement_policy();
}
}
void LFU::cache_replacement_policy()
{
auto min = LFU_Container.begin();
for (auto a = LFU_Container.begin()++; a != LFU_Container.end(); ++a)
{
if (a->get_LFU_count() < min->get_LFU_count())
{
min = a;
}
}
LFU_Container.erase(min);
}
vector<queryItem> LFU::load_query_file()
{
vector<vector<string>> temp_queryItemSet;
ifstream infile(querySetFilePath);
string temp;
if (!infile.is_open())
{
cout << querySetFilePath + "the file is not opened under the path" << endl;
exit(0);
}
while (getline(infile, temp))
{
vector<string> tempvec;
SplitString(temp, tempvec, ",");
temp_queryItemSet.push_back(tempvec);
}
infile.close();
vector<queryItem> ret_queryItemSet;
for (auto a : temp_queryItemSet)
{
dims_type temp_dims;
measure_type temp_measure = 0.0;
for (int i = 0; i < dimsNum; ++i)
{
temp_dims[i] = atoi(a[i].c_str());
}
queryItem temp_queryItem(temp_dims, temp_measure);
ret_queryItemSet.push_back(temp_queryItem);
}
return ret_queryItemSet;
}
void LFU::displasy_cachaItem() const
{
cout << endl
<< endl
<< "the number of the data cells:" << get_the_number_of_data_units() << endl;
for_each(LFU_Container.begin(), LFU_Container.end(), [](const queryItem &every_quertItem)
{ every_quertItem.display(); });
}
void LFU::dispaly_experimental_result_to_file(data_cell_num_type queryItemSet_size, int hit, time_t all_time) const
{
ofstream to_file;
to_file.open(basePath + "LFUalg.txt", ios::trunc);
if (!to_file.is_open())
{
cout << "fail to write results into files" << endl;
exit(0);
}
to_file << "alg name:LFU" << endl;
to_file << "the number ofquery items:" << queryItemSet_size << endl;
to_file << "cache limit(the number of the data cells):" << data_cells_limit_size << endl;
to_file << "hit number:" << hit << endl;
to_file << "hit rate:" << (float)hit / queryItemSet_size << endl;
to_file << "total use time(s):" << all_time << endl;
to_file << "the number of data cells:" << get_the_number_of_data_units() << endl;
}
vector<queryItem> LFU::loadQueryFile()
{
vector<queryItem> ret_queryItemSet;
return ret_queryItemSet;
}