-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopList.h
82 lines (69 loc) · 1.8 KB
/
TopList.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
/*
* TopList.h
*
* Created on: 23 Dec, 2014
\* Author: Mehrdad Tahernia
*/
#ifndef TOPLIST_H_
#define TOPLIST_H_
#include "GFq.h"
/************************************************************************
*
* TopList - for use in greedy
*
************************************************************************/
// TODO: I don't Understand
class TopList {
public:
GFq *BestChange;
int *VariableIndex;
double *DistortionImprovement;
int MaxSize, CurrentSize;
public:
TopList() : MaxSize(-1), CurrentSize(-1) ,BestChange(NULL),DistortionImprovement(NULL),VariableIndex(NULL) {}
~TopList() {
DeAllocate();
}
void DeAllocate() {
if (MaxSize > -1) {
delete BestChange;
delete VariableIndex;
delete DistortionImprovement;
}
MaxSize = -1;
}
void Init(int p_MaxSize) {
if (MaxSize != p_MaxSize) {
DeAllocate();
BestChange = new GFq[p_MaxSize];
VariableIndex = new int[p_MaxSize];
DistortionImprovement = new double[p_MaxSize];
}
MaxSize = p_MaxSize;
CurrentSize = 0;
}
void Add(GFq p_BestChange, int p_VariableIndex,
double p_DistortionImprovement) {
// Find place for new distortion
int Place;
for (Place = 0; Place < CurrentSize; Place++)
if (p_DistortionImprovement > DistortionImprovement[Place])
break;
// Shift all the rest one place up
if (CurrentSize < MaxSize)
CurrentSize++;
for (int i = CurrentSize - 1; i > Place; i--) {
BestChange[i] = BestChange[i - 1];
VariableIndex[i] = VariableIndex[i - 1];
DistortionImprovement[i] = DistortionImprovement[i - 1];
}
// Place new entry
if (Place < CurrentSize) // If not passed last place in TopList
{
BestChange[Place] = p_BestChange;
VariableIndex[Place] = p_VariableIndex;
DistortionImprovement[Place] = p_DistortionImprovement;
}
}
};
#endif /* TOPLIST_H_ */