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

added minimal complete defenition, version changed, cosmetical change of toList #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hashtables.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Cabal-Version: 2.2
Name: hashtables
Version: 1.3.1
Version: 1.3.2
Synopsis: Mutable hash tables in the ST monad
Homepage: http://github.com/gregorycollins/hashtables
License: BSD-3-Clause
Expand Down
8 changes: 5 additions & 3 deletions src/Data/HashTable/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,17 @@ class HashTable h where
--
-- /O(n)/ worst case, /O(1)/ amortized.
insert :: (Eq k, Hashable k) => h s k v -> k -> v -> ST s ()
insert tbl k v = mutate tbl k (const (Just v, ()))

-- | Deletes a key-value mapping from a hash table. /O(n)/ worst case,
-- /O(1)/ amortized.
delete :: (Eq k, Hashable k) => h s k v -> k -> ST s ()
delete tbl k = mutate tbl k (const (Nothing, ()))

-- | Looks up a key-value mapping in a hash table. /O(n)/ worst case,
-- (/O(1)/ for cuckoo hash), /O(1)/ amortized.
lookup :: (Eq k, Hashable k) => h s k v -> k -> ST s (Maybe v)
lookup tbl k = mutate tbl k (\v -> (v, v))

-- | A strict fold over the key-value records of a hash table in the 'ST'
-- monad. /O(n)/.
Expand All @@ -117,6 +120,7 @@ class HashTable h where
-- debugging, etc; time complexity depends on the underlying hash table
-- implementation. /O(n)/.
computeOverhead :: h s k v -> ST s Double
{-# MINIMAL new, newSized, foldM, mapM_, lookupIndex, nextByIndex, computeOverhead, mutateST, (mutate), (insert), (delete), (lookup) #-}


------------------------------------------------------------------------------
Expand Down Expand Up @@ -159,9 +163,7 @@ fromListWithSizeHint n l = do
------------------------------------------------------------------------------
-- | Given a hash table, produce a list of key-value pairs. /O(n)/.
toList :: (HashTable h) => h s k v -> ST s [(k,v)]
toList ht = do
l <- foldM f [] ht
return l
toList = foldM f []

where
f !l !t = return (t:l)
Expand Down