aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/unsafe/lux/data/collection/array.lux
blob: c8c98508cad0f46a2402a9748d41b95979a41ee0 (plain)
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
(.require
 [library
  [lux (.except list type)
   [abstract
    ["[0]" functor]]
   [data
    [collection
     ["[0]" list]]]
   [meta
    ["@" target]
    [type
     ["[0]" variance]]]]])

(def .public primitive
  "#Array")

(with_template [<item> <array>]
  [(.type .public <array>
     {.#Primitive ..primitive {.#Item <item> {.#End}}})]

  [(variance.Mutable r w) (Array' r w)]
  [(variance.Mutable a a) (Array a)]
  )

(with_expansions [<index_type> (.Primitive "java.lang.Long")
                  <item_type> (.Primitive "java.lang.Object")]
  (for @.jvm (def jvm_int
               (template (jvm_int value)
                 [(.|> value
                       (.as <index_type>)
                       .jvm_object_cast#
                       .jvm_conversion_long_to_int#)]))
       (these))
  
  (`` (def .public empty
        (template (empty <size>)
          [((.is (.All (_ a) (.-> .Nat (..Array a)))
                 (.function (empty size)
                   (.as_expected
                    (.for (,, (.static @.old))
                          ("jvm anewarray" "(java.lang.Object )" size)

                          (,, (.static @.jvm))
                          (|> (,, (..jvm_int size))
                              .jvm_array_new_object#
                              (.is (..Array <item_type>)))

                          (,, (.static @.js)) ("js array new" size)
                          (,, (.static @.python)) ("python array new" size)
                          (,, (.static @.lua)) ("lua array new" size)
                          (,, (.static @.ruby)) ("ruby array new" size)
                          (,, (.static @.php)) ("php array new" size)
                          (,, (.static @.scheme)) ("scheme array new" size)))))
            <size>)])))

  (`` (def .public size
        (template (size <array>)
          [((.is (.All (_ r w) (.-> (..Array' r w) .Nat))
                 (.function (size array)
                   (.for (,, (.static @.old))
                         ("jvm arraylength" array)

                         (,, (.static @.jvm))
                         (.|> array
                              .jvm_array_length_object#
                              .jvm_conversion_int_to_long#
                              .jvm_object_cast#
                              (.is <index_type>)
                              (.as .Nat))

                         (,, (.static @.js)) ("js array length" array)
                         (,, (.static @.python)) ("python array length" array)
                         (,, (.static @.lua)) ("lua array length" array)
                         (,, (.static @.ruby)) ("ruby array length" array)
                         (,, (.static @.php)) ("php array length" array)
                         (,, (.static @.scheme)) ("scheme array length" array))))
            <array>)])))

  (def lacks?'
    (template (lacks?' <read!> <null?> index array)
      [(<null?> (<read!> index array))]))

  (`` (def .public lacks?
        (template (lacks? <index> <array>)
          [((.is (.All (_ r w)
                   (.-> .Nat (..Array' r w) .Bit))
                 (.function (lacks? index array)
                   (.let [size (..size array)]
                     (.if (.int_<# (.int size) (.int index))
                       (.for (,, (.static @.old))
                             ("jvm object null?" ("jvm aaload" array index))

                             (,, (.static @.jvm))
                             (.|> array
                                  (.jvm_array_read_object# (,, (jvm_int index)))
                                  .jvm_object_null?#)

                             (,, (.static @.js)) (,, (lacks?' "js array read" "js object undefined?" index array))
                             (,, (.static @.python)) (,, (lacks?' "python array read" "python object none?" index array))
                             (,, (.static @.lua)) (,, (lacks?' "lua array read" "lua object nil?" index array))
                             (,, (.static @.ruby)) (,, (lacks?' "ruby array read" "ruby object nil?" index array))
                             (,, (.static @.php)) (,, (lacks?' "php array read" "php object null?" index array))
                             (,, (.static @.scheme)) (,, (lacks?' "scheme array read" "scheme object nil?" index array)))
                       .true))))
            <index> <array>)])))

  (def .public has?
    (template (has? index array)
      [(.not (..lacks? index array))]))

  (`` (def .public item
        (template (item <index> <array>)
          [((.is (.All (_ r w)
                   (.-> .Nat (..Array' r w) r))
                 (.function (item index array)
                   (.as_expected
                    (.for (,, (.static @.old))
                          ("jvm aaload" array index)

                          (,, (.static @.jvm))
                          (.jvm_array_read_object# (,, (jvm_int index)) array)

                          (,, (.static @.js)) ("js array read" index array)
                          (,, (.static @.python)) ("python array read" index array)
                          (,, (.static @.lua)) ("lua array read" index array)
                          (,, (.static @.ruby)) ("ruby array read" index array)
                          (,, (.static @.php)) ("php array read" index array)
                          (,, (.static @.scheme)) ("scheme array read" index array)))))
            <index> <array>)])))

  (`` (def .public has!
        (template (has! <index> <value> <array>)
          [((.is (.All (_ r w)
                   (.-> .Nat w (..Array' r w) (..Array' r w)))
                 (.function (has! index value array)
                   (.for (,, (.static @.old))
                         ("jvm aastore" array index value)

                         (,, (.static @.jvm))
                         (.|> array
                              (.jvm_array_write_object# (,, (jvm_int index)) value)
                              .as_expected)

                         (,, (.static @.js)) ("js array write" index (.as_expected value) array)
                         (,, (.static @.python)) ("python array write" index (.as_expected value) array)
                         (,, (.static @.lua)) ("lua array write" index (.as_expected value) array)
                         (,, (.static @.ruby)) ("ruby array write" index (.as_expected value) array)
                         (,, (.static @.php)) ("php array write" index (.as_expected value) array)
                         (,, (.static @.scheme)) ("scheme array write" index (.as_expected value) array))))
            <index> <value> <array>)])))

  (`` (def .public lacks!
        (template (lacks! <index> <array>)
          [((.is (.All (_ r w)
                   (.-> .Nat (..Array' r w) (..Array' r w)))
                 (.function (lacks! index array)
                   (.let [size (..size array)]
                     (.if (.int_<# (.int size) (.int index))
                       (.for (,, (.static @.old))
                             (..has! index (.as_expected ("jvm object null")) array)
                             
                             (,, (.static @.jvm))
                             (..has! index (.as_expected (is <item_type> (.jvm_object_null#))) array)

                             (,, (.static @.js)) ("js array delete" index array)
                             (,, (.static @.python)) ("python array delete" index array)
                             (,, (.static @.lua)) ("lua array delete" index array)
                             (,, (.static @.ruby)) ("ruby array delete" index array)
                             (,, (.static @.php)) ("php array delete" index array)
                             (,, (.static @.scheme)) ("scheme array delete" index array))
                       array))))
            <index> <array>)])))
  )

(def .public revised!
  (template (revised! <index> <$> <array>)
    [((.is (.All (_ r w)
             (.-> .Nat (.-> r w) (..Array' r w) (..Array' r w)))
           (.function (revised! index $ array)
             (.if (..lacks? index array)
               array
               (..has! index ($ (..item index array)) array))))
      <index> <$> <array>)]))

(def .public upsert!
  (template (upsert! <index> <default> <$> <array>)
    [((.is (.All (_ r w)
             (.-> .Nat r (.-> r w) (..Array' r w) (..Array' r w)))
           (.function (upsert! index default $ array)
             (..has! index
                     ($ (.if (..lacks? index array)
                          default
                          (..item index array)))
                     array)))
      <index> <default> <$> <array>)]))

(def .public copy!
  (template (copy! <length> <src_start> <src_array> <dest_start> <dest_array>)
    [((.is (.All (_ r w)
             (.-> .Nat .Nat (..Array' w .Nothing) .Nat (..Array' r w)
                  (..Array' r w)))
           (.function (copy! length src_start src_array dest_start dest_array)
             (.loop (again [offset 0])
               (.if (.int_<# (.int length) (.int offset))
                 (.exec
                   (.if (..lacks? (.i64_+# offset src_start) src_array)
                     (..lacks! (.i64_+# offset dest_start) dest_array)
                     (..has! (.i64_+# offset dest_start)
                             (..item (.i64_+# offset src_start) src_array)
                             dest_array))
                   (again (.i64_+# 1 offset)))
                 dest_array))))
      <length> <src_start> <src_array> <dest_start> <dest_array>)]))

(with_template [<name> <when_lacks> <when_has>]
  [(def .public <name>
     (template (<name> <array>)
       [((.is (.All (_ r w) (.-> (..Array' r w) .Nat))
              (.function (occupancy array)
                (.let [size (..size array)]
                  (.loop (again [index 0
                                 it 0])
                    (.if (.int_<# (.int size) (.int index))
                      (.if (..lacks? index array)
                        (again (.i64_+# 1 index) <when_lacks>)
                        (again (.i64_+# 1 index) <when_has>))
                      it)))))
         <array>)]))]

  [occupancy it (.i64_+# 1 it)]
  [vacancy (.i64_+# 1 it) it]
  )

(def .public only!
  (template (only! <?> <it>)
    [((.is (.All (_ r w)
             (.-> (.-> r .Bit) (..Array' r w) (..Array' r w)))
           (.function (only! ? it)
             (.let [size (..size it)]
               (.loop (again [index 0])
                 (.if (.int_<# (.int size) (.int index))
                   (.exec
                     (.if (..lacks? index it)
                       it
                       (.if (? (..item index it))
                         it
                         (..lacks! index it)))
                     (again (.i64_+# 1 index)))
                   it)))))
      <?> <it>)]))

(with_template [<name> <predicate> <test> <type> <term>]
  [(def .public <name>
     (template (<name> <?> <it>)
       [((.is (.All (_ r w)
                (.-> <predicate> (..Array' r w) (.Maybe <type>)))
              (.function (<name> ? it)
                (.let [size (..size it)]
                  (.loop (again [index 0])
                    (.if (.int_<# (.int size) (.int index))
                      (.if (..lacks? index it)
                        (again (.i64_+# 1 index))
                        (.let [it (..item index it)]
                          (.if <test>
                            {.#Some <term>}
                            (again (.i64_+# 1 index)))))
                      {.#None})))))
         <?> <it>)]))]

  [example (.-> r .Bit) (? it) r it]
  [example' (.-> Nat r .Bit) (? index it) [Nat r] [index it]]
  )

(def .public clone
  (template (clone <it>)
    [((.is (.All (_ a) (.-> (..Array a) (..Array a)))
           (.function (clone it)
             (.let [size (..size it)]
               (..copy! size 0 it 0 (..empty size)))))
      <it>)]))

(def .public of_list
  (template (of_list <input>)
    [((.is (.All (_ a) (.-> (.List a) (..Array a)))
           (.function (of_list input)
             (.let [size (list.size input)
                    output (..empty size)]
               (.loop (again [index 0
                              input input])
                 (.when input
                   {.#End}
                   output
                   
                   {.#Item head tail}
                   (.exec
                     (..has! index head output)
                     (again (.i64_+# 1 index) tail)))))))
      <input>)]))

(def underflow
  Nat
  (-- 0))

(`` (def list|-default
      (template (list|-default <empty> <array>)
        [((.is (.All (_ r w) (.-> (.List r) (..Array' r w) (.List r)))
               (.function (list|-default empty array)
                 (.loop (again [index (.i64_-# 1 (..size array))
                                output empty])
                   (.if (.i64_=# (,, (.static ..underflow)) index)
                     output
                     (again (.i64_-# 1 index)
                            (.if (..lacks? index array)
                              output
                              {.#Item (..item index array) output}))))))
          <empty> <array>)])))

(`` (def list|+default
      (template (list|+default <default> <array>)
        [((.is (.All (_ r w) (.-> r (..Array' r w) (.List r)))
               (.function (list|+default default array)
                 (.loop (again [index (.i64_-# 1 (..size array))
                                output (`` (.is (.List (,, (.these (,, (.type_of default)))))
                                                {.#End}))])
                   (.if (.i64_=# (,, (.static ..underflow)) index)
                     output
                     (again (.i64_-# 1 index)
                            {.#Item (.if (..lacks? index array)
                                      default
                                      (..item index array))
                                    output})))))
          <default> <array>)])))

(`` (def .public list
      (template (list <default> <array>)
        [((.is (.All (_ r w) (.-> (.Maybe r) (..Array' r w) (.List r)))
               (.function (list default array)
                 (.when default
                   {.#Some default}
                   (,, (..list|+default default array))

                   {.#None}
                   (,, (..list|-default {.#End} array)))))
          <default> <array>)])))

(def .public =
  (template (= <//#=> <left/*> <right/*>)
    [((.is (.All (_ r w0 w1) (.-> (.-> r r .Bit) (..Array' r w0) (..Array' r w1) .Bit))
           (.function (= //#= left/* right/*)
             (.let [size (..size left/*)]
               (.and (.i64_=# (..size right/*) size)
                     (.loop (again [index 0])
                       (.if (.int_<# (.int size) (.int index))
                         (.if (..lacks? index left/*)
                           (..lacks? index right/*)
                           (.if (..lacks? index right/*)
                             .false
                             (.and (//#= (..item index left/*)
                                         (..item index right/*))
                                   (again (.i64_+# 1 index)))))
                         true))))))
      <//#=> <left/*> <right/*>)]))

(def .public composite
  (template (composite <left/*> <right/*>)
    [((.is (.All (_ a) (.-> (..Array' a .Nothing) (..Array' a .Nothing) (..Array a)))
           (.function (composite left/* right/*)
             (.let [|left| (..size left/*)
                    |right| (..size right/*)]
               (.|> (..empty (.i64_+# |left| |right|))
                    (..copy! |left| 0 left/* 0)
                    (..copy! |right| 0 right/* |left|)))))
      <left/*> <right/*>)]))

(def .public mix
  (template (mix <$> <init> <it>)
    [((.is (.All (_ r w s)
             (.-> (.-> Nat r s s) s (..Array' r w) s))
           (.function (mix $ init it)
             (.let [size (..size it)]
               (.loop (again [index 0
                              so_far init])
                 (.if (.int_<# (.int size) (.int index))
                   (.if (..lacks? index it)
                     (again (.i64_+# 1 index) so_far)
                     (again (.i64_+# 1 index) ($ index (..item index it) so_far)))
                   so_far)))))
      <$> <init> <it>)]))

(def .public each
  (template (each <$> <input>)
    [((.is (functor.Functor ..Array)
           (.function (each $ input)
             (..mix (.function (_ index item output)
                      (..has! index ($ item) output))
                    (..empty (..size input))
                    input)))
      <$> <input>)]))

(with_template [<name> <init> <op>]
  [(def .public <name>
     (template (<name> <?> <it>)
       [((.is (.All (_ r w)
                (.-> (.-> r .Bit)
                     (.-> (..Array' r w) .Bit)))
              (.function (<name> ? it)
                (.let [size (..size it)]
                  (.loop (again [index 0])
                    (.if (.int_<# (.int size) (.int index))
                      (.if (..lacks? index it)
                        (again (.i64_+# 1 index))
                        (<op> (? (..item index it))
                              (again (.i64_+# 1 index))))
                      <init>)))))
         <?> <it>)]))]

  [every? .true  and]
  [any?   .false or]
  )

(def .public one
  (template (one <?> <it>)
    [((.is (.All (_ r r' w)
             (.-> (.-> r (.Maybe r')) (..Array' r w) (.Maybe r')))
           (.function (one ? it)
             (.let [size (..size it)]
               (.loop (again [index 0])
                 (.if (.int_<# (.int size) (.int index))
                   (with_expansions [<again> (again (.i64_+# 1 index))]
                     (.if (..lacks? index it)
                       <again>
                       (.when (? (..item index it))
                         {.#None}
                         <again>

                         output
                         output)))
                   {.#None})))))
      <?> <it>)]))