SetBy.dcl 7.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
definition module Data.SetBy

/**
 * An efficient implementation of sets.
 *
 * This version is the same as Data.Set, except that the overloaded API is replaced
 * with a higher-order function API.
 *
 * For all documentation, please consult Data.Set.
 *
 * The `morally equivalent` function from Data.Set is added in the comment.
 *
 * When using the functions in Data.SetBy, make sure to use the same higher-order
 * function parameter for the same data structure to ensure internal integrity. 
 * This higher-order function represents the < ordering on your set elements and
 * should have the usual ordering properties:
 *
 * - if a < b and b < c then a < c
 * - if a < b then not (b < a)
 * - if not (a < b) and not (b < a) then a and b are considered to 'equal'
 *
 */

from StdOverloaded	import class ==, class < (..)
from StdClass import class Ord (..), <=, >
from StdList import foldl, map
from Data.Maybe	import :: Maybe
from StdBool import not, &&
from Data.GenLexOrd import :: LexOrd
import qualified Data.Foldable
from Data.Foldable import class Foldable

:: SetBy a = TipBy
           | BinBy !Int !a !(SetBy a) !(SetBy a)

instance == (SetBy a) | == a

/**
 * True iff first set is `smaller` than second set, according to 
 * first argument (assuming the two sets are ordered with the
 * same first function argument).
 *
 * Morally equivalent function: instance < (Set a) | < a
 */
isOrderedBy :: !(a a -> Bool) !(SetBy a) !(SetBy a) -> Bool

/**
 * EQ iff the two sets have the same number of elements, occurring in the 
 *    same order. 
 * LT iff the first set is the common prefix of the second set or the common
 *    prefix is followed in the first set with an element that is considered 
 *    than the corresponding element in the second set.
 * GT iff the second set is the common prefix of the first set or the common
 *    prefix is followed in the second set with an element that is considered
 *    greater than the corresponding element in the first set.
 * The comparison of elements is done with the first function argument. 
 *
 * Morally equivalent function: derive gLexOrd Set
 */
lexOrdBy :: !(a a -> Bool) !(SetBy a) !(SetBy a) -> LexOrd

instance Foldable SetBy

/**
 * True iff this is the empty set.
 * @type (SetBy a) -> Bool
 * @property equivalence with size 0: A.s :: SetBy a:
 *     size s == 0 <==> null s
 * @property equivalence with newSet: A.s :: SetBy a:
 *     s == newSetBy <==> null s
 */
null s :== case s of
             TipBy -> True
             (BinBy sz _ _ _) -> False

/**
 * The number of elements in the set.
 * @type (SetBy a) -> Int
 * @property correctness: A.s :: SetBy a:
 *     size s =.= length (toList s)
 */
size s :== case s of
             TipBy -> 0
             (BinBy sz _ _ _) -> sz

/**
 * Is the element in the set?
 *
 * Morally equivalent function: Data.Set.member x s = Data.SetBy.memberBy (<) x s
 */
memberBy :: !(a a -> Bool) !a !(SetBy a) -> Bool

/**
 * Checks if an element is not in the set.
 */
notMemberBy comp x t :== not (memberBy comp x t)

/**
 * Is t1 a subset of t2?
 *
 * Morally equivalent function: Data.Set.isSubsetOf s1 s2 = Data.SetBy.isSubsetOfBy (<) s1 s2
 */
isSubsetOfBy comp t1 t2 :== (size t1 <= size t2) && (isSubsetOfXBy comp t1 t2)

isSubsetOfXBy :: !(a a -> Bool) !(SetBy a) !(SetBy a) -> Bool


/**
 * Is t1 a proper subset of t2?
 *
 * Morally equivalent function: Data.Set.isProperSubsetOf s1 s2 = Data.SetBy.isProperSubsetOfBy (<) s1 s2
 */
isProperSubsetOfBy comp s1 s2 :== (size s1 < size s2) && (isSubsetOfBy comp s1 s2)

/**
 * The empty set.
 * @complexity O(1)
 * @property is null:
 *     null newSetBy
 */
newSetBy :: SetBy a

/**
 * Create a singleton set.
 * @complexity O(1)
 */
singletonBy :: !u:a -> w:(SetBy u:a), [w <= u]

/**
 * Insert an element in a set. If the set already contains an element equal to
 * the given value, it is replaced with the new value.
 *
 * Morally equivalent function: Data.Set.insert x s = Data.SetBy.insertBy (<) x s
 */
insertBy :: !(a a -> Bool) !a !.(SetBy a) -> SetBy a

/**
 * Delete an element from a set.
 *
 * Morally equivalent function: Data.Set.delete x s = Data.SetBy (<) x s
 */
deleteBy :: !(a a -> Bool) !a !.(SetBy a) -> SetBy a


/**
 * The minimal element of a set.
 *
 * Morally equivalent function: Data.Set.findMin
 */
findMin :: !(SetBy a) -> a

/**
 * The maximal element of a set.
 *
 * Morally equivalent function: Data.Set.findMax
 */
findMax :: !(SetBy a) -> a

/**
 * Delete the minimal element.
 *
 * Morally equivalent function: Data.Set.deleteMin
 */
deleteMin :: !.(SetBy a) -> SetBy a

/**
 * Delete the maximal element.
 *
 * Morally equivalent function: Data.Set.deleteMax
 */
deleteMax :: !.(SetBy a) -> SetBy a

/**
 * deleteFindMin set = (findMin set, deleteMin set)
 */
deleteFindMin :: !.(SetBy a) -> (!a, !SetBy a)

/**
 * deleteFindMax set = (findMax set, deleteMax set)
 */
deleteFindMax :: !.(SetBy a) -> (!a, !SetBy a)

/**
 * Retrieves the minimal key of the set, and the set stripped of that element,
 * or 'Nothing' if passed an empty set.
 */
minView :: !.(SetBy a) -> .(Maybe (!a, !SetBy a))

/**
 * Retrieves the maximal key of the set, and the set stripped of that element,
 * or 'Nothing' if passed an empty set.
 */
maxView :: !.(SetBy a) -> .(Maybe (!a, !SetBy a))

/**
 * The union of two sets, preferring the first set when equal elements are
 * encountered.
 *
 * Morally equivalent function: Data.Set.union s1 s2 = Data.SetBy.unionBy (<) s1 s2
 */
unionBy :: !(a a -> Bool) !u:(SetBy a) !u:(SetBy a) -> SetBy a

/**
 * The union of a list of sets.
 *
 * Morally equivalent function: Data.Set.unions ts = Data.SetBy.unionsBy (<) ts
 */
unionsBy ts :== foldl unionBy newSetBy ts

/**
 * Difference of two sets.
 *
 * Morally equivalent function: Data.Set.difference s1 s2 = Data.SetBy.differenceBy (<) s1 s2
 */
differenceBy :: !(a a -> Bool) !(SetBy a) !(SetBy a) -> SetBy a

/**
 * The intersection of two sets.
 * Elements of the result come from the first set.
 *
 * Morally equivalent function: Data.Set.intersection s1 s2 = Data.SetBy.intersectionBy (<) s1 s2
 */
intersectionBy :: !(a a -> Bool) !(SetBy a) !(SetBy a) -> SetBy a

/**
 * The intersection of a list of sets.
 * Elements of the result come from the first set
 *
 * Morally equivalent function: Data.Set.intersections ts = Data.SetBy.intersectionsBy (<) ts
 */
intersectionsBy :: !(a a -> Bool) ![SetBy a] -> SetBy a

/**
 * Filter all elements that satisfy the predicate.
 *
 * Morally equivalent function: Data.Set.filter
 */
filter :: !(a -> Bool) !(SetBy a) -> SetBy a

/**
 * Partition the set into two sets, one with all elements that satisfy the
 * predicate and one with all elements that don't satisfy the predicate.
 *
 * Morally equivalent function: Data.Set.partition
 */
partition :: !(a -> Bool) !(SetBy a) -> (!SetBy a, !SetBy a)

/**
 * Split a set in elements less and elements greater than a certain pivot.
 *
 * Morally equivalent function: Data.Set.split x s = Data.SetBy.splitBy (<) x s
 */
splitBy :: !(a a -> Bool) !a !(SetBy a) -> (!SetBy a, !SetBy a)

/**
 * Performs a 'split' but also returns whether the pivot element was found in
 * the original set.
 *
 * Morally equivalent function: Data.Set.splitMember x s = Data.SetBy.splitMemberBy (<) x s
 */
splitMemberBy :: !(a a -> Bool) !a !(SetBy a) -> (!SetBy a, !Bool, !SetBy a)

/**
 * Convert the set to an ascending list of elements.
 */
toList s :== toAscList s

/**
 * Same as toList.
 */
toAscList t :== 'Data.Foldable'.foldr` (\a as -> [a:as]) [] t

/**
 * Create a set from a list of elements.
 *
 * Morally equivalent function: Data.Set.fromList xs = Data.SetBy.fromListBy (<) xs
 */
fromListBy :: !(a a -> Bool) ![a] -> SetBy a

/**
 * Map a function to all elements in a set.
 *
 * Morally equivalent function: Data.Set.mapSet f s = Data.SetBy.mapSetBy (<) f s
 */
mapSetBy comp_b f s :== fromListBy comp_b (map f (toList s))

/**
 * Map a set without converting it to and from a list.
 *
 * Morally equivalent function: Data.Set.mapSetMonotonic
 */
mapSetByMonotonic :: !(a -> b) !(SetBy a) -> SetBy b