package octez-libs

  1. Overview
  2. Docs
A package that contains multiple base libraries used by the Octez suite

Install

Dune Dependency

Authors

Maintainers

Sources

octez-19.0.tar.gz
sha256=c6df840ebbf115e454db949028c595bec558a59a66cade73b52a6d099d6fa4d4
sha512=d8aee903b9fe130d73176bc8ec38b78c9ff65317da3cb4f3415f09af0c625b4384e7498201fdb61aa39086a7d5d409d0ab3423f9bc3ab989a680cf444a79bc13

doc/src/octez-libs.mec/iterator.ml.html

Source file iterator.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
type bit = int

module Bit = struct
  type t = {f : unit -> bit option; size : int; mutable i : int}

  let of_bytes_le b =
    let length = Bytes.length b in
    if length = 0 then {f = (fun () -> None); size = 0; i = 0}
    else
      let current_byte_int = ref (Bytes.get_uint8 b 0) in
      let i = ref 0 in
      let j = ref 0 in
      let rec f () : bit option =
        if !i = length - 1 && !j = 8 then None
        else if !j = 8 then (
          i := !i + 1 ;
          current_byte_int := Bytes.get_uint8 b !i ;
          j := 0 ;
          f ())
        else
          let b = !current_byte_int mod 2 in
          j := !j + 1 ;
          current_byte_int := !current_byte_int lsr 1 ;
          Some b
      in
      {f; size = length * 8; i = 0}

  let is_processed {f = _; size; i} = size = i

  let of_bool_list bs =
    let bs_ref = ref bs in
    let length = List.length bs in
    if length = 0 then {f = (fun () -> None); size = length; i = 0}
    else
      let f () =
        match !bs_ref with
        | [] -> None
        | b :: bs ->
            bs_ref := bs ;
            Some (if b then 1 else 0)
      in
      {f; size = length; i = 0}

  let next iterator =
    if is_processed iterator then None
    else
      let res = iterator.f () in
      iterator.i <- iterator.i + 1 ;
      res

  let get_chunk iterator ?(default = 0) n =
    if is_processed iterator then List.init n (fun _ -> default)
    else List.init n (fun _ -> Option.value (next iterator) ~default)
end
OCaml

Innovation. Community. Security.