The bliss C++ API
Loading...
Searching...
No Matches
src
heap.hh
1
#pragma once
2
3
/*
4
Copyright (c) 2003-2021 Tommi Junttila
5
Released under the GNU Lesser General Public License version 3.
6
7
This file is part of bliss.
8
9
bliss is free software: you can redistribute it and/or modify
10
it under the terms of the GNU Lesser General Public License as published by
11
the Free Software Foundation, version 3 of the License.
12
13
bliss is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU Lesser General Public License for more details.
17
18
You should have received a copy of the GNU Lesser General Public License
19
along with bliss. If not, see <http://www.gnu.org/licenses/>.
20
*/
21
22
#include <vector>
23
#include <algorithm>
24
25
namespace
bliss
{
26
31
class
Heap
32
{
33
std::vector<unsigned int> contents;
37
struct
{
39
bool
operator()(
const
unsigned
int
e1,
const
unsigned
int
e2) {
return
e1 > e2; }
40
} gt;
41
public
:
42
47
bool
is_empty
()
const
{
return
contents.empty(); }
48
53
void
clear
() {contents.clear(); }
54
60
void
insert
(
const
unsigned
int
e) {
61
contents.push_back(e);
62
std::push_heap(contents.begin(), contents.end(), gt);
63
}
64
69
unsigned
int
smallest
()
const
{
return
contents.front(); }
70
76
unsigned
int
remove
() {
77
const
unsigned
int
result =
smallest
();
78
std::pop_heap(contents.begin(),contents.end(), gt);
79
contents.pop_back();
80
return
result;
81
}
82
86
size_t
size
()
const
{
return
contents.size(); }
87
};
88
89
}
// namespace bliss
bliss::Heap
A min-heap of unsigned integers.
Definition
heap.hh:32
bliss::Heap::clear
void clear()
Definition
heap.hh:53
bliss::Heap::insert
void insert(const unsigned int e)
Definition
heap.hh:60
bliss::Heap::size
size_t size() const
Definition
heap.hh:86
bliss::Heap::smallest
unsigned int smallest() const
Definition
heap.hh:69
bliss::Heap::remove
unsigned int remove()
Definition
heap.hh:76
bliss::Heap::is_empty
bool is_empty() const
Definition
heap.hh:47
bliss
Definition
abstractgraph.cc:35
Generated by
1.10.0