hashtables icon indicating copy to clipboard operation
hashtables copied to clipboard

Feature request: modifyAll (internal but exposed helper)

Open patrickmn opened this issue 10 years ago • 1 comments

I am implementing a cache using HashTable. It will occasionally scan the entire table looking for out-of-date values (based on a field of v) and expunge them. I would love to be able to implement this without doing multiple scans, hashing, lookups, and more.

Any chance you'd be willing to expose something like the below in an ".Internal.[Cuckoo|Basic|Linear]" or similar module, or accept a pull request for the same?

modifyAll :: ((k,v) -> Either Bool v)
          -> HashTable_ s k v
          -> ST s ()
modifyAll f (HashTable sz _ hashes keys values _) = go 0
  where
    totSz = numElemsInCacheLine * sz

    go !i | i >= totSz = return ()
          | otherwise  = do
        h <- U.readArray hashes i
        if h /= emptyMarker
          then do
            k <- readArray keys i
            v <- readArray values i
            case f (k,v) of
              Left False -> do
                U.writeArray hashes i emptyMarker
                writeArray keys i undefined
                writeArray values i undefined
              Left True  -> return ()
              Right nv   -> writeArray values i nv
            go (i+1)
          else
            go (i+1)

For my purposes I don't actually need k or the ability to write a new v, but this seems more generally applicable.

patrickmn avatar Dec 24 '14 02:12 patrickmn

I realized I also need to be able to "flush" the cache, e.g. by allocating new arrays and replacing the ht ref or zeroing the arrays. I could ref the hashtable itself and replace that ref, but I'd like to keep the arrays at whatever size they've grown to.

I know this stuff is pretty specific. Perhaps it'd be better to expose htref/constructors in an Internal module instead?

patrickmn avatar Dec 27 '14 02:12 patrickmn