From 9f2ef2f461e9f9506e30cf5c56ee026640fcee18 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 27 Feb 2018 21:07:01 -0600 Subject: [PATCH] add `CCInt{32,64}.Infix` --- src/core/CCInt32.ml | 63 +++++++++++++++++++++++++------------------ src/core/CCInt32.mli | 26 ++++++++++++++++-- src/core/CCInt64.ml | 64 ++++++++++++++++++++++++++------------------ src/core/CCInt64.mli | 24 +++++++++++++++++ 4 files changed, 123 insertions(+), 54 deletions(-) diff --git a/src/core/CCInt32.ml b/src/core/CCInt32.ml index 4aaf73d9..74aaab63 100644 --- a/src/core/CCInt32.ml +++ b/src/core/CCInt32.ml @@ -2,34 +2,45 @@ include Int32 -let (+) = add - -let (-) = sub - -let (~-) = neg - -let ( * ) = mul - -let (/) = div - -let (mod) = rem - -let (land) = logand - -let (lor) = logor - -let (lxor) = logxor - -let lnot = lognot - -let (lsl) = shift_left - -let (lsr) = shift_right_logical - -let (asr) = shift_right - let equal (x:t) y = Pervasives.(=) x y +module Infix = struct + let (+) = add + + let (-) = sub + + let (~-) = neg + + let ( * ) = mul + + let (/) = div + + let (mod) = rem + + let (land) = logand + + let (lor) = logor + + let (lxor) = logxor + + let lnot = lognot + + let (lsl) = shift_left + + let (lsr) = shift_right_logical + + let (asr) = shift_right + + let (=) = equal + + let (<>) = Pervasives.(<>) + let (<) = Pervasives.(<) + let (<=) = Pervasives.(<=) + let (>) = Pervasives.(>) + let (>=) = Pervasives.(>=) +end +include Infix + let hash x = Pervasives.abs (to_int x) (** {2 Conversion} *) diff --git a/src/core/CCInt32.mli b/src/core/CCInt32.mli index 60b4fb3f..1386abf1 100644 --- a/src/core/CCInt32.mli +++ b/src/core/CCInt32.mli @@ -9,8 +9,8 @@ 32-bit wide on all platforms. All arithmetic operations over int32 are taken modulo 2{^32}. - Performance notice: values of type int32 occupy more memory space than values - of type int, and arithmetic operations on int32 are generally slower than + Performance notice: values of type int32 occupy more memory space than values + of type int, and arithmetic operations on int32 are generally slower than those on int. Use int32 only when the application requires exact 32-bit arithmetic. @since NEXT_RELEASE *) @@ -68,6 +68,28 @@ val ( asr ) : t -> int -> t and inserted in the vacated bits. The result is unspecified if [y < 0] or [y >= 32]. *) +module Infix : sig + val (+) : t -> t -> t + val (-) : t -> t -> t + val (~-) : t -> t + val ( * ) : t -> t -> t + val (/) : t -> t -> t + val (mod) : t -> t -> t + val (land) : t -> t -> t + val (lor) : t -> t -> t + val (lxor) : t -> t -> t + val lnot : t -> t + val (lsl) : t -> int -> t + val (lsr) : t -> int -> t + val (asr) : t -> int -> 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 +end + val equal : t -> t -> bool (** The equal function for 32-bit integers. Like {!Pervasives.(=) x y)}. *) diff --git a/src/core/CCInt64.ml b/src/core/CCInt64.ml index 025142c2..bfaed6f9 100644 --- a/src/core/CCInt64.ml +++ b/src/core/CCInt64.ml @@ -2,34 +2,46 @@ include Int64 -let (+) = add - -let (-) = sub - -let (~-) = neg - -let ( * ) = mul - -let (/) = div - -let (mod) = rem - -let (land) = logand - -let (lor) = logor - -let (lxor) = logxor - -let lnot = lognot - -let (lsl) = shift_left - -let (lsr) = shift_right_logical - -let (asr) = shift_right - let equal (x:t) y = Pervasives.(=) x y +module Infix = struct + let (+) = add + + let (-) = sub + + let (~-) = neg + + let ( * ) = mul + + let (/) = div + + let (mod) = rem + + let (land) = logand + + let (lor) = logor + + let (lxor) = logxor + + let lnot = lognot + + let (lsl) = shift_left + + let (lsr) = shift_right_logical + + let (asr) = shift_right + + let (=) = equal + + let (<>) = Pervasives.(<>) + let (<) = Pervasives.(<) + let (<=) = Pervasives.(<=) + let (>) = Pervasives.(>) + let (>=) = Pervasives.(>=) +end + +include Infix + let hash x = Pervasives.abs (to_int x) (** {2 Conversion} *) diff --git a/src/core/CCInt64.mli b/src/core/CCInt64.mli index 2f830c45..5ab65c3f 100644 --- a/src/core/CCInt64.mli +++ b/src/core/CCInt64.mli @@ -66,6 +66,30 @@ val (asr) : t -> int -> t and inserted in the vacated bits. The result is unspecified if [y < 0] or [y >= 64]. *) +(** Infix operators + @since NEXT_RELEASE *) +module Infix : sig + val (+) : t -> t -> t + val (-) : t -> t -> t + val (~-) : t -> t + val ( * ) : t -> t -> t + val (/) : t -> t -> t + val (mod) : t -> t -> t + val (land) : t -> t -> t + val (lor) : t -> t -> t + val (lxor) : t -> t -> t + val lnot : t -> t + val (lsl) : t -> int -> t + val (lsr) : t -> int -> t + val (asr) : t -> int -> 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 +end + val equal : t -> t -> bool (** The equal function for 64-bit integers. Like {!Pervasives.(=) x y)}. *)