Last active
March 30, 2020 08:41
-
-
Save viercc/2840a67ca409ebfdce203c23706c9eed to your computer and use it in GitHub Desktop.
On hashtables with "undo-able" insert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Just wrote down in the web editor. | |
| -- Not tested at all but I hope you get the idea | |
| import qualified Data.HashTable.ST.Basic as HT | |
| import Data.List.NonEmpty(NonEmpty(..)) | |
| import qualified Data.List.NonEmpty as NE | |
| type MyHashTable s k v = HT.HashTable s k (NonEmpty v) | |
| insert :: MyHashTable s k v -> k -> v -> ST s () | |
| insert hashTable k v = | |
| HT.mutate hashTable k $ \prev -> | |
| (Just (v :| maybe [] NE.toList prev), ()) | |
| update :: MyHashTable s k v -> k -> v -> ST s () | |
| update hashTable k v = HT.insert hashTable k (v :| []) | |
| pop :: MyHashTable s k v -> k -> ST s (Maybe v) | |
| pop hashTable k = | |
| HT.mutate hashTable k $ \prev -> | |
| case prev of | |
| Nothing -> (Nothing, Nothing) | |
| Just (v :| rest) -> (NE.nonEmpty rest, Just v) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment