| 1 |
{-# LANGUAGE RecordWildCards #-} |
| 2 |
module Network.Wai.Middleware.Push.Referer.Multi where |
| 3 |
|
| 4 |
import Data.Set (Set) |
| 5 |
import qualified Data.Set as Set |
| 6 |
|
| 7 |
data Multi a = Multi { |
| 8 |
limit :: Int |
| 9 |
, list :: [a] |
| 10 |
, check :: Set a |
| 11 |
} deriving (Eq, Show) |
| 12 |
|
| 13 |
empty :: Int -> Multi a |
| 14 |
empty n = Multi n [] Set.empty |
| 15 |
|
| 16 |
singleton :: Int -> a -> Multi a |
| 17 |
singleton n v = Multi n [v] $ Set.singleton v |
| 18 |
|
| 19 |
insert :: Ord a => a -> Multi a -> Multi a |
| 20 |
insert _ m@Multi{..} |
| 21 |
| Set.size check == limit = m |
| 22 |
insert v m@Multi{..} |
| 23 |
| Set.size check == Set.size check' = m |
| 24 |
| otherwise = Multi limit (v:list) check' |
| 25 |
where |
| 26 |
check' = Set.insert v check |