forked from giordamaug/WisardClassifier-C_vectors
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ram.cpp
62 lines (54 loc) · 1.88 KB
/
Ram.cpp
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
//
// Ram.cpp
//
//
// Created by Maurizio Giordano on 25/05/18.
//
#include "Ram.h"
namespace wnn {
Ram::Ram(int Id, int NLocs) {
idx = Id;
n_locs = NLocs;
}
Ram::~Ram() {};
double Ram::getEntry(unsigned long long key) {
auto it = std::find_if( wentries.begin(), wentries.end(), [key](const Wentry& element){ return element.first == key;} );
if (it != wentries.end())
return it->second;
else
return 0.0;
}
void Ram::setEntry(unsigned long long key, double value) {
auto it = std::find_if( wentries.begin(), wentries.end(), [key](const Wentry& element){ return element.first == key;} );
if (it != wentries.end())
it->second = value;
else
wentries.push_back(std::make_pair(key,value));
}
void Ram::incrEntry(unsigned long long key, double value) {
auto it = std::find_if( wentries.begin(), wentries.end(), [key](const Wentry& element){ return element.first == key;} );
if (it != wentries.end())
it->second += value;
else {
wentries.push_back(std::make_pair(key,value));
}
};
void Ram::decrEntry(unsigned long long key, double value) {
auto it = std::find_if( wentries.begin(), wentries.end(), [key](const Wentry& element){ return element.first == key;} );
if (it != wentries.end()) {
it->second -= value;
if (it->second == 0.0) wentries.erase(it);
}
};
std::string Ram::toString() {
std::stringstream ss;
ss << "{";
std::vector<Wentry>::iterator it;
for (it = wentries.begin(); it != wentries.end(); it++) {
ss << it->first << ":" << it->second << " ";
}
if (wentries.size() > 0) ss.seekp(-1, std::ios_base::end);
ss << "},";
return ss.str();
}
}