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
|
||||
|
||||
type 'a gen = unit -> 'a option
|
||||
type 'a sequence = ('a -> unit) -> unit
|
||||
|
||||
module type S = sig
|
||||
type string
|
||||
|
|
@ -56,12 +57,17 @@ module type S = sig
|
|||
|
||||
val find_all : pattern:pattern -> string -> int -> int gen
|
||||
(** 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} *)
|
||||
|
||||
val search' : pattern:string -> string -> int option
|
||||
|
||||
val find_all' : pattern:string -> string -> int gen
|
||||
|
||||
val seq' : pattern:string -> string -> int sequence
|
||||
end
|
||||
|
||||
module Make(Str : STRING) = struct
|
||||
|
|
@ -141,11 +147,24 @@ module Make(Str : STRING) = struct
|
|||
i := j + pattern.len;
|
||||
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 =
|
||||
search ~pattern:(compile pattern) s
|
||||
|
||||
let find_all' ~pattern s =
|
||||
find_all ~pattern:(compile pattern) s 0
|
||||
|
||||
let seq' ~pattern s =
|
||||
seq ~pattern:(compile pattern) s 0
|
||||
end
|
||||
|
||||
module Default = Make(struct
|
||||
|
|
|
|||
6
KMP.mli
6
KMP.mli
|
|
@ -36,6 +36,7 @@ module type STRING = sig
|
|||
end
|
||||
|
||||
type 'a gen = unit -> 'a option
|
||||
type 'a sequence = ('a -> unit) -> unit
|
||||
|
||||
module type S = sig
|
||||
type string
|
||||
|
|
@ -56,12 +57,17 @@ module type S = sig
|
|||
|
||||
val find_all : pattern:pattern -> string -> int -> int gen
|
||||
(** 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} *)
|
||||
|
||||
val search' : pattern:string -> string -> int option
|
||||
|
||||
val find_all' : pattern:string -> string -> int gen
|
||||
|
||||
val seq' : pattern:string -> string -> int sequence
|
||||
end
|
||||
|
||||
module Make(Str : STRING) : S with type string = Str.t
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue