diff options
author | Sébastien Dailly <sebastien@dailly.me> | 2022-01-28 14:44:57 +0100 |
---|---|---|
committer | Sébastien Dailly <sebastien@dailly.me> | 2022-02-07 16:22:43 +0100 |
commit | 741f88ab405995003eb6e9f301d3b065c1e84a4a (patch) | |
tree | 08ba9ec2247c56680611d542ed9b096c5f1a083b /motus/lib/criteria.ml | |
parent | c0c82a7bfe8300b1bd50fee11074837ff32d3da0 (diff) |
Added a motus solver
Diffstat (limited to 'motus/lib/criteria.ml')
-rw-r--r-- | motus/lib/criteria.ml | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/motus/lib/criteria.ml b/motus/lib/criteria.ml new file mode 100644 index 0000000..61bb539 --- /dev/null +++ b/motus/lib/criteria.ml @@ -0,0 +1,42 @@ +open StdLabels + +type t = + | Lenght of int + | Contain of char * int option + | NotContain of char * int option + +(** Return true if the word match the given filter *) +let check_filter : string -> t -> bool = + fun word f -> + match f with + | Lenght l -> l = String.length word + | Contain (c, pos) -> + ( match pos with + | None -> String.contains word c + | Some i -> Char.equal c (String.get word i) ) + | NotContain (c, pos) -> + ( match pos with + | None -> not (String.contains word c) + | Some i -> not (Char.equal c (String.get word i)) ) + + +let is_valid : t -> t -> bool = + fun t1 t2 -> + match (t1, t2) with + | Lenght _, Lenght _ -> false + | Contain (c1, _), NotContain (c2, None) -> not (Char.equal c1 c2) + | NotContain (c1, None), Contain (c2, _) -> not (Char.equal c1 c2) + | Contain (c1, Some i1), Contain (c2, Some i2) -> Char.equal c1 c2 || i1 <> i2 + | _ -> true + + +(** Add a new filter in the list if it is compatible with the existing ones *) +let add : t -> t list -> t list = + fun t filters -> + match List.for_all ~f:(is_valid t) filters with + | true -> t :: filters + | false -> filters + + +let merge_lists : init:t list -> t list -> t list = + fun ~init news -> List.fold_left ~f:(fun acc t -> add t acc) ~init news |