diff --git a/_oasis b/_oasis index 90e512cb..30340442 100644 --- a/_oasis +++ b/_oasis @@ -48,7 +48,7 @@ Flag "advanced" Library "containers" Path: src/core Modules: CCVector, CCPrint, CCError, CCHeap, CCList, CCOpt, CCPair, - CCFun, CCHash, CCInt, CCBool, CCFloat, CCArray, CCRef, + CCFun, CCHash, CCInt, CCBool, CCFloat, CCArray, CCRef, CCSet, CCOrd, CCRandom, CCString, CCHashtbl, CCMap, CCFormat BuildDepends: bytes diff --git a/doc/intro.txt b/doc/intro.txt index 400e2e7c..631dec78 100644 --- a/doc/intro.txt +++ b/doc/intro.txt @@ -41,6 +41,7 @@ CCPair CCPrint CCRandom CCRef +CCSet CCString CCVector } diff --git a/src/core/CCSet.ml b/src/core/CCSet.ml new file mode 100644 index 00000000..5abed74a --- /dev/null +++ b/src/core/CCSet.ml @@ -0,0 +1,86 @@ + +(* +copyright (c) 2013-2014, simon cruanes +all rights reserved. + +redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. redistributions in binary +form must reproduce the above copyright notice, this list of conditions and the +following disclaimer in the documentation and/or other materials provided with +the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*) + +(** {1 Wrapper around Set} *) + +type 'a sequence = ('a -> unit) -> unit +type 'a printer = Buffer.t -> 'a -> unit +type 'a formatter = Format.formatter -> 'a -> unit + +module type S = sig + include Set.S + + val of_seq : elt sequence -> t + + val to_seq : t -> elt sequence + + val of_list : elt list -> t + + val to_list : t -> elt list + + val pp : ?start:string -> ?stop:string -> ?sep:string -> + elt printer -> t printer + + val print : ?start:string -> ?stop:string -> ?sep:string -> + elt formatter -> t formatter +end + +module Make(O : Map.OrderedType) = struct + include Set.Make(O) + + let of_seq s = + let set = ref empty in + s (fun x -> set := add x !set); + !set + + let to_seq s yield = iter yield s + + let of_list l = List.fold_left (fun set x -> add x set) empty l + + let to_list = elements + + let pp ?(start="{") ?(stop="}") ?(sep=", ") pp_x buf m = + let first = ref true in + Buffer.add_string buf start; + iter + (fun x -> + if !first then first := false else Buffer.add_string buf sep; + pp_x buf x; + ) m; + Buffer.add_string buf stop + + let print ?(start="[") ?(stop="]") ?(sep=", ") pp_x fmt m = + Format.pp_print_string fmt start; + let first = ref true in + iter + (fun x -> + if !first then first := false else Format.pp_print_string fmt sep; + pp_x fmt x; + Format.pp_print_cut fmt () + ) m; + Format.pp_print_string fmt stop +end + diff --git a/src/core/CCSet.mli b/src/core/CCSet.mli new file mode 100644 index 00000000..cc396ddc --- /dev/null +++ b/src/core/CCSet.mli @@ -0,0 +1,53 @@ + +(* +copyright (c) 2013-2014, simon cruanes +all rights reserved. + +redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. redistributions in binary +form must reproduce the above copyright notice, this list of conditions and the +following disclaimer in the documentation and/or other materials provided with +the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*) + +(** {1 Wrapper around Set} *) + +type 'a sequence = ('a -> unit) -> unit +type 'a printer = Buffer.t -> 'a -> unit +type 'a formatter = Format.formatter -> 'a -> unit + +module type S = sig + include Set.S + + val of_seq : elt sequence -> t + + val to_seq : t -> elt sequence + + val of_list : elt list -> t + + val to_list : t -> elt list + + val pp : ?start:string -> ?stop:string -> ?sep:string -> + elt printer -> t printer + + val print : ?start:string -> ?stop:string -> ?sep:string -> + elt formatter -> t formatter +end + +module Make(O : Set.OrderedType) : S + with type t = Set.Make(O).t + and type elt = O.t