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

Keep track of number of elements for efficient len() #32

Closed
jonhoo opened this issue Apr 12, 2017 · 1 comment
Closed

Keep track of number of elements for efficient len() #32

jonhoo opened this issue Apr 12, 2017 · 1 comment

Comments

@jonhoo
Copy link
Contributor

jonhoo commented Apr 12, 2017

Currently, VecMap::len() takes O(highest index) time to compute every time (same goes for VecMap::is_empty(). It'd be nice if VecMap instead kept track of the number of Some it has, and just use this value in len() and is_empty(). It would add a usize to the VecMap struct, but this seems negligible compared to the performance gains of doing so. I'll submit a PR shortly which you can merge if you feel like this would be a useful addition :)

@jonhoo
Copy link
Contributor Author

jonhoo commented Apr 12, 2017

Looking at this, it seems like this could be used to speed up other operations too. For example, VecMap::iter() can now cheaply return an ExactSizeIterator.

jonhoo added a commit to jonhoo/vec-map that referenced this issue Apr 12, 2017
This allows much more efficient versions of len() and is_empty(), and
also enables ExactSizeIterator to be implemented to VecMap's iterators.

Fixes contain-rs#32
jonhoo added a commit to jonhoo/vec-map that referenced this issue Apr 12, 2017
This allows much more efficient versions of len() and is_empty(), and
also enables ExactSizeIterator to be implemented for VecMap's iterators.

Fixes contain-rs#32
jonhoo added a commit to jonhoo/vec-map that referenced this issue Apr 13, 2017
This allows much more efficient versions of len() and is_empty(), and
also enables ExactSizeIterator to be implemented for VecMap's iterators.

Fixes contain-rs#32
@Gankra Gankra closed this as completed in #33 May 7, 2017
camsteffen pushed a commit to camsteffen/vec-map that referenced this issue May 14, 2017
This allows much more efficient versions of len() and is_empty(), and
also enables ExactSizeIterator to be implemented for VecMap's iterators.

Fixes contain-rs#32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant