-
Notifications
You must be signed in to change notification settings - Fork 17
/
FreqTable.h
131 lines (122 loc) · 3.24 KB
/
FreqTable.h
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
128
129
130
131
#ifndef _FREQTABLE_H_
#define _FREQTABLE_H_
template <class T>
class FreqTable{
public:
void add(const T& t) {
if (this->data.find(t) == this->data.end()) {
this->data[t] = 1;
} else {
this->data[t] ++;
}
this->isSorted = false;
}
/**
* if successfully minus one count, @return true; otherwise, @return false
*/
bool remove(const T& t) {
if (this->data.find(t) == this->data.end()) {
return false;
}
this->data[t] -- ;
if (this->data[t] == 0){
this->data.erase(this->data.find(t));
}
this->isSorted = false;
return true;
}
size_t size() const{ return this->data.size();};
// return the frequency in ascending order
void at(const unsigned int idx, T* t, int* v) {
if (!this->isSorted)
this->sortByFrequency();
*v = this->orderedData[idx].first;
*t = *(this->orderedData[idx].second);
}
void clear() {
this->data.clear();
this->orderedData.clear();
this->isSorted = false;
}
void dump() {
for (size_t i = 0; i < this->orderedData.size(); ++i) {
std::cout << i << "\t"
<< orderedData[i].first << "\t"
<< *(orderedData[i].second) << "\n";
}
}
private:
void sortByFrequency() {
this->sortByKey();
std::stable_sort(this->orderedData.begin(), this->orderedData.end(), FreqTable::sortFirstInPair);
this->isSorted = true;
/* dump(); */
}
void sortByKey() {
this->orderedData.clear();
typename std::map<T, int >::iterator it;
for (it = this->data.begin();
it != this->data.end() ; it++) {
this->orderedData.push_back(std::make_pair( (*it).second, &((*it).first)) );
}
/* dump(); */
/* std::stable_sort(this->orderedData.begin(), this->orderedData.end(), FreqTable::sortSecondInPair); */
/* dump(); */
this->isSorted = true;
}
static bool sortFirstInPair( const std::pair<int, const T*>& t1,
const std::pair<int, const T*>& t2) {
return t1.first < t2.first;
}
static bool sortSecondInPair( const std::pair<int, const T*>& t1,
const std::pair<int, const T*>& t2) {
return *(t1.second) < *(t2.second);
}
std::map< T, int > data;
std::vector< std::pair<int, const T*> > orderedData;
bool isSorted;
};
//////////////////////////////////////////////////////////////////////
// Test case 1 //
/*
FreqTable<std::string> codonFreq;
codonFreq.add("A");
codonFreq.add("B");
codonFreq.add("C");
codonFreq.add("D");
codonFreq.add("A");
codonFreq.add("B");
codonFreq.add("C");
codonFreq.add("A");
codonFreq.add("B");
codonFreq.add("A");
std::string s;
int f;
for (unsigned int i = 0 ; i < codonFreq.size(); i++) {
codonFreq.at(i, &s, &f);
printf("freq of %s is %d\n", s.c_str(), f);
};
return 0;
*/
// Test case 2 //
/*
FreqTable<char> codonFreq;
codonFreq.add('A');
codonFreq.add('B');
codonFreq.add('C');
codonFreq.add('D');
codonFreq.add('A');
codonFreq.add('B');
codonFreq.add('C');
codonFreq.add('A');
codonFreq.add('B');
codonFreq.add('A');
char s;
int f;
for (unsigned int i = 0 ; i < codonFreq.size(); i++) {
codonFreq.at(i, &s, &f);
printf("freq of %c is %d\n", s, f);
};
return 0;
*/
#endif /* _FREQTABLE_H_ */