Discussion
Loading...

Post

Log in
  • About
  • Code of conduct
  • Privacy
  • Users
  • Instances
  • About Bonfire
Federation Bot
Federation Bot
@Federation_Bot  ·  activity timestamp 3 months ago

@aparrish I'm pretty sure sampling at random until you get a hit is the optimal algorithm for this. Bad worst-case performance that you will almost certainly never see, constant time average-case performance.

You'll probably want to add checks for low fill ratios if those can happen, like when you're first populating the table, but you could also implement a linear congruential PRNG to use as a secondary PRNG since it's guaranteed to hit every slot without repeats. Or if you only have low fill ratios at small array sizes, like with a growable hash table, and if you know the number of stored items, you can choose the nth item at random and scan the array linearly, counting until you hit it. Slow but potentially faster than calling the PRNG over and over at low fill ratios.

  • Copy link
  • Flag this post
  • Block
Federation Bot
Federation Bot
@Federation_Bot replied  ·  activity timestamp 3 months ago

@aparrish You mention not wanting to use an auxiliary datastructure, but if you have POPCNT or equivalent available, finding the nth non-empty element using an auxiliary bitmap would be extremely fast and cache friendly. If you don't have a way to count bits quickly, you could also divide the array into blocks and keep a count of the number of nonempty cells in each block. Very low overhead either way.

  • Copy link
  • Flag this comment
  • Block

bonfire.cafe

A space for Bonfire maintainers and contributors to communicate

bonfire.cafe: About · Code of conduct · Privacy · Users · Instances
Bonfire social · 1.0.1 no JS en
Automatic federation enabled
Log in
  • Explore
  • About
  • Members
  • Code of Conduct