-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElementInMinHeapAndMaxHeap.h
94 lines (74 loc) · 2.51 KB
/
ElementInMinHeapAndMaxHeap.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
#ifndef ELEMENT_IN_MIN_HEAP_AND_MAX_HEAP_H
#define ELEMENT_IN_MIN_HEAP_AND_MAX_HEAP_H
#include <ostream>
/**
* @brief This class is designed to wrap an `Element` with two indexes.
* @li one index tells at which index this `element` is placed on
* a minimum-heap.
* @li the other index tells at which index this `element` is placed on
* a maximum-heap.
* @tparam E the type of `element`, that *must* be *comparable* to other
* `elements`.
*/
template<typename E> class ElementInMinHeapAndMaxHeap {
protected:
Unique<E> *_uniqueElement = nullptr;
protected:
long int _maxHeapIndex = 0;
protected:
long int _minHeapIndex = 0;
public:
explicit ElementInMinHeapAndMaxHeap(E *element) {
_uniqueElement = new Unique<E>(element);
}
public:
explicit ElementInMinHeapAndMaxHeap(E &&element) {
_uniqueElement = new Unique<E>((E &&) element);
}
public:
virtual ~ElementInMinHeapAndMaxHeap() {
delete _uniqueElement;
}
public:
Unique<E> *getUniqueElement() const { return _uniqueElement; }
public:
void setUniqueElement(Unique<E> *uniqueElement) {
_uniqueElement = uniqueElement;
}
public:
long getMaxHeapIndex() const { return _maxHeapIndex; }
public:
void setMaxHeapIndex(long maxHeapIndex) { _maxHeapIndex = maxHeapIndex; }
public:
long getMinHeapIndex() const { return _minHeapIndex; }
public:
void setMinHeapIndex(long minHeapIndex) { _minHeapIndex = minHeapIndex; }
public:
friend std::ostream &
operator<<(std::ostream & os,
const ElementInMinHeapAndMaxHeap &entryInMinHeapAndMaxHeap) {
os << *(entryInMinHeapAndMaxHeap._uniqueElement);
return os;
}
public:
bool operator==(const ElementInMinHeapAndMaxHeap &rhs) const {
return _uniqueElement == rhs._uniqueElement; // Compare by value.
}
bool operator!=(const ElementInMinHeapAndMaxHeap &rhs) const {
return !(rhs == *this);
}
public:
bool operator<(const ElementInMinHeapAndMaxHeap &rhs) const {
return *_uniqueElement < *(rhs._uniqueElement); // Compare by value.
}
bool operator>(const ElementInMinHeapAndMaxHeap &rhs) const {
return rhs < *this;
}
bool operator<=(const ElementInMinHeapAndMaxHeap &rhs) const {
return !(rhs < *this);
}
bool operator>=(const ElementInMinHeapAndMaxHeap &rhs) const {
return !(*this < rhs);
}
};
#endif // ELEMENT_IN_MIN_HEAP_AND_MAX_HEAP_H