Skip to content

Commit 68d783e

Browse files
committed
askrene: refine: add helper function remove_flows
remove_flows is a helper function to remove flows from a set so that we keep the number of flows limited. Signed-off-by: Lagrang3 <[email protected]>
1 parent afffb53 commit 68d783e

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

plugins/askrene/refine.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,3 +632,31 @@ double flows_probability(const tal_t *ctx, struct route_query *rq,
632632
tal_free(working_ctx);
633633
return probability;
634634
}
635+
636+
bool remove_flows(const tal_t *ctx, struct flow ***flows, u32 n)
637+
{
638+
const tal_t *working_ctx = tal(ctx, tal_t);
639+
size_t *flows_index;
640+
641+
if (n == 0)
642+
goto fail;
643+
if (n > tal_count(*flows))
644+
goto fail;
645+
646+
flows_index = tal_arrz(working_ctx, size_t, tal_count(*flows));
647+
for (size_t i = 0; i < tal_count(*flows); i++) {
648+
flows_index[i] = i;
649+
}
650+
asort(flows_index, tal_count(flows_index), revcmp_flows, *flows);
651+
for (size_t count = tal_count(flows_index); n > 0; n--, count--) {
652+
assert(count > 0);
653+
tal_arr_remove(&flows_index, count - 1);
654+
}
655+
write_selected_flows(working_ctx, flows_index, flows);
656+
657+
tal_free(working_ctx);
658+
return true;
659+
fail:
660+
tal_free(working_ctx);
661+
return false;
662+
}

plugins/askrene/refine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,8 @@ void squash_flows(const tal_t *ctx, struct route_query *rq,
3333

3434
double flows_probability(const tal_t *ctx, struct route_query *rq,
3535
struct flow ***flows);
36+
37+
/* Helper function: removes n flows from the set. It will remove those flows
38+
* with the lowest amount values. */
39+
bool remove_flows(const tal_t *ctx, struct flow ***flows, u32 n);
3640
#endif /* LIGHTNING_PLUGINS_ASKRENE_REFINE_H */

0 commit comments

Comments
 (0)