Source file sched_req_data_unit_skeleton.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
open Int64_utils
type ('task_seg_related_data, 'time) fixed = {
task_seg_related_data : 'task_seg_related_data;
start : 'time;
}
type ('task_seg_related_data, 'time_slot) shift = {
task_seg_related_data_list : 'task_seg_related_data list;
time_slots : 'time_slot list;
incre : int64;
}
type split_count =
| Max_split of int64
| Exact_split of int64
type ('task_seg_related_data, 'time_slot) split_and_shift = {
task_seg_related_data : 'task_seg_related_data;
time_slots : 'time_slot list;
incre : int64;
split_count : split_count;
min_seg_size : int64;
max_seg_size : int64 option;
}
type ('task_seg_related_data, 'time_slot) split_even = {
task_seg_related_data : 'task_seg_related_data;
time_slots : 'time_slot list;
buckets : 'time_slot list;
incre : int64;
}
type ('task_seg_related_data, 'time_slot) time_share = {
task_seg_related_data_list : 'task_seg_related_data list;
time_slots : 'time_slot list;
interval_size : int64;
}
type ('task_seg_related_data, 'time, 'time_slot) push_toward = {
task_seg_related_data : 'task_seg_related_data;
target : 'time;
time_slots : 'time_slot list;
incre : int64;
}
type ('task_seg_related_data, 'time, 'time_slot) t =
| Fixed of ('task_seg_related_data, 'time) fixed
| Shift of ('task_seg_related_data, 'time_slot) shift
| Split_and_shift of ('task_seg_related_data, 'time_slot) split_and_shift
| Split_even of ('task_seg_related_data, 'time_slot) split_even
| Time_share of ('task_seg_related_data, 'time_slot) time_share
| Push_toward of ('task_seg_related_data, 'time, 'time_slot) push_toward
let shift_time ~offset (t : ('a, int64, Time_slot.t) t) :
('a, int64, Time_slot.t) t =
match t with
| Fixed { task_seg_related_data; start } ->
Fixed { task_seg_related_data; start = start +^ offset }
| Shift x ->
Shift { x with time_slots = Time_slots.shift_list ~offset x.time_slots }
| Split_and_shift x ->
Split_and_shift
{ x with time_slots = Time_slots.shift_list ~offset x.time_slots }
| Split_even x ->
Split_even
{
x with
time_slots = Time_slots.shift_list ~offset x.time_slots;
buckets = Time_slots.shift_list ~offset x.buckets;
}
| Time_share x ->
Time_share
{ x with time_slots = Time_slots.shift_list ~offset x.time_slots }
| Push_toward x ->
Push_toward
{
x with
target = x.target +^ offset;
time_slots = Time_slots.shift_list ~offset x.time_slots;
}
let shift_time_list ~offset (ts : ('a, int64, Time_slot.t) t list) :
('a, int64, Time_slot.t) t list =
List.map (shift_time ~offset) ts
let map (type a b c d e f) ~(f_data : a -> d) ~(f_time : b -> e)
~(f_time_slot : c -> f) (t : (a, b, c) t) : (d, e, f) t =
match t with
| Fixed { task_seg_related_data; start } ->
Fixed
{
task_seg_related_data = f_data task_seg_related_data;
start = f_time start;
}
| Shift x ->
Shift
{
x with
task_seg_related_data_list =
List.map f_data x.task_seg_related_data_list;
time_slots = List.map f_time_slot x.time_slots;
}
| Split_and_shift x ->
Split_and_shift
{
x with
task_seg_related_data = f_data x.task_seg_related_data;
time_slots = List.map f_time_slot x.time_slots;
}
| Split_even x ->
Split_even
{
x with
task_seg_related_data = f_data x.task_seg_related_data;
time_slots = List.map f_time_slot x.time_slots;
buckets = List.map f_time_slot x.buckets;
}
| Time_share x ->
Time_share
{
x with
task_seg_related_data_list =
List.map f_data x.task_seg_related_data_list;
time_slots = List.map f_time_slot x.time_slots;
}
| Push_toward x ->
Push_toward
{
x with
task_seg_related_data = f_data x.task_seg_related_data;
target = f_time x.target;
time_slots = List.map f_time_slot x.time_slots;
}
let map_list ~f_data ~f_time ~f_time_slot ts =
List.map (map ~f_data ~f_time ~f_time_slot) ts
module Check = struct
let check (type a b c) ~(f_data : a -> bool) ~(f_time : b -> bool)
~(f_time_slot : c -> bool) (t : (a, b, c) t) : bool =
match t with
| Fixed { task_seg_related_data; start } ->
f_data task_seg_related_data && f_time start
| Shift { task_seg_related_data_list; time_slots; incre } ->
List.for_all f_data task_seg_related_data_list
&& List.for_all f_time_slot time_slots
&& incre > 0L
| Split_and_shift
{
task_seg_related_data;
time_slots;
incre;
split_count;
min_seg_size;
max_seg_size;
} -> (
f_data task_seg_related_data
&& List.for_all f_time_slot time_slots
&& incre > 0L
&& ( match split_count with
| Max_split x -> x >= 0L
| Exact_split x -> x >= 0L )
&& min_seg_size > 0L
&& match max_seg_size with None -> true | Some x -> x >= min_seg_size )
| Split_even { task_seg_related_data; time_slots; buckets; incre } ->
f_data task_seg_related_data
&& List.for_all f_time_slot time_slots
&& List.for_all f_time_slot buckets
&& incre > 0L
| Time_share { task_seg_related_data_list; time_slots; interval_size } ->
List.for_all f_data task_seg_related_data_list
&& List.for_all f_time_slot time_slots
&& interval_size > 0L
| Push_toward { task_seg_related_data; target; time_slots; incre } ->
f_data task_seg_related_data
&& f_time target
&& List.for_all f_time_slot time_slots
&& incre > 0L
end
let get_inner_data (type a b c) (t : (a, b, c) t) : a list =
match t with
| Fixed { task_seg_related_data; _ } -> [ task_seg_related_data ]
| Shift { task_seg_related_data_list; _ } -> task_seg_related_data_list
| Split_and_shift { task_seg_related_data; _ } -> [ task_seg_related_data ]
| Split_even { task_seg_related_data } -> [ task_seg_related_data ]
| Time_share { task_seg_related_data_list; _ } -> task_seg_related_data_list
| Push_toward { task_seg_related_data; _ } -> [ task_seg_related_data ]
let contains_matching_inner_data (type a b c) (f : a -> bool) (t : (a, b, c) t)
: bool =
List.exists f (get_inner_data t)
let list_contains_matching_inner_data (type a b c) (f : a -> bool)
(ts : (a, b, c) t list) : bool =
List.exists (fun t -> contains_matching_inner_data f t) ts
let remove_data_units_with_matching_inner_data (type a b c) (f : a -> bool)
(ts : (a, b, c) t list) : (a, b, c) t list =
List.filter (fun t -> not (contains_matching_inner_data f t)) ts
module To_string = struct
let debug_string_of_sched_req_data_unit_skeleton (type a b c)
?(indent_level = 0) ?(buffer = Buffer.create 4096)
~(string_of_data : a -> string) ~(string_of_time : b -> string)
~(string_of_time_slot : c -> string) (t : (a, b, c) t) =
( match t with
| Fixed { task_seg_related_data; start } ->
Debug_print.bprintf ~indent_level buffer "fixed\n";
Debug_print.bprintf ~indent_level:(indent_level + 1) buffer
"data = %s\n"
(string_of_data task_seg_related_data);
Debug_print.bprintf ~indent_level:(indent_level + 1) buffer
"start = %s\n" (string_of_time start)
| Shift x ->
Debug_print.bprintf ~indent_level buffer "shift\n";
Debug_print.bprintf ~indent_level:(indent_level + 1) buffer "data\n";
List.iter
(fun x ->
Debug_print.bprintf ~indent_level:(indent_level + 2) buffer "%s\n"
(string_of_data x))
x.task_seg_related_data_list;
Debug_print.bprintf ~indent_level:(indent_level + 1) buffer
"time slots\n";
List.iter
(fun x ->
Debug_print.bprintf ~indent_level:(indent_level + 2) buffer "%s\n"
(string_of_time_slot x))
x.time_slots
| Split_and_shift x ->
Debug_print.bprintf ~indent_level buffer "split and shift\n";
Debug_print.bprintf ~indent_level:(indent_level + 1) buffer
"data = %s\n"
(string_of_data x.task_seg_related_data);
Debug_print.bprintf ~indent_level:(indent_level + 1) buffer
"time slots\n";
List.iter
(fun x ->
Debug_print.bprintf ~indent_level:(indent_level + 2) buffer "%s\n"
(string_of_time_slot x))
x.time_slots
| Split_even x ->
Debug_print.bprintf ~indent_level buffer "split even\n";
Debug_print.bprintf ~indent_level:(indent_level + 1) buffer
"data = %s\n"
(string_of_data x.task_seg_related_data);
Debug_print.bprintf ~indent_level:(indent_level + 1) buffer
"time slots\n";
List.iter
(fun x ->
Debug_print.bprintf ~indent_level:(indent_level + 2) buffer "%s\n"
(string_of_time_slot x))
x.time_slots;
Debug_print.bprintf ~indent_level:(indent_level + 1) buffer "buckets\n";
List.iter
(fun x ->
Debug_print.bprintf ~indent_level:(indent_level + 2) buffer "%s\n"
(string_of_time_slot x))
x.buckets
| Time_share x ->
Debug_print.bprintf ~indent_level buffer "time share\n";
Debug_print.bprintf ~indent_level:(indent_level + 1) buffer "data\n";
List.iter
(fun x ->
Debug_print.bprintf ~indent_level:(indent_level + 2) buffer "%s\n"
(string_of_data x))
x.task_seg_related_data_list;
Debug_print.bprintf ~indent_level:(indent_level + 1) buffer
"time slots\n";
List.iter
(fun x ->
Debug_print.bprintf ~indent_level:(indent_level + 2) buffer "%s\n"
(string_of_time_slot x))
x.time_slots
| Push_toward x ->
Debug_print.bprintf ~indent_level buffer "push toward\n";
Debug_print.bprintf ~indent_level:(indent_level + 1) buffer
"data = %s\n"
(string_of_data x.task_seg_related_data);
Debug_print.bprintf ~indent_level:(indent_level + 1) buffer "target\n";
Debug_print.bprintf ~indent_level:(indent_level + 2) buffer "%s\n"
(string_of_time x.target);
Debug_print.bprintf ~indent_level:(indent_level + 1) buffer
"time slots\n";
List.iter
(fun x ->
Debug_print.bprintf ~indent_level:(indent_level + 2) buffer "%s\n"
(string_of_time_slot x))
x.time_slots );
Buffer.contents buffer
end
module Serialize = struct
let pack (type a b c d e f) ~(pack_data : a -> d) ~(pack_time : b -> e)
~(pack_time_slot : c -> f) (t : (a, b, c) t) :
(d, e, f) Sched_req_data_unit_skeleton_t.sched_req_data_unit_skeleton =
match t with
| Fixed { task_seg_related_data; start } ->
`Fixed
{
task_seg_related_data = pack_data task_seg_related_data;
start = pack_time start;
}
| Shift x ->
`Shift
{
task_seg_related_data_list =
List.map pack_data x.task_seg_related_data_list;
incre = Misc_utils.int32_int32_of_int64 x.incre;
time_slots = List.map pack_time_slot x.time_slots;
}
| Split_and_shift x ->
`Split_and_shift
{
task_seg_related_data = pack_data x.task_seg_related_data;
incre = Misc_utils.int32_int32_of_int64 x.incre;
split_count =
( match x.split_count with
| Max_split x -> `Max_split (Misc_utils.int32_int32_of_int64 x)
| Exact_split x ->
`Exact_split (Misc_utils.int32_int32_of_int64 x) );
min_seg_size = Misc_utils.int32_int32_of_int64 x.min_seg_size;
max_seg_size =
Option.map Misc_utils.int32_int32_of_int64 x.max_seg_size;
time_slots = List.map pack_time_slot x.time_slots;
}
| Split_even x ->
`Split_even
{
task_seg_related_data = pack_data x.task_seg_related_data;
time_slots = List.map pack_time_slot x.time_slots;
buckets = List.map pack_time_slot x.buckets;
incre = Misc_utils.int32_int32_of_int64 x.incre;
}
| Time_share x ->
`Time_share
{
task_seg_related_data_list =
List.map pack_data x.task_seg_related_data_list;
interval_size = Misc_utils.int32_int32_of_int64 x.interval_size;
time_slots = List.map pack_time_slot x.time_slots;
}
| Push_toward x ->
`Push_toward
{
task_seg_related_data = pack_data x.task_seg_related_data;
target = pack_time x.target;
time_slots = List.map pack_time_slot x.time_slots;
incre = Misc_utils.int32_int32_of_int64 x.incre;
}
end
module Deserialize = struct
let unpack (type a b c d e f) ~(unpack_data : d -> a) ~(unpack_time : e -> b)
~(unpack_time_slot : f -> c)
(x :
(d, e, f) Sched_req_data_unit_skeleton_t.sched_req_data_unit_skeleton) :
(a, b, c) t =
match x with
| `Fixed { task_seg_related_data; start } ->
Fixed
{
task_seg_related_data = unpack_data task_seg_related_data;
start = unpack_time start;
}
| `Shift x ->
Shift
{
task_seg_related_data_list =
List.map unpack_data x.task_seg_related_data_list;
time_slots = List.map unpack_time_slot x.time_slots;
incre = Misc_utils.int64_of_int32_int32 x.incre;
}
| `Split_and_shift x ->
Split_and_shift
{
task_seg_related_data = unpack_data x.task_seg_related_data;
time_slots = List.map unpack_time_slot x.time_slots;
incre = Misc_utils.int64_of_int32_int32 x.incre;
split_count =
( match x.split_count with
| `Max_split x -> Max_split (Misc_utils.int64_of_int32_int32 x)
| `Exact_split x ->
Exact_split (Misc_utils.int64_of_int32_int32 x) );
min_seg_size = Misc_utils.int64_of_int32_int32 x.min_seg_size;
max_seg_size =
Option.map Misc_utils.int64_of_int32_int32 x.max_seg_size;
}
| `Split_even x ->
Split_even
{
task_seg_related_data = unpack_data x.task_seg_related_data;
time_slots = List.map unpack_time_slot x.time_slots;
buckets = List.map unpack_time_slot x.buckets;
incre = Misc_utils.int64_of_int32_int32 x.incre;
}
| `Time_share x ->
Time_share
{
task_seg_related_data_list =
List.map unpack_data x.task_seg_related_data_list;
time_slots = List.map unpack_time_slot x.time_slots;
interval_size = Misc_utils.int64_of_int32_int32 x.interval_size;
}
| `Push_toward x ->
Push_toward
{
task_seg_related_data = unpack_data x.task_seg_related_data;
target = unpack_time x.target;
time_slots = List.map unpack_time_slot x.time_slots;
incre = Misc_utils.int64_of_int32_int32 x.incre;
}
end