-
Notifications
You must be signed in to change notification settings - Fork 0
/
dranges.ml
47 lines (40 loc) · 1.48 KB
/
dranges.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
open Common
let minMax1 (oldMin, oldMax) x =
let newMin = min oldMin x in
let newMax = max oldMax x in
(newMin, newMax)
let minMax2 (oldMin, oldMax) (x,y) =
let newMin = min oldMin x in
let newMax = max oldMax y in
(newMin, newMax)
(* find the day range when each user exists in dreps *)
let dayRanges dreps =
let doDays _ doReps =
if H.is_empty doReps then None
else
let days = H.keys doReps in
let aDay = match Enum.peek days with
| Some day -> day
| _ -> failwith "should have some days here" in
let dayday = (aDay,aDay) in
let ranges = Enum.fold (fun res elem -> minMax1 res elem) dayday days in
Some ranges
in
H.filter_map doDays dreps
let startsRange dreps dments =
let dranges = hashMergeWith minMax2 (dayRanges dreps) (dayRanges dments) in
let dstarts = H.fold (fun user (d1,d2) res ->
let users = H.find_default res d1 [] in H.replace res d1 (user::users); res)
dranges (H.create 100000) in
let firstLast = H.fold (fun _ v res -> minMax2 v res) dranges (dranges |> hashFirst |> snd) in
(dstarts,firstLast)
let startsArray dreps dments =
let (dstarts,(firstDay,lastDay)) = startsRange dreps dments in
let a : starts = Array.create (succ lastDay) [] in
H.iter begin fun day users ->
a.(day) <- users end dstarts;
a
let startsArrayTotalUsers dstarts =
A.fold_left begin fun res users ->
res + L.length users
end 0 dstarts