Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ on:
pull_request:
branches:
- master
- next
- ci
push:
branches:
- master
- next
- ci

jobs:
Expand All @@ -20,33 +22,33 @@ jobs:
- macos-latest
- windows-latest
ocaml-compiler:
- 4.10.0
- 4.14.2
skip_test:
- false

include:
- os: ubuntu-latest
ocaml-compiler: 4.08.0
ocaml-compiler: 4.14.2
skip_test: true


runs-on: ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Retrieve opam cache
uses: actions/cache@v2
uses: actions/cache@v3
id: cache-opam
with:
path: ~/.opam
key: v2-${{ runner.os }}-bitpack_serialize-${{ matrix.ocaml-compiler }}-${{ hashFiles('opam/*.opam') }}
key: v3-${{ runner.os }}-bitpack_serialize-${{ matrix.ocaml-compiler }}-${{ hashFiles('*.opam') }}
# restore-keys: |
# v2-${{ runner.os }}-opam-${{ matrix.ocaml-compiler }}-
# v3-${{ runner.os }}-opam-${{ matrix.ocaml-compiler }}-

- name: Use OCaml ${{ matrix.ocaml-compiler }}
uses: avsm/setup-ocaml@v2
uses: avsm/setup-ocaml@v3
with:
ocaml-compiler: ${{ matrix.ocaml-compiler }}
opam-pin: false
Expand All @@ -64,13 +66,9 @@ jobs:
- run: opam --version

- run: opam depext -y bitpack_serializer
# if: steps.cache-opam.outputs.cache-hit != 'true'
if: matrix.os != 'windows-latest'

- run: opam install -y opam/*.opam --deps-only --with-test
# if: steps.cache-opam.outputs.cache-hit != 'true'

# - run: opam upgrade --fixup
# if: steps.cache-opam.outputs.cache-hit == 'true'
- run: opam install -y . --deps-only --with-test

- run: make build

Expand All @@ -82,10 +80,10 @@ jobs:
- name: test source is well formatted
run: make fmt
continue-on-error: true
if: matrix.ocaml-compiler == '4.10.0' && matrix.os == 'ubuntu-latest'
if: matrix.ocaml-compiler == '4.14.2' && matrix.os == 'ubuntu-latest'

- name: build-doc
if: matrix.ocaml-compiler == '4.10.0' && matrix.os == 'ubuntu-latest'
if: matrix.ocaml-compiler == '4.14.2' && matrix.os == 'ubuntu-latest'
run: |
sudo apt install -yqq python3-sphinx python3-sphinx-rtd-theme
opam install -y dune odoc
Expand Down
File renamed without changes.
23 changes: 12 additions & 11 deletions src/bitpack_serializer.ml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
(**************************************************************************)
(* *)
(* Copyright (c) 2019-2022 Ocaml Pro *)
(* *)
(* All rights reserved. *)
(* This file is distributed under the terms of the GNU Lesser General *)
(* Public License version 2.1, with the special exception on linking *)
(* described in the LICENSE.md file in the root directory. *)
(* *)
(* *)
(**************************************************************************)
(******************************************************************************)
(* *)
(* Copyright (c) 2019-2026 OCamlPro *)
(* *)
(* All rights reserved. *)
(* *)
(* This file is distributed under the terms of the GNU Lesser General Public *)
(* License version 2.1, with the special exception on linking described in *)
(* the LICENSE.md file in the root directory. *)
(* *)
(******************************************************************************)


module Buffer = Buffer
module Lens = Lens
Expand Down
23 changes: 12 additions & 11 deletions src/bitpack_serializer.mli
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
(**************************************************************************)
(* *)
(* Copyright (c) 2019-2022 OcamlPro *)
(* *)
(* All rights reserved. *)
(* This file is distributed under the terms of the GNU Lesser General *)
(* Public License version 2.1, with the special exception on linking *)
(* described in the LICENSE.md file in the root directory. *)
(* *)
(* *)
(**************************************************************************)
(******************************************************************************)
(* *)
(* Copyright (c) 2019-2026 OCamlPro *)
(* *)
(* All rights reserved. *)
(* *)
(* This file is distributed under the terms of the GNU Lesser General Public *)
(* License version 2.1, with the special exception on linking described in *)
(* the LICENSE.md file in the root directory. *)
(* *)
(******************************************************************************)


module Buffer : module type of Buffer
module Lens : module type of Lens
Expand Down
83 changes: 42 additions & 41 deletions src/buffer.ml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
(**************************************************************************)
(* *)
(* Copyright (c) 2019-2022 OcamlPro *)
(* *)
(* All rights reserved. *)
(* This file is distributed under the terms of the GNU Lesser General *)
(* Public License version 2.1, with the special exception on linking *)
(* described in the LICENSE.md file in the root directory. *)
(* *)
(* *)
(**************************************************************************)
(******************************************************************************)
(* *)
(* Copyright (c) 2019-2026 OCamlPro *)
(* *)
(* All rights reserved. *)
(* *)
(* This file is distributed under the terms of the GNU Lesser General Public *)
(* License version 2.1, with the special exception on linking described in *)
(* the LICENSE.md file in the root directory. *)
(* *)
(******************************************************************************)


open Utils
open Collections
Expand All @@ -28,8 +29,10 @@ type t =
}

type 'dict dictionary =
| NoDictionary
| Dictionary of 'dict
[ `NoDictionary
| `Dictionary of 'dict]

type dictionary_config = int option dictionary

(** A reading specialized buffer. The dictionary is used to decode strings that
have been encoded as integers. *)
Expand All @@ -48,7 +51,7 @@ type writer = {
mutable stats : stats;
}

let default_with_dict = Dictionary None
let default_with_dict = `Dictionary None

(** Statistic utilities *)
let stats_bytes t i = t.stats.bits_bytes <- t.stats.bits_bytes + i
Expand All @@ -60,7 +63,7 @@ let stats_z t i = t.stats.bits_z <- t.stats.bits_z + i

let dictionary_size_to_max_bit_size = function
| None -> None
| Some s -> Some (Utils.numbits s - 1)
| Some s -> Some (Utils.numbits (s - 1))

(** Prints an int64 in binary, with at least 8 bits. *)
let print_int_bin fmt i =
Expand Down Expand Up @@ -355,14 +358,14 @@ let write_with_stats stats_add (t : writer) (v : int64) (size : int) =
Log.debug "[write] Writing %a on %i bits@." print_int_bin v size;
let () =
check_size size;

if v >= (Int64.(shift_left one size))
let max_possible_value =
if size = 64
then Int64.max_int
else Int64.(sub (shift_left one size) one)
in
if v > max_possible_value
then invalid_argument
"Value %Ld does not fit on %i bits" v size;

if v < Int64.zero
then invalid_argument
"Value %Ld is negative" v
in
write_byte
t.writer
Expand All @@ -376,8 +379,7 @@ let write = write_with_stats stats_int
let read_bool t : bool = (read t 1) = Int64.one
let read_uint8 t : int = read t 8 |> Int64.to_int
let read_uint16 t : int = read t 16 |> Int64.to_int
let read_uint32 t : int = read t 32 |> Int64.to_int
let read_uint63 t : int64 = read t 63
let read_uint32 t : int64 = read t 32

let write_stat_int = write_with_stats stats_int

Expand All @@ -386,8 +388,7 @@ let write_bool t (b : bool) = write_stat_int t Int64.(if b then one else zero) 1
let write_int t (v : int) nbits = write_stat_int t (Int64.of_int v) nbits
let write_uint8 t (v : int) = write_int t v 8
let write_uint16 t (v : int) = write_int t v 16
let write_uint32 t (v : int) = write_int t v 32
let write_uint63 t (v : int64) = write_stat_int t v 63
let write_uint32 t (v : int64) = write_stat_int t v 32

let z_length value = (Z.numbits value + 1 + 6) / 7

Expand Down Expand Up @@ -589,8 +590,8 @@ let write_string_id (t : writer) (str_id : int) =
correspondance between integers and strings. *)
let write_str_repr (t : writer) s =
match t.dictionary with
| NoDictionary -> write_string t s
| Dictionary dict ->
| `NoDictionary -> write_string t s
| `Dictionary dict ->
match StringMap.find_opt s !dict with
| None ->
let new_id = StringMap.cardinal !dict in
Expand All @@ -606,8 +607,8 @@ let read_string_id (t : reader) =
(** Decodes a string encoded as an integer with the reader dictionary. *)
let read_str_repr (t : reader) =
match t.dictionary with
| NoDictionary -> read_string t
| Dictionary d ->
| `NoDictionary -> read_string t
| `Dictionary d ->
let id = read_string_id t in
match IntMap.find_opt id d with
None ->
Expand All @@ -619,9 +620,9 @@ let read_str_repr (t : reader) =
let initialize_writer ?(with_dict=default_with_dict) ?(init_size = 4096) () =
let dictionary, dictionary_max_bit_size =
match with_dict with
| NoDictionary -> NoDictionary, None
| Dictionary size ->
Dictionary (ref StringMap.empty),
| `NoDictionary -> `NoDictionary, None
| `Dictionary size ->
`Dictionary (ref StringMap.empty),
dictionary_size_to_max_bit_size size
in
{ writer =
Expand All @@ -637,8 +638,8 @@ let initialize_writer ?(with_dict=default_with_dict) ?(init_size = 4096) () =
(** Encodes and writes the dictionary in a fresh Bytes buffer *)
let write_dict d =
match d with
| NoDictionary -> Bytes.empty
| Dictionary {contents = d} ->
| `NoDictionary -> Bytes.empty
| `Dictionary {contents = d} ->
let t = initialize_writer () in
let size = StringMap.cardinal d in
write_signed_int t size;
Expand Down Expand Up @@ -667,17 +668,17 @@ let initialize_reader ?(with_dict=default_with_dict) buffer =
buffer;
offset = 0;
};
dictionary = NoDictionary; (* updated later *)
dictionary = `NoDictionary; (* updated later *)
dictionary_max_bit_size = None; (* updated later *)
}
in
let () =
match with_dict with
| NoDictionary -> ()
| Dictionary size -> begin
| `NoDictionary -> ()
| `Dictionary size -> begin
let dictionary =
let cardinal = read_signed_int t in
Log.debug "[initialize_reader] Dictionary size = %i@." cardinal;
Log.debug "[initialize_reader] `Dictionary size = %i@." cardinal;
let rec build_dic dict id =
if id = cardinal
then (
Expand All @@ -695,16 +696,16 @@ let initialize_reader ?(with_dict=default_with_dict) buffer =
in
build_dic IntMap.empty 0
in
t.dictionary <- Dictionary dictionary;
t.dictionary <- `Dictionary dictionary;
t.dictionary_max_bit_size <- dictionary_size_to_max_bit_size size;
end
in
Log.debug "[initialize_reader] Reader initialized@.";
t

let print_dict fmt = function
| NoDictionary -> Format.fprintf fmt "(none)@."
| Dictionary {contents = d} ->
| `NoDictionary -> Format.fprintf fmt "(none)@."
| `Dictionary {contents = d} ->
StringMap.iter
(fun key elt -> Format.fprintf fmt "%i: %s@." elt key)
d
Expand Down
Loading
Loading