ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
heap.h
Go to the documentation of this file.
1//===--- heap.h ----------------------------------------------------------===
2//
3// satoko: Satisfiability solver
4//
5// This file is distributed under the BSD 2-Clause License.
6// See LICENSE for details.
7//
8//===------------------------------------------------------------------------===
9#ifndef satoko__utils__heap_h
10#define satoko__utils__heap_h
11
12#include "mem.h"
13#include "../types.h"
14#include "vec/vec_sdbl.h"
15#include "vec/vec_int.h"
16#include "vec/vec_uint.h"
17
20
21typedef struct heap_t_ heap_t;
27//===------------------------------------------------------------------------===
28// Heap internal functions
29//===------------------------------------------------------------------------===
30static inline unsigned left(unsigned i) { return 2 * i + 1; }
31static inline unsigned right(unsigned i) { return (i + 1) * 2; }
32static inline unsigned parent(unsigned i) { return (i - 1) >> 1; }
33
34static inline int compare(heap_t *p, unsigned x, unsigned y)
35{
36 return vec_act_at(p->weights, x) > vec_act_at(p->weights, y);
37}
38
39static inline void heap_percolate_up(heap_t *h, unsigned i)
40{
41 unsigned x = vec_uint_at(h->data, i);
42 unsigned p = parent(i);
43
44 while (i != 0 && compare(h, x, vec_uint_at(h->data, p))) {
45 vec_uint_assign(h->data, i, vec_uint_at(h->data, p));
46 vec_int_assign(h->indices, vec_uint_at(h->data, p), (int) i);
47 i = p;
48 p = parent(p);
49 }
50 vec_uint_assign(h->data, i, x);
51 vec_int_assign(h->indices, x, (int) i);
52}
53
54static inline void heap_percolate_down(heap_t *h, unsigned i)
55{
56 unsigned x = vec_uint_at(h->data, i);
57
58 while (left(i) < vec_uint_size(h->data)) {
59 unsigned child = right(i) < vec_uint_size(h->data) &&
60 compare(h, vec_uint_at(h->data, right(i)), vec_uint_at(h->data, left(i)))
61 ? right(i)
62 : left(i);
63
64 if (!compare(h, vec_uint_at(h->data, child), x))
65 break;
66
67 vec_uint_assign(h->data, i, vec_uint_at(h->data, child));
68 vec_int_assign(h->indices, vec_uint_at(h->data, i), (int) i);
69 i = child;
70 }
71 vec_uint_assign(h->data, i, x);
72 vec_int_assign(h->indices, x, (int) i);
73}
74
75//===------------------------------------------------------------------------===
76// Heap API
77//===------------------------------------------------------------------------===
78static inline heap_t *heap_alloc(vec_act_t *weights)
79{
81 p->weights = weights;
82 p->indices = vec_int_alloc(0);
83 p->data = vec_uint_alloc(0);
84 return p;
85}
86
87static inline void heap_free(heap_t *p)
88{
89 vec_int_free(p->indices);
90 vec_uint_free(p->data);
92}
93
94static inline unsigned heap_size(heap_t *p)
95{
96 return vec_uint_size(p->data);
97}
98
99static inline int heap_in_heap(heap_t *p, unsigned entry)
100{
101 return (entry < vec_int_size(p->indices)) &&
102 (vec_int_at(p->indices, entry) >= 0);
103}
104
105static inline void heap_increase(heap_t *p, unsigned entry)
106{
107 assert(heap_in_heap(p, entry));
108 heap_percolate_down(p, (unsigned) vec_int_at(p->indices, entry));
109}
110
111static inline void heap_decrease(heap_t *p, unsigned entry)
112{
113 assert(heap_in_heap(p, entry));
114 heap_percolate_up(p, (unsigned) vec_int_at(p->indices, entry));
115}
116
117static inline void heap_insert(heap_t *p, unsigned entry)
118{
119 if (vec_int_size(p->indices) < entry + 1) {
120 unsigned old_size = vec_int_size(p->indices);
121 unsigned i;
122 int e;
123 vec_int_resize(p->indices, entry + 1);
124 vec_int_foreach_start(p->indices, e, i, old_size)
125 vec_int_assign(p->indices, i, -1);
126 }
127 assert(!heap_in_heap(p, entry));
128 vec_int_assign(p->indices, entry, (int) vec_uint_size(p->data));
129 vec_uint_push_back(p->data, entry);
130 heap_percolate_up(p, (unsigned) vec_int_at(p->indices, entry));
131}
132
133static inline void heap_update(heap_t *p, unsigned i)
134{
135 if (!heap_in_heap(p, i))
136 heap_insert(p, i);
137 else {
138 heap_percolate_up(p, (unsigned) vec_int_at(p->indices, i));
139 heap_percolate_down(p, (unsigned) vec_int_at(p->indices, i));
140 }
141}
142
143static inline void heap_build(heap_t *p, vec_uint_t *entries)
144{
145 int i;
146 unsigned j, entry;
147
148 vec_uint_foreach(p->data, entry, j)
149 vec_int_assign(p->indices, entry, -1);
150 vec_uint_clear(p->data);
151 vec_uint_foreach(entries, entry, j) {
152 vec_int_assign(p->indices, entry, (int) j);
153 vec_uint_push_back(p->data, entry);
154 }
155 for ((i = vec_uint_size(p->data) / 2 - 1); i >= 0; i--)
156 heap_percolate_down(p, (unsigned) i);
157}
158
159static inline void heap_clear(heap_t *p)
160{
161 vec_int_clear(p->indices);
162 vec_uint_clear(p->data);
163}
164
165static inline unsigned heap_remove_min(heap_t *p)
166{
167 unsigned x = vec_uint_at(p->data, 0);
168 vec_uint_assign(p->data, 0, vec_uint_at(p->data, vec_uint_size(p->data) - 1));
169 vec_int_assign(p->indices, vec_uint_at(p->data, 0), 0);
170 vec_int_assign(p->indices, x, -1);
171 vec_uint_pop_back(p->data);
172 if (vec_uint_size(p->data) > 1)
173 heap_percolate_down(p, 0);
174 return x;
175}
176
178#endif /* satoko__utils__heap_h */
#define ABC_NAMESPACE_HEADER_END
#define ABC_NAMESPACE_HEADER_START
NAMESPACES ///.
Cube * p
Definition exorList.c:222
#define satoko_free(p)
Definition mem.h:20
#define satoko_alloc(type, n_elements)
Definition mem.h:17
typedefABC_NAMESPACE_HEADER_START struct heap_t_ heap_t
Definition heap.h:21
Definition heap.h:22
vec_int_t * indices
Definition heap.h:23
vec_act_t * weights
Definition heap.h:25
vec_uint_t * data
Definition heap.h:24
#define vec_act_at(vec, idx)
Definition types.h:31
vec_sdbl_t vec_act_t
Definition types.h:24
#define assert(ex)
Definition util_old.h:213
typedefABC_NAMESPACE_HEADER_START struct vec_int_t_ vec_int_t
Definition vec_int.h:21
#define vec_int_foreach_start(vec, entry, i, start)
Definition vec_int.h:34
#define vec_uint_foreach(vec, entry, i)
Definition vec_uint.h:31
typedefABC_NAMESPACE_HEADER_START struct vec_uint_t_ vec_uint_t
Definition vec_uint.h:21