diff --git a/_oasis b/_oasis index c1421116..7eafd834 100644 --- a/_oasis +++ b/_oasis @@ -41,7 +41,7 @@ Library "containers" Modules: CCVector, CCDeque, CCGen, CCSequence, CCFQueue, CCMultiMap, CCMultiSet, CCBV, CCPrint, CCPersistentHashtbl, CCLeftistheap, CCList, CCOpt, CCPair, CCFun, CCHash, - CCKList, CCInt, CCBool, CCArray, CCBatch, CCLinq + CCKList, CCInt, CCBool, CCArray, CCBatch, CCOrd, CCLinq FindlibName: containers Library "containers_string" diff --git a/core/CCOrd.ml b/core/CCOrd.ml new file mode 100644 index 00000000..30fa2598 --- /dev/null +++ b/core/CCOrd.ml @@ -0,0 +1,55 @@ + +(* +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 Comparisons} *) + +type 'a t = 'a -> 'a -> int +(** Comparison (total ordering) between two elements, that returns an int *) + +let compare = Pervasives.compare + +let int_ (x:int) y = Pervasives.compare x y +let string_ (x:string) y = Pervasives.compare x y +let bool_ (x:bool) y = Pervasives.compare x y +let float_ (x:float) y = Pervasives.compare x y + +(** {2 Lexicographic Combination} *) + +let () c (ord,x,y) = + if c = 0 + then ord x y + else c + +let rec list_ ord l1 l2 = match l1, l2 with + | [], [] -> 0 + | [], _ -> -1 + | _, [] -> 1 + | x1::l1', x2::l2' -> + let c = ord x1 x2 in + if c = 0 + then list_ ord l1' l2' + else c + diff --git a/core/CCOrd.mli b/core/CCOrd.mli new file mode 100644 index 00000000..11b22398 --- /dev/null +++ b/core/CCOrd.mli @@ -0,0 +1,54 @@ + +(* +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 Comparisons} *) + +type 'a t = 'a -> 'a -> int +(** Comparison (total ordering) between two elements, that returns an int *) + +val compare : 'a t +(** Polymorphic "magic" comparison *) + +val int_ : int t +val string_ : string t +val bool_ : bool t +val float_ : float t + +(** {2 Lexicographic Combination} *) + +val () : int -> ('a t * 'a * 'a) -> int +(** [c1 @@? (ord, x, y)] returns the same as [c1] if [c1] is not [0]; + otherwise it uses [ord] to compare the two values [x] and [y], + of type ['a]. + + Example: + {[CCInt.compare 1 3 + (String.compare, "a", "b") + (CCBool.compare, true, false)]} +*) + +val list_ : 'a t -> 'a list t +(** Lexicographic combination on lists *)