roaring-rs icon indicating copy to clipboard operation
roaring-rs copied to clipboard

Get element by index

Open irevoire opened this issue 2 years ago • 2 comments

It's not the first time that I need a function to get element(s) by index in a roaring bitmap. The main functions that I could have used are;

  • nth(n: u32) -> u32 => returns the element at the n position
  • first_n(n: u32) -> RoaringBitmap => returns the first n elements
  • last_n(n: u32) -> RoaringBitmap => returns the last n elements

irevoire avatar Apr 24 '23 15:04 irevoire

Hey 👋,

The nth method makes me think of the select one. Is it what you are looking for?

However, there could be a better way than implementing the first_n and last_n methods than using the Iterator trait. As I don't think we can simply implement some methods on the Iterator trait, maybe implementing those on the RoaringBitmap type could be enough 🤷‍♂️

Kerollmops avatar Apr 24 '23 16:04 Kerollmops

The nth method makes me think of the select one. Is it what you are looking for?

Oops, yes, definitely, thanks!

However, there could be a better way than implementing the first_n and last_n methods than using the Iterator trait

Yeah, if n is large, we could easily skip through whole containers without opening them and without any resizing happening while allocating all the Vecs.

irevoire avatar Apr 24 '23 16:04 irevoire