-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount.pine
More file actions
1368 lines (1143 loc) · 62.6 KB
/
Count.pine
File metadata and controls
1368 lines (1143 loc) · 62.6 KB
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
// This Pine Script™ code is subject to the terms of the MIT License at https://opensource.org/license/mit/
// © joebaus
//@version=6
//@description Library of functions for counting the number of times (frequency) that elements occur in an array or matrix.
library("Count")
//#region Comparison Check Functions
// validOperator(string op) {
// @function Ensures only valid comparison operators are used for `int` and `float` methods of `count()`.
// @param op The comparison operator string to check for validity.
// @returns Does nothing for valid `op` parameters.
// Returns a `runtime.error()` message for invalid parameters.
validOperator(string op) =>
array<string> validComparisons = array.from('==', '<', '>', '=<', '=>', '!=')
if not validComparisons.includes(op)
runtime.error('Count: An invalid comparison operator string was used! ' +
"Valid operator strings are: '==', '<', '>', '=<', '=>', '!='")
// }
// comparisonCheck(int value1, string op, int value2) {
// @function Checks if two values satisfy a comparison operator.
// @param value1 The first value to compare.
// @param op A comparison operator. Valid strings: `'=='`, `'<'`, `'>'`, `'<='`, `'>='`, or `'!='`.
// @param value2 The second value to compare.
// @returns A `bool` representing the result of the comparison.
// ___
// **Usage**
//
// Check if `1` is less than `2`.
// ```
// bool cc = comparisonCheck(1, '<', 2)
// ```
method comparisonCheck(int value1, string op, int value2) =>
validOperator(op)
switch
op == '==' => value1 == value2
op == '!=' => value1 != value2
op == '<' => value1 < value2
op == '>' => value1 > value2
op == '<=' => value1 <= value2
op == '>=' => value1 >= value2
=> false
// }
// comparisonCheck(float value1, string op, float value2) {
// @function Checks if two values satisfy a comparison operator.
// @param value1 The first value to compare.
// @param op A comparison operator. Valid strings: `'=='`, `'<'`, `'>'`, `'<='`, `'>='`, or `'!='`.
// @param value2 The second value to compare.
// @returns A `bool` representing the result of the comparison.
// ___
// **Usage**
//
// Check if `3.5` is greater than `7.0`.
// ```
// bool cc = comparisonCheck(3.5, '>', 7.0)
// ```
method comparisonCheck(float value1, string op, float value2) =>
validOperator(op)
switch
op == '==' => value1 == value2
op == '!=' => value1 != value2
op == '<' => value1 < value2
op == '>' => value1 > value2
op == '<=' => value1 <= value2
op == '>=' => value1 >= value2
=> false
// }
// comparisonCheck(bool value1, string op, bool value2) {
// @function Checks if two values satisfy a comparison operator.
// @param value1 The first value to compare.
// @param op A comparison operator. Valid strings: `'=='` or `'!='`.
// @param value2 The second value to compare.
// @returns A `bool` representing the result of the comparison.
// ___
// **Usage**
//
// Check if `true` is not equal to `false`.
// ```
// bool cc = comparisonCheck(true, '!=', false)
// ```
method comparisonCheck(bool value1, string op, bool value2) =>
array<string> validComparisons = array.from('==', '!=')
if not validComparisons.includes(op)
runtime.error('Count: An invalid comparison operator string was used! ' +
"Valid operator strings are: '==' and '!='")
switch
op == '==' => value1 == value2
op == '!=' => value1 != value2
=> false
// }
//#endregion Comparison Check Functions
//#region Array to Map Count
// count(array<int> id) {
// @function Counts the number of times each unique element occurs within an `array<int>`.
// @param id The array to count elements from.
// @returns A `map<int, int>` where the `.keys()` are unique `int` elements,
// and `.values()` are each unique element's respective count as an `int`.
//___
// **Usage**
//
// Create a `map<int, int>` count map from an array.
// ```
// array<int> intArray = array.from(2, 2, 2, 3, 4, 4, 4, 4, 4, 6, 6)
// map<int, int> countMap = intArray.count()
// array<int> mapKeys = countMap.keys() // Returns [2, 3, 4, 6]
// array<int> mapValues = countMap.values() // Returns [3, 1, 5, 2]
// ```
export method count(array<int> id) =>
map<int, int> countMap = map.new<int, int>()
for i in id
if countMap.keys().includes(i) // Check if the key already exists in the map.
countMap.put(i, countMap.get(i) + 1) // Increment the key's value by 1.
else // Initalize new key.
countMap.put(i, 1)
countMap
// }
// count(array<float> id) {
// @function Counts the number of times each unique element occurs within an `array<float>`.
// @param id The array to count elements from.
// @returns A `map<float, int>` where the `.keys()` are unique `float` elements,
// and `.values()` are each unique element's respective count as an `int`.
// ___
// **Usage**
//
// Create a `map<float, int>` count map from an array.
// ```
// array<float> floatArray = array.from(1.00, 1.50, 1.25, 1.00, 0.75, 1.25, 1.75, 1.25)
// map<float, int> countMap = floatArray.count()
// array<float> mapKeys = countMap.keys() // Returns [1.0, 1.5, 1.25, 0.75, 1.75]
// array<int> mapValues = countMap.values() // Returns [2, 1, 3, 1, 1]
// ```
export method count(array<float> id) =>
map<float, int> countMap = map.new<float, int>()
for i in id
if countMap.keys().includes(i)
countMap.put(i, countMap.get(i) + 1)
else
countMap.put(i, 1)
countMap
// }
// count(array<string> id) {
// @function Counts the number of times each unique element occurs within an `array<string>`.
// @param id The array to count elements from.
// @returns A `map<string, int>` where the `.keys()` are unique `string` elements,
// and `.values()` are each unique element's respective count as an `int`.
// ___
// **Usage**
//
// Create a `map<string, int>` count map from an array.
// ```
// array<string> stringArray = array.from('bullish', 'bull', 'bullish', 'bear', 'bull', 'bearish', 'bearish')
// map<string, int> countMap = stringArray.count()
// array<string> mapKeys = countMap.keys() // Returns [bullish, bull, bear, bearish]
// array<int> mapValues = countMap.values() // Returns [2, 2, 1, 2]
// ```
export method count(array<string> id) =>
map<string, int> countMap = map.new<string, int>()
for i in id
if countMap.keys().includes(i)
countMap.put(i, countMap.get(i) + 1)
else
countMap.put(i, 1)
countMap
// }
// count(array<bool> id) {
// @function Counts the number of times each unique element occurs within an `array<bool>`.
// @param id The array to count elements from.
// @returns A `map<bool, int>` where the `.keys()` are unique `bool` elements,
// and `.values()` are each unique element's respective count as an `int`.
// ___
// **Usage**
//
// Create a `map<bool, int>` count map from an array.
// ```
// array<bool> boolArray = array.from(true, true, false, true, false, false, true)
// map<bool, int> countMap = boolArray.count()
// array<bool> mapKeys = countMap.keys() // Returns [true, false]
// array<int> mapValues = countMap.values() // Returns [4, 4]
// ```
export method count(array<bool> id) =>
map<bool, int> countMap = map.new<bool, int>()
for i in id
if countMap.keys().includes(i)
countMap.put(i, countMap.get(i) + 1)
else
countMap.put(i, 1)
countMap
// }
//#endregion Array to Map Count
//#region Array Element Count
// count(array<int> id, int value, string op = '==') {
// @function Counts the number of times an element occurs within an `array<int>`.
// @param id The array to count elements from.
// @param value The element to count.
// @param op Optional. A comparison operator. Default is `==`.
// Valid strings: `'=='`, `'<'`, `'>'`, `'<='`, `'>='`, or `'!='`.
// @returns An `int` count representing the number of times `value` occurred inside the `id` array.
// ___
// **Usage**
//
// Count the number of times `4` occurs in an `array<int>`.
// ```
// array<int> intArray = array.from(2, 2, 2, 3, 4, 4, 4, 4, 4, 6, 6)
// int count = intArray.count(4) // Returns 5
// ```
// Count the number of times an element is less than `4`.
// ```
// int countComparison = intArray.count(4, '<') // Returns 4
// ```
export method count(array<int> id, int value, string op = '==') =>
int count = 0
for i in id
if comparisonCheck(i, op, value)
count += 1
count
// }
// count(array<float> id, float value, string op = '==') {
// @function Counts the number of times an element occurs within an `array<float>`.
// @param id The array to count elements from.
// @param value The element to count.
// @param op Optional. A comparison operator. Default is `==`.
// Valid strings: `'=='`, `'<'`, `'>'`, `'<='`, `'>='`, or `'!='`.
// @returns An `int` count representing the number of times `value` occurred inside the `id` array.
// ___
// **Usage**
//
// Check the number of occurrences of `1.0` in an `array<float>`.
// ```
// array<float> floatArray = array.from(1.00, 1.50, 1.25, 1.00, 0.75, 1.25, 1.75, 1.25)
// int count = floatArray.count(1.0) // Returns 2
// ```
// Apply a comparison operator to all elements counted.
// ```
// int countComparison = floatArray.count(1.0, '>') // Returns 5
// ```
export method count(array<float> id, float value, string op = '==') =>
int count = 0
for i in id
if comparisonCheck(i, op, value)
count += 1
count
// }
// count(array<string> id, string regex) {
// @function Counts the number of times an element occurs within an `array<string>`.
// @param id The array to count elements from.
// @param regex The regex pattern used to count elements.
// @returns An `int` count of the number of times `regex` occurred inside the `id` array.
// ___
// **Usage**
//
// Count the number of times the string `'bullish'` occurs in an `array<string>`.
// ```
// array<string> stringArray = array.from('bullish', 'bull', 'bullish', 'bear', 'bull', 'bearish', 'bearish')
// int count = stringArray.count('bullish') // Returns 2
// ```
// Count the number of times the regex pattern `'bear*'` occurs in an `array<string>`.
// ```
// int countRegex = stringArray.count('bear*') // Returns 3
// ```
export method count(array<string> id, string regex) =>
int count = 0
for i in id
if str.match(i, regex) != ''
count += 1
count
// }
// count(array<bool> id, bool value, string op = '==') {
// @function Counts the number of times an element occurs within an `array<bool>`.
// @param id The array to count elements from.
// @param value The element to count.
// @param op Optional. A comparison operator. Default is `==`. Valid strings: `'=='` or `'!='`.
// @returns An `int` of the number of times `value` occurred inside the `id` array.
// ___
// **Usage**
//
// Check the number of occurrences of `true` in an `array<bool>`.
// ```
// array<bool> boolArray = array.from(true, true, false, true, false, false, true)
// int boolCountValue = boolArray.count(true) // Returns 4
// ```
// Apply a comparison operator to all elements counted.
// ```
// int boolCountComparison = boolArray.count(true, '!=') // Returns 3
// ```
export method count(array<bool> id, bool value, string op = '==') =>
int count = 0
for i in id
if comparisonCheck(i, op, value)
count += 1
count
// }
//#endregion Array Element Count
//#region Array Elements Count
// count(array<int> id, array<int> values, string op = '==') {
// @function Counts the number of times a set of elements occurs within an `array<int>`.
// @param id The array to count elements from.
// @param values The elements to count.
// @param op Optional. A comparison operator. Default is `==`.
// Valid strings: `'=='`, `'<'`, `'>'`, `'<='`, `'>='`, or `'!='`.
// @returns A `map<int, int>` where the `.keys()` is an `array<int>` of each unique `values` element,
// and `.values()` is an `array<int> of the number of times each element was counted.
// ___
// **Usage**
//
// Count the number of times multiple elements occur in an array.
// ```
// array<int> intArray = array.from(2, 2, 2, 3, 4, 4, 4, 4, 4, 6, 6)
// array<int> intValues = array.from(2, 4)
//
// map<int, int> countMap = intArray.count(intValues)
// array<int> mapKeys = countMap.keys() // Returns [2, 4]
// array<int> mapValues = countMap.values() // Returns [3, 5]
// ```
// Apply a comparison operator to all elements counted.
// ```
// map<int, int> countMap = intArray.count(intValues, '<')
// array<int> mapKeys = countMap.keys() // Returns [2, 4]
// array<int> mapValues = countMap.values() // Returns [0, 4]
// ```
export method count(array<int> id, array<int> values, string op = '==') =>
map<int, int> countMap = map.new<int, int>()
for i in id
for value in values
if countMap.keys().includes(value)
int count = countMap.get(value)
if comparisonCheck(i, op, value)
count += 1
countMap.put(value, count)
else
countMap.put(value, 1)
countMap
// }
// count(array<int> id, array<int> values, array<string> ops) {
// @function Counts the number of times a set of elements occurs within an `array<int>`.
// @param id The array to count elements from.
// @param values The elements to count.
// @param ops The comparison operator for each `values` element.
// Valid strings: `'=='`, `'<'`, `'>'`, `'<='`, `'>='`, or `'!='`.
// @returns A `map<int, int>` where the `.keys()` is an `array<int>` of each unique `values` element,
// and `.values()` is an `array<int> of the number of times each element was counted.
//
// Returns a runtime error if `values` is not the same size as `ops`.
// ___
// **Usage**
//
// Count the number of times multiple elements occur in an array.
// ```
// array<int> intArray = array.from(2, 2, 2, 3, 4, 4, 4, 4, 4, 6, 6)
// array<int> intValues = array.from(2, 4)
//
// map<int, int> countMap = intArray.count(intValues)
// array<int> mapKeys = countMap.keys() // Returns [2, 4]
// array<int> mapValues = countMap.values() // Returns [3, 5]
// ```
// Apply a unique comparison operator to each value counted.
// ```
// array<string> comparisons = array.from('>', '==')
//
// map<int, int> countMap = intArray.count(intValues, comparisons)
// array<int> mapKeys = countMap.keys() // Returns [2, 3]
// array<int> mapValues = countMap.values() // Returns [8, 1]
// ```
export method count(array<int> id, array<int> values, array<string> ops) =>
if values.size() != ops.size()
runtime.error('count(array<int> id, array<int> values, array<string> ops): ' +
'`values` and `ops` must be the same size!')
map<int, int> countMap = map.new<int, int>()
for i in id
for [j, value] in values
if countMap.keys().includes(value)
int count = countMap.get(value)
string op = ops.get(j)
if comparisonCheck(i, op, value)
count += 1
countMap.put(value, count)
else
countMap.put(value, 1)
countMap
// }
// count(array<float> id, array<float> values, string op = '==') {
// @function Counts the number of times a set of elements occurs within an `array<float>`.
// @param id The array to count elements from.
// @param values The elements to count.
// @param op Optional. A comparison operator. Default is `==`.
// Valid strings: `'=='`, `'<'`, `'>'`, `'<='`, `'>='`, or `'!='`.
// @returns A `map<float, int>` where the `.keys()` is an `array<float>` of each unique `values` element,
// and `.values()` is an `array<int> of the number of times each element was counted.
// ___
// **Usage**
//
// Count the number of times multiple elements occur in an array.
// ```
// array<float> floatArray = array.from(1.00, 1.50, 1.25, 1.00, 0.75, 1.25, 1.75, 1.25)
// array<float> floatValues = array.from(1.00, 1.25)
//
// map<float, int> countMap = floatArray.count(floatValues)
// array<float> mapKeys = countMap.keys() // Returns [1.00, 1.25]
// array<int> mapValues = countMap.values() // Returns [2, 3]
// ```
// Apply a comparison operator to all elements counted.
// ```
// map<float, int> countMap = floatArray.count(floatValues, '<')
// array<float> mapKeys = countMap.keys() // Returns [1.00, 1.25]
// array<int> mapValues = countMap.values() // Returns [1, 3]
// ```
export method count(array<float> id, array<float> values, string op = '==') =>
map<float, int> countMap = map.new<float, int>()
for i in id
for value in values
if countMap.keys().includes(value)
int count = countMap.get(value)
if comparisonCheck(i, op, value)
count += 1
countMap.put(value, count)
else
countMap.put(value, 1)
countMap
// }
// count(array<float> id, array<float> values, array<string> ops) {
// @function Counts the number of times a set of elements occurs within an `array<float>`.
// @param id The array to count elements from.
// @param values The elements to count.
// @param ops The comparison operator for each `values` element.
// Valid strings: `'=='`, `'<'`, `'>'`, `'<='`, `'>='`, or `'!='`.
// @returns A `map<float, int>` where the `.keys()` is an `array<float>` of each unique `values` element,
// and `.values()` is an `array<int> of the number of times each element was counted.
//
// Returns a runtime error if `values` is not the same size as `ops`.
// ___
// **Usage**
//
// Count the number of times multiple elements occur in an array.
// ```
// array<float> floatArray = array.from(1.00, 1.50, 1.25, 1.00, 0.75, 1.25, 1.75, 1.25)
// array<float> floatValues = array.from(1.00, 1.25)
//
// map<float, int> countMap = floatArray.count(floatValues)
// array<float> mapKeys = countMap.keys() // Returns [1.00, 1.25]
// array<int> mapValues = countMap.values() // Returns [2, 3]
// ```
// Apply a unique comparison operator to each value counted.
// ```
// array<string> comparisons = array.from('>', '==')
//
// map<float, int> countMap = floatArray.count(floatValues, comparisons)
// array<float> mapKeys = countMap.keys() // Returns [1.00, 1.25]
// array<int> mapValues = countMap.values() // Returns [5, 2]
// ```
export method count(array<float> id, array<float> values, array<string> ops) =>
if values.size() != ops.size()
runtime.error('count(array<float> id, array<float> values, array<string> ops): ' +
'`values` and `ops` must be the same size!')
map<float, int> countMap = map.new<float, int>()
for i in id
for [j, value] in values
if countMap.keys().includes(value)
int count = countMap.get(value)
string op = ops.get(j)
if comparisonCheck(i, op, value)
count += 1
countMap.put(value, count)
else
countMap.put(value, 1)
countMap
// }
// count(array<string> id, array<string> regexes) {
// @function Counts the number of times a set of regex patterns occurs within an `array<string>`.
// @param id The array to count elements from.
// @param regexes The regex patterns used to count elements.
// @returns A `map<string, int>` where the `.keys()` is an `array<string>` of each unique `regexes` element,
// and `.values()` is an `array<int> of the number of times each element was counted.
// ___
// **Usage**
//
// Count the number of element in `stringArray` that match an array of regex patterns.
// ```
// array<string> stringArray = array.from('bullish', 'bull', 'bullish', 'bearish', 'bull', 'bearish', 'bearish')
// array<string> stringValues = array.from('bull*', 'bear*')
//
// map<string, int> countMap = stringArray.count(stringValues)
// array<string> mapKeys countMap.keys() // Returns ['bull*', 'bear*']
// array<int> mapValues countMap.values() // Returns [4, 3]
// ```
export method count(array<string> id, array<string> regexes) =>
map<string, int> countMap = map.new<string, int>()
for i in id
for regex in regexes
if countMap.keys().includes(regex)
if str.match(i, regex) != ''
countMap.put(regex, countMap.get(regex) + 1)
else
countMap.put(regex, 1)
countMap
// }
// count(array<bool> id, array<bool> values, string op = '==') {
// @function Counts the number of times a set of elements occurs within an `array<bool>`.
// @param id The array to count elements from.
// @param values The elements to count.
// @param op Optional. A comparison operator. Default is `==`. Valid strings: `'=='` or `'!='`.
// @returns A `map<bool, int>` where the `.keys()` is an `array<bool>` of each unique `values` element,
// and `.values()` is an `array<int> of the number of times each element was counted.
// ___
// **Usage**
//
// Count the number of times multiple elements occur in an array.
// ```
// array<bool> boolArray = array.from(true, true, false, true, false, false, true)
// array<bool> boolValues = array.from(true, false)
//
// map<bool, int> countMap = boolArray.count(boolValues)
// array<bool> mapKeys = boolArray.keys() // Returns [true, false]
// array<int> mapValues = boolArray.values() // Returns [4, 3]
// ```
// Apply a comparison operator to all elements counted.
// ```
// map<bool, int> countMap = boolArray.count(boolValues, '<')
// array<bool> mapKeys = countMap.keys() // Returns [true, false]
// array<int> mapValues = countMap.values() // Returns [0, 3]
// ```
export method count(array<bool> id, array<bool> values, string op = '==') =>
map<bool, int> countMap = map.new<bool, int>()
for i in id
for value in values
if countMap.keys().includes(value)
int count = countMap.get(value)
if comparisonCheck(i, op, value)
count += 1
countMap.put(value, count)
else
countMap.put(value, 1)
countMap
// }
// count(array<bool> id, array<bool> values, array<string> ops) {
// @function Counts the number of times an element occurs within an `array<bool>`.
// @param id The array to count elements from.
// @param values The elements to count.
// @param ops The comparison operator for each `values` element. Valid strings: `'=='` or `'!='`.
// @returns A `map<bool, int>` where the `.keys()` is an `array<bool>` of each unique `values` element,
// and `.values()` is an `array<int> of the number of times each element was counted.
//
// Returns a runtime error if `values` is not the same size as `ops`.
// ___
// **Usage**
//
// Count the number of times multiple elements occur in an array.
// ```
// array<bool> boolArray = array.from(true, true, false, true, false, false, true)
// array<bool> boolValues = array.from(true, false)
//
// map<bool, int> countMap = boolArray.count(boolValues)
// array<bool> mapKeys = countMap.keys() // Returns [true, false]
// array<int> mapValues = countMap.values() // Returns [4, 3]
// ```
// Apply a unique comparison operator to each value counted.
// ```
// array<string> comparisons = array.from('!=', '==')
//
// map<bool, int> countMap = boolArray.count(boolValues, comparisons)
// array<bool> mapKeys = countMap.keys() // Returns [true, false]
// array<int> mapValues = countMap.values() // Returns [3, 3]
// ```
export method count(array<bool> id, array<bool> values, array<string> ops) =>
if values.size() != ops.size()
runtime.error('count(array<bool> id, array<bool> values, array<string> ops): ' +
'`values` and `ops` must be the same size!')
map<bool, int> countMap = map.new<bool, int>()
for i in id
for [j, value] in values
if countMap.keys().includes(value)
int count = countMap.get(value)
string op = ops.get(j)
if comparisonCheck(i, op, value)
count += 1
countMap.put(value, count)
else
countMap.put(value, 1)
countMap
// }
//#endregion Array Elements Count
//#region Matrix to Map Count
// count(matrix<int> id) {
// @function Counts the number of times each unique element occurs within a `matrix<int>`.
// @param id The matrix to count elements from.
// @returns A `map<int, int>` where the `.keys()` are unique `int` elements,
// and `.values()` are each unique element's respective count as an `int`.
// ___
// **Usage**
//
// Create a `map<int, int>` count map from a matrix.
// ```
// matrix<int> intMatrix = matrix.new<int>(3, 3, 1)
// intMatrix.set(0, 0, 2), intMatrix.set(1, 0, 3), intMatrix.set(2, 0, 5)
// intMatrix.set(0, 1, 2), intMatrix.set(1, 1, 4), intMatrix.set(2, 1, 2)
// intMatrix.set(0, 2, 5), intMatrix.set(1, 2, 2), intMatrix.set(2, 2, 3)
//
// map<int, int> intMatrixCountMap = intMatrix.count()
// array<int> mapKeys = intMatrixCountMap.keys() // Returns [2, 3, 4, 5]
// array<int> mapValues = intMatrixCountMap.values() // Returns [4, 2, 1, 2]
// ```
export method count(matrix<int> id) =>
map<int, int> countMap = map.new<int, int>()
for row = 0 to id.rows() - 1
for col = 0 to id.columns() - 1
int element = id.get(row, col)
if countMap.keys().includes(element)
countMap.put(element, countMap.get(element) + 1)
else
countMap.put(element, 1)
countMap
// }
// count(matrix<float> id) {
// @function Counts the number of times each unique element occurs within a `matrix<float>`.
// @param id The matrix to count elements from.
// @returns A `map<float, int>` where the `.keys()` are unique `float` elements,
// and the `.value` object is that element's count as an `int`.
// ___
// **Usage**
//
// Create a `map<float, int>` count map from a matrix.
// ```
// matrix<float> floatMatrix = matrix.new<float>(3, 3, 0.0)
// floatMatrix.set(0, 0, 1.0), floatMatrix.set(1, 0, 1.0), floatMatrix.set(2, 0, 1.0)
// floatMatrix.set(0, 1, 1.5), floatMatrix.set(1, 1, 2.0), floatMatrix.set(2, 1, 2.5)
// floatMatrix.set(0, 2, 1.0), floatMatrix.set(1, 2, 2.5), floatMatrix.set(2, 2, 1.5)
//
// map<float, int> floatMatrixCountMap = floatMatrix.count()
// array<float> mapKeys = floatMatrixCountMap.keys() // Returns [1.0, 1.5, 2.0, 2.5]
// array<int> mapValues = floatMatrixCountMap.values() // Returns [3, 2, 1, 2]
// ```
export method count(matrix<float> id) =>
map<float, int> countMap = map.new<float, int>()
for row = 0 to id.rows() - 1
for col = 0 to id.columns() - 1
float element = id.get(row, col)
if countMap.keys().includes(element)
countMap.put(element, countMap.get(element) + 1)
else
countMap.put(element, 1)
countMap
// }
// count(matrix<string> id) {
// @function Counts the number of times each unique element occurs within a `matrix<string>`.
// @param id The matrix to count elements from.
// @returns A `map<string, int>` where the `.keys()` are unique `string` elements,
// and `.values()` are each unique element's respective count as an `int`.
// ___
// **Usage**
//
// Create a `map<string, int>` count map from a matrix.
// ```
// matrix<string> stringMatrix = matrix.new<string>(3, 3, '')
// stringMatrix.set(0, 0, 'a'), stringMatrix.set(1, 0, 'a'), stringMatrix.set(2, 0, 'a')
// stringMatrix.set(0, 1, 'b'), stringMatrix.set(1, 1, 'c'), stringMatrix.set(2, 1, 'd')
// stringMatrix.set(0, 2, 'a'), stringMatrix.set(1, 2, 'd'), stringMatrix.set(2, 2, 'b')
//
// map<string, int> stringMatrixCountMap = stringMatrix.count()
// array<string> mapKeys = stringMatrixCountMap.keys(): ['a', 'b', 'c', 'd']
// array<int> mapValues = stringMatrixCountMap.values() // Returns [3, 2, 1, 2]
// ```
export method count(matrix<string> id) =>
map<string, int> countMap = map.new<string, int>()
for row = 0 to id.rows() - 1
for col = 0 to id.columns() - 1
string element = id.get(row, col)
if countMap.keys().includes(element)
countMap.put(element, countMap.get(element) + 1)
else
countMap.put(element, 1)
countMap
// }
// count(matrix<bool> id) {
// @function Counts the number of times each unique element occurs within a `matrix<bool>`.
// @param id The matrix to count elements from.
// @returns A `map<bool, int>` where the `.keys()` are unique `bool` elements,
// and `.values()` are each unique element's respective count as an `int`.
// ___
// **Usage**
//
// Create a `map<bool, int>` count map from a matrix.
// ```
// matrix<bool> boolMatrix = matrix.new<bool>(3, 3, false)
// boolMatrix.set(0, 0, true), boolMatrix.set(1, 0, true), boolMatrix.set(2, 0, true)
// boolMatrix.set(0, 1, false), boolMatrix.set(1, 1, false), boolMatrix.set(2, 1, false)
// boolMatrix.set(0, 2, true), boolMatrix.set(1, 2, false), boolMatrix.set(2, 2, true)
//
// map<bool, int> boolMatrixCountMap = boolMatrix.count()
// array<bool> mapKeys = boolMatrixCountMap.keys(): [true, false]
// array<bool> mapValues = boolMatrixCountMap.values(): [5, 4]
// ```
export method count(matrix<bool> id) =>
map<bool, int> countMap = map.new<bool, int>()
for row = 0 to id.rows() - 1
for col = 0 to id.columns() - 1
bool element = id.get(row, col)
if countMap.keys().includes(element)
countMap.put(element, countMap.get(element) + 1)
else
countMap.put(element, 1)
countMap
// }
//#endregion Matrix to Map Count
//#region Element Matrix Count
// count(matrix<int> id, int value, string op = '==') {
// @function Counts the number of times an element occurs within a `matrix<int>`.
// @param id The matrix to count elements from.
// @param value The element to count.
// @param op Optional. A comparison operator. Default is `==`.
// Valid strings: `'=='`, `'<'`, `'>'`, `'<='`, `'>='`, or `'!='`.
// @returns An `int` count of the number of times `value` occurred inside the `id` matrix.
// ___
// **Usage**
//
// Count the number of times `4` occurs in a `matrix<int>`.
// ```
// matrix<int> intMatrix = matrix.new<int>(3, 3, 1)
// intMatrix.set(0, 0, 2), intMatrix.set(1, 0, 3), intMatrix.set(2, 0, 5)
// intMatrix.set(0, 1, 2), intMatrix.set(1, 1, 4), intMatrix.set(2, 1, 2)
// intMatrix.set(0, 2, 5), intMatrix.set(1, 2, 2), intMatrix.set(2, 2, 3)
//
// int intMatrixCount = intMatrix.count(4) // Returns 5
// ```
// Count the number of times an element is less than `4`.
// ```
// int intMatrixCountComparison = intMatrix.count(4, '<') // Returns 5
// ```
export method count(matrix<int> id, int value, string op = '==') =>
int count = 0
for row = 0 to id.rows() - 1
for col = 0 to id.columns() - 1
int element = id.get(row, col)
if comparisonCheck(element, op, value)
count += 1
count
// }
// count(matrix<float> id, float value, string op = '==') {
// @function Counts the number of times an element occurs within a `matrix<float>`.
// @param id The matrix to count elements from.
// @param value The element to count.
// @param op Optional. A comparison operator. Default is `==`.
// Valid strings: `'=='`, `'<'`, `'>'`, `'<='`, `'>='`, or `'!='`.
// @returns An `int` count of the number of times `value` occurred inside the `id` matrix.
// ___
// **Usage**
//
// Count the number of times `4` occurs in a `matrix<float>`.
// ```
// matrix<float> floatMatrix = matrix.new<float>(3, 3, 0.0)
// floatMatrix.set(0, 0, 1.0), floatMatrix.set(1, 0, 1.0), floatMatrix.set(2, 0, 1.0)
// floatMatrix.set(0, 1, 1.5), floatMatrix.set(1, 1, 2.0), floatMatrix.set(2, 1, 2.5)
// floatMatrix.set(0, 2, 1.0), floatMatrix.set(1, 2, 2.5), floatMatrix.set(2, 2, 1.5)
//
// int floatMatrixCount = matrixFloat.count(4)
// ```
// Count the number of times an element is less than `4`.
// ```
// int floatMatrixCountComparison = floatMatrix.count(1.0, '>') // Returns 5
// ```
export method count(matrix<float> id, float value, string op = '==') =>
int count = 0
for row = 0 to id.rows() - 1
for col = 0 to id.columns() - 1
float element = id.get(row, col)
if comparisonCheck(element, op, value)
count += 1
count
// }
// count(matrix<string> id, string regex) {
// @function Counts the number of times an element occurs within a `matrix<string>`.
// @param id The matrix to count elements from.
// @param regex The regex to count.
// @returns An `int` count of the number of times `regex` occurred inside the `id` matrix.
// ___
// **Usage**
//
// Count the number of times `'a'` occurs in a `matrix<string>`.
// ```
// matrix<string> stringMatrix = matrix.new<string>(3, 3, '')
// stringMatrix.set(0, 0, 'a'), stringMatrix.set(1, 0, 'a'), stringMatrix.set(2, 0, 'a')
// stringMatrix.set(0, 1, 'b'), stringMatrix.set(1, 1, 'c'), stringMatrix.set(2, 1, 'd')
// stringMatrix.set(0, 2, 'a'), stringMatrix.set(1, 2, 'd'), stringMatrix.set(2, 2, 'b')
//
// int stringMatrixCount = stringMatrix.count('a') // Returns 3
// ```
export method count(matrix<string> id, string regex) =>
int count = 0
for row = 0 to id.rows() - 1
for col = 0 to id.columns() - 1
if str.match(id.get(row, col), regex) != ''
count += 1
count
// }
// count(matrix<bool> id, bool value, string op = '==') {
// @function Counts the number of times an element occurs within a `matrix<bool>`.
// @param id The matrix to count elements from.
// @param value The `bool` value to count occurrences of.
// @param op Optional. A comparison operator. Default is `==`. Valid strings: '=='` or `'!='`.
// @returns An `int` of the number of times `value` occurred in the matrix.
// ___
// **Usage**
//
// Count the number of times `true` occurs in a `matrix<bool>`.
// ```
// matrix<bool> boolMatrix = matrix.new<bool>(3, 3, false)
// boolMatrix.set(0, 0, true), boolMatrix.set(1, 0, true), boolMatrix.set(2, 0, true)
// boolMatrix.set(0, 1, false), boolMatrix.set(1, 1, false), boolMatrix.set(2, 1, false)
// boolMatrix.set(0, 2, true), boolMatrix.set(1, 2, false), boolMatrix.set(2, 2, true)
//
// int boolMatrixCount = boolMatrix.count(true) // Returns 5
// ```
// Count the number of times an element is not `true`.
// ```
// int boolMatrixCountComparison = boolMatrix.count(true, '!=') // Returns 4
// ```
export method count(matrix<bool> id, bool value, string op = '==') =>
int count = 0
for row = 0 to id.rows() - 1
for col = 0 to id.columns() - 1
bool element = id.get(row, col)
if comparisonCheck(element, op, value)
count += 1
count
// }
//#endregion Element Matrix Count
//#region Elements Matrix Count
// count(matrix<int> id, array<int> values, string op = '==') {
// @function Counts the number of times a set of elements occurs within a `matrix<int>`.
// @param id The matrix to count elements from.
// @param values The elements to count.