let find_regions ?(max_gap=0) pred a =
if max_gap < 0 then failwith ("max gap must be non-negative but is " ^ (string_of_int max_gap));
let size = Array.length a in
let ans = ref [] in
let add_region curr_index start_index currGap =
if start_index >= 0 then
let finish_index = curr_index - currGap - 1 in
ans := (start_index,finish_index)::!ans
in
let rec loop i start_index currGap =
if i = size
then add_region i start_index currGap
else
(
if pred a.(i) then
if start_index >= 0
then loop (i+1) start_index 0
else loop (i+1) i 0
else
(
if currGap >= max_gap
then (add_region i start_index currGap; loop (i+1) (-1) (currGap+1))
else loop (i+1) start_index (currGap+1)
)
)
in
loop 0 (-1) 0;
Array.of_list (List.rev !ans)