From e2bc0cf55a464f4a6bd84f2ffc0caa693916e702 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 15 Apr 2016 15:04:31 +0200 Subject: [PATCH] add `CCRef.{get_then_incr,incr_then_get}` functions --- src/core/CCRef.ml | 8 ++++++++ src/core/CCRef.mli | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/core/CCRef.ml b/src/core/CCRef.ml index 047b0e92..050e17c4 100644 --- a/src/core/CCRef.ml +++ b/src/core/CCRef.ml @@ -21,6 +21,14 @@ let iter f r = f !r let update f r = r := (f !r) +let incr_then_get r = + incr r; !r + +let get_then_incr r = + let x = !r in + incr r; + x + let compare f r1 r2 = f !r1 !r2 let equal f r1 r2 = f !r1 !r2 diff --git a/src/core/CCRef.mli b/src/core/CCRef.mli index fed1091e..574cc6bf 100644 --- a/src/core/CCRef.mli +++ b/src/core/CCRef.mli @@ -24,6 +24,14 @@ val iter : ('a -> unit) -> 'a t -> unit val update : ('a -> 'a) -> 'a t -> unit (** Update the reference's content with the given function *) +val incr_then_get : int t -> int +(** [incr_then_get r] increments [r] and returns its new value, think [++ r] + @since NEXT_RELEASE *) + +val get_then_incr : int t -> int +(** [get_then_incr r] increments [r] and returns its old value, think [r++] + @since NEXT_RELEASE *) + val compare : 'a ord -> 'a t ord val equal : 'a eq -> 'a t eq