(* ord-key-sig.sml * * COPYRIGHT (c) 1993 by AT&T Bell Laboratories. See COPYRIGHT file for details. * * Abstract linearly ordered keys. * *) signature ORD_KEY = sig type ord_key val compare : ord_key * ord_key -> order end (* ORD_KEY *) (* ordset-sig.sml * * COPYRIGHT (c) 1993 by AT&T Bell Laboratories. See COPYRIGHT file for details. * * Signature for a set of values with an order relation. *) signature ORD_SET = sig structure Key : ORD_KEY type item = Key.ord_key type set val empty : set (* The empty set *) val singleton : item -> set (* Create a singleton set *) val add : set * item -> set val add' : (item * set) -> set (* Insert an item. *) val addList : set * item list -> set (* Insert items from list. *) val delete : set * item -> set (* Remove an item. Raise NotFound if not found. *) val member : set * item -> bool (* Return true if and only if item is an element in the set *) val isEmpty : set -> bool (* Return true if and only if the set is empty *) val equal : (set * set) -> bool (* Return true if and only if the two sets are equal *) val compare : (set * set) -> order (* does a lexical comparison of two sets *) val isSubset : (set * set) -> bool (* Return true if and only if the first set is a subset of the second *) val numItems : set -> int (* Return the number of items in the table *) val listItems : set -> item list (* Return an ordered list of the items in the set *) val union : set * set -> set (* Union *) val intersection : set * set -> set (* Intersection *) val difference : set * set -> set (* Difference *) val map : (item -> item) -> set -> set (* Create a new set by applying a map function to the elements * of the set. *) val app : (item -> unit) -> set -> unit (* Apply a function to the entries of the set * in increasing order *) val foldl : (item * 'b -> 'b) -> 'b -> set -> 'b (* Apply a folding function to the entries of the set * in increasing order *) val foldr : (item * 'b -> 'b) -> 'b -> set -> 'b (* Apply a folding function to the entries of the set * in decreasing order *) val partition : (item -> bool) -> set -> (set * set) val filter : (item -> bool) -> set -> set val exists : (item -> bool) -> set -> bool val find : (item -> bool) -> set -> item option end (* ORD_SET *) (* list-set-fn.sml * * COPYRIGHT (c) 1996 by AT&T Research. See COPYRIGHT file for details. * * An implementation of finite sets of ordered values, which uses a sorted list * representation. *) functor ListSetFn (K : ORD_KEY) : ORD_SET where type Key.ord_key = K.ord_key = struct structure Key = K (* sets are represented as ordered lists of key values *) type item = Key.ord_key type set = item list exception NotFound val empty = [] fun singleton x = [x] fun add (l, item) = let fun f [] = [item] | f (elem::r) = (case Key.compare(item, elem) of LESS => item :: elem :: r | EQUAL => item :: r | GREATER => elem :: (f r) (* end case *)) in f l end fun add' (s, x) = add(x, s) fun union (s1, s2) = let fun merge ([], l2) = l2 | merge (l1, []) = l1 | merge (x::r1, y::r2) = (case Key.compare(x, y) of LESS => x :: merge(r1, y::r2) | EQUAL => x :: merge(r1, r2) | GREATER => y :: merge(x::r1, r2) (* end case *)) in merge (s1, s2) end fun intersection (s1, s2) = let fun merge ([], l2) = [] | merge (l1, []) = [] | merge (x::r1, y::r2) = (case Key.compare(x, y) of LESS => merge(r1, y::r2) | EQUAL => x :: merge(r1, r2) | GREATER => merge(x::r1, r2) (* end case *)) in merge (s1, s2) end fun difference (s1, s2) = let fun merge ([], l2) = [] | merge (l1, []) = l1 | merge (x::r1, y::r2) = (case Key.compare(x, y) of LESS => x :: merge(r1, y::r2) | EQUAL => merge(r1, r2) | GREATER => merge(x::r1, r2) (* end case *)) in merge (s1, s2) end fun addList (l, items) = let val items' = List.foldl (fn (x, set) => add(set, x)) [] items in union (l, items') end (* Remove an item, returning new map and value removed. * Raise LibBase.NotFound if not found. *) fun delete (l, elem) = let fun f (_, []) = raise NotFound | f (prefix, elem' :: r) = (case Key.compare(elem, elem') of LESS => raise NotFound | EQUAL => List.revAppend(prefix, r) | GREATER => f(elem' :: prefix, r) (* end case *)) in f ([], l) end fun member (l, item) = let fun f [] = false | f (elem :: r) = (case Key.compare(item, elem) of LESS => false | EQUAL => true | GREATER => f r (* end case *)) in f l end fun isEmpty [] = true | isEmpty _ = false fun equal (s1, s2) = let fun f ([], []) = true | f (x::r1, y::r2) = (Key.compare(x, y) = EQUAL) andalso f (r1, r2) | f _ = false in f (s1, s2) end fun compare ([], []) = EQUAL | compare ([], _) = LESS | compare (_, []) = GREATER | compare (x1::r1, x2::r2) = (case Key.compare(x1, x2) of EQUAL => compare (r1, r2) | order => order (* end case *)) (* Return true if and only if the first set is a subset of the second *) fun isSubset (s1, s2) = let fun f ([], _) = true | f (_, []) = false | f (x::r1, y::r2) = (case Key.compare(x, y) of LESS => false | EQUAL => f (r1, r2) | GREATER => f (x::r1, r2) (* end case *)) in f (s1, s2) end (* Return the number of items in the set *) fun numItems l = List.length l (* Return a list of the items in the set *) fun listItems l = l val app = List.app fun map f s1 = List.foldl (fn (x, s) => add(s, f x)) [] s1 val foldr = List.foldr val foldl = List.foldl val filter = List.filter val partition = List.partition val exists = List.exists val find = List.find end (* IntListMap *)