-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_shared.ml
115 lines (96 loc) · 3.09 KB
/
util_shared.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
let table_to_pairs t =
Hashtbl.fold begin
fun key val_ l -> (key, val_) :: l
end t []
let table_of_pairs pairs num =
List.fold_left begin
fun table (key, value) ->
Hashtbl.add table key value;
table
end (Hashtbl.create num) pairs
module Hashtbl_ext = struct
include Hashtbl
type ('a, 'b) pairs_list = ('a * 'b) list [@@deriving yojson]
let to_yojson a_to_yojson b_to_yojson t = pairs_list_to_yojson
a_to_yojson b_to_yojson
(table_to_pairs t)
let of_yojson a_of_yojson b_of_yojson json =
match pairs_list_of_yojson a_of_yojson b_of_yojson json with
| Ok pairs -> Ok (table_of_pairs pairs 10)
| Error _ as err -> err
let keys t = fold (fun key _ res -> key:: res) t []
end
let list_limit l limit =
let rec proc l limit res =
if limit == 0 then res
else match l with
| h :: t -> proc t (limit - 1) (h :: res)
| [] -> res in
if limit = -1 then l
else List.rev (proc l limit [])
let list_cons_dedup el l =
let rec proc l el res =
match l with
| h :: t -> proc t el (if h = el then res else h :: res)
| [] -> List.rev res in
el :: (proc l el [])
let list_find l v =
let rec proc l v i =
match l with
| h :: t ->
if h = v then Some i
else proc t v (i + 1)
| [] -> None in
proc l v 0
let queue_to_list q =
let rec to_list q =
if Queue.is_empty q then []
else let elem = Queue.pop q in
elem :: (to_list q)
in
List.rev (to_list (Queue.copy q))
let deoption_tuple4 t =
match t with
| (None, _, _, _) | (_, None, _, _)
| (_, _, None, _) | (_,_, _, None) -> None
| (Some a, Some b, Some c, Some d) -> Some (a, b, c, d)
let table_inc t key =
let exists = Hashtbl.mem t key in
Hashtbl.replace t key (if exists then (Hashtbl.find t key) + 1 else 1);
exists
let table_dec t key =
if Hashtbl.mem t key then
let count = Hashtbl.find t key in
if count = 1 then begin
Hashtbl.remove t key;
false
end else begin
Hashtbl.replace t key (count - 1);
true
end
else false
let table_get t key =
if Hashtbl.mem t key
then Some (Hashtbl.find t key)
else None
let filter_some l =
List.fold_left begin
fun acc opt ->
match opt with
| Some a -> a :: acc
| None -> acc
end [] l
let print_i_two i = Printf.sprintf "%02u" i
let print_i i = Printf.sprintf "%u" i
let get_time_str t = (print_i_two t.Unix.tm_hour) ^ ":" ^ (print_i_two t.Unix.tm_min)
let get_date_str t =
(print_i_two t.Unix.tm_mday) ^ "." ^
(print_i_two (t.Unix.tm_mon + 1)) ^ "." ^
(print_i (t.Unix.tm_year + 1900))
let get_timestamp t =
(print_i (t.Unix.tm_year + 1900)) ^ "-" ^
(print_i_two (t.Unix.tm_mon + 1)) ^ "-" ^
(print_i_two t.Unix.tm_mday) ^ " " ^
(print_i_two t.Unix.tm_hour) ^ ":" ^
(print_i_two t.Unix.tm_min) ^ ":" ^
(print_i_two t.Unix.tm_sec)