mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 19:25:28 -05:00
basic combinators for ordering (including lexico combination)
This commit is contained in:
parent
86cd5c0e8d
commit
e9e861479c
3 changed files with 110 additions and 1 deletions
2
_oasis
2
_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"
|
||||
|
|
|
|||
55
core/CCOrd.ml
Normal file
55
core/CCOrd.ml
Normal file
|
|
@ -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
|
||||
|
||||
54
core/CCOrd.mli
Normal file
54
core/CCOrd.mli
Normal file
|
|
@ -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 *)
|
||||
Loading…
Add table
Reference in a new issue