-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevolvingList.h
126 lines (90 loc) · 3.09 KB
/
evolvingList.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
#ifndef EVOLVING_LIST_H
#define EVOLVING_LIST_H
#include <algorithm>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <random>
class EvolvingList
{
struct Element
{
Element()
: ptr(NULL)
{}
Element(Element* ptr_)
:ptr(ptr_)
{}
Element* ptr;
};
public:
enum class SwapType
{
RANDOMSWAP, HOTSPOT
};
class Stats
{
private:
struct SingleStat
{
long compares = 0;
long inversionCount = 0;
long goodSwaps = 0;
long badSwaps = 0;
friend std::ostream& operator<<(std::ostream &output, const SingleStat& stat ) {
output << stat.inversionCount << ","
<< stat.goodSwaps << ","
<< stat.badSwaps << ",";
output << stat.compares ;
return output;
}
};
public:
void addCompare() { ++currentStat.compares; }
void addGoodSwap() { ++currentStat.goodSwaps; }
void addBadSwap() { ++currentStat.badSwaps; }
void setInversionCount(long newCount) { currentStat.inversionCount = newCount;}
void sampleStat() { allStats.push_back(currentStat); }
int getCompares() const { return currentStat.compares;}
friend std::ostream& operator<<(std::ostream &output, const Stats& stats ) {
for(auto const& s : stats.allStats)
output << s << std::endl;
output << stats.currentStat << std::endl;
return output;
}
SingleStat currentStat;
std::vector<SingleStat> allStats;
};
EvolvingList(std::vector<unsigned int> const& startConfig, unsigned int sampleRate_, SwapType swapType_, unsigned int swapRate_, unsigned int seed);
int
compare(unsigned int i, unsigned int j);
void swap(unsigned int i, unsigned int j);
/*
* Applies a permutation to the answer list.
*/
void permuteAnswer(std::vector<int>& ansPerm);
void printValues() const;
unsigned int size() const { return n; }
unsigned long getInversions() const;
void sampleStat();
Stats getStats() const { return stats; }
int getTime() const { return stats.getCompares(); }
unsigned long totalInversions() const;
unsigned long mergeSortInversions(std::vector<int>& v) const;
private:
void randomSwap();
void hotSpotMovement();
// The "true" value of an element is the index in the real list
// The elements are linked by pointers which must be maintained when doind swaps
std::vector<Element> real; // The list seen by the random process
std::vector<Element> aprox; // The list seen by the sorting algorithm
const int n;
Stats stats;
const int sampleRate;
const SwapType swapType;
const int swapRate;
// Random generator stuff, should be encapsulated in a SwapGenerator class
std::mt19937_64 rng;
std::uniform_int_distribution<int> swapDist;
};
#endif // EVOLVING_LIST_H