/src/igraph/src/isomorphism/bliss/heap.cc
Line | Count | Source |
1 | | #include "heap.hh" |
2 | | |
3 | | #include <new> |
4 | | #include <cassert> |
5 | | |
6 | | /* Allow using 'and' instead of '&&' with MSVC */ |
7 | | #if _MSC_VER |
8 | | #include <ciso646> |
9 | | #endif |
10 | | |
11 | | /* |
12 | | Copyright (c) 2003-2021 Tommi Junttila |
13 | | Released under the GNU Lesser General Public License version 3. |
14 | | |
15 | | This file is part of bliss. |
16 | | |
17 | | bliss is free software: you can redistribute it and/or modify |
18 | | it under the terms of the GNU Lesser General Public License as published by |
19 | | the Free Software Foundation, version 3 of the License. |
20 | | |
21 | | bliss is distributed in the hope that it will be useful, |
22 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
23 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24 | | GNU Lesser General Public License for more details. |
25 | | |
26 | | You should have received a copy of the GNU Lesser General Public License |
27 | | along with bliss. If not, see <http://www.gnu.org/licenses/>. |
28 | | */ |
29 | | |
30 | | namespace bliss { |
31 | | |
32 | 4.62k | Heap::Heap() { |
33 | 4.62k | array = nullptr; |
34 | 4.62k | n = 0; |
35 | 4.62k | N = 0; |
36 | 4.62k | } |
37 | | |
38 | | Heap::~Heap() |
39 | 4.62k | { |
40 | 4.62k | delete[] array; |
41 | 4.62k | array = nullptr; |
42 | 4.62k | n = 0; |
43 | 4.62k | N = 0; |
44 | 4.62k | } |
45 | | |
46 | | void Heap::upheap(unsigned int index) |
47 | 598k | { |
48 | 598k | assert(n >= 1); |
49 | 598k | assert(index >= 1 and index <= n); |
50 | 598k | const unsigned int v = array[index]; |
51 | 598k | array[0] = 0; |
52 | 660k | while(array[index/2] > v) |
53 | 62.1k | { |
54 | 62.1k | array[index] = array[index/2]; |
55 | 62.1k | index = index/2; |
56 | 62.1k | } |
57 | 598k | array[index] = v; |
58 | 598k | } |
59 | | |
60 | | void Heap::downheap(unsigned int index) |
61 | 598k | { |
62 | 598k | const unsigned int v = array[index]; |
63 | 598k | const unsigned int lim = n/2; |
64 | 612k | while(index <= lim) |
65 | 22.1k | { |
66 | 22.1k | unsigned int new_index = index + index; |
67 | 22.1k | if((new_index < n) and (array[new_index] > array[new_index+1])) |
68 | 2.52k | new_index++; |
69 | 22.1k | if(v <= array[new_index]) |
70 | 8.62k | break; |
71 | 13.4k | array[index] = array[new_index]; |
72 | 13.4k | index = new_index; |
73 | 13.4k | } |
74 | 598k | array[index] = v; |
75 | 598k | } |
76 | | |
77 | | void Heap::init(const unsigned int size) |
78 | 4.61k | { |
79 | 4.61k | assert(size > 0); |
80 | 4.61k | if(size > N) |
81 | 4.61k | { |
82 | 4.61k | delete[] array; |
83 | 4.61k | array = new unsigned int[size + 1]; |
84 | 4.61k | N = size; |
85 | 4.61k | } |
86 | 4.61k | n = 0; |
87 | 4.61k | } |
88 | | |
89 | | void Heap::insert(const unsigned int v) |
90 | 598k | { |
91 | 598k | assert(n < N); |
92 | 598k | array[++n] = v; |
93 | 598k | upheap(n); |
94 | 598k | } |
95 | | |
96 | | unsigned int Heap::smallest() const |
97 | 0 | { |
98 | 0 | assert(n >= 1 and n <= N); |
99 | 0 | return array[1]; |
100 | 0 | } |
101 | | |
102 | | unsigned int Heap::remove() |
103 | 598k | { |
104 | 598k | assert(n >= 1 and n <= N); |
105 | 598k | const unsigned int v = array[1]; |
106 | 598k | array[1] = array[n--]; |
107 | 598k | downheap(1); |
108 | 598k | return v; |
109 | 598k | } |
110 | | |
111 | | } // namespace bliss |