Well, you could just copy out all the IDs of the expired sessions and then delete them but that might be a bit slow (
n*O(log(n))). You could do the same thing with a hash-map in
O(n) time but then you pay the price for hashing twice.
Also, this doesn’t answer your specific question, but I’ve written a library for exactly this use-case called stash. Basically, it’s an very fast
O(1) insert/delete/lookup table for cases where you don’t care about the keys (it will pick them for you). In this case, I assume that you never want to reuse session IDs so you’d need to use UniqueStash. Unfortunately,
- You’re keys will be larger (128bits on a 64bit machine).
- Iteration is
O(capacity) even if you’ve deleted lots of items. This is fine as long as you don’t have usage spikes. I can probably optimize this but I don’t really have the time ATM.
- It doesn’t yet have any form of retain method (what you’re looking for). However, you can implement this as described above. (I can also add it when I have some free time).
If you find it useful, I’ll put it on crates.io (I just don’t want to needlessly pollute the crates.io namespace).