b6449cae87
* `lib_stdlib`: basic extended OCaml stdlib and generic data structures * `lib_data_encoding`: almost independant 'Data_encoding' * `lib_error_monad`: almost independant 'Error_monad' * `lib_stdlib_lwt`: extended Lwt library * `lib_crypto`: all the crypto stuff (hashing, signing, cryptobox). * `lib_base`: - basic type definitions (Block_header, Operation, ...) - a module `TzPervasives` to bind them all and to be the single module opened everywhere. In the process, I splitted `Tezos_data` and `Hash` in multiple submodules, thus removing a lot of `-open`. The following two modules may not have found their place yet: - Base58 (currently in `lib_crypto`) - Cli_entries (currently in `lib_stdlib_lwt`)
60 lines
1.6 KiB
OCaml
60 lines
1.6 KiB
OCaml
(**************************************************************************)
|
|
(* *)
|
|
(* Copyright (c) 2014 - 2017. *)
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
(* *)
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
(* *)
|
|
(**************************************************************************)
|
|
|
|
type 'a raw =
|
|
| Empty of int
|
|
| Inited of {
|
|
data : 'a array ;
|
|
mutable pos : int ;
|
|
}
|
|
|
|
type 'a t = 'a raw ref
|
|
|
|
let create size = ref (Empty size)
|
|
|
|
let add r v =
|
|
match !r with
|
|
| Empty size ->
|
|
r := Inited { data = Array.make size v ; pos = 0 }
|
|
| Inited s ->
|
|
s.pos <-
|
|
if s.pos = 2 * Array.length s.data - 1 then
|
|
Array.length s.data
|
|
else
|
|
s.pos + 1 ;
|
|
s.data.(s.pos mod Array.length s.data) <- v
|
|
|
|
let add_list r l = List.iter (add r) l
|
|
|
|
let last r =
|
|
match !r with
|
|
| Empty _ -> None
|
|
| Inited { data ; pos } -> Some data.(pos mod Array.length data)
|
|
|
|
let fold r ~init ~f =
|
|
match !r with
|
|
| Empty _ -> init
|
|
| Inited { data ; pos } ->
|
|
let size = Array.length data in
|
|
let acc = ref init in
|
|
for i = 0 to min pos (size - 1) do
|
|
acc := f !acc data.((pos - i) mod size)
|
|
done ;
|
|
!acc
|
|
|
|
let elements t =
|
|
fold t ~init:[] ~f:(fun acc elt -> elt :: acc)
|
|
|
|
exception Empty
|
|
|
|
let last_exn r =
|
|
match last r with
|
|
| None -> raise Empty
|
|
| Some d -> d
|