Commit 097f0616 authored by Raphaël Proust's avatar Raphaël Proust
Browse files

Tutorial

parent 529d8b09
......@@ -20,6 +20,7 @@ depends: [
"either"
"ocamlformat" { = "0.20.1" & with-doc } # not technically a doc dep; modify when with-dev becomes available
"odoc" { with-doc }
"md2mld" { with-doc }
"js_of_ocaml-compiler" { with-test }
]
build: [
......
(* length in bytes for some num representations *)
let l_Z i = String.length (Result.get_ok (Binary.to_string z (Z.of_int64 i)))
let l_N i = String.length (Result.get_ok (Binary.to_string n (Z.of_int64 i)))
(* note about compact: we use a full byte of tag, but if there are two free bits
of shared tag this is free. *)
let l_C i =
String.length
(Result.get_ok
(Binary.to_string (Compact.make ~tag_size:`Uint8 Compact.int64) i))
(* [find l target 0L Int64.max_int] finds a pair [(a,b)] of int64 such that:
- a + 1 = b
- l a = target
- l b > tagert
I.e., [a] is the last of the int64 to use target bytes and [b] is the first
to use more than target bytes
*)
let rec find l target low high =
if low = Int64.sub high 1L then (low, high)
else
let llow = l low in
let lhigh = l high in
(* Printf.printf "FIND: %Ld(%d) - %Ld(%d)\n%!" low llow high lhigh; *)
assert (llow < lhigh) ;
assert (llow <= target) ;
assert (target < lhigh) ;
let middle =
let open Int64 in
add (div low 2L) (div high 2L)
in
let middle =
let open Int64 in
min middle (sub high 1L)
in
let lmiddle = l middle in
if lmiddle <= target then find l target middle high
else find l target low middle
let cutoffs_Z = List.init 9 (fun i -> find l_Z (i + 1) 0L Int64.max_int)
let cutoffs_N = List.init 8 (fun i -> find l_N (i + 1) 0L Int64.max_int)
let cutoffs_C =
(* because of the share tag, we round up to the next full bytes, but the sizes
should be considered 1byte+2bits, 2bytes+2bits, and 4bytes+2bits *)
List.map (fun i -> find l_C i 0L Int64.max_int) [2; 3; 5]
......@@ -26,6 +26,12 @@
(** Type-safe serialization and deserialization of data structures. *)
(** This page is for the API documentation of data-encoding: the technical
description of each of the available type and combinator exported by the
data-encoding library.
For a high-level view and a tutorial, see {!page-tutorial}. *)
(** {1 Data Encoding} *)
(** {2 Overview}
......
......@@ -9,3 +9,14 @@
(libraries json-data-encoding json-data-encoding-bson zarith zarith_stubs_js
ezjsonm either)
(flags (:standard)))
(documentation
(mld_files tutorial))
(rule
(targets tutorial.mld)
(deps tutorial.md)
(action
(with-stdout-to
%{targets}
(run md2mld %{deps}))))
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment