@@ -16,22 +16,12 @@ type OrderedSet a = (TQueue a, Set a)
1616
1717-- | Insert an element into the ordered set.
1818-- If the element is not already present, it is added to both the queue and set.
19- -- If the element already exists, it is moved to the end of the queue to maintain
20- -- most-recently-inserted ordering semantics.
21- -- It take O(n), not very good.
22-
23- -- Alternative: preserve original position and ignore new one.
24- -- I am not sure which one is better.
19+ -- If the element already exists, ignore it
2520insert :: Hashable a => a -> OrderedSet a -> STM ()
2621insert a (que, s) = do
2722 (_, inserted) <- S. focus (Focus. testingIfInserts $ Focus. insert () ) a s
2823 -- if already in the set
29- -- update the position of the element in the queue
30- when (not inserted) $ do
31- items <- filter (== a) <$> flushTQueue que
32- mapM_ (writeTQueue que) items
33- return ()
34- writeTQueue que a
24+ when inserted $ writeTQueue que a
3525
3626newIO :: Hashable a => IO (OrderedSet a )
3727newIO = do
@@ -58,5 +48,5 @@ lookup a (_, s) = S.lookup a s
5848delete :: Hashable a => a -> OrderedSet a -> STM ()
5949delete a (_, s) = S. delete a s
6050
61- toHashSet :: Hashable a => OrderedSet a -> Data. HashSet a
62- toHashSet (_, s) = TreeSet. fromList $ LT. toList $ S. listT s
51+ toHashSet :: Hashable a => OrderedSet a -> STM ( Data.HashSet. HashSet a )
52+ toHashSet (_, s) = Data.HashSet. fromList <$> LT. toList ( S. listT s)
0 commit comments