package octez-libs

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file compare.ml

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
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(* Copyright (c) 2021 Nomadic Labs <contact@nomadic-labs.com>                *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

module type COMPARABLE = sig
  type t

  val compare : t -> t -> int
end

module type S = sig
  type t

  val ( = ) : t -> t -> bool

  val ( <> ) : t -> t -> bool

  val ( < ) : t -> t -> bool

  val ( <= ) : t -> t -> bool

  val ( >= ) : t -> t -> bool

  val ( > ) : t -> t -> bool

  val compare : t -> t -> int

  val equal : t -> t -> bool

  val max : t -> t -> t

  val min : t -> t -> t
end

module Make (P : COMPARABLE) = struct
  include P

  let compare = compare

  let ( = ) a b = compare a b = 0

  let ( <> ) a b = compare a b <> 0

  let ( < ) a b = compare a b < 0

  let ( <= ) a b = compare a b <= 0

  let ( >= ) a b = compare a b >= 0

  let ( > ) a b = compare a b > 0

  let equal = ( = )

  let max x y = if x >= y then x else y

  let min x y = if x <= y then x else y
end

module List (P : COMPARABLE) = Make (struct
  type t = P.t list

  let rec compare xs ys =
    match (xs, ys) with
    | [], [] -> 0
    | [], _ -> -1
    | _, [] -> 1
    | x :: xs, y :: ys ->
        let hd = P.compare x y in
        if hd <> 0 then hd else compare xs ys
end)

module Option (P : COMPARABLE) = Make (struct
  type t = P.t option

  let compare xs ys =
    match (xs, ys) with
    | None, None -> 0
    | None, _ -> -1
    | _, None -> 1
    | Some x, Some y -> P.compare x y
end)

module Result (Ok : COMPARABLE) (Error : COMPARABLE) = Make (struct
  type t = (Ok.t, Error.t) result

  (* Note: [Ok _ < Error _] for compatibility with the Stdlib's polymorphic
           comparison. *)
  let compare ra rb =
    match (ra, rb) with
    | Ok a, Ok b -> Ok.compare a b
    | Error a, Error b -> Error.compare a b
    | Ok _, Error _ -> -1
    | Error _, Ok _ -> 1
end)

module Char = Make (Char)

module Bool = Make (struct
  type t = bool

  let compare = Stdlib.compare
end)

module Int = struct
  type t = int

  (**

     Integer comparisons are ubiquitous and must be efficiently
     compiled. The OCaml compiler provides the following builtin
     externals to make sure that aliases to integer comparisons are
     properly detected.

     Moving from the generic comparison, which ultimately calls the C
     function caml_compare, to these builtins divides execution time
     by one or two orders of magnitude.

     This optimization is especially important in critical routines
     like the gas update and the check for the gas exhaustion of the
     Michelson runtime.


     References:
     -----------
     - https://gitlab.com/-/snippets/2031392
     - https://tarides.com/blog/2019-09-13-decompress-experiences-with-ocaml-optimization
     - https://blog.janestreet.com/the-perils-of-polymorphic-compare/

  *)

  external ( = ) : int -> int -> bool = "%equal"

  external ( <> ) : int -> int -> bool = "%notequal"

  external ( < ) : int -> int -> bool = "%lessthan"

  external ( > ) : int -> int -> bool = "%greaterthan"

  external ( <= ) : int -> int -> bool = "%lessequal"

  external ( >= ) : int -> int -> bool = "%greaterequal"

  external compare : int -> int -> int = "%compare"

  external equal : int -> int -> bool = "%equal"

  let max x y = if x >= y then x else y

  let min x y = if x <= y then x else y
end

module Int32 = Make (Int32)
module Int64 = Make (Int64)

module MakeUnsigned
    (Int : S) (Z : sig
      val zero : Int.t
    end) =
struct
  type t = Int.t

  let compare va vb =
    Int.(
      if va >= Z.zero then if vb >= Z.zero then compare va vb else -1
      else if vb >= Z.zero then 1
      else compare va vb)

  let ( = ) = (( = ) : t -> t -> bool)

  let ( <> ) = (( <> ) : t -> t -> bool)

  let ( < ) a b =
    Int.(if Z.zero <= a then a < b || b < Z.zero else b < Z.zero && a < b)

  let ( <= ) a b =
    Int.(if Z.zero <= a then a <= b || b < Z.zero else b < Z.zero && a <= b)

  let ( >= ) a b = b <= a

  let ( > ) a b = b < a

  let equal = ( = )

  let max x y = if x >= y then x else y

  let min x y = if x <= y then x else y
end

module Uint32 =
  MakeUnsigned
    (Int32)
    (struct
      let zero = 0l
    end)

module Uint64 =
  MakeUnsigned
    (Int64)
    (struct
      let zero = 0L
    end)

module Float = Make (struct
  type t = float

  let compare = Stdlib.compare
end)

module String = Make (String)
module Bytes = Make (Bytes)

module Z = struct
  type t = Z.t

  include Z.Compare

  let compare = Z.compare

  let equal = Z.equal

  let max = Z.max

  let min = Z.min
end

module Q = Q

module List_length_with = struct
  let ( = ) l i = Stdlib.List.compare_length_with l i = 0

  let ( <> ) l i = Stdlib.List.compare_length_with l i <> 0

  let ( < ) l i = Stdlib.List.compare_length_with l i < 0

  let ( <= ) l i = Stdlib.List.compare_length_with l i <= 0

  let ( >= ) l i = Stdlib.List.compare_length_with l i >= 0

  let ( > ) l i = Stdlib.List.compare_length_with l i > 0

  let compare l i = Stdlib.List.compare_length_with l i

  let equal = ( = )
end

module List_lengths = struct
  let ( = ) l1 l2 = Stdlib.List.compare_lengths l1 l2 = 0

  let ( <> ) l1 l2 = Stdlib.List.compare_lengths l1 l2 <> 0

  let ( < ) l1 l2 = Stdlib.List.compare_lengths l1 l2 < 0

  let ( <= ) l1 l2 = Stdlib.List.compare_lengths l1 l2 <= 0

  let ( >= ) l1 l2 = Stdlib.List.compare_lengths l1 l2 >= 0

  let ( > ) l1 l2 = Stdlib.List.compare_lengths l1 l2 > 0

  let compare l1 l2 = Stdlib.List.compare_lengths l1 l2

  let equal = ( = )
end

let or_else c f = if c <> 0 then c else f ()
OCaml

Innovation. Community. Security.