14template <
typename T,
typename Comparator = std::less < T > >
23 void insert (
const T & value );
53 Node(
const T & val,
unsigned deg = 0,
Node * par =
nullptr,
Node * chld =
nullptr,
Node * pr =
nullptr,
Node * ne =
nullptr)
81template <
typename T,
typename Comparator >
83 deleteTreeList( m_max );
86template <
typename T,
typename Comparator >
94 deleteTreeList( actual->
child );
97 }
while (actual != head);
100template <
typename T,
typename Comparator >
102 Node * newNode =
new Node( value, 0,
nullptr,
nullptr,
nullptr,
nullptr );
103 newNode->
prev = newNode;
104 newNode->
next = newNode;
112 mergeHeaps( m_max, newNode );
114 m_max = m_comparator ( newNode->
value, m_max->value ) ? m_max : newNode;
119template <
typename T,
typename Comparator >
121 if ( m_max ==
nullptr )
122 throw std::out_of_range (
"Heap is empty." );
127 if (z->
child !=
nullptr) {
132 }
while (z1 != z->
child);
135 mergeHeaps ( z, z->
child );
151template <
typename T,
typename Comparator >
153 if (this->m_comparator != that.m_comparator)
154 throw std::logic_error(
"compare functions aren't equal, unable to merge");
156 mergeHeaps( this->m_max, that.m_max );
157 this->m_max = m_comparator ( that.m_max->value, m_max->value ) ? this->m_max : that.m_max;
158 that.m_max =
nullptr;
160 this->m_size += that.m_size;
164template <
typename T,
typename Comparator >
174 max1->
prev = max2prev;
178template <
typename T,
typename Comparator >
180 unsigned maxDegree = log2( m_size );
181 Node * * rootsByDegree =
new Node * [ maxDegree + 1 ];
182 for (
unsigned i = 0;
i <= maxDegree;
i++)
183 rootsByDegree[
i] =
nullptr;
186 Node * actual = next;
188 bool finish = next == next->
next;
192 finish = next == rootsByDegree [ next->
degree ];
194 while ( rootsByDegree[ actual->
degree ] !=
nullptr) {
195 Node * other = rootsByDegree[ actual->
degree ];
196 rootsByDegree[ actual->
degree ] =
nullptr;
198 if ( m_comparator ( other->
value, actual->
value ) ) {
199 linkTreeToNode( actual, other );
201 linkTreeToNode( other, actual );
206 rootsByDegree[ actual->
degree ] = actual;
209 delete [] rootsByDegree;
214 if ( m_comparator ( m_max->value,
node->value ) )
217 }
while (
node != actual );
220template <
typename T,
typename Comparator >
Definition: FibonacciHeap.h:15
Comparator m_comparator
Definition: FibonacciHeap.h:59
FibonacciHeap(Comparator comparator=Comparator())
Definition: FibonacciHeap.h:17
void mergeWith(FibonacciHeap< T, Comparator > &&that)
Definition: FibonacciHeap.h:152
void linkTreeToNode(Node *node, Node *tree)
Definition: FibonacciHeap.h:221
void deleteTreeList(Node *head)
Definition: FibonacciHeap.h:87
void mergeHeaps(Node *&max1, Node *&max2)
Definition: FibonacciHeap.h:165
void insert(const T &value)
Definition: FibonacciHeap.h:101
~FibonacciHeap()
Definition: FibonacciHeap.h:82
size_t m_size
Definition: FibonacciHeap.h:58
size_t size() const
Definition: FibonacciHeap.h:36
Node * m_max
Definition: FibonacciHeap.h:57
T extractMax()
Definition: FibonacciHeap.h:120
const T & getMax() const
Definition: FibonacciHeap.h:26
void consolidate()
Definition: FibonacciHeap.h:179
Definition: ExceptionHandler.cpp:13
int i
Definition: AllEpsilonClosure.h:118
Definition: BackwardOccurrenceTest.h:17
Definition: FibonacciHeap.h:45
Node * parent
Definition: FibonacciHeap.h:49
Node * prev
Definition: FibonacciHeap.h:51
bool mark
Definition: FibonacciHeap.h:48
T value
Definition: FibonacciHeap.h:46
Node * child
Definition: FibonacciHeap.h:50
Node(const T &val, unsigned deg=0, Node *par=nullptr, Node *chld=nullptr, Node *pr=nullptr, Node *ne=nullptr)
Definition: FibonacciHeap.h:53
Node * next
Definition: FibonacciHeap.h:52
unsigned degree
Definition: FibonacciHeap.h:47