/
enum.ex
1457 lines (1183 loc) · 45.2 KB
/
enum.ex
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
defmodule Aja.Enum do
@moduledoc """
Drop-in replacement for the `Enum` module, optimized to work with Aja's data structures such as `Aja.Vector`.
It currently only covers a subset of `Enum`, but `Aja.Enum` aims to completely mirror the API of `Enum`,
and should behave exactly the same for any type of `Enumerable`.
The only expected difference should be a significant increase in performance for Aja structures.
## Rationale
Structures such as `Aja.Vector` or `Aja.OrdMap` are implementing the `Enumerable` protocol, which means they can be
used directly with the `Enum` module. The `Enumerable` protocol however comes with its overhead and is strongly
limited in terms of performance.
On the other hand, `Aja.Enum` provides hand-crafted highly-optimized functions that fully take advantage of
immutable vectors. The speedup can easily reach more than a factor 10 compared to `Enum` used on non-list
structures, and sometimes even be noticeably faster than `Enum` used over lists.
One of the main reasons to adopt a specific data structure is the performance.
Using vectors with `Enum` would defeat the purpose, hence the introduction of `Aja.Enum`.
iex> vector = Aja.Vector.new(1..10000)
iex> Enum.sum(vector) # slow
50005000
iex> Aja.Enum.sum(vector) # same result, much faster
50005000
"""
require Aja.Vector.Raw, as: RawVector
alias Aja.EnumHelper, as: H
@compile :inline_list_funcs
@type index :: integer
@type value :: any
@type t(value) :: Aja.Vector.t(value) | [value] | Enumerable.t()
@empty_vector RawVector.empty()
# TODO optimize ranges (sum, random...)
@doc """
Converts `enumerable` to a list.
Mirrors `Enum.to_list/1` with higher performance for Aja structures.
"""
@spec to_list(t(val)) :: [val] when val: value
defdelegate to_list(enumerable), to: H
@doc """
Returns the size of the `enumerable`.
Mirrors `Enum.count/1` with higher performance for Aja structures.
"""
@spec count(t(any)) :: non_neg_integer
def count(enumerable) do
case enumerable do
list when is_list(list) -> length(list)
%Aja.Vector{__vector__: vector} -> RawVector.size(vector)
%Aja.OrdMap{__ord_map__: map} -> map_size(map)
%MapSet{} -> MapSet.size(enumerable)
start..stop -> abs(start - stop) + 1
_ -> Enum.count(enumerable)
end
end
@doc """
Returns the count of elements in the `enumerable` for which `fun` returns
a truthy value.
Mirrors `Enum.count/2` with higher performance for Aja structures.
"""
@spec count(t(val), (val -> as_boolean(term))) :: non_neg_integer when val: value
def count(enumerable, fun) do
case H.try_get_raw_vec_or_list(enumerable) do
nil ->
Enum.count(enumerable, fun)
list when is_list(list) ->
count_list(list, fun, 0)
vector ->
RawVector.count(vector, fun)
end
end
defp count_list([], _fun, acc), do: acc
defp count_list([head | tail], fun, acc) do
new_acc =
if fun.(head) do
acc + 1
else
acc
end
count_list(tail, fun, new_acc)
end
@doc """
Returns `true` if `enumerable` is empty, otherwise `false`.
Mirrors `Enum.empty?/1` with higher performance for Aja structures.
"""
@spec empty?(t(any)) :: boolean
def empty?(enumerable) do
case enumerable do
list when is_list(list) -> list == []
%Aja.Vector{__vector__: vector} -> vector === @empty_vector
%Aja.OrdMap{__ord_map__: map} -> map == %{}
%MapSet{} -> MapSet.size(enumerable) == 0
%Range{} -> false
_ -> Enum.empty?(enumerable)
end
end
# Note: Could not optimize it noticeably for vectors
@doc """
Checks if `element` exists within the `enumerable`.
Just an alias for `Enum.member?/2`, does not improve performance.
"""
@spec member?(t(val), val) :: boolean when val: value
defdelegate member?(enumerable, value), to: Enum
# TODO optimize for vector
@doc """
Returns a subset list of the given `enumerable` by `index_range`.
Mirrors `Enum.slice/2` with higher performance for Aja structures.
"""
@spec slice(t(val), Range.t()) :: [val] when val: value
defdelegate slice(enumerable, index_range), to: Enum
@doc """
Returns a subset list of the given `enumerable`, from `start_index` (zero-based)
with `amount` number of elements if available.
Mirrors `Enum.slice/3`.
"""
@spec slice(t(val), index, non_neg_integer) :: [val] when val: value
defdelegate slice(enumerable, start_index, amount), to: Enum
@doc """
Inserts the given `enumerable` into a `collectable`.
Mirrors `Enum.into/2` with higher performance for Aja structures.
"""
@spec into(t(val), Collectable.t()) :: Collectable.t() when val: value
def into(enumerable, collectable)
def into(enumerable, %Aja.Vector{} = vector) do
# TODO improve when this is the empty vector/ord_map
Aja.Vector.concat(vector, enumerable)
end
def into(enumerable, %Aja.OrdMap{} = ord_map) do
Aja.OrdMap.merge_list(ord_map, H.to_list(enumerable))
end
def into(enumerable, collectable) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> enumerable
list when is_list(list) -> list
vector -> RawVector.to_list(vector)
end
|> Enum.into(collectable)
end
@doc """
Inserts the given `enumerable` into a `collectable` according to the `transform` function.
Mirrors `Enum.into/3` with higher performance for Aja structures.
"""
def into(enumerable, collectable, transform)
def into(enumerable, %Aja.Vector{} = vector, transform) do
# TODO we can probably improve this with the builder
Aja.Vector.concat(vector, H.map(enumerable, transform))
end
def into(enumerable, %Aja.OrdMap{} = ord_map, transform) do
Aja.OrdMap.merge_list(ord_map, H.map(enumerable, transform))
end
def into(enumerable, collectable, transform) when is_function(transform, 1) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> enumerable
list when is_list(list) -> list
vector -> RawVector.to_list(vector)
end
|> Enum.into(collectable, transform)
end
@doc """
Given an enumerable of enumerables, concatenates the `enumerables` into
a single list.
Mirrors `Enum.concat/1` with higher performance for Aja structures.
"""
@spec concat(t(t(val))) :: t(val) when val: value
def concat(enumerables) do
case H.try_get_raw_vec_or_list(enumerables) do
nil -> Enum.reverse(enumerables) |> concat_wrap([])
list when is_list(list) -> :lists.reverse(list) |> concat_wrap([])
vector -> RawVector.foldr(vector, [], &concat/2)
end
end
defp concat_wrap(_reversed = [], acc), do: acc
defp concat_wrap([head | tail], acc) do
concat_wrap(tail, concat(head, acc))
end
@doc """
Concatenates the enumerable on the `right` with the enumerable on the `left`.
Mirrors `Enum.concat/2` with higher performance for Aja structures.
"""
@spec concat(t(val), t(val)) :: t(val) when val: value
def concat(left, right)
def concat(left, right) when is_list(left) and is_list(right) do
left ++ right
end
def concat(left, right) do
case H.try_get_raw_vec_or_list(left) do
nil -> Enum.concat(left, right)
list when is_list(list) -> list ++ to_list(right)
vector -> RawVector.to_list(vector, to_list(right))
end
end
@doc """
Finds the element at the given `index` (zero-based).
Mirrors `Enum.at/3` with higher performance for Aja structures.
"""
@spec at(t(val), integer, default) :: val | default when val: value, default: any
def at(enumerable, index, default \\ nil) when is_integer(index) do
case H.try_get_raw_vec_or_list(enumerable) do
nil ->
Enum.at(enumerable, index, default)
list when is_list(list) ->
Enum.at(list, index, default)
vector ->
size = RawVector.size(vector)
case RawVector.actual_index(index, size) do
nil -> default
actual_index -> RawVector.fetch_positive!(vector, actual_index)
end
end
end
@doc """
Finds the element at the given `index` (zero-based).
Mirrors `Enum.fetch/2` with higher performance for Aja structures.
"""
@spec fetch(t(val), integer) :: {:ok, val} | :error when val: value
def fetch(enumerable, index) when is_integer(index) do
case H.try_get_raw_vec_or_list(enumerable) do
nil ->
Enum.fetch(enumerable, index)
list when is_list(list) ->
Enum.fetch(list, index)
vector ->
size = RawVector.size(vector)
case RawVector.actual_index(index, size) do
nil -> :error
actual_index -> {:ok, RawVector.fetch_positive!(vector, actual_index)}
end
end
end
@doc """
Finds the element at the given `index` (zero-based).
Mirrors `Enum.fetch!/2` with higher performance for Aja structures.
"""
@spec fetch!(t(val), integer) :: val when val: value
def fetch!(enumerable, index) when is_integer(index) do
case H.try_get_raw_vec_or_list(enumerable) do
nil ->
Enum.fetch!(enumerable, index)
list when is_list(list) ->
Enum.fetch!(list, index)
vector ->
size = RawVector.size(vector)
case RawVector.actual_index(index, size) do
nil -> raise Enum.OutOfBoundsError
actual_index -> RawVector.fetch_positive!(vector, actual_index)
end
end
end
@doc """
Returns a list of elements in `enumerable` in reverse order.
Mirrors `Enum.reverse/1` with higher performance for Aja structures.
"""
@spec reverse(t(val)) :: [val] when val: value
def reverse(enumerable) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.reverse(enumerable)
list when is_list(list) -> :lists.reverse(list)
vector -> RawVector.reverse_to_list(vector, [])
end
end
@doc """
Reverses the elements in `enumerable`, concatenates the `tail`,
and returns it as a list.
Mirrors `Enum.reverse/2` with higher performance for Aja structures.
"""
@spec reverse(t(val), t(val)) :: [val] when val: value
def reverse(enumerable, tail) do
tail = H.to_list(tail)
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.reverse(enumerable, tail)
list when is_list(list) -> :lists.reverse(list, tail)
vector -> RawVector.reverse_to_list(vector, tail)
end
end
@doc """
Returns a list where each element is the result of invoking
`fun` on each corresponding element of `enumerable`.
Mirrors `Enum.map/2` with higher performance for Aja structures.
"""
@spec map(t(v1), (v1 -> v2)) :: [v2] when v1: value, v2: value
defdelegate map(enumerable, fun), to: H
@doc """
Filters the `enumerable`, i.e. returns only those elements
for which `fun` returns a truthy value.
Mirrors `Enum.filter/2` with higher performance for Aja structures.
"""
@spec filter(t(val), (val -> as_boolean(term))) :: [val] when val: value
def filter(enumerable, fun) when is_function(fun, 1) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.filter(enumerable, fun)
list when is_list(list) -> filter_list(list, fun, [])
vector -> RawVector.filter_to_list(vector, fun)
end
end
defp filter_list([], _fun, acc), do: :lists.reverse(acc)
defp filter_list([head | tail], fun, acc) do
acc =
if fun.(head) do
[head | acc]
else
acc
end
filter_list(tail, fun, acc)
end
@doc """
Returns a list of elements in `enumerable` excluding those for which the function `fun` returns
a truthy value.
Mirrors `Enum.reject/2` with higher performance for Aja structures.
"""
@spec reject(t(val), (val -> as_boolean(term))) :: [val] when val: value
def reject(enumerable, fun) when is_function(fun, 1) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.reject(enumerable, fun)
list when is_list(list) -> Enum.reject(list, fun)
vector -> RawVector.reject_to_list(vector, fun)
end
end
@doc """
Splits the `enumerable` in two lists according to the given function `fun`.
Mirrors `Enum.split_with/2` with higher performance for Aja structures.
"""
@spec split_with(t(val), (val -> as_boolean(term))) :: {[val], [val]} when val: value
def split_with(enumerable, fun) when is_function(fun, 1) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.split_with(enumerable, fun)
list when is_list(list) -> Enum.split_with(list, fun)
vector -> vector |> RawVector.to_list() |> Enum.split_with(fun)
end
end
@doc """
Invokes `fun` for each element in the `enumerable` with the
accumulator.
Mirrors `Enum.reduce/2` with higher performance for Aja structures.
"""
@spec reduce(t(val), (val, val -> val)) :: val when val: value
def reduce(enumerable, fun) when is_function(fun, 2) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.reduce(enumerable, fun)
list when is_list(list) -> Enum.reduce(list, fun)
vector -> RawVector.reduce(vector, fun)
end
end
@doc """
Invokes `fun` for each element in the `enumerable` with the accumulator.
Mirrors `Enum.reduce/3` with higher performance for Aja structures.
"""
@spec reduce(t(val), acc, (val, acc -> acc)) :: acc when val: value, acc: term
def reduce(enumerable, acc, fun) when is_function(fun, 2) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.reduce(enumerable, acc, fun)
list when is_list(list) -> :lists.foldl(fun, acc, list)
vector -> RawVector.foldl(vector, acc, fun)
end
end
# FINDS
@doc """
Returns `true` if at least one element in `enumerable` is truthy.
When an element has a truthy value (neither `false` nor `nil`) iteration stops
immediately and `true` is returned. In all other cases `false` is returned.
## Examples
iex> Aja.Enum.any?([false, false, false])
false
iex> Aja.Enum.any?([false, true, false])
true
iex> Aja.Enum.any?([])
false
"""
@spec any?(t(as_boolean(val))) :: boolean when val: value
def any?(enumerable) do
case enumerable do
%Aja.Vector{__vector__: vector} -> RawVector.any?(vector)
_ -> Enum.any?(enumerable)
end
end
@doc """
Returns `true` if `fun.(element)` is truthy for at least one element in `enumerable`.
Iterates over the `enumerable` and invokes `fun` on each element. When an invocation
of `fun` returns a truthy value (neither `false` nor `nil`) iteration stops
immediately and `true` is returned. In all other cases `false` is returned.
## Examples
iex> Aja.Enum.any?([2, 4, 6], fn x -> rem(x, 2) == 1 end)
false
iex> Aja.Enum.any?([2, 3, 4], fn x -> rem(x, 2) == 1 end)
true
iex> Aja.Enum.any?([], fn x -> x > 0 end)
false
"""
# TODO When only support Elixir 1.12
# @doc copy_doc_for.(:any?, 2)
@spec any?(t(val), (val -> as_boolean(term))) :: boolean when val: value
def any?(enumerable, fun) when is_function(fun, 1) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.any?(enumerable, fun)
list when is_list(list) -> Enum.any?(list, fun)
vector -> RawVector.any?(vector, fun)
end
end
@doc """
Returns `true` if all elements in `enumerable` are truthy.
When an element has a falsy value (`false` or `nil`) iteration stops immediately
and `false` is returned. In all other cases `true` is returned.
## Examples
iex> Aja.Enum.all?([1, 2, 3])
true
iex> Aja.Enum.all?([1, nil, 3])
false
iex> Aja.Enum.all?([])
true
"""
@spec all?(t(as_boolean(val))) :: boolean when val: value
def all?(enumerable) do
case enumerable do
%Aja.Vector{__vector__: vector} -> RawVector.all?(vector)
_ -> Enum.all?(enumerable)
end
end
@doc """
Returns `true` if `fun.(element)` is truthy for all elements in `enumerable`.
Iterates over `enumerable` and invokes `fun` on each element. If `fun` ever
returns a falsy value (`false` or `nil`), iteration stops immediately and
`false` is returned. Otherwise, `true` is returned.
## Examples
iex> Aja.Enum.all?([2, 4, 6], fn x -> rem(x, 2) == 0 end)
true
iex> Aja.Enum.all?([2, 3, 4], fn x -> rem(x, 2) == 0 end)
false
iex> Aja.Enum.all?([], fn _ -> nil end)
true
"""
# TODO When only support Elixir 1.12
# @doc copy_doc_for.(:all?, 2)
@spec all?(t(val), (val -> as_boolean(term))) :: boolean when val: value
def all?(enumerable, fun) when is_function(fun, 1) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.all?(enumerable, fun)
list when is_list(list) -> Enum.all?(list, fun)
vector -> RawVector.all?(vector, fun)
end
end
@doc """
Returns the first element for which `fun` returns a truthy value.
If no such element is found, returns `default`.
Mirrors `Enum.find/3` with higher performance for Aja structures.
"""
@spec find(t(val), default, (val -> as_boolean(term))) :: val | default
when val: value, default: value
def find(enumerable, default \\ nil, fun) when is_function(fun, 1) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.find(enumerable, default, fun)
list when is_list(list) -> Enum.find(list, default, fun)
vector -> RawVector.find(vector, default, fun)
end
end
@doc """
Similar to `find/3`, but returns the value of the function
invocation instead of the element itself.
Mirrors `Enum.find_value/3` with higher performance for Aja structures.
"""
@spec find_value(t(val), default, (val -> new_val)) :: new_val | default
when val: value, new_val: value, default: value
def find_value(enumerable, default \\ nil, fun) when is_function(fun, 1) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.find_value(enumerable, default, fun)
list when is_list(list) -> Enum.find_value(list, default, fun)
vector -> RawVector.find_value(vector, fun) || default
end
end
@doc """
Similar to `find/3`, but returns the index (zero-based)
of the element instead of the element itself.
Mirrors `Enum.find_index/2` with higher performance for Aja structures.
"""
@spec find_index(t(val), (val -> as_boolean(term))) :: non_neg_integer | nil when val: value
def find_index(enumerable, fun) when is_function(fun, 1) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.find_index(enumerable, fun)
list when is_list(list) -> Enum.find_index(list, fun)
vector -> RawVector.find_index(vector, fun)
end
end
## FOLDS
@doc """
Returns the sum of all elements.
Mirrors `Enum.sum/1` with higher performance for Aja structures.
"""
@spec sum(t(num)) :: num when num: number
def sum(enumerable) do
case H.try_get_raw_vec_or_list(enumerable) do
nil ->
Enum.sum(enumerable)
list when is_list(list) ->
:lists.sum(list)
vector ->
RawVector.sum(vector)
end
end
@doc """
Returns the product of all elements in the `enumerable`.
Mirrors `Enum.product/1`.
Raises `ArithmeticError` if `enumerable` contains a non-numeric value.
## Examples
iex> 1..5 |> Aja.Enum.product()
120
iex> [] |> Aja.Enum.product()
1
"""
@spec product(t(num)) :: num when num: number
def product(enumerable) do
case H.try_get_raw_vec_or_list(enumerable) do
nil ->
# TODO use Enum.product/1 for Elixir 1.11
reduce(enumerable, 1, &*/2)
list when is_list(list) ->
product_list(list, 1)
vector ->
RawVector.product(vector)
end
end
defp product_list([], acc), do: acc
defp product_list([head | rest], acc) do
product_list(rest, head * acc)
end
@doc """
Joins the given `enumerable` into a string using `joiner` as a
separator.
Mirrors `Enum.join/2` with higher performance for Aja structures.
"""
@spec join(t(val), String.t()) :: String.t() when val: String.Chars.t()
def join(enumerable, joiner \\ "") when is_binary(joiner) do
case H.try_get_raw_vec_or_list(enumerable) do
nil ->
Enum.join(enumerable, joiner)
list when is_list(list) ->
Enum.join(list, joiner)
vector ->
# TODO add join_as_iodata
RawVector.join_as_iodata(vector, joiner) |> IO.iodata_to_binary()
end
end
@doc """
Maps and joins the given `enumerable` in one pass.
Mirrors `Enum.map_join/3` with higher performance for Aja structures.
"""
@spec map_join(t(val), String.t(), (val -> String.Chars.t())) :: String.t()
when val: value
def map_join(enumerable, joiner \\ "", mapper)
when is_binary(joiner) and is_function(mapper, 1) do
case H.try_get_raw_vec_or_list(enumerable) do
nil ->
Enum.map_join(enumerable, joiner, mapper)
list when is_list(list) ->
Enum.map_join(list, joiner, mapper)
# TODO do this in one pass
vector ->
vector
|> RawVector.map(mapper)
|> RawVector.join_as_iodata(joiner)
|> IO.iodata_to_binary()
end
end
@doc """
Intersperses `separator` between each element of the given `enumerable`.
Mirrors `Enum.intersperse/2` with higher performance for Aja structures.
"""
@spec intersperse(t(val), separator) :: [val | separator] when val: value, separator: value
def intersperse(enumerable, separator) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.intersperse(enumerable, separator)
list when is_list(list) -> Enum.intersperse(list, separator)
vector -> RawVector.intersperse_to_list(vector, separator)
end
end
@doc """
Maps and intersperses the given `enumerable` in one pass.
Mirrors `Enum.map_intersperse/3` with higher performance for Aja structures.
"""
@spec map_intersperse(t(val), separator, (val -> mapped_val)) :: [mapped_val | separator]
when val: value, separator: value, mapped_val: value
def map_intersperse(enumerable, separator, mapper)
when is_function(mapper, 1) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.map_intersperse(enumerable, separator, mapper)
list when is_list(list) -> Enum.map_intersperse(list, separator, mapper)
vector -> RawVector.map_intersperse_to_list(vector, separator, mapper)
end
end
@doc """
Maps the given `fun` over `enumerable` and flattens the result.
Mirrors `Enum.flat_map/2` with higher performance for Aja structures.
"""
@spec flat_map(t(val), (val -> t(mapped_val))) :: [mapped_val]
when val: value, mapped_val: value
defdelegate flat_map(enumerable, fun), to: H
@doc """
Returns a map with keys as unique elements of `enumerable` and values
as the count of every element.
Mirrors `Enum.frequencies/1` with higher performance for Aja structures.
"""
@spec frequencies(t(val)) :: %{optional(val) => non_neg_integer} when val: value
def frequencies(enumerable) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.frequencies(enumerable)
list when is_list(list) -> Enum.frequencies(list)
vector -> RawVector.frequencies(vector)
end
end
@doc """
Returns a map with keys as unique elements given by `key_fun` and values
as the count of every element.
Mirrors `Enum.frequencies_by/2` with higher performance for Aja structures.
"""
@spec frequencies_by(t(val), (val -> key)) :: %{optional(key) => non_neg_integer}
when val: value, key: any
def frequencies_by(enumerable, key_fun) when is_function(key_fun, 1) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.frequencies_by(enumerable, key_fun)
list when is_list(list) -> Enum.frequencies_by(list, key_fun)
vector -> RawVector.frequencies_by(vector, key_fun)
end
end
@doc """
Splits the `enumerable` into groups based on `key_fun`.
Mirrors `Enum.group_by/3` with higher performance for Aja structures.
"""
@spec group_by(t(val), (val -> key), (val -> mapped_val)) :: %{optional(key) => [mapped_val]}
when val: value, key: any, mapped_val: any
def group_by(enumerable, key_fun, value_fun \\ fn x -> x end)
when is_function(key_fun, 1) and is_function(value_fun, 1) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.group_by(enumerable, key_fun, value_fun)
list when is_list(list) -> Enum.group_by(list, key_fun, value_fun)
vector -> RawVector.group_by(vector, key_fun, value_fun)
end
end
@doc """
Invokes the given `fun` for each element in the `enumerable`.
Mirrors `Enum.each/2` with higher performance for Aja structures.
"""
@spec each(t(val), (val -> term)) :: :ok when val: value
def each(enumerable, fun) when is_function(fun, 1) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.each(enumerable, fun)
list when is_list(list) -> :lists.foreach(fun, list)
vector -> RawVector.each(vector, fun)
end
end
## RANDOM
@doc """
Returns a random element of an `enumerable`.
Mirrors `Enum.random/1` with higher performance for Aja structures.
"""
@spec random(t(val)) :: val when val: value
def random(enumerable) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.random(enumerable)
list when is_list(list) -> Enum.random(list)
vector -> RawVector.random(vector)
end
end
@doc """
Takes `count` random elements from `enumerable`.
Mirrors `Enum.take_random/2` with higher performance for Aja structures.
"""
@spec take_random(t(val), non_neg_integer) :: [val] when val: value
def take_random(enumerable, count)
def take_random(_enumerable, 0), do: []
# TODO: optimize 1 for non-empty vectors
def take_random(enumerable, count) do
enumerable
|> H.to_list()
|> Enum.take_random(count)
end
@doc """
Returns a list with the elements of `enumerable` shuffled.
Mirrors `Enum.shuffle/1` with higher performance for Aja structures.
"""
@spec shuffle(t(val)) :: [val] when val: value
def shuffle(enumerable) do
enumerable
|> H.to_list()
|> Enum.shuffle()
end
# UNIQ
@doc """
Enumerates the `enumerable`, returning a list where all consecutive
duplicated elements are collapsed to a single element.
Mirrors `Enum.dedup/1` with higher performance for Aja structures.
"""
@spec dedup(t(val)) :: [val] when val: value
def dedup(enumerable)
def dedup(%MapSet{} = set) do
MapSet.to_list(set)
end
def dedup(enumerable) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.dedup(enumerable)
list when is_list(list) -> dedup_list(list)
vector -> RawVector.dedup_list(vector)
end
end
@doc """
Enumerates the `enumerable`, returning a list where all consecutive
duplicated elements are collapsed to a single element.
Mirrors `Enum.dedup_by/2` with higher performance for Aja structures.
"""
@spec dedup_by(t(val), (val -> term)) :: [val] when val: value
def dedup_by(enumerable, fun) when is_function(fun, 1) do
enumerable
|> H.to_list()
|> Enum.dedup_by(fun)
end
@doc """
Enumerates the `enumerable`, removing all duplicated elements.
Mirrors `Enum.uniq/1` with higher performance for Aja structures.
"""
@spec uniq(t(val)) :: [val] when val: value
def uniq(enumerable)
def uniq(%MapSet{} = set) do
MapSet.to_list(set)
end
def uniq(enumerable) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.uniq(enumerable)
list when is_list(list) -> Enum.uniq(list)
vector -> RawVector.uniq_list(vector)
end
end
@doc """
Enumerates the `enumerable`, by removing the elements for which
function `fun` returned duplicate elements.
Mirrors `Enum.uniq_by/2` with higher performance for Aja structures.
"""
@spec uniq_by(t(val), (val -> term)) :: [val] when val: value
def uniq_by(enumerable, fun) when is_function(fun, 1) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.uniq_by(enumerable, fun)
list when is_list(list) -> Enum.uniq_by(list, fun)
vector -> RawVector.uniq_by_list(vector, fun)
end
end
# ## MIN-MAX
defguardp is_list_or_struct(enumerable)
when is_list(enumerable) or :erlang.map_get(:__struct__, enumerable) |> is_atom()
defguardp is_empty_list_or_vec(list_or_vec)
when list_or_vec === [] or list_or_vec === @empty_vector
@doc false
def min(enumerable) when is_list_or_struct(enumerable) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.min(enumerable)
empty when is_empty_list_or_vec(empty) -> raise Enum.EmptyError
list when is_list(list) -> :lists.min(list)
vector -> RawVector.min(vector)
end
end
@doc false
@spec min(t(val), (-> empty_result)) :: val | empty_result when val: value, empty_result: any
def min(enumerable, empty_fallback) when is_function(empty_fallback, 0) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.min(enumerable, empty_fallback)
empty when is_empty_list_or_vec(empty) -> empty_fallback.()
list when is_list(list) -> :lists.min(list)
vector -> RawVector.min(vector)
end
end
@doc """
Returns the minimal element in the `enumerable` according
to Erlang's term ordering.
Mirrors `Enum.min/3` with higher performance for Aja structures.
"""
@spec min(t(val), (val, val -> boolean) | module, (-> empty_result)) :: val | empty_result
when val: value, empty_result: any
def min(enumerable, sorter \\ &<=/2, empty_fallback \\ fn -> raise Enum.EmptyError end)
when is_function(empty_fallback, 0) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.min(enumerable, sorter, empty_fallback)
@empty_vector -> empty_fallback.()
list when is_list(list) -> Enum.min(list, sorter, empty_fallback)
vector -> RawVector.custom_min_max(vector, min_sort_fun(sorter))
end
end
@doc false
def max(enumerable) when is_list_or_struct(enumerable) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.max(enumerable)
empty when is_empty_list_or_vec(empty) -> raise Enum.EmptyError
list when is_list(list) -> :lists.max(list)
vector -> RawVector.max(vector)
end
end
@doc false
@spec max(t(val), (-> empty_result)) :: val | empty_result when val: value, empty_result: any
def max(enumerable, empty_fallback) when is_function(empty_fallback, 0) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.max(enumerable, empty_fallback)
empty when is_empty_list_or_vec(empty) -> empty_fallback.()
list when is_list(list) -> :lists.max(list)
vector -> RawVector.max(vector)
end
end
@doc """
Returns the maximal element in the `enumerable` according
to Erlang's term ordering.
Mirrors `Enum.max/3` with higher performance for Aja structures.
"""
@spec max(t(val), (val, val -> boolean) | module, (-> empty_result)) :: val | empty_result
when val: value, empty_result: any
def max(enumerable, sorter \\ &>=/2, empty_fallback \\ fn -> raise Enum.EmptyError end)
when is_function(empty_fallback, 0) do
case H.try_get_raw_vec_or_list(enumerable) do
nil -> Enum.max(enumerable, sorter, empty_fallback)
@empty_vector -> empty_fallback.()
list when is_list(list) -> Enum.max(list, sorter, empty_fallback)
vector -> RawVector.custom_min_max(vector, max_sort_fun(sorter))
end
end