Skip to content
Permalink
Browse files

Collection.fold: added ?form and ?until params (for perfs).

  • Loading branch information...
sagotch committed Aug 7, 2019
1 parent b5c61ef commit becad141e0098c045287b7cc2422c34f723259f3
Showing with 14 additions and 20 deletions.
  1. +6 −15 lib/api.ml
  2. +2 −1 lib/gwdb.mli/gwdb.mli
  3. +5 −3 lib/gwdb1/gwdb1_internal.ml
  4. +1 −1 test/gwdb.ml
@@ -840,26 +840,17 @@ let print_base_warnings conf base =
let print_all_persons conf base =
let params = get_params conf Mext.parse_all_persons_params in
let filters = get_filters conf in
let from = params.M.All_persons_params.from in
let limit = params.M.All_persons_params.limit in
let (from, limit) =
match (from, limit) with
| (Some f, Some l) -> (Int32.to_int f, Int32.to_int f + Int32.to_int l)
| (Some f, None) -> (Int32.to_int f, nb_of_persons base)
let (from, until) =
match (params.M.All_persons_params.from, params.M.All_persons_params.limit) with
| (Some f, Some l) -> (Int32.to_int f, min (nb_of_persons base - 1) (Int32.to_int f + Int32.to_int l))
| (Some f, None) -> (Int32.to_int f, nb_of_persons base - 1)
| (None, Some l) -> (0, Int32.to_int l)
| (None, None) -> (0, nb_of_persons base)
| (None, None) -> (0, nb_of_persons base - 1)
in
let () = Perso.build_sosa_ht conf base in
let len = limit - from in
let list =
Gwdb.Collection.fold_until
(fun (_, n) -> n < len)
begin fun ((list, n) as acc) i ->
if n < from then acc
else (i :: list, n + 1)
end ([], 0) (Gwdb.ipers base)
Gwdb.Collection.fold ~from ~until (fun acc i -> i :: acc) [] (Gwdb.persons base)
in
let list = Gwdb.poi_batch base (List.rev @@ fst list) in
let list = List.filter (apply_filters_p conf filters Perso.get_sosa_person) list in
let data = conv_data_list_person conf base filters list in
print_result conf data
@@ -261,8 +261,9 @@ module Collection : sig
[fn] first argument is the result computed so far as we traverse the
collection, and second element is the current element being combined.
[acc] is the starting combined value.
Start at [from]-nth and finish with [until]-nth element (included).
*)
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
val fold : ?from:int -> ?until:int -> ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a

(** [fold_until continue fn acc c]
Same as [fold fn acc c], but computation stops as soon as [continue]
@@ -413,12 +413,14 @@ module Collection = struct
let iteri fn { get ; length } =
for i = 0 to length - 1 do match get i with Some x -> fn i x | None -> () done

let fold fn acc { get ; length } =
let fold ?from ?until fn acc { get ; length } =
let from = match from with Some x -> x | None -> 0 in
let until = match until with Some x -> x + 1 | None -> length in
let rec loop acc i =
if i = length then acc
if i = until then acc
else loop (match get i with Some x -> fn acc x | None -> acc) (i + 1)
in
loop acc 0
loop acc from

let fold_until continue fn acc { get ; length } =
let rec loop acc i =
@@ -151,7 +151,7 @@ module Collection = struct
let map _ = assert false
let iter _ = assert false
let iteri _ = assert false
let fold _ = assert false
let fold ?from:_ ?until:_ _ = assert false
let fold_until _ = assert false
let iterator _ = assert false
end

0 comments on commit becad14

Please sign in to comment.
You can’t perform that action at this time.