wvwwvwwv / scalable-concurrent-containers

High performance containers and utilities for concurrent and asynchronous programming
Apache License 2.0
319 stars 16 forks source link

Request for `split()` method on `HashMap` #107

Closed rsdy closed 1 year ago

rsdy commented 1 year ago

It is a very common use-case for us in bloop to use a parallel process to create a scc::HashMap, then split that hashmap based on various properties. Currently I do this:

        let mut keys = vec![];
        self.cache
            .scan_async(|k, v| {
                if v.fresh {
                    keys.push(k.clone())
                }
            })
            .await;

        let to_upsert = vec![];
        for k in keys {
            to_upsert.push(self.cache.remove_async(&k).await);
        }
...

It would be great if I had a native way to help do this.

wvwwvwwv commented 1 year ago

It looks like something like HashMap::consume_async<F: FnMut(K, V) -> Option<(K, V)>> or OccupiedEntry::remove_next() -> V should be implemented.

I'll implement something in a couple of weeks!

wvwwvwwv commented 1 year ago

Hi, implemented prune and prune_async.

So, the usage would be,

let mut to_upsert = vec![];
self.cache.prune_async(|_, v| {
    if v.fresh {
        to_upsert.push(v);
        None
    } else {
        Some(v)
    }
}).await;

=> v1.8.2 (to be published soon).