0 | module Libraries.Data.NatSet
15 | elem : Nat -> NatSet -> Bool
19 | drop : NatSet -> List a -> List a
21 | drop ds xs = go 0 xs
23 | go : Nat -> List a -> List a
28 | else x :: go (S i) xs
31 | take : NatSet -> List a -> List a
32 | take = drop . complement
35 | isEmpty : NatSet -> Bool
40 | size : NatSet -> Nat
43 | go : Nat -> NatSet -> Nat
47 | let acc = acc + popCount (the Int64 (cast n)) in
48 | go acc (assert_smaller n (shiftR n 64))
51 | Cast NatSet Integer where
55 | Cast Integer NatSet where
59 | insert : Nat -> NatSet -> NatSet
60 | insert = flip setBit
63 | delete : Nat -> NatSet -> NatSet
64 | delete = flip clearBit
67 | toList : NatSet -> List Nat
70 | go : Nat -> NatSet -> List Nat
73 | let is = go (S i) (assert_smaller ns (shiftR ns 1)) in
74 | if 0 `elem` ns then i :: is else is
76 | fromList : List Nat -> NatSet
77 | fromList = foldr insert empty
81 | show ns = show (toList ns)
84 | partition : NatSet -> List a -> (List a , List a)
87 | go : Nat -> List a -> (List a , List a)
90 | = let xys = go (S i) xs in
92 | then mapFst (x ::) xys
93 | else mapSnd (x ::) xys
96 | intersection : NatSet -> NatSet -> NatSet
97 | intersection = (.&.)
100 | union : NatSet -> NatSet -> NatSet
104 | intersectAll : List NatSet -> NatSet
105 | intersectAll [] = empty
106 | intersectAll (x::xs) = foldr intersection x xs
109 | allLessThan : Nat -> NatSet
110 | allLessThan n = shiftL 1 n - 1
112 | 0 allLessThanSpecEmpty : toList (allLessThan 0) === []
113 | allLessThanSpecEmpty = Refl
115 | 0 allLessThanSpecNonEmpty : toList (allLessThan 10) === [0..9]
116 | allLessThanSpecNonEmpty = Refl
119 | overwrite : a -> NatSet -> List a -> List a
120 | overwrite c 0 xs = xs
121 | overwrite c ds xs = go 0 xs
123 | go : Nat -> List a -> List a
127 | then c :: go (S i) xs
128 | else x :: go (S i) xs
136 | popZ : NatSet -> NatSet
137 | popZ = (`shiftR` 1)
140 | popNs : Nat -> NatSet -> NatSet
141 | popNs = flip shiftR
146 | addZ : NatSet -> NatSet
147 | addZ = (`shiftL` 1)