Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider Adding Implementations for Sets of Points #131

Open
Chris3606 opened this issue Jul 20, 2023 · 0 comments
Open

Consider Adding Implementations for Sets of Points #131

Chris3606 opened this issue Jul 20, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@Chris3606
Copy link
Contributor

It would make sense for the primitives library to offer some sort of SparseSet implementation that works, at minimum, with Point, in some capacity.

Consider the use case of storing a subset of Point objects which reside on a grid view (a fairly common when dealing with grid and/or grid algorithms).

There are two options, currently, that don't involve creating custom data structures. The first, is to use an IGridView<bool> (probably BitArrayView, in most cases). This is quite memory efficient (around 1 bit per location on a grid), and provides very quick Contains operations. What it does not provide, is O(n) iteration over elements in the set; you instead have to iterate over all elements in the grid view; an operation proportional to the size of the grid, rather than the number of elements in the set.

In the case where you need iteration, you can instead use a HashSet<Point>. This provides O(1) contains, and O(n) iteration; but is not particularly CPU cache efficient, and involves allocating during Add and Remove operations; therefore this does not fit all use cases either.

In cases like this, where a grid size is known, and doing allocation up front is preferable to allocation during Add/Remove operations, a sparse set structure (good example here) can be useful. It provides O(1) contains, O(1) add and remove, and O(n) iteration with good cache locality, in exchange for a fixed max size (not a problem for grid views) and performing allocation up front (a trade-off that grid views make anyway).

Another option is something like BitArrayView paired with List<int>. This would provide some memory reduction over the set above, and still provide O(1) contains, O(1) add, and O(n) iteration, in exchange for O(n)` remove. This in particular may fit use cases that clear the set entirely, rather than removing items.

@Chris3606 Chris3606 added the enhancement New feature or request label Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant