-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin_heap.h
More file actions
116 lines (91 loc) · 2.36 KB
/
min_heap.h
File metadata and controls
116 lines (91 loc) · 2.36 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
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
//
// Created by guser on 12/16/17.
//
#ifndef BMATCHING_P_QUEUE_H
#define BMATCHING_P_QUEUE_H
#include "atomic"
class spinlock {
std::atomic_flag locked = ATOMIC_FLAG_INIT ;
public:
void lock() {
while (locked.test_and_set(std::memory_order_acquire)) { ; }
}
void unlock() {
locked.clear(std::memory_order_release);
}
};
#include <memory>
#include <queue>
#include <mutex>
#include <set>
#include <shared_mutex>
#include "functional"
extern std::vector<unsigned int> id_tId;
struct edge {
struct hash {
size_t operator()(const edge& e) const {
return (std::hash<int>()(e.to) << 1) ^
(std::hash<int>()(e.from << 1) >> 1) ^
(std::hash<int>()(e.to) << 1);
}
};
edge(int from, double weight, int to) : from(from), weight(weight), to(to) {}
edge() = default;
bool operator<(const edge& other) const {
if(from==-1){ return true; }
if (from == other.from) {
if (weight == other.weight) {
return id_tId[to] < id_tId[other.to];
} else {
return weight < other.weight;
}
} else {
return from < other.from;
}
}
bool operator==(const edge& other) const {
return from == other.from && weight == other.weight && to == other.to;
}
bool operator!=(const edge& other) const {
return !(*this == other);
}
bool operator>(const edge& other) const {
return other < *this;
};
edge reverse() { return {to, weight, from}; };
edge canonical() {
if (from > to) {
return this->reverse();
}
return *this;
}
static edge empty;
int from;
double weight;
int to;
};
class min_heap {
public:
min_heap(const min_heap& other) {
container = (other.container);
limit = other.limit;
}
explicit min_heap(unsigned int max_size) {
container = std::set<edge>();
limit = max_size;
};
std::set<edge> get_container() {
return container;
};
edge push(edge p);
edge min();
bool contains(edge e);
void reset(unsigned int n_limit );
// std::mutex gen_mut;
std::shared_mutex gen_mut;
// spinlock gen_mut;
private:
std::set<edge> container;
unsigned int limit;
};
#endif //BMATCHING_P_QUEUE_H