Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: modifyAll (internal but exposed helper) #23

Open
patrickmn opened this issue Dec 24, 2014 · 1 comment
Open

Feature request: modifyAll (internal but exposed helper) #23

patrickmn opened this issue Dec 24, 2014 · 1 comment

Comments

@patrickmn
Copy link

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
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant