From 0375c0f721d7497113462152bcc155fee4d7bc2c Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sun, 11 May 2014 18:52:05 +0200 Subject: [PATCH] KMP with sequences --- KMP.ml | 19 +++++++++++++++++++ KMP.mli | 6 ++++++ 2 files changed, 25 insertions(+) diff --git a/KMP.ml b/KMP.ml index dafecace..a49ce282 100644 --- a/KMP.ml +++ b/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 diff --git a/KMP.mli b/KMP.mli index a2cc09f6..f431137a 100644 --- a/KMP.mli +++ b/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