package tablecloth-base

  1. Overview
  2. Docs

Source file TableclothMap.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
module Comparator = TableclothComparator

type ('key, 'value, 'id) t = ('key, 'value, 'id) Base.Map.t

module Of (M : TableclothComparator.S) = struct
  type nonrec 'value t = (M.t, 'value, M.identity) t
end

let keep_latest_only _ latest = latest

let empty (comparator : ('key, 'identity) TableclothComparator.s) :
    ('key, 'value, 'identity) t =
  Base.Map.empty (Internal.to_base_comparator comparator)


let singleton
    (comparator : ('key, 'identity) TableclothComparator.s) ~key ~value :
    ('key, 'value, 'identity) t =
  Base.Map.of_alist_reduce
    (Internal.to_base_comparator comparator)
    [ (key, value) ]
    ~f:keep_latest_only


let from_array
    (comparator : ('key, 'identity) TableclothComparator.s)
    (elements : ('key * 'value) array) : ('key, 'value, 'identity) t =
  Base.Map.of_alist_reduce
    (Internal.to_base_comparator comparator)
    (Array.to_list elements)
    ~f:keep_latest_only


let from_list
    (comparator : ('key, 'identity) TableclothComparator.s)
    (elements : ('key * 'value) list) : ('key, 'value, 'identity) t =
  Base.Map.of_alist_reduce
    (Internal.to_base_comparator comparator)
    elements
    ~f:keep_latest_only


let is_empty = Base.Map.is_empty

let includes m ~key = Base.Map.mem m key

let length = Base.Map.length

let minimum t = Base.Map.min_elt t |> Option.map fst

let maximum t = Base.Map.max_elt t |> Option.map fst

let extent t = TableclothOption.both (minimum t) (maximum t)

let add m ~key ~value = Base.Map.set m ~key ~data:value

let ( .?{}<- ) (map : ('key, 'value, 'id) t) (key : 'key) (value : 'value) :
    ('key, 'value, 'id) t =
  add map ~key ~value


let remove m ~key = Base.Map.remove m key

let get m ~key = Base.Map.find m key

let ( .?{} ) (map : ('key, 'value, _) t) (key : 'key) : 'value option =
  get map ~key


let update m ~key ~f = Base.Map.change m key ~f

let merge m1 m2 ~f =
  Base.Map.merge m1 m2 ~f:(fun ~key desc ->
      match desc with
      | `Left v1 ->
          f key (Some v1) None
      | `Right v2 ->
          f key None (Some v2)
      | `Both (v1, v2) ->
          f key (Some v1) (Some v2) )


let map = Base.Map.map

let map_with_index t ~f = Base.Map.mapi t ~f:(fun ~key ~data -> f key data)

let filter = Base.Map.filter

let filter_map m ~f =
  Base.Map.filter_mapi m ~f:(fun ~key ~data:value -> f ~key ~value)


let partition m ~f =
  Base.Map.partitioni_tf m ~f:(fun ~key ~data -> f ~key ~value:data)


let find m ~f =
  Base.Map.fold m ~init:None ~f:(fun ~key ~data matching ->
      match matching with
      | Some _ ->
          matching
      | None ->
          if f ~key ~value:data then Some (key, data) else None )


let any = Base.Map.exists

let all = Base.Map.for_all

let for_each = Base.Map.iter

let for_each_with_index
    (map : ('key, 'value, _) t) ~(f : key:'key -> value:'value -> unit) : unit =
  Base.Map.iteri map ~f:(fun ~key ~data -> f ~key ~value:data)


let fold m ~initial ~f =
  Base.Map.fold m ~init:initial ~f:(fun ~key ~data acc ->
      f acc ~key ~value:data )


let keys = Base.Map.keys

let values = Base.Map.data

let to_array m = Base.Map.to_alist m |> Base.List.to_array

let to_list m = Base.Map.to_alist m

module Poly = struct
  type identity = Base.Comparator.Poly.comparator_witness

  type nonrec ('k, 'v) t = ('k, 'v, identity) t

  let empty () = Base.Map.Poly.empty

  let singleton ~key ~value = Base.Map.Poly.singleton key value

  let from_list l = Base.Map.Poly.of_alist_reduce l ~f:(fun _ curr -> curr)

  let from_array a = Base.Array.to_list a |> from_list
end

module Int = struct
  type nonrec 'v t = (TableclothInt.t, 'v, TableclothInt.identity) t

  let empty = Obj.magic (Base.Map.empty (module Base.Int))

  let singleton ~key ~value =
    Obj.magic (Base.Map.singleton (module Base.Int) key value)


  let from_list l =
    Obj.magic
      (Base.Map.of_alist_reduce (module Base.Int) l ~f:(fun _ curr -> curr))


  let from_array a = Obj.magic (Base.Array.to_list a |> from_list)
end

module String = struct
  type nonrec 'value t =
    (TableclothString.t, 'value, TableclothString.identity) t

  let empty = Obj.magic (Base.Map.empty (module Base.String))

  let singleton ~key ~value =
    Obj.magic (Base.Map.singleton (module Base.String) key value)


  let from_list l =
    Obj.magic
      (Base.Map.of_alist_reduce (module Base.String) l ~f:(fun _ curr -> curr))


  let from_array a = Obj.magic (Base.Array.to_list a |> from_list)
end
OCaml

Innovation. Community. Security.