package menhirLib

  1. Overview
  2. Docs

Module MenhirLib.PackedIntArraySource

This module allows packing an array of (small) integers inside a string, using less than one word of memory per array element.

Sourcetype t = int * string

A packed integer array is represented as a pair of an integer k and a string s. The integer k is the number of bits per integer that we use. The string s is just an array of bits, which is read in 8-bit chunks.

The OCaml programming language treats string literals and array literals in slightly different ways: the former are statically allocated, while the latter are dynamically allocated. (This is rather arbitrary.) In the context of Menhir's table-based back-end, where compact, immutable integer arrays are needed, ocaml strings are preferable to ocaml arrays.

Sourceval pack : int array -> t

pack a turns an array of integers into a packed integer array.

Because the sign bit is the most significant bit, the magnitude of any negative number is the word size. In other words, as soon as the array a contains any negative numbers, pack does not achieve any space savings.

Sourceval get1 : string -> int -> int

get1 s i returns the integer stored in the packed array s at index i. It assumes (and does not check) that the array's bit width is 1. The packed array s is just a string; there is no bit width component. In other words, it is get, specialized to the bit width 1.

Sourceval get2 : string -> int -> int

get2 is get, specialized to the bit width 2.

Sourceval get4 : string -> int -> int

get4 is get, specialized to the bit width 4.

Sourceval get8 : string -> int -> int

get8 is get, specialized to the bit width 8.

Sourceval get16 : string -> int -> int

get16 is get, specialized to the bit width 16.

Sourceval get32 : string -> int -> int

get32 is get, specialized to the bit width 32.