-
Notifications
You must be signed in to change notification settings - Fork 0
/
adorate_struct.h
131 lines (105 loc) · 2.54 KB
/
adorate_struct.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 MIMUW_ADORATE_42_ADORATE_STRUCT_H
#define MIMUW_ADORATE_42_ADORATE_STRUCT_H
#include <iostream>
#include <fstream>
#include <list>
#include <unordered_map>
#include <condition_variable>
#include <algorithm>
#include <thread>
#include <mutex>
#include <queue>
#include <atomic>
const int NOT_FOUND = -1;
std::vector<long> decode;
struct Edge {
long id;
long weigh;
bool operator < (Edge e) {
if (this->weigh == e.weigh) return decode.at(this->id) < decode.at(e.id);
return this->weigh < e.weigh;
}
};
bool comp(Edge a, Edge b) {
return b < a;
}
template<typename T>
class neighbours {
public:
neighbours() : it(N.rbegin()), sorted(N.rbegin()) {}
neighbours(const neighbours &other) : N(other.N), it(N.rbegin()),
sorted(N.rbegin()) {}
neighbours(neighbours && other) = default;
bool empty() const {
return it == N.rend();
}
void set_bvalue(unsigned int b) {
bvalue = 2 * b;
}
T back() {
if (it == sorted) {
sort();
}
return *it;
}
void pop_back() {
++it;
}
void push_back(const T &el) {
N.push_back(el);
it = N.rbegin();
sorted = N.rbegin();
}
void sort() {
if (sorted + bvalue > N.rend()) {
std::sort(it, N.rend(), comp);
sorted = N.rend();
} else {
std::partial_sort(it, sorted + bvalue, N.rend(), comp);
sorted += bvalue;
}
}
void reload() {
it = N.rbegin();
}
private:
std::vector<T> N;
typename std::vector<T>::reverse_iterator it;
typename std::vector<T>::reverse_iterator sorted;
unsigned int bvalue;
};
template<typename T>
class atom_queue {
public:
void push(const T& value ) {
std::lock_guard<std::mutex> lock(m_mutex);
m_queue.push(value);
}
long pop() {
std::lock_guard<std::mutex> lock(m_mutex);
if (m_queue.empty()) {
return NOT_FOUND;
}
long u = m_queue.front();
m_queue.pop();
return u;
}
bool empty() const {
std::lock_guard<std::mutex> lock(m_mutex);
return m_queue.empty();
}
void swap(atom_queue &Q) {
std::lock_guard<std::mutex> lock(m_mutex);
std::swap(Q.m_queue, m_queue);
}
private:
std::queue<T> m_queue;
mutable std::mutex m_mutex;
};
class Compare {
public:
bool operator() (Edge a, Edge b) {
return b < a;
}
};
#endif //MIMUW_ADORATE_42_ADORATE_STRUCT_H