Open Bug 1486967 Opened Last year

Add nsTArray::ApplyAndRemoveIf

Categories

(Core :: XPCOM, enhancement)

63 Branch
enhancement
Not set

Tracking

()

Tracking Status
firefox63 --- affected

People

(Reporter: gerald, Unassigned)

References

Details

Currently, removing an element requires knowing its index, and the programmer needs to be careful with subsequent array accesses.

A common pattern is to find an element, optionally do one last thing with it, and then remove it from the array. This could be captured in one call, similar to ApplyIf but with an extra removal step at the end.

Some advantages:
- Better declaration of intent, as `ApplyAndRemoveIf` clearly shows that we're going to work on an element and then remove it.
- The final handling of the element is done in place, is guaranteed to happen before its removal, and the caller doesn't need to know about its index; this removes the footgun of keeping a dangling index/reference (almost: it could still be done, but it's harder to do, so less likely to happen).
- Some optimizations may be possible, as we know the element's address and we could hopefully avoid some unneeded ptr->index->ptr conversions.


Leaving this bug unassigned for now, as I can't promise I'll work on it soon; please take it if you want&can!
(I've got an ugly prototype working, if anyone's interested.)
You need to log in before you can comment on or make changes to this bug.