-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathmax_cache.cpp
More file actions
92 lines (69 loc) · 2.29 KB
/
max_cache.cpp
File metadata and controls
92 lines (69 loc) · 2.29 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
#include <cmath>
#include "max_cache.h"
using namespace std;
bool MaxCache::isEmpty() {
int s = cache.size();
if (!s) return true;
if (s == 1) return cache.begin()->i == -1;
return false;
}
MaxCache::MaxCache(unsigned int size) {
this->size = size;
Max m = {-1, -1, -INFINITY};
cache.insert(m);
}
void MaxCache::check(unsigned int i, unsigned int j, double s) {
if (s > cache.begin()->s) {
Max m = {i, j, s};
// multiset<Max>::iterator it =
cache.insert(m);
// manteniamo la giusta dimensione della cache.
while (cache.size() > size) cache.erase(cache.begin());
}
}
/*
// ma questa funzione viene chiamata?
void MaxCache::update(int i, int j, int a)
{
multiset<Max>::iterator it = cache.begin();
// questa operazione è parallelizzabile, volendo. Come fare?
// inserire gli iteratori, tutti, in un array e poi darli in pasto a omp for
while (it != cache.end())
{
// questa non è un'operazione molto pulita da fare, ma preferisco fare così
// (aggiro il const) piuttosto che rimuovere e reinserire.
// eppure non capisco molto bene il motivo di questa operazione.
// se mettessi &n ?
Max n = *it;
// il valore che assume
((Max)*it).i += (n.i >= a) -(n.i >= i) -(n.i >= j);
((Max)*it).j += (n.j >= a) -(n.j >= i) -(n.j >= j);
// vado avanti con l'iteratore
it++;
}
}
*/
Max MaxCache::get() {
// il migliore dei massimi sarà alla fine, lo estraggo
Max r = *(--cache.end());
cache.erase(--cache.end());
// Iteratore per scorrere nel set. Dobbiamo filtrare gli elementi
multiset<Max>::iterator it = cache.begin();
while (it != cache.end()) {
if (it->i == r.i || it->j == r.i || it->i == r.j || it->j == r.j) {
// copio l'iteratore attuale su uno temporaneo
multiset<Max>::iterator aux = it;
// incremento quello attuale, così punta al prossimo elemento
it++;
// cancello l'elemento putanto dall'iteratore temporaneo. Non posso usare
// it normalmente, perché l'operazione di erase invalida l'iteratore
cache.erase(aux);
} else it++;
}
return r;
}
void MaxCache::clear() {
cache.clear();
Max m = {-1, -1, -INFINITY};
cache.insert(m);
}