16 #define BEAM_CONSTANT_SIZE 0 94 bool (*is_equivalent)(T *, T *);
97 beam(
size_t beam_size,
float prune_coeff = FLT_MAX,
bool (*test_equiv)(T *, T *) =
nullptr,
bool kbest =
false)
98 : beam_size(beam_size), pruning_coefficient(prune_coeff), do_kbest(kbest), is_equivalent(test_equiv)
101 worst_cost = -FLT_MAX;
103 prune_if_gt = FLT_MAX;
104 best_cost_data =
nullptr;
105 A = v_init<beam_element<T>>();
107 A.
resize(beam_size,
true);
109 A.
resize((beam_size + 1) * 4,
true);
114 inline bool might_insert(
float cost) {
return (cost <= prune_if_gt) && ((count < beam_size) || (cost < worst_cost)); }
118 if (!might_insert(cost))
138 size_t worst_idx = 0;
139 float worst_idx_cost = A[0].cost;
140 for (
size_t i = 1; i < beam_size; i++)
141 if (A[i].cost > worst_idx_cost)
144 worst_idx_cost = A[i].cost;
145 if (worst_idx_cost <= worst_cost)
148 if (cost >= worst_idx_cost)
151 A[worst_idx].hash =
hash;
152 A[worst_idx].cost =
cost;
153 A[worst_idx].data =
data;
154 A[worst_idx].active =
true;
173 if (cost < best_cost)
176 best_cost_data =
data;
178 if (cost > worst_cost)
181 prune_if_gt = std::max(1.
f, best_cost) * pruning_coefficient;
191 while ((ret != A.
end) && (!ret->
active)) ++ret;
192 return (ret == A.
end) ? nullptr : ret;
201 float next_best_cost = FLT_MAX;
203 if ((ret ==
nullptr) && el->active && (el->cost <= best_cost))
205 else if (el->active && (el->cost < next_best_cost))
207 next_best_cost = el->
cost;
208 best_cost_data = el->data;
213 best_cost = next_best_cost;
214 prune_if_gt = std::max(1.
f, best_cost) * pruning_coefficient;
221 prune_if_gt = FLT_MAX;
222 best_cost_data =
nullptr;
232 while (start < A.
size() - 1)
234 size_t end = start + 1;
235 for (; (end < A.
size()) && (A[start].
hash == A[end].
hash); end++)
237 assert(start < A.
size());
238 assert(end <= A.
size());
241 for (
size_t i = start; i < end; i++)
245 assert(i < A.
size());
246 for (
size_t j = i + 1; j < end; j++)
250 assert(j < A.
size());
252 if (is_equivalent(A[i].
data, A[j].data))
265 void compact(
void (*free_data)(T *) =
nullptr)
271 if (count <= beam_size)
276 while ((count > 1) && !A[count - 1].
active) count--;
283 best_cost = A[0].cost;
284 worst_cost = A[count - 1].cost;
285 prune_if_gt = std::max(1.
f, best_cost) * pruning_coefficient;
286 best_cost_data = A[0].data;
291 if (count >= beam_size * 10)
295 void erase(
void (*free_data)(T *) =
nullptr)
301 worst_cost = -FLT_MAX;
303 prune_if_gt = FLT_MAX;
304 best_cost_data =
nullptr;
309 assert(A.
size() == 0);
315 size_t size() {
return count; }
void compact(void(*free_data)(T *)=nullptr)
void resize(size_t length)
void maybe_compact(void(*free_data)(T *)=nullptr)
int compare_on_cost(const void *void_a, const void *void_b)
#define BEAM_CONSTANT_SIZE
v_array< beam_element< T > > A
float pruning_coefficient
beam_element< T > * begin()
beam(size_t beam_size, float prune_coeff=FLT_MAX, bool(*test_equiv)(T *, T *)=nullptr, bool kbest=false)
beam_element< T > * pop_best_item()
bool might_insert(float cost)
void erase(void(*free_data)(T *)=nullptr)
int compare_on_hash_then_cost(const void *void_a, const void *void_b)
void push_back(const T &new_ele)
beam_element< T > * end()
bool insert(T *data, float cost, uint32_t hash)
beam_element< T > * get_best_item()