ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
inlineheap.h
Go to the documentation of this file.
1#ifndef _inlineheap_h_INCLUDED
2#define _inlineheap_h_INCLUDED
3
4#include "allocate.h"
5#include "internal.h"
6#include "logging.h"
7
8#include "global.h"
10
11#define HEAP_CHILD(POS) (KISSAT_assert ((POS) < (1u << 31)), (2 * (POS) + 1))
12
13#define HEAP_PARENT(POS) (KISSAT_assert ((POS) > 0), (((POS) - 1) / 2))
14
15static inline void kissat_bubble_up (kissat *solver, heap *heap,
16 unsigned idx) {
17 unsigned *stack = BEGIN_STACK (heap->stack);
18 unsigned *pos = heap->pos;
19 unsigned idx_pos = pos[idx];
20 const double *const score = heap->score;
21 const double idx_score = score[idx];
22 while (idx_pos) {
23 const unsigned parent_pos = HEAP_PARENT (idx_pos);
24 const unsigned parent = stack[parent_pos];
25 if (score[parent] >= idx_score)
26 break;
27 LOG ("heap bubble up: %u@%u = %g swapped with %u@%u = %g", parent,
28 parent_pos, score[parent], idx, idx_pos, idx_score);
29 stack[idx_pos] = parent;
30 pos[parent] = idx_pos;
31 idx_pos = parent_pos;
32 }
33 stack[idx_pos] = idx;
34 pos[idx] = idx_pos;
35#ifndef LOGGING
36 (void) solver;
37#endif
38}
39
40static inline void kissat_bubble_down (kissat *solver, heap *heap,
41 unsigned idx) {
42 unsigned *stack = BEGIN_STACK (heap->stack);
43 const unsigned end = SIZE_STACK (heap->stack);
44 unsigned *pos = heap->pos;
45 unsigned idx_pos = pos[idx];
46 const double *const score = heap->score;
47 const double idx_score = score[idx];
48 for (;;) {
49 unsigned child_pos = HEAP_CHILD (idx_pos);
50 if (child_pos >= end)
51 break;
52 unsigned child = stack[child_pos];
53 double child_score = score[child];
54 const unsigned sibling_pos = child_pos + 1;
55 if (sibling_pos < end) {
56 const unsigned sibling = stack[sibling_pos];
57 const double sibling_score = score[sibling];
58 if (sibling_score > child_score) {
59 child = sibling;
60 child_pos = sibling_pos;
61 child_score = sibling_score;
62 }
63 }
64 if (child_score <= idx_score)
65 break;
66 LOG ("heap bubble down: %u@%u = %g swapped with %u@%u = %g", child,
67 child_pos, score[child], idx, idx_pos, idx_score);
68 stack[idx_pos] = child;
69 pos[child] = idx_pos;
70 idx_pos = child_pos;
71 }
72 stack[idx_pos] = idx;
73 pos[idx] = idx_pos;
74#ifndef LOGGING
75 (void) solver;
76#endif
77}
78
79#define HEAP_IMPORT(IDX) \
80 do { \
81 KISSAT_assert ((IDX) < UINT_MAX - 1); \
82 if (heap->vars <= (IDX)) \
83 kissat_enlarge_heap (solver, heap, (IDX) + 1); \
84 } while (0)
85
86#define CHECK_HEAP_IMPORTED(IDX)
87
88static inline void kissat_push_heap (kissat *solver, heap *heap,
89 unsigned idx) {
90 LOG ("push heap %u", idx);
91 KISSAT_assert (!kissat_heap_contains (heap, idx));
92 HEAP_IMPORT (idx);
93 heap->pos[idx] = SIZE_STACK (heap->stack);
94 PUSH_STACK (heap->stack, idx);
95 kissat_bubble_up (solver, heap, idx);
96}
97
98static inline void kissat_pop_heap (kissat *solver, heap *heap,
99 unsigned idx) {
100 LOG ("pop heap %u", idx);
101 KISSAT_assert (kissat_heap_contains (heap, idx));
102 const unsigned last = POP_STACK (heap->stack);
103 heap->pos[last] = DISCONTAIN;
104 if (last == idx)
105 return;
106 const unsigned idx_pos = heap->pos[idx];
107 heap->pos[idx] = DISCONTAIN;
108 POKE_STACK (heap->stack, idx_pos, last);
109 heap->pos[last] = idx_pos;
110 kissat_bubble_up (solver, heap, last);
111 kissat_bubble_down (solver, heap, last);
112#ifdef CHECK_HEAP
114#endif
115}
116
117static inline unsigned kissat_pop_max_heap (kissat *solver, heap *heap) {
119 unsigneds *stack = &heap->stack;
120 unsigned *const begin = BEGIN_STACK (*stack);
121 const unsigned idx = *begin;
122 KISSAT_assert (!heap->pos[idx]);
123 LOG ("pop max heap %u", idx);
124 const unsigned last = POP_STACK (*stack);
125 unsigned *const pos = heap->pos;
126 pos[last] = DISCONTAIN;
127 if (last == idx)
128 return idx;
129 pos[idx] = DISCONTAIN;
130 *begin = last;
131 pos[last] = 0;
132 kissat_bubble_down (solver, heap, last);
133#ifdef CHECK_HEAP
135#endif
136 return idx;
137}
138
139static inline void kissat_adjust_heap (kissat *solver, heap *heap,
140 unsigned idx) {
141 const unsigned new_vars = idx + 1;
142 const unsigned old_vars = heap->vars;
143 if (new_vars <= old_vars)
144 return;
145 const unsigned old_size = heap->size;
146 if (idx >= old_size) {
147 size_t new_size = old_size ? 2 * old_size : 1;
148 while (idx >= new_size)
149 new_size *= 2;
150 KISSAT_assert (new_size < DISCONTAIN);
151 kissat_resize_heap (solver, heap, new_size);
152 }
153 kissat_enlarge_heap (solver, heap, idx + 1);
154}
155
156static inline void kissat_update_heap (kissat *solver, heap *heap,
157 unsigned idx, double new_score) {
158 const double old_score = kissat_get_heap_score (heap, idx);
159 if (old_score == new_score)
160 return;
161 HEAP_IMPORT (idx);
162 LOG ("update heap %u score from %g to %g", idx, old_score, new_score);
163 heap->score[idx] = new_score;
164 if (!heap->tainted) {
165 heap->tainted = true;
166 LOG ("tainted heap");
167 }
168 if (!kissat_heap_contains (heap, idx))
169 return;
170 if (new_score > old_score)
171 kissat_bubble_up (solver, heap, idx);
172 else
173 kissat_bubble_down (solver, heap, idx);
174#ifdef CHECK_HEAP
176#endif
177}
178
180
181#endif
#define ABC_NAMESPACE_HEADER_END
#define ABC_NAMESPACE_HEADER_START
NAMESPACES ///.
#define BEGIN_STACK(S)
Definition stack.h:46
#define POP_STACK(S)
Definition stack.h:37
#define POKE_STACK(S, N, E)
Definition stack.h:32
#define SIZE_STACK(S)
Definition stack.h:19
#define EMPTY_STACK(S)
Definition stack.h:18
#define PUSH_STACK(S, E)
Definition stack.h:39
#define LOG(...)
bool pos
Definition globals.c:30
void kissat_enlarge_heap(kissat *solver, heap *heap, unsigned new_vars)
Definition heap.c:82
void kissat_resize_heap(kissat *solver, heap *heap, unsigned new_size)
Definition heap.c:46
#define HEAP_IMPORT(IDX)
Definition inlineheap.h:79
#define HEAP_PARENT(POS)
Definition inlineheap.h:13
#define HEAP_CHILD(POS)
Definition inlineheap.h:11
#define KISSAT_assert(ignore)
Definition global.h:13
#define DISCONTAIN
Definition heap.h:14
#define kissat_check_heap(...)
Definition heap.h:83
#define solver
Definition kitten.c:211
Definition heap.h:19
unsigneds stack
Definition heap.h:23
unsigned * pos
Definition heap.h:25
double * score
Definition heap.h:24
unsigned vars
Definition heap.h:21
unsigned size
Definition heap.h:22
bool tainted
Definition heap.h:20