@@ -16,22 +16,12 @@ type OrderedSet a = (TQueue a, Set a)
16
16
17
17
-- | Insert an element into the ordered set.
18
18
-- 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
25
20
insert :: Hashable a => a -> OrderedSet a -> STM ()
26
21
insert a (que, s) = do
27
22
(_, inserted) <- S. focus (Focus. testingIfInserts $ Focus. insert () ) a s
28
23
-- 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
35
25
36
26
newIO :: Hashable a => IO (OrderedSet a )
37
27
newIO = do
@@ -58,5 +48,5 @@ lookup a (_, s) = S.lookup a s
58
48
delete :: Hashable a => a -> OrderedSet a -> STM ()
59
49
delete a (_, s) = S. delete a s
60
50
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