mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
formatting, qtests
This commit is contained in:
parent
39cac7bc08
commit
51b2828af3
2 changed files with 25 additions and 21 deletions
|
|
@ -29,8 +29,10 @@ module Array = struct
|
||||||
type t
|
type t
|
||||||
|
|
||||||
val empty : t
|
val empty : t
|
||||||
val make: int -> elt -> t
|
|
||||||
val length: t -> int
|
val make: int -> elt -> t
|
||||||
|
|
||||||
|
val length: t -> int
|
||||||
|
|
||||||
val get: t -> int -> elt
|
val get: t -> int -> elt
|
||||||
|
|
||||||
|
|
@ -39,12 +41,13 @@ module Array = struct
|
||||||
val sub: t -> int -> int -> t
|
val sub: t -> int -> int -> t
|
||||||
|
|
||||||
val copy : t -> t
|
val copy : t -> t
|
||||||
val blit : t -> int -> t -> int -> int -> unit
|
|
||||||
|
val blit : t -> int -> t -> int -> int -> unit
|
||||||
|
|
||||||
val iter : (elt -> unit) -> t -> unit
|
val iter : (elt -> unit) -> t -> unit
|
||||||
end
|
end
|
||||||
|
|
||||||
module ByteArray :
|
module ByteArray :
|
||||||
S with type elt = char and type t = bytes = struct
|
S with type elt = char and type t = bytes = struct
|
||||||
type elt = char
|
type elt = char
|
||||||
include Bytes
|
include Bytes
|
||||||
|
|
@ -200,6 +203,14 @@ struct
|
||||||
let copy b =
|
let copy b =
|
||||||
{ b with buf=Array.copy b.buf; }
|
{ b with buf=Array.copy b.buf; }
|
||||||
|
|
||||||
|
(*$T
|
||||||
|
let b = ByteBuffer.create 3 in \
|
||||||
|
let s = Bytes.of_string "hello world" in \
|
||||||
|
ByteBuffer.blit_from b s 0 (Bytes.length s); \
|
||||||
|
let b' = ByteBuffer.copy b in \
|
||||||
|
try ByteBuffer.iteri b (fun i c -> if ByteBuffer.get_front b' i <> c then raise Exit); true with Exit -> false
|
||||||
|
*)
|
||||||
|
|
||||||
|
|
||||||
let capacity b = Array.length b.buf
|
let capacity b = Array.length b.buf
|
||||||
|
|
||||||
|
|
@ -300,15 +311,7 @@ struct
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
let add b s = blit_from b s 0 (Array.length s)
|
let clear b =
|
||||||
|
|
||||||
(*$Q
|
|
||||||
(Q.pair Q.printable_string Q.printable_string) (fun (s,s') -> \
|
|
||||||
let b = create 24 in add b s; add_string b s'; \
|
|
||||||
Array.length s + String.length s' = length b)
|
|
||||||
*)
|
|
||||||
|
|
||||||
let clear b =
|
|
||||||
b.stop <- 0;
|
b.stop <- 0;
|
||||||
b.start <- 0;
|
b.start <- 0;
|
||||||
()
|
()
|
||||||
|
|
@ -358,10 +361,11 @@ struct
|
||||||
|
|
||||||
(*$Q
|
(*$Q
|
||||||
(Q.pair Q.printable_string Q.printable_string) (fun (s,s') -> \
|
(Q.pair Q.printable_string Q.printable_string) (fun (s,s') -> \
|
||||||
let b = create 24 in add_string b s; add_string b s'; \
|
(let b = ByteBuffer.create 24 in ByteBuffer.blit_from b s 0 (Bytes.length s);
|
||||||
add_string b "hello world"; (* big enough *) \
|
ByteBuffer.blit_from b s' 0 (Bytes.length s'); \
|
||||||
let l = length b in let l' = l/2 in skip b l'; \
|
ByteBuffer.blit_from b "hello world" 0 (Bytes.length "hello word"); (* big enough *) \
|
||||||
length b + l' = l)
|
let l = ByteBuffer.length b in let l' = l/2 in ByteBuffer.skip b l'; \
|
||||||
|
ByteBuffer.length b + l' = l))
|
||||||
*)
|
*)
|
||||||
|
|
||||||
let iteri b f =
|
let iteri b f =
|
||||||
|
|
@ -411,7 +415,7 @@ struct
|
||||||
build ((get_front b i)::l) (i-1) in
|
build ((get_front b i)::l) (i-1) in
|
||||||
build [] (len-1)
|
build [] (len-1)
|
||||||
|
|
||||||
let push_back b e = add b (Array.make 1 e)
|
let push_back b e = blit_from b (Array.make 1 e) 0 1
|
||||||
|
|
||||||
let peek_front b = if is_empty b then
|
let peek_front b = if is_empty b then
|
||||||
raise Empty else Array.get b.buf b.start
|
raise Empty else Array.get b.buf b.start
|
||||||
|
|
@ -421,7 +425,7 @@ struct
|
||||||
(if b.stop = 0 then capacity b - 1 else b.stop-1)
|
(if b.stop = 0 then capacity b - 1 else b.stop-1)
|
||||||
end
|
end
|
||||||
|
|
||||||
module Bytes = Make_array(Array.ByteArray)
|
module ByteBuffer = Make_array(Array.ByteArray)
|
||||||
|
|
||||||
module Make(Elt:sig type t end) = Make_array(Array.Make(Elt))
|
module Make(Elt:sig type t end) = Make_array(Array.Make(Elt))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ module Array : sig
|
||||||
end
|
end
|
||||||
|
|
||||||
module ByteArray :
|
module ByteArray :
|
||||||
S with type elt = char and type t = bytes
|
S with type elt = char and type t = bytes
|
||||||
|
|
||||||
module FloatArray :
|
module FloatArray :
|
||||||
S with type elt = float and type t = float array
|
S with type elt = float and type t = float array
|
||||||
|
|
@ -168,7 +168,7 @@ end
|
||||||
module Make_array : functor (Array:Array.S) -> S with module Array = Array
|
module Make_array : functor (Array:Array.S) -> S with module Array = Array
|
||||||
|
|
||||||
(** An efficient byte based ring buffer *)
|
(** An efficient byte based ring buffer *)
|
||||||
module Bytes : S with module Array = Array.ByteArray
|
module ByteBuffer : S with module Array = Array.ByteArray
|
||||||
|
|
||||||
(** Makes a ring buffer module given the element type *)
|
(** Makes a ring buffer module given the element type *)
|
||||||
module Make: functor(Elt:sig type t end) -> S with module Array = Array.Make(Elt)
|
module Make: functor(Elt:sig type t end) -> S with module Array = Array.Make(Elt)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue