File: heap.cc

package info (click to toggle)
mrcal 2.5-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 8,992 kB
  • sloc: python: 40,651; ansic: 15,632; cpp: 1,754; perl: 303; makefile: 160; sh: 99; lisp: 84
file content (57 lines) | stat: -rw-r--r-- 1,392 bytes parent folder | download | duplicates (2)
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
/*
  A simple min-priority-queue implementation. Uses STL internally
*/
#include <algorithm>
#include <functional>

extern "C"
{
#include "heap.h"
}

// empty namespace to prevent C++ from exporting any symbols here
namespace {
struct Compare_nodes_greater
{
    mrcal_heap_node_t* nodes;
    Compare_nodes_greater(mrcal_heap_node_t* _nodes) : nodes(_nodes) {}
    bool operator()(const uint16_t& idx_a, const uint16_t& idx_b) const
    {
        return nodes[idx_a].cost > nodes[idx_b].cost;
    }
};
}

extern "C"
bool     mrcal_heap_empty (mrcal_heap_t* heap, mrcal_heap_node_t* nodes)
{
    return heap->size==0;
}

extern "C"
void     mrcal_heap_push  (mrcal_heap_t* heap, mrcal_heap_node_t* nodes, uint16_t x)
{
    heap->buffer[heap->size++] = x;
    std::push_heap(&heap->buffer[0],
                   &heap->buffer[heap->size],
                   Compare_nodes_greater(nodes));
}

extern "C"
uint16_t mrcal_heap_pop   (mrcal_heap_t* heap, mrcal_heap_node_t* nodes)
{
    uint16_t x = heap->buffer[0];
    std::pop_heap(&heap->buffer[0],
                  &heap->buffer[heap->size],
                  Compare_nodes_greater(nodes));
    heap->size--;
    return x;
}

extern "C"
void mrcal_heap_resort(mrcal_heap_t* heap, mrcal_heap_node_t* nodes)
{
    std::make_heap(&heap->buffer[0],
                   &heap->buffer[heap->size],
                   Compare_nodes_greater(nodes));
}