update readme and CCParse to remove uses of CCVector

This commit is contained in:
Simon Cruanes 2023-06-06 13:30:22 -04:00
parent 8388e17ed9
commit 8d5b012111
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 28 additions and 33 deletions

View file

@ -66,7 +66,7 @@ changes in this release.
2. Another large change is the removal (at last!) of functions deprecated 2. Another large change is the removal (at last!) of functions deprecated
in 2.8, related to the spread of `Seq.t` as the standard iterator type. in 2.8, related to the spread of `Seq.t` as the standard iterator type.
Functions like `CCVector.of_seq` now operate on this standard `Seq.t` type, Functions like `CCVec.of_seq` now operate on this standard `Seq.t` type,
and old-time iteration based on [iter](https://github.com/c-cube/iter) and old-time iteration based on [iter](https://github.com/c-cube/iter)
is now named `of_iter`, `to_iter`, etc. is now named `of_iter`, `to_iter`, etc.
@ -439,18 +439,14 @@ map =
- : string option = Some "33" - : string option = Some "33"
``` ```
### New types: `CCVector`, `CCHeap`, `CCResult`, `CCSexp`, `CCByte_buffer` ### New types: `CCVec`, `CCHeap`, `CCResult`, `CCSexp`, `CCByte_buffer`
Containers also contains (!) a few datatypes that are not from the standard Containers also contains (!) a few datatypes that are not from the standard
library but that are useful in a lot of situations: library but that are useful in a lot of situations:
- `CCVector`: - `CCVec`:
A resizable array, with a mutability parameter. A value of type A simple and efficient dynamic array.
`('a, CCVector.ro) CCVector.t` is an immutable vector of values of type `'a`, This was called `CCVector` but the old module is deprecated now.
whereas a `('a, CCVector.rw) CCVector.t` is a mutable vector that
can be modified. This way, vectors can be used in a quite functional
way, using operations such as `map` or `flat_map`, or in a more
imperative way.
- `CCHeap`: - `CCHeap`:
A priority queue (currently, leftist heaps) functorized over A priority queue (currently, leftist heaps) functorized over
a module `sig val t val leq : t -> t -> bool` that provides a type `t` a module `sig val t val leq : t -> t -> bool` that provides a type `t`
@ -474,43 +470,42 @@ library but that are useful in a lot of situations:
Now for a few examples: Now for a few examples:
```ocaml ```ocaml
# (* create a new empty vector. It is mutable, for otherwise it would # (* create a new empty vector. *)
not be very useful. *) CCVec.create;;
CCVector.create;; - : unit -> 'a CCVec.t = <fun>
- : unit -> ('a, CCVector.rw) CCVector.t = <fun>
# (* init, similar to Array.init, can be used to produce a # (* init, similar to Array.init, can be used to produce a new vector
vector that is mutable OR immutable (see the 'mut parameter?) *) of given size *)
CCVector.init ;; CCVec.init ;;
- : int -> (int -> 'a) -> ('a, 'mut) CCVector.t = <fun> - : int -> (int -> 'a) -> 'a CCVec.t = <fun>
``` ```
```ocaml non-deterministic=output ```ocaml non-deterministic=output
# (* use the infix (--) operator for creating a range. Notice # (* use the infix (--) operator for creating a range. Notice
that v is a vector of integer but its mutability is not that v is a vector of integer but its mutability is not
decided yet. *) decided yet. *)
let v = CCVector.(1 -- 10);; let v = CCVec.(1 -- 10);;
val v : (int, '_a) CCVector.t = <abstr> val v : (int, '_a) CCVector.t = <abstr>
``` ```
```ocaml ```ocaml
# Format.printf "v = @[%a@]@." (CCVector.pp CCInt.pp) v;; # Format.printf "v = @[%a@]@." (CCVec.pp CCInt.pp) v;;
v = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 v = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- : unit = () - : unit = ()
# CCVector.push v 42;; # CCVec.push v 42;;
- : unit = () - : unit = ()
# v (* now v is a mutable vector *);; # v (* now v is a mutable vector *);;
- : (int, CCVector.rw) CCVector.t = <abstr> - : int CCVec.t = <abstr>
# (* functional combinators! *) # (* functional combinators! *)
let v2 : _ CCVector.ro_vector = v let v2 : _ CCVec.t = v
|> CCVector.map (fun x-> x+1) |> CCVec.map (fun x-> x+1)
|> CCVector.filter (fun x-> x mod 2=0) |> CCVec.filter (fun x-> x mod 2=0)
|> CCVector.rev ;; |> CCVec.rev ;;
val v2 : int CCVector.ro_vector = <abstr> val v2 : int CCVec.t = <abstr>
# Format.printf "v2 = @[%a@]@." (CCVector.pp CCInt.pp) v2;; # Format.printf "v2 = @[%a@]@." (CCVec.pp CCInt.pp) v2;;
v2 = 10, 8, 6, 4, 2 v2 = 10, 8, 6, 4, 2
- : unit = () - : unit = ()
``` ```
@ -521,7 +516,7 @@ module IntHeap = CCHeap.Make(struct type t = int let leq = (<=) end);;
``` ```
```ocaml ```ocaml
# let h = v2 |> CCVector.to_iter |> IntHeap.of_iter ;; # let h = v2 |> CCVec.to_iter |> IntHeap.of_iter ;;
val h : IntHeap.t = <abstr> val h : IntHeap.t = <abstr>
# (* We can print the content of h # (* We can print the content of h

View file

@ -30,17 +30,17 @@ module Position = struct
type t = position type t = position
let compute_line_offsets_ (s : string) : int array = let compute_line_offsets_ (s : string) : int array =
let lines = CCVector.create () in let lines = CCVec.create () in
let i = ref 0 in let i = ref 0 in
CCVector.push lines 0; CCVec.push lines 0;
while !i < String.length s do while !i < String.length s do
match String.index_from s !i '\n' with match String.index_from s !i '\n' with
| exception Not_found -> i := String.length s | exception Not_found -> i := String.length s
| j -> | j ->
CCVector.push lines j; CCVec.push lines j;
i := j + 1 i := j + 1
done; done;
CCVector.to_array lines CCVec.to_array lines
let line_offsets_ cs = let line_offsets_ cs =
match cs.line_offsets with match cs.line_offsets with
@ -114,7 +114,7 @@ type state = {
where: where:
[type global = { [type global = {
mutable memo: Memo_state.t option; mutable memo: Memo_state.t option;
line_offsets: int CCVector.vector; line_offsets: int CCVec.vector;
} }
with line_offsets used to cache the offset where each line begins, with line_offsets used to cache the offset where each line begins,