diff options
author | Simon Glass <sjg@chromium.org> | 2024-10-19 09:21:47 -0600 |
---|---|---|
committer | Simon Glass <sjg@chromium.org> | 2024-11-02 11:13:59 -0600 |
commit | d01c58acb71874721c05684472412ce169bb7df8 (patch) | |
tree | 792545e85e3308e171ed70646e1d59a349e9e641 /lib | |
parent | 70f5f17415a257ce8d5f32df5b40194b89a27960 (diff) |
alist: Add a way to efficiently filter an alist
Unlike linked lists, it is inefficient to remove items from an alist,
particularly if it is large. If most items need to be removed, then the
time-complexity approaches O(n2).
Provide a way to do this efficiently, by working through the alist once
and copying elements down.
Signed-off-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/alist.c | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/lib/alist.c b/lib/alist.c index 32cd45b0b01..4ce651f5c45 100644 --- a/lib/alist.c +++ b/lib/alist.c @@ -123,6 +123,14 @@ int alist_calc_index(const struct alist *lst, const void *ptr) return index; } +void alist_update_end(struct alist *lst, const void *ptr) +{ + int index; + + index = alist_calc_index(lst, ptr); + lst->count = index == -1 ? 0 : index; +} + bool alist_chk_ptr(const struct alist *lst, const void *ptr) { int index = alist_calc_index(lst, ptr); |