mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
KMP with sequences
This commit is contained in:
parent
cd35c46a58
commit
0375c0f721
2 changed files with 25 additions and 0 deletions
19
KMP.ml
19
KMP.ml
|
|
@ -36,6 +36,7 @@ module type STRING = sig
|
||||||
end
|
end
|
||||||
|
|
||||||
type 'a gen = unit -> 'a option
|
type 'a gen = unit -> 'a option
|
||||||
|
type 'a sequence = ('a -> unit) -> unit
|
||||||
|
|
||||||
module type S = sig
|
module type S = sig
|
||||||
type string
|
type string
|
||||||
|
|
@ -57,11 +58,16 @@ module type S = sig
|
||||||
val find_all : pattern:pattern -> string -> int -> int gen
|
val find_all : pattern:pattern -> string -> int -> int gen
|
||||||
(** Generator on all occurrences of the pattern *)
|
(** Generator on all occurrences of the pattern *)
|
||||||
|
|
||||||
|
val seq : pattern:pattern -> string -> int -> int sequence
|
||||||
|
(** iterate on matching positions *)
|
||||||
|
|
||||||
(** {6 One-shot functions that compile the pattern on-the-fly} *)
|
(** {6 One-shot functions that compile the pattern on-the-fly} *)
|
||||||
|
|
||||||
val search' : pattern:string -> string -> int option
|
val search' : pattern:string -> string -> int option
|
||||||
|
|
||||||
val find_all' : pattern:string -> string -> int gen
|
val find_all' : pattern:string -> string -> int gen
|
||||||
|
|
||||||
|
val seq' : pattern:string -> string -> int sequence
|
||||||
end
|
end
|
||||||
|
|
||||||
module Make(Str : STRING) = struct
|
module Make(Str : STRING) = struct
|
||||||
|
|
@ -141,11 +147,24 @@ module Make(Str : STRING) = struct
|
||||||
i := j + pattern.len;
|
i := j + pattern.len;
|
||||||
res
|
res
|
||||||
|
|
||||||
|
let seq ~pattern s i k =
|
||||||
|
let rec iter i =
|
||||||
|
match find ~pattern s i with
|
||||||
|
| None -> ()
|
||||||
|
| Some j ->
|
||||||
|
k j;
|
||||||
|
iter (j+pattern.len)
|
||||||
|
in
|
||||||
|
iter i
|
||||||
|
|
||||||
let search' ~pattern s =
|
let search' ~pattern s =
|
||||||
search ~pattern:(compile pattern) s
|
search ~pattern:(compile pattern) s
|
||||||
|
|
||||||
let find_all' ~pattern s =
|
let find_all' ~pattern s =
|
||||||
find_all ~pattern:(compile pattern) s 0
|
find_all ~pattern:(compile pattern) s 0
|
||||||
|
|
||||||
|
let seq' ~pattern s =
|
||||||
|
seq ~pattern:(compile pattern) s 0
|
||||||
end
|
end
|
||||||
|
|
||||||
module Default = Make(struct
|
module Default = Make(struct
|
||||||
|
|
|
||||||
6
KMP.mli
6
KMP.mli
|
|
@ -36,6 +36,7 @@ module type STRING = sig
|
||||||
end
|
end
|
||||||
|
|
||||||
type 'a gen = unit -> 'a option
|
type 'a gen = unit -> 'a option
|
||||||
|
type 'a sequence = ('a -> unit) -> unit
|
||||||
|
|
||||||
module type S = sig
|
module type S = sig
|
||||||
type string
|
type string
|
||||||
|
|
@ -57,11 +58,16 @@ module type S = sig
|
||||||
val find_all : pattern:pattern -> string -> int -> int gen
|
val find_all : pattern:pattern -> string -> int -> int gen
|
||||||
(** Generator on all occurrences of the pattern *)
|
(** Generator on all occurrences of the pattern *)
|
||||||
|
|
||||||
|
val seq : pattern:pattern -> string -> int -> int sequence
|
||||||
|
(** iterate on matching positions *)
|
||||||
|
|
||||||
(** {6 One-shot functions that compile the pattern on-the-fly} *)
|
(** {6 One-shot functions that compile the pattern on-the-fly} *)
|
||||||
|
|
||||||
val search' : pattern:string -> string -> int option
|
val search' : pattern:string -> string -> int option
|
||||||
|
|
||||||
val find_all' : pattern:string -> string -> int gen
|
val find_all' : pattern:string -> string -> int gen
|
||||||
|
|
||||||
|
val seq' : pattern:string -> string -> int sequence
|
||||||
end
|
end
|
||||||
|
|
||||||
module Make(Str : STRING) : S with type string = Str.t
|
module Make(Str : STRING) : S with type string = Str.t
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue