Suppose we have a very large BTreeMap<K, V> and in addition to the 'proper' usage, we want to choose entries randomly from time to time.
Is this possible? A naive solution would be to store another "structure" with say <i64, K> pairs. Then generate integers at random, get the key and use it to find V in the BTreeMap. However if the BTreeMap is very large, say in the GBs, then this additional structure is also large..
Maybe there is a way to do it, without much additional overhead?
Not the most performant solution, but if you need to do it for whatever reason (without having a side structure) you can just grab the length and then run iterate you hit it. It's not ideal, but it's probably the cheapest way to do it without knowing what K is. Logically I'd write a small function which takes the tree (mutably or not, depending on needs), then iterating until it hits, then returning. You can even do the random roll there, or return key + value by reference (again mutable or not).
I'm sure there's a better solution I'm not thinking of, but since it's a BTree, it limits your options.