mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
specialized primitive module arrays, functorized version for remainder
This commit is contained in:
parent
c7607f8ce7
commit
847286597b
2 changed files with 129 additions and 38 deletions
|
|
@ -22,10 +22,13 @@
|
||||||
|
|
||||||
(** Polymorphic Circular Buffer for IO *)
|
(** Polymorphic Circular Buffer for IO *)
|
||||||
|
|
||||||
module type ARRAY = sig
|
module Array = struct
|
||||||
|
|
||||||
|
module type S = sig
|
||||||
type elt
|
type elt
|
||||||
type t
|
type t
|
||||||
|
|
||||||
|
val empty : t
|
||||||
val make: int -> elt -> t
|
val make: int -> elt -> t
|
||||||
val length: t -> int
|
val length: t -> int
|
||||||
|
|
||||||
|
|
@ -34,17 +37,85 @@ module type ARRAY = sig
|
||||||
val set: t -> int -> elt -> unit
|
val set: t -> int -> elt -> unit
|
||||||
|
|
||||||
val sub: t -> int -> int -> t
|
val sub: t -> int -> int -> t
|
||||||
val max_length: int
|
|
||||||
|
|
||||||
val copy : t -> t
|
val copy : t -> t
|
||||||
val of_list : elt list -> t
|
|
||||||
val to_list : t -> elt list
|
|
||||||
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
|
||||||
|
|
||||||
|
module ByteArray :
|
||||||
|
S with type elt = char and type t = bytes = struct
|
||||||
|
type elt = char
|
||||||
|
include Bytes
|
||||||
|
end
|
||||||
|
|
||||||
|
module FloatArray :
|
||||||
|
S with type elt = float and type t = float array = struct
|
||||||
|
type t = float array
|
||||||
|
type elt = float
|
||||||
|
let make = Array.make
|
||||||
|
let length = Array.length
|
||||||
|
let get = Array.get
|
||||||
|
let set = Array.set
|
||||||
|
let copy = Array.copy
|
||||||
|
let blit = Array.blit
|
||||||
|
let iter = Array.iter
|
||||||
|
let sub = Array.sub
|
||||||
|
let empty = Array.of_list []
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
module IntArray :
|
||||||
|
S with type elt = int and type t = int array = struct
|
||||||
|
type t = int array
|
||||||
|
type elt = int
|
||||||
|
let make = Array.make
|
||||||
|
let length = Array.length
|
||||||
|
let get = Array.get
|
||||||
|
let set = Array.set
|
||||||
|
let copy = Array.copy
|
||||||
|
let blit = Array.blit
|
||||||
|
let iter = Array.iter
|
||||||
|
let sub = Array.sub
|
||||||
|
let empty = Array.of_list []
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
module BoolArray :
|
||||||
|
S with type elt = bool and type t = bool array = struct
|
||||||
|
type t = bool array
|
||||||
|
type elt = bool
|
||||||
|
let make = Array.make
|
||||||
|
let length = Array.length
|
||||||
|
let get = Array.get
|
||||||
|
let set = Array.set
|
||||||
|
let copy = Array.copy
|
||||||
|
let blit = Array.blit
|
||||||
|
let iter = Array.iter
|
||||||
|
let sub = Array.sub
|
||||||
|
let empty = Array.of_list []
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
module Make(Elt:sig type t end) :
|
||||||
|
S with type elt = Elt.t and type t = Elt.t array = struct
|
||||||
|
type elt = Elt.t
|
||||||
|
type t = Elt.t array
|
||||||
|
let make = Array.make
|
||||||
|
let length = Array.length
|
||||||
|
let get = Array.get
|
||||||
|
let set = Array.set
|
||||||
|
let copy = Array.copy
|
||||||
|
let blit = Array.blit
|
||||||
|
let iter = Array.iter
|
||||||
|
let sub = Array.sub
|
||||||
|
let empty = Array.of_list []
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
module Make(Array:ARRAY) =
|
module Make(Array:Array.S) =
|
||||||
struct
|
struct
|
||||||
|
|
||||||
type t = {
|
type t = {
|
||||||
|
|
@ -62,7 +133,7 @@ struct
|
||||||
stop=0;
|
stop=0;
|
||||||
bounded;
|
bounded;
|
||||||
size;
|
size;
|
||||||
buf = Array.of_list []
|
buf = Array.empty
|
||||||
}
|
}
|
||||||
|
|
||||||
let copy b =
|
let copy b =
|
||||||
|
|
@ -183,7 +254,7 @@ struct
|
||||||
|
|
||||||
let reset b =
|
let reset b =
|
||||||
clear b;
|
clear b;
|
||||||
b.buf <- Array.of_list []
|
b.buf <- Array.empty
|
||||||
|
|
||||||
let is_empty b = b.start = b.stop
|
let is_empty b = b.start = b.stop
|
||||||
|
|
||||||
|
|
@ -259,13 +330,13 @@ struct
|
||||||
else b.buf.(i - len_end)
|
else b.buf.(i - len_end)
|
||||||
|
|
||||||
let to_list b =
|
let to_list b =
|
||||||
if (b.stop >= b.start)
|
let len = length b in
|
||||||
then Array.to_list (Array.sub b.buf b.start (b.stop-b.start))
|
let rec build l i =
|
||||||
else List.append
|
if i < 0 then l else
|
||||||
(Array.to_list (Array.sub b.buf b.start (Array.length b.buf - b.start)))
|
build ((get b i)::l) (i-1) in
|
||||||
(Array.to_list (Array.sub b.buf 0 b.stop))
|
build [] (len-1)
|
||||||
|
|
||||||
let push_back b e = add b (Array.of_list [e])
|
let push_back b e = add b (Array.make 1 e)
|
||||||
|
|
||||||
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
|
||||||
|
|
@ -274,3 +345,4 @@ struct
|
||||||
raise Empty else Array.get b.buf
|
raise Empty else Array.get b.buf
|
||||||
(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
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,10 +20,13 @@
|
||||||
|
|
||||||
(** Circular Polymorphic Buffer for IO *)
|
(** Circular Polymorphic Buffer for IO *)
|
||||||
|
|
||||||
module type ARRAY = sig
|
module Array : sig
|
||||||
|
|
||||||
|
module type S = sig
|
||||||
type elt
|
type elt
|
||||||
type t
|
type t
|
||||||
|
|
||||||
|
val empty : t
|
||||||
val make: int -> elt -> t
|
val make: int -> elt -> t
|
||||||
val length: t -> int
|
val length: t -> int
|
||||||
|
|
||||||
|
|
@ -32,18 +35,34 @@ module type ARRAY = sig
|
||||||
val set: t -> int -> elt -> unit
|
val set: t -> int -> elt -> unit
|
||||||
|
|
||||||
val sub: t -> int -> int -> t
|
val sub: t -> int -> int -> t
|
||||||
val max_length: int
|
|
||||||
|
|
||||||
val copy : t -> t
|
val copy : t -> t
|
||||||
val of_list : elt list -> t
|
|
||||||
val to_list : t -> elt list
|
|
||||||
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
|
||||||
|
|
||||||
|
module ByteArray :
|
||||||
|
S with type elt = char and type t = bytes
|
||||||
|
|
||||||
|
module FloatArray :
|
||||||
|
S with type elt = float and type t = float array
|
||||||
|
|
||||||
|
|
||||||
|
module IntArray :
|
||||||
|
S with type elt = int and type t = int array
|
||||||
|
|
||||||
|
module BoolArray :
|
||||||
|
S with type elt = bool and type t = bool array
|
||||||
|
|
||||||
|
module Make :
|
||||||
|
functor (Elt:sig type t end) ->
|
||||||
|
S with type elt = Elt.t and type t = Elt.t array
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
module Make : functor (Array:ARRAY) ->
|
module Make : functor (Array:Array.S) ->
|
||||||
sig
|
sig
|
||||||
|
|
||||||
type t = private {
|
type t = private {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue