FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
jruby/core/src/main/java/org/jruby/RubyEnumerable.java at master · jruby/jruby · GitHub
jruby/core/src/main/java/org/jruby/RubyEnumerable.java at master · jruby/jruby · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
Accelerator
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
jruby
/
jruby
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
946
Star
3.9k
Code
Issues
840
Pull requests
109
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
jruby
/
core
/
src
/
main
/
java
/
org
/
jruby
/
RubyEnumerable.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
2302 lines (1955 loc) · 101 KB
Breadcrumbs
jruby
/
core
/
src
/
main
/
java
/
org
/
jruby
/
RubyEnumerable.java
Copy path
File metadata and controls
2302 lines (1955 loc) · 101 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 2.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v20.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2006 Ola Bini <ola@ologix.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package
org
.
jruby
;
import
org
.
jruby
.
anno
.
JRubyMethod
;
import
org
.
jruby
.
anno
.
JRubyModule
;
import
org
.
jruby
.
exceptions
.
JumpException
;
import
org
.
jruby
.
exceptions
.
RaiseException
;
import
org
.
jruby
.
runtime
.
Block
;
import
org
.
jruby
.
runtime
.
BlockBody
;
import
org
.
jruby
.
runtime
.
BlockCallback
;
import
org
.
jruby
.
runtime
.
CallBlock
;
import
org
.
jruby
.
runtime
.
CallBlock19
;
import
org
.
jruby
.
runtime
.
CallSite
;
import
org
.
jruby
.
runtime
.
ClassIndex
;
import
org
.
jruby
.
runtime
.
Helpers
;
import
org
.
jruby
.
runtime
.
JavaInternalBlockBody
;
import
org
.
jruby
.
runtime
.
JavaSites
.
EnumerableSites
;
import
org
.
jruby
.
runtime
.
Signature
;
import
org
.
jruby
.
runtime
.
ThreadContext
;
import
org
.
jruby
.
runtime
.
Visibility
;
import
org
.
jruby
.
runtime
.
builtin
.
IRubyObject
;
import
org
.
jruby
.
runtime
.
builtin
.
InternalVariables
;
import
org
.
jruby
.
runtime
.
callsite
.
CachingCallSite
;
import
org
.
jruby
.
runtime
.
callsite
.
MonomorphicCallSite
;
import
org
.
jruby
.
util
.
TypeConverter
;
import
org
.
jruby
.
util
.
collections
.
DoubleObject
;
import
org
.
jruby
.
util
.
collections
.
SingleBoolean
;
import
org
.
jruby
.
util
.
collections
.
SingleDouble
;
import
org
.
jruby
.
util
.
collections
.
SingleInt
;
import
org
.
jruby
.
util
.
collections
.
SingleLong
;
import
org
.
jruby
.
util
.
collections
.
SingleObject
;
import
org
.
jruby
.
util
.
func
.
ObjectObjectIntFunction
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Collections
;
import
java
.
util
.
List
;
import
java
.
util
.
concurrent
.
atomic
.
AtomicInteger
;
import
static
org
.
jruby
.
RubyEnumerator
.
SizeFn
;
import
static
org
.
jruby
.
RubyEnumerator
.
enumeratorize
;
import
static
org
.
jruby
.
RubyEnumerator
.
enumeratorizeWithSize
;
import
static
org
.
jruby
.
RubyObject
.
equalInternal
;
import
static
org
.
jruby
.
api
.
Access
.
arrayClass
;
import
static
org
.
jruby
.
api
.
Access
.
enumerableModule
;
import
static
org
.
jruby
.
api
.
Access
.
hashClass
;
import
static
org
.
jruby
.
api
.
Convert
.
asFixnum
;
import
static
org
.
jruby
.
api
.
Convert
.
asFloat
;
import
static
org
.
jruby
.
api
.
Convert
.
asSymbol
;
import
static
org
.
jruby
.
api
.
Convert
.
toInt
;
import
static
org
.
jruby
.
api
.
Convert
.
toLong
;
import
static
org
.
jruby
.
api
.
Create
.*;
import
static
org
.
jruby
.
api
.
Define
.
defineModule
;
import
static
org
.
jruby
.
api
.
Error
.*;
import
static
org
.
jruby
.
api
.
Warn
.
warn
;
import
static
org
.
jruby
.
runtime
.
Helpers
.
arrayOf
;
import
static
org
.
jruby
.
runtime
.
Helpers
.
invokedynamic
;
import
static
org
.
jruby
.
runtime
.
builtin
.
IRubyObject
.
NULL_ARRAY
;
import
static
org
.
jruby
.
runtime
.
invokedynamic
.
MethodNames
.
OP_CMP
;
/**
* The implementation of Ruby's Enumerable module.
*/
@
JRubyModule
(
name
=
"Enumerable"
)
public
class
RubyEnumerable
{
public
static
RubyModule
createEnumerableModule
(
ThreadContext
context
) {
return
defineModule
(
context
,
"Enumerable"
).
defineMethods
(
context
,
RubyEnumerable
.
class
);
}
public
static
IRubyObject
callEach
(
ThreadContext
context
,
IRubyObject
self
,
Signature
signature
,
BlockCallback
callback
) {
return
callEach
(
context
,
eachSite
(
context
),
self
,
signature
,
callback
);
}
public
static
IRubyObject
callEach
(
ThreadContext
context
,
CallSite
each
,
IRubyObject
self
,
Signature
signature
,
BlockCallback
callback
) {
return
each
.
call
(
context
,
self
,
self
,
CallBlock
.
newCallClosure
(
context
,
self
,
signature
,
callback
));
}
public
static
IRubyObject
callEach
(
ThreadContext
context
,
CallSite
each
,
IRubyObject
self
,
BlockCallback
callback
) {
return
each
.
call
(
context
,
self
,
self
,
CallBlock
.
newCallClosure
(
context
,
self
,
Signature
.
OPTIONAL
,
callback
));
}
public
static
IRubyObject
callEach
(
ThreadContext
context
,
CallSite
each
,
IRubyObject
self
,
IRubyObject
[]
args
,
Signature
signature
,
BlockCallback
callback
) {
return
each
.
call
(
context
,
self
,
self
,
args
,
CallBlock
.
newCallClosure
(
context
,
self
,
signature
,
callback
));
}
public
static
IRubyObject
callEach
(
ThreadContext
context
,
CallSite
each
,
IRubyObject
self
,
IRubyObject
arg0
,
Signature
signature
,
BlockCallback
callback
) {
return
each
.
call
(
context
,
self
,
self
,
arg0
,
CallBlock
.
newCallClosure
(
context
,
self
,
signature
,
callback
));
}
public
static
IRubyObject
each
(
ThreadContext
context
,
CallSite
site
,
IRubyObject
self
,
BlockBody
body
) {
return
site
.
call
(
context
,
self
,
self
,
new
Block
(
body
,
context
.
currentBinding
(
self
,
Visibility
.
PUBLIC
)));
}
private
static
void
checkContext
(
ThreadContext
firstContext
,
ThreadContext
secondContext
,
String
name
) {
if
(
firstContext
!=
secondContext
) {
throw
secondContext
.
runtime
.
newThreadError
(
"Enumerable#"
+
name
+
" cannot be parallelized"
);
}
}
@
JRubyMethod
(
name
=
"count"
)
public
static
IRubyObject
count
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
countCommon
(
context
,
eachSite
(
context
),
self
,
block
);
}
private
static
IRubyObject
countCommon
(
ThreadContext
context
,
CallSite
each
,
IRubyObject
self
,
final
Block
block
) {
final
SingleInt
result
=
new
SingleInt
();
if
(
block
.
isGiven
()) {
each
(
context
,
each
,
self
,
new
JavaInternalBlockBody
(
context
.
runtime
,
context
,
"Enumerable#count"
,
block
.
getSignature
()) {
public
IRubyObject
yield
(
ThreadContext
context1
,
IRubyObject
[]
args
) {
return
this
.
yield
(
context1
,
packEnumValues
(
context1
,
args
));
}
@
Override
public
IRubyObject
yield
(
ThreadContext
context1
,
IRubyObject
value
) {
if
(
block
.
yield
(
context1
,
value
).
isTrue
())
result
.
i
++;
return
context1
.
nil
;
}
});
}
else
{
each
(
context
,
each
,
self
,
new
JavaInternalBlockBody
(
context
.
runtime
,
context
,
"Enumerable#count"
,
Signature
.
NO_ARGUMENTS
) {
public
IRubyObject
yield
(
ThreadContext
context1
,
IRubyObject
[]
args
) {
result
.
i
++;
return
context1
.
nil
;
}
@
Override
public
IRubyObject
yield
(
ThreadContext
context1
,
IRubyObject
value
) {
result
.
i
++;
return
context1
.
nil
;
}
});
}
return
asFixnum
(
context
,
result
.
i
);
}
@
JRubyMethod
(
name
=
"count"
)
public
static
IRubyObject
count
(
ThreadContext
context
,
IRubyObject
self
,
final
IRubyObject
methodArg
,
final
Block
block
) {
final
SingleInt
result
=
new
SingleInt
();
if
(
block
.
isGiven
())
warn
(
context
,
"given block not used"
);
each
(
context
,
eachSite
(
context
),
self
,
new
JavaInternalBlockBody
(
context
.
runtime
,
context
,
"Enumerable#count"
,
Signature
.
ONE_REQUIRED
) {
public
IRubyObject
yield
(
ThreadContext
context1
,
IRubyObject
[]
args
) {
return
this
.
yield
(
context1
,
packEnumValues
(
context1
,
args
));
}
@
Override
public
IRubyObject
yield
(
ThreadContext
context1
,
IRubyObject
value
) {
if
(
value
.
equals
(
methodArg
))
result
.
i
++;
return
context1
.
nil
;
}
});
return
asFixnum
(
context
,
result
.
i
);
}
@
JRubyMethod
public
static
IRubyObject
cycle
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
if
(!
block
.
isGiven
()) {
return
enumeratorizeWithSize
(
context
,
self
,
"cycle"
,
RubyEnumerable
::
cycleSize
);
}
return
cycleCommon
(
context
,
self
, -
1
,
block
);
}
@
JRubyMethod
public
static
IRubyObject
cycle
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
arg
,
final
Block
block
) {
if
(
arg
.
isNil
())
return
cycle
(
context
,
self
,
block
);
if
(!
block
.
isGiven
()) {
return
enumeratorizeWithSize
(
context
,
self
,
"cycle"
,
new
IRubyObject
[] {
arg
},
RubyEnumerable
::
cycleSize
);
}
long
times
=
toLong
(
context
,
arg
);
if
(
times
<=
0
)
return
context
.
nil
;
return
cycleCommon
(
context
,
self
,
times
,
block
);
}
/*
* @param nv number of times to cycle or -1 to cycle indefinitely
*/
private
static
IRubyObject
cycleCommon
(
ThreadContext
context
,
IRubyObject
self
,
long
nv
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
runtime
;
final
List
<
IRubyObject
>
result
=
new
ArrayList
<>();
each
(
context
,
eachSite
(
context
),
self
,
new
JavaInternalBlockBody
(
runtime
,
Signature
.
OPTIONAL
) {
@
Override
public
IRubyObject
yield
(
ThreadContext
context1
,
IRubyObject
[]
args
) {
return
doYield
(
context1
,
null
,
packEnumValues
(
context1
,
args
));
}
@
Override
public
IRubyObject
doYield
(
ThreadContext
context1
,
Block
unused
,
IRubyObject
args
) {
synchronized
(
result
) {
result
.
add
(
args
); }
block
.
yield
(
context1
,
args
);
return
context1
.
nil
;
}
});
int
length
=
result
.
size
();
if
(
length
==
0
)
return
context
.
nil
;
while
(
nv
<
0
||
0
< --
nv
) {
for
(
int
i
=
0
;
i
<
length
;
i
++) {
block
.
yield
(
context
,
result
.
get
(
i
));
}
}
return
context
.
nil
;
}
/**
* A cycle size method suitable for lambda method reference implementation of {@link SizeFn#size(ThreadContext, IRubyObject, IRubyObject[])}
*
* @see SizeFn#size(ThreadContext, IRubyObject, IRubyObject[])
*/
private
static
IRubyObject
cycleSize
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
[]
args
) {
long
mul
=
0
;
IRubyObject
n
=
context
.
nil
;
if
(
args
!=
null
&&
args
.
length
>
0
) {
n
=
args
[
0
];
if
(!
n
.
isNil
())
mul
=
toLong
(
context
,
n
);
}
IRubyObject
size
=
size
(
context
,
self
,
args
);
if
(
size
==
null
||
size
.
isNil
() ||
size
.
equals
(
asFixnum
(
context
,
0
)))
return
size
;
if
(
n
==
null
||
n
.
isNil
())
return
asFloat
(
context
,
RubyFloat
.
INFINITY
);
if
(
mul
<=
0
)
return
asFixnum
(
context
,
0
);
return
sites
(
context
).
cycle_op_mul
.
call
(
context
,
size
,
size
,
mul
);
}
@
JRubyMethod
(
name
=
"take"
)
public
static
IRubyObject
take
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
n
,
Block
block
) {
final
long
len
=
toLong
(
context
,
n
);
if
(
len
<
0
)
throw
argumentError
(
context
,
"attempt to take negative size"
);
if
(
len
==
0
)
return
newEmptyArray
(
context
);
final
var
result
=
newArray
(
context
);
try
{
// Atomic ?
each
(
context
,
eachSite
(
context
),
self
,
new
JavaInternalBlockBody
(
context
.
runtime
,
Signature
.
OPTIONAL
) {
long
i
=
len
;
// Atomic ?
@
Override
public
IRubyObject
yield
(
ThreadContext
context1
,
IRubyObject
[]
args
) {
return
this
.
yield
(
context1
,
packEnumValues
(
context1
,
args
));
}
@
Override
public
IRubyObject
yield
(
ThreadContext
context1
,
IRubyObject
value
) {
synchronized
(
result
) {
result
.
append
(
context
,
value
);
if
(--
i
==
0
)
throw
JumpException
.
SPECIAL_JUMP
;
}
return
context1
.
nil
;
}
});
}
catch
(
JumpException
.
SpecialJump
e
) {}
return
result
;
}
@
JRubyMethod
(
name
=
"take_while"
)
public
static
IRubyObject
take_while
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
if
(!
block
.
isGiven
())
return
enumeratorize
(
context
.
runtime
,
self
,
"take_while"
);
final
var
result
=
newArray
(
context
);
try
{
callEach
(
context
,
eachSite
(
context
),
self
,
Signature
.
OPTIONAL
, (
ctx
,
largs
,
unused
) -> {
final
IRubyObject
larg
;
boolean
ary
=
false
;
switch
(
largs
.
length
) {
case
0
:
larg
=
ctx
.
nil
;
break
;
case
1
:
larg
=
largs
[
0
];
break
;
default
:
larg
=
RubyArray
.
newArrayMayCopy
(
ctx
.
runtime
,
largs
);
ary
=
true
;
}
IRubyObject
val
=
ary
?
block
.
yieldArray
(
ctx
,
larg
,
null
) :
block
.
yield
(
ctx
,
larg
);
if
( !
val
.
isTrue
() )
throw
JumpException
.
SPECIAL_JUMP
;
synchronized
(
result
) {
result
.
append
(
context
,
larg
); }
return
ctx
.
nil
;
});
}
catch
(
JumpException
.
SpecialJump
ignored
) {}
return
result
;
}
@
JRubyMethod
(
name
=
"drop"
)
public
static
IRubyObject
drop
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
n
,
final
Block
block
) {
final
long
len
=
toLong
(
context
,
n
);
if
(
len
<
0
)
throw
argumentError
(
context
,
"attempt to drop negative size"
);
final
var
result
=
newArray
(
context
);
try
{
// Atomic ?
each
(
context
,
eachSite
(
context
),
self
,
new
JavaInternalBlockBody
(
context
.
runtime
,
Signature
.
OPTIONAL
) {
long
i
=
len
;
// Atomic ?
@
Override
public
IRubyObject
yield
(
ThreadContext
context1
,
IRubyObject
[]
args
) {
return
this
.
yield
(
context1
,
packEnumValues
(
context1
,
args
));
}
@
Override
public
IRubyObject
yield
(
ThreadContext
context1
,
IRubyObject
value
) {
synchronized
(
result
) {
if
(
i
==
0
) {
result
.
append
(
context
,
value
);
}
else
{
--
i
;
}
}
return
context1
.
nil
;
}
});
}
catch
(
JumpException
.
SpecialJump
e
) {}
return
result
;
}
@
JRubyMethod
public
static
IRubyObject
drop_while
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
if
(!
block
.
isGiven
())
return
enumeratorize
(
context
.
runtime
,
self
,
"drop_while"
);
final
var
result
=
newArray
(
context
);
try
{
each
(
context
,
eachSite
(
context
),
self
,
new
JavaInternalBlockBody
(
context
.
runtime
,
context
,
"Enumerable#drop_while"
,
Signature
.
OPTIONAL
) {
boolean
memo
=
false
;
@
Override
public
IRubyObject
yield
(
ThreadContext
context1
,
IRubyObject
[]
args
) {
return
this
.
yield
(
context1
,
packEnumValues
(
context1
,
args
));
}
@
Override
public
IRubyObject
yield
(
ThreadContext
context1
,
IRubyObject
value
) {
if
(!
memo
&& !
block
.
yield
(
context1
,
value
).
isTrue
())
memo
=
true
;
if
(
memo
)
synchronized
(
result
) {
result
.
append
(
context
,
value
); }
return
context1
.
nil
;
}
});
}
catch
(
JumpException
.
SpecialJump
sj
) {}
return
result
;
}
@
JRubyMethod
(
name
=
"first"
)
public
static
IRubyObject
first
(
ThreadContext
context
,
IRubyObject
self
) {
final
SingleObject
<
IRubyObject
>
holder
=
new
SingleObject
(
context
.
nil
);
try
{
each
(
context
,
eachSite
(
context
),
self
,
new
JavaInternalBlockBody
(
context
.
runtime
,
context
,
"Enumerable#first"
,
Signature
.
OPTIONAL
) {
@
Override
public
IRubyObject
yield
(
ThreadContext
context1
,
IRubyObject
[]
args
) {
return
this
.
yield
(
context1
,
packEnumValues
(
context1
,
args
));
}
@
Override
public
IRubyObject
yield
(
ThreadContext
context1
,
IRubyObject
value
) {
holder
.
object
=
value
;
throw
JumpException
.
SPECIAL_JUMP
;
}
});
}
catch
(
JumpException
.
SpecialJump
sj
) {}
return
holder
.
object
;
}
@
JRubyMethod
(
name
=
"first"
)
public
static
IRubyObject
first
(
ThreadContext
context
,
IRubyObject
self
,
final
IRubyObject
num
) {
final
long
firstCount
=
toLong
(
context
,
num
);
if
(
firstCount
==
0
)
return
newEmptyArray
(
context
);
if
(
firstCount
<
0
)
throw
argumentError
(
context
,
"attempt to take negative size"
);
final
RubyArray
<?>
result
=
allocArray
(
context
,
firstCount
);
try
{
each
(
context
,
eachSite
(
context
),
self
,
new
JavaInternalBlockBody
(
context
.
runtime
,
context
,
"Enumerable#first"
,
Signature
.
OPTIONAL
) {
private
long
iter
=
firstCount
;
@
Override
public
IRubyObject
yield
(
ThreadContext
context1
,
IRubyObject
[]
args
) {
return
this
.
yield
(
context1
,
packEnumValues
(
context1
,
args
));
}
@
Override
public
IRubyObject
yield
(
ThreadContext
context1
,
IRubyObject
value
) {
result
.
append
(
context
,
value
);
if
(
iter
-- ==
1
)
throw
JumpException
.
SPECIAL_JUMP
;
return
context1
.
nil
;
}
});
}
catch
(
JumpException
.
SpecialJump
sj
) {}
return
result
;
}
@
JRubyMethod
public
static
IRubyObject
tally
(
ThreadContext
context
,
IRubyObject
self
) {
RubyHash
result
=
newHash
(
context
);
callEach
(
context
,
eachSite
(
context
),
self
,
Signature
.
NO_ARGUMENTS
,
new
TallyCallback
(
result
));
return
result
;
}
@
JRubyMethod
public
static
IRubyObject
tally
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
hashArg
) {
RubyHash
result
= (
RubyHash
)
TypeConverter
.
convertToType
(
hashArg
,
hashClass
(
context
),
"to_hash"
);
result
.
checkFrozen
();
callEach
(
context
,
eachSite
(
context
),
self
,
Signature
.
NO_ARGUMENTS
,
new
TallyCallback
(
result
));
return
result
;
}
@
JRubyMethod
(
name
= {
"to_a"
,
"entries"
},
keywords
=
true
)
public
static
IRubyObject
to_a
(
ThreadContext
context
,
IRubyObject
self
) {
var
result
=
newArray
(
context
);
callEach
(
context
,
eachSite
(
context
),
self
,
Signature
.
OPTIONAL
,
new
AppendBlockCallback
(
result
));
return
result
;
}
@
JRubyMethod
(
name
= {
"to_a"
,
"entries"
},
keywords
=
true
)
public
static
IRubyObject
to_a
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
arg
) {
var
result
=
newArray
(
context
);
Helpers
.
invoke
(
context
,
self
,
"each"
,
arg
,
CallBlock
.
newCallClosure
(
context
,
self
,
Signature
.
OPTIONAL
,
new
AppendBlockCallback
(
result
)));
return
result
;
}
@
JRubyMethod
(
name
= {
"to_a"
,
"entries"
},
rest
=
true
,
keywords
=
true
)
public
static
IRubyObject
to_a
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
[]
args
) {
final
var
result
=
newArray
(
context
);
Helpers
.
invoke
(
context
,
self
,
"each"
,
args
,
CallBlock
.
newCallClosure
(
context
,
self
,
Signature
.
OPTIONAL
,
new
AppendBlockCallback
(
result
)));
return
result
;
}
@
JRubyMethod
(
name
=
"to_h"
,
rest
=
true
)
public
static
IRubyObject
to_h
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
[]
args
,
Block
block
) {
final
RubyHash
result
=
newHash
(
context
);
Helpers
.
invoke
(
context
,
self
,
"each"
,
args
,
CallBlock
.
newCallClosure
(
context
,
self
,
Signature
.
OPTIONAL
,
new
PutKeyValueCallback
(
result
,
block
)));
return
result
;
}
@
JRubyMethod
public
static
IRubyObject
sort
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
final
var
result
=
newArray
(
context
);
callEach
(
context
,
eachSite
(
context
),
self
,
Signature
.
OPTIONAL
,
new
AppendBlockCallback
(
result
));
result
.
sort_bang
(
context
,
block
);
return
result
;
}
@
JRubyMethod
public
static
IRubyObject
sort_by
(
final
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
runtime
;
List
<
DoubleObject
<
IRubyObject
,
IRubyObject
>>
valuesAndCriteria
;
if
(!
block
.
isGiven
()) {
return
enumeratorizeWithSize
(
context
,
self
,
"sort_by"
, (
SizeFn
)
RubyEnumerable
::
size
);
}
final
CachingCallSite
each
=
eachSite
(
context
);
final
ArrayList
<
DoubleObject
<
IRubyObject
,
IRubyObject
>>
valuesAndCriteriaList
=
new
ArrayList
<>();
callEach
(
context
,
each
,
self
,
Signature
.
OPTIONAL
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
context1
,
IRubyObject
[]
args
,
Block
unused
) {
return
call
(
context1
,
packEnumValues
(
context1
,
args
),
unused
);
}
@
Override
public
IRubyObject
call
(
ThreadContext
context1
,
IRubyObject
arg
,
Block
unused
) {
IRubyObject
value
=
block
.
yield
(
context1
,
arg
);
synchronized
(
valuesAndCriteriaList
) {
valuesAndCriteriaList
.
add
(
new
DoubleObject
<>(
arg
,
value
));
}
return
context1
.
nil
;
}
});
valuesAndCriteria
=
valuesAndCriteriaList
;
Collections
.
sort
(
valuesAndCriteria
,
(
o1
,
o2
) ->
RubyComparable
.
cmpint
(
context
,
invokedynamic
(
context
,
o1
.
object2
,
OP_CMP
,
o2
.
object2
),
o1
.
object2
,
o2
.
object2
));
IRubyObject
dstArray
[] =
new
IRubyObject
[
valuesAndCriteria
.
size
()];
for
(
int
i
=
0
;
i
<
dstArray
.
length
;
i
++) {
dstArray
[
i
] =
valuesAndCriteria
.
get
(
i
).
object1
;
}
return
RubyArray
.
newArrayMayCopy
(
runtime
,
dstArray
);
}
@
JRubyMethod
public
static
IRubyObject
grep
(
ThreadContext
context
,
IRubyObject
self
,
final
IRubyObject
pattern
,
final
Block
block
) {
return
grep
(
context
,
self
,
pattern
,
block
,
true
);
}
@
JRubyMethod
(
name
=
"grep_v"
)
public
static
IRubyObject
inverseGrep
(
ThreadContext
context
,
IRubyObject
self
,
final
IRubyObject
pattern
,
final
Block
block
) {
return
grep
(
context
,
self
,
pattern
,
block
,
false
);
}
private
static
IRubyObject
grep
(
ThreadContext
context
,
IRubyObject
self
,
final
IRubyObject
pattern
,
final
Block
block
,
final
boolean
isPresent
) {
final
var
result
=
newArray
(
context
);
final
CachingCallSite
each
=
eachSite
(
context
);
if
(
block
.
isGiven
()) {
// pattern === arg
callEach
(
context
,
each
,
self
,
Signature
.
ONE_REQUIRED
,
new
BlockCallback
() {
final
MonomorphicCallSite
site
=
new
MonomorphicCallSite
(
"==="
);
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
args
,
Block
unused
) {
return
call
(
ctx
,
packEnumValues
(
ctx
,
args
),
unused
);
}
@
Override
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
arg
,
Block
unused
) {
if
(
site
.
call
(
ctx
,
pattern
,
pattern
,
arg
).
isTrue
() ==
isPresent
) {
// pattern === arg
IRubyObject
value
=
block
.
yield
(
ctx
,
arg
);
synchronized
(
result
) {
result
.
append
(
context
,
value
); }
}
return
ctx
.
nil
;
}
});
}
else
if
((
pattern
instanceof
RubyRegexp
) &&
pattern
.
getMetaClass
().
checkMethodBasicDefinition
(
"==="
)) {
callEach
(
context
,
each
,
self
,
Signature
.
ONE_REQUIRED
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
args
,
Block
unused
) {
return
call
(
ctx
,
packEnumValues
(
ctx
,
args
),
unused
);
}
@
Override
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
arg
,
Block
unused
) {
IRubyObject
converted
=
arg
instanceof
RubySymbol
?
arg
:
TypeConverter
.
checkStringType
(
ctx
.
runtime
,
arg
);
if
(((
RubyRegexp
)
pattern
).
match_p
(
ctx
,
converted
).
isTrue
() ==
isPresent
) {
synchronized
(
result
) {
result
.
append
(
context
,
arg
); }
}
return
ctx
.
nil
;
}
});
}
else
{
// pattern === arg
callEach
(
context
,
each
,
self
,
Signature
.
ONE_REQUIRED
,
new
BlockCallback
() {
final
MonomorphicCallSite
site
=
new
MonomorphicCallSite
(
"==="
);
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
args
,
Block
unused
) {
return
call
(
ctx
,
packEnumValues
(
ctx
,
args
),
unused
);
}
@
Override
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
arg
,
Block
unused
) {
if
(
site
.
call
(
ctx
,
pattern
,
pattern
,
arg
).
isTrue
() ==
isPresent
) {
// pattern === arg
synchronized
(
result
) {
result
.
append
(
ctx
,
arg
); }
}
return
ctx
.
nil
;
}
});
}
return
result
;
}
public
static
IRubyObject
detectCommon
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
detectCommon
(
context
,
eachSite
(
context
),
self
,
null
,
block
);
}
public
static
IRubyObject
detectCommon
(
ThreadContext
context
,
CallSite
each
,
IRubyObject
self
,
final
Block
block
) {
return
detectCommon
(
context
,
each
,
self
,
null
,
block
);
}
public
static
IRubyObject
detectCommon
(
final
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
ifnone
,
final
Block
block
) {
return
detectCommon
(
context
,
eachSite
(
context
),
self
,
ifnone
,
block
);
}
public
static
IRubyObject
detectCommon
(
final
ThreadContext
context
,
CallSite
each
,
IRubyObject
self
,
IRubyObject
ifnone
,
final
Block
block
) {
final
SingleObject
<
IRubyObject
>
result
=
new
SingleObject
<>(
null
);
try
{
callEach
(
context
,
each
,
self
,
Signature
.
OPTIONAL
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
return
call
(
ctx
,
packEnumValues
(
ctx
,
largs
),
blk
);
}
@
Override
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
larg
,
Block
blk
) {
checkContext
(
context
,
ctx
,
"detect/find"
);
if
(
block
.
yield
(
ctx
,
larg
).
isTrue
()) {
result
.
object
=
larg
;
throw
JumpException
.
SPECIAL_JUMP
;
}
return
ctx
.
nil
;
}
});
}
catch
(
JumpException
.
SpecialJump
sj
) {
return
result
.
object
;
}
return
ifnone
!=
null
&& !
ifnone
.
isNil
() ?
sites
(
context
).
detect_call
.
call
(
context
,
ifnone
,
ifnone
) :
context
.
nil
;
}
@
JRubyMethod
public
static
IRubyObject
detect
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
boolean
blockGiven
=
block
.
isGiven
();
if
(
self
instanceof
RubyArray
&&
blockGiven
)
return
((
RubyArray
)
self
).
find
(
context
,
null
,
block
);
return
block
.
isGiven
() ?
detectCommon
(
context
,
eachSite
(
context
),
self
,
null
,
block
) :
enumeratorize
(
context
.
runtime
,
self
,
"detect"
);
}
@
JRubyMethod
public
static
IRubyObject
detect
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
ifnone
,
final
Block
block
) {
boolean
blockGiven
=
block
.
isGiven
();
if
(
self
instanceof
RubyArray
&&
blockGiven
)
return
((
RubyArray
)
self
).
find
(
context
,
ifnone
,
block
);
return
block
.
isGiven
() ?
detectCommon
(
context
,
eachSite
(
context
),
self
,
ifnone
,
block
) :
enumeratorize
(
context
.
runtime
,
self
,
"detect"
,
ifnone
);
}
// FIXME: Custom Array enumeratorize should be made for all of these methods which skip Array without a supplied block.
@
JRubyMethod
public
static
IRubyObject
find
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
boolean
blockGiven
=
block
.
isGiven
();
if
(
self
instanceof
RubyArray
&&
blockGiven
)
return
((
RubyArray
)
self
).
find
(
context
,
null
,
block
);
return
blockGiven
?
detectCommon
(
context
,
eachSite
(
context
),
self
,
null
,
block
) :
enumeratorize
(
context
.
runtime
,
self
,
"find"
);
}
@
JRubyMethod
public
static
IRubyObject
find
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
ifnone
,
final
Block
block
) {
boolean
blockGiven
=
block
.
isGiven
();
if
(
self
instanceof
RubyArray
&&
blockGiven
)
return
((
RubyArray
)
self
).
find
(
context
,
ifnone
,
block
);
return
blockGiven
?
detectCommon
(
context
,
eachSite
(
context
),
self
,
ifnone
,
block
) :
enumeratorize
(
context
.
runtime
,
self
,
"find"
,
ifnone
);
}
@
JRubyMethod
(
name
=
"find_index"
)
public
static
IRubyObject
find_index
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
boolean
blockGiven
=
block
.
isGiven
();
if
(
self
instanceof
RubyArray
&&
blockGiven
)
return
((
RubyArray
)
self
).
find_index
(
context
,
block
);
return
blockGiven
?
find_indexCommon
(
context
,
eachSite
(
context
),
self
,
block
,
block
.
getSignature
()) :
enumeratorize
(
context
.
runtime
,
self
,
"find_index"
);
}
@
JRubyMethod
(
name
=
"find_index"
)
public
static
IRubyObject
find_index
(
ThreadContext
context
,
IRubyObject
self
,
final
IRubyObject
cond
,
final
Block
block
) {
if
(
block
.
isGiven
())
warn
(
context
,
"given block not used"
);
return
self
instanceof
RubyArray
ary
?
ary
.
find_index
(
context
,
cond
) :
find_indexCommon
(
context
,
eachSite
(
context
),
self
,
cond
);
}
public
static
IRubyObject
find_indexCommon
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
,
Signature
callbackArity
) {
return
find_indexCommon
(
context
,
eachSite
(
context
),
self
,
block
,
callbackArity
);
}
public
static
IRubyObject
find_indexCommon
(
ThreadContext
context
,
CallSite
each
,
IRubyObject
self
,
final
Block
block
,
Signature
callbackArity
) {
final
SingleLong
result
=
new
SingleLong
();
try
{
callEach
(
context
,
each
,
self
,
callbackArity
, (
ctx
,
largs
,
blk
) -> {
if
(
block
.
yieldValues
(
ctx
,
largs
).
isTrue
())
throw
JumpException
.
SPECIAL_JUMP
;
result
.
l
++;
return
ctx
.
nil
;
});
}
catch
(
JumpException
.
SpecialJump
sj
) {
return
asFixnum
(
context
,
result
.
l
);
}
return
context
.
nil
;
}
public
static
IRubyObject
find_indexCommon
(
ThreadContext
context
,
IRubyObject
self
,
final
IRubyObject
cond
) {
return
find_indexCommon
(
context
,
eachSite
(
context
),
self
,
cond
);
}
public
static
IRubyObject
find_indexCommon
(
ThreadContext
context
,
CallSite
each
,
IRubyObject
self
,
final
IRubyObject
cond
) {
final
SingleLong
result
=
new
SingleLong
(
0
);
try
{
callEach
(
context
,
each
,
self
,
Signature
.
OPTIONAL
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
return
call
(
ctx
,
packEnumValues
(
ctx
,
largs
),
blk
);
}
@
Override
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
larg
,
Block
blk
) {
if
(
equalInternal
(
ctx
,
larg
,
cond
))
throw
JumpException
.
SPECIAL_JUMP
;
result
.
l
++;
return
ctx
.
nil
;
}
});
}
catch
(
JumpException
.
SpecialJump
sj
) {
return
asFixnum
(
context
,
result
.
l
);
}
return
context
.
nil
;
}
public
static
IRubyObject
selectCommon
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
,
String
methodName
) {
if
(!
block
.
isGiven
())
return
enumeratorizeWithSize
(
context
,
self
,
methodName
, (
SizeFn
)
RubyEnumerable
::
size
);
final
var
result
=
newArray
(
context
);
callEach
(
context
,
eachSite
(
context
),
self
,
Signature
.
OPTIONAL
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
return
call
(
ctx
,
packEnumValues
(
ctx
,
largs
),
blk
);
}
@
Override
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
larg
,
Block
blk
) {
if
(
block
.
yield
(
ctx
,
larg
).
isTrue
()) {
synchronized
(
result
) {
result
.
append
(
ctx
,
larg
); }
}
return
ctx
.
nil
;
}
});
return
result
;
}
@
JRubyMethod
public
static
IRubyObject
select
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
selectCommon
(
context
,
self
,
block
,
"select"
);
}
@
JRubyMethod
(
alias
=
"filter"
)
public
static
IRubyObject
find_all
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
selectCommon
(
context
,
self
,
block
,
"find_all"
);
}
@
JRubyMethod
public
static
IRubyObject
reject
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
if
(!
block
.
isGiven
())
return
enumeratorizeWithSize
(
context
,
self
,
"reject"
, (
SizeFn
)
RubyEnumerable
::
size
);
final
var
result
=
newArray
(
context
);
callEach
(
context
,
eachSite
(
context
),
self
,
Signature
.
OPTIONAL
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
return
call
(
ctx
,
packEnumValues
(
ctx
,
largs
),
blk
);
}
@
Override
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
larg
,
Block
blk
) {
if
( !
block
.
yield
(
ctx
,
larg
).
isTrue
() ) {
synchronized
(
result
) {
result
.
append
(
ctx
,
larg
); }
}
return
ctx
.
nil
;
}
@
Override
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
larg
) {
if
( !
block
.
yield
(
ctx
,
larg
).
isTrue
() ) {
synchronized
(
result
) {
result
.
append
(
ctx
,
larg
); }
}
return
ctx
.
nil
;
}
});
return
result
;
}
@
JRubyMethod
(
name
=
"collect"
)
public
static
IRubyObject
collect
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
collectCommon
(
context
,
self
,
block
,
"collect"
);
}
@
JRubyMethod
(
name
=
"map"
)
public
static
IRubyObject
map
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
collectCommon
(
context
,
self
,
block
,
"map"
);
}
private
static
IRubyObject
collectCommon
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
,
String
methodName
) {
if
(!
block
.
isGiven
())
return
enumeratorizeWithSize
(
context
,
self
,
methodName
, (
SizeFn
)
RubyEnumerable
::
size
);
final
var
result
=
newArray
(
context
);
eachSite
(
context
).
call
(
context
,
self
,
self
,
CallBlock19
.
newCallClosure
(
self
,
enumerableModule
(
context
),
block
.
getSignature
(),
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
final
IRubyObject
larg
;
boolean
ary
=
false
;
switch
(
largs
.
length
) {
case
0
:
larg
=
ctx
.
nil
;
break
;
case
1
:
larg
=
largs
[
0
];
break
;
default
:
larg
=
RubyArray
.
newArrayMayCopy
(
ctx
.
runtime
,
largs
);
ary
=
true
;
}
IRubyObject
val
=
ary
?
block
.
yieldArray
(
ctx
,
larg
,
null
) :
block
.
yield
(
ctx
,
larg
);
synchronized
(
result
) {
result
.
append
(
ctx
,
val
); }
return
ctx
.
nil
;
}
@
Override
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
larg
,
Block
blk
) {
IRubyObject
val
=
block
.
yield
(
ctx
,
larg
);
synchronized
(
result
) {
result
.
append
(
ctx
,
val
); }
return
ctx
.
nil
;
}
},
context
));
return
result
;
}
@
JRubyMethod
public
static
IRubyObject
filter_map
(
ThreadContext
context
,
IRubyObject
self
,
Block
block
) {
if
(!
block
.
isGiven
())
return
enumeratorizeWithSize
(
context
,
self
,
"filter_map"
, (
SizeFn
)
RubyEnumerable
::
size
);
var
result
=
newArray
(
context
);
eachSite
(
context
).
call
(
context
,
self
,
self
,
CallBlock19
.
newCallClosure
(
self
,
enumerableModule
(
context
),
block
.
getSignature
(),
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
final
IRubyObject
larg
;
boolean
ary
=
false
;
switch
(
largs
.
length
) {
case
0
:
larg
=
ctx
.
nil
;
break
;
case
1
:
larg
=
largs
[
0
];
break
;
default
:
larg
=
RubyArray
.
newArrayMayCopy
(
ctx
.
runtime
,
largs
);
ary
=
true
;
}
IRubyObject
val
=
ary
?
block
.
yieldArray
(
ctx
,
larg
,
null
) :
block
.
yield
(
ctx
,
larg
);
if
(
val
.
isTrue
()) {
synchronized
(
result
) {
result
.
append
(
ctx
,
val
); }
}
return
ctx
.
nil
;
}
@
Override
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
larg
,
Block
blk
) {
IRubyObject
val
=
block
.
yield
(
ctx
,
larg
);
if
(
val
.
isTrue
()) {
synchronized
(
result
) {
result
.
append
(
ctx
,
val
); }
}
return
ctx
.
nil
;
}
},
context
));
return
result
;
}
@
JRubyMethod
(
name
=
"flat_map"
)
public
static
IRubyObject
flat_map
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
flatMapCommon
(
context
,
self
,
block
,
"flat_map"
);
}
@
JRubyMethod
(
name
=
"collect_concat"
)
public
static
IRubyObject
collect_concat
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
flatMapCommon
(
context
,
self
,
block
,
"collect_concat"
);
}
private
static
IRubyObject
flatMapCommon
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
,
String
methodName
) {
if
(!
block
.
isGiven
())
return
enumeratorizeWithSize
(
context
,
self
,
methodName
, (
SizeFn
)
RubyEnumerable
::
size
);
final
var
ary
=
newArray
(
context
);
callEach
(
context
,
eachSite
(
context
),
self
,
block
.
getSignature
(),
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
return
call
(
ctx
,
packEnumValues
(
ctx
,
largs
),
blk
);
}
@
Override
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
larg
,
Block
blk
) {
IRubyObject
i
=
block
.
yield
(
ctx
,
larg
);
IRubyObject
tmp
=
i
.
checkArrayType
();
synchronized
(
ary
) {
if
(
tmp
.
isNil
()) {
ary
.
append
(
ctx
,
i
);
}
else
{
ary
.
concat
(
ctx
,
tmp
);
}
}
return
ctx
.
nil
;
}
});
return
ary
;
}
@
JRubyMethod
public
static
IRubyObject
sum
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
runtime
;
RubyFixnum
zero
=
RubyFixnum
.
zero
(
runtime
);
return
sumCommon
(
context
,
self
,
zero
,
block
);
}
@
JRubyMethod
public
static
IRubyObject
sum
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
init
,
final
Block
block
) {
return
sumCommon
(
context
,
self
,
init
,
block
);
}
public
static
IRubyObject
sumCommon
(
final
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
init
,
final
Block
block
) {
return
sumCommon
(
context
,
eachSite
(
context
),
self
,
init
,
block
);
}
/* TODO: optimise for special types (e.g. Range, Hash, ...) */
public
static
IRubyObject
sumCommon
(
final
ThreadContext
context
,
CallSite
each
,
IRubyObject
self
,
IRubyObject
init
,
final
Block
block
) {
final
SingleObject
<
IRubyObject
>
result
=
new
SingleObject
<>(
init
);
final
SingleDouble
memo
=
new
SingleDouble
(
0.0
);
if
(
block
.
isGiven
()) {
callEach
(
context
,
each
,
self
,
Signature
.
OPTIONAL
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
return
call
(
ctx
,
packEnumValues
(
ctx
,
largs
),
blk
);
}
@
Override
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
larg
,
Block
blk
) {
result
.
object
=
sumAdd
(
ctx
,
result
.
object
,
block
.
yield
(
ctx
,
larg
),
memo
);
return
ctx
.
nil
;
}
});
}
else
{
callEach
(
context
,
each
,
self
,
Signature
.
OPTIONAL
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
return
call
(
ctx
,
packEnumValues
(
ctx
,
largs
),
blk
);
}
@
Override
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
larg
,
Block
blk
) {
result
.
object
=
sumAdd
(
ctx
,
result
.
object
,
larg
,
memo
);
return
ctx
.
nil
;
}
});
}
if
(
result
.
object
instanceof
RubyFloat
) {
return
((
RubyFloat
)
result
.
object
).
op_plus
(
context
,
memo
.
d
);
}
return
result
.
object
;
}
/* FIXME: optimise for special types (e.g. Integer)? */
/* NB: MRI says "Enumerable#sum method may not respect method redefinition of "+" methods such as Integer#+." */
public
static
IRubyObject
sumAdd
(
final
ThreadContext
context
,
IRubyObject
lhs
,
IRubyObject
rhs
,
final
SingleDouble
c
) {
switch
(
lhs
.
getType
().
classIndex
) {
default
:
return
sites
(
context
).
sum_op_plus
.
call
(
context
,
lhs
,
lhs
,
rhs
);
case
ClassIndex
.
FLOAT
:
case
ClassIndex
.
INTEGER
:
case
ClassIndex
.
RATIONAL
:
switch
(
rhs
.
getType
().
classIndex
) {
default
:
return
sites
(
context
).
sum_op_plus
.
call
(
context
,
lhs
,
lhs
,
rhs
);
case
ClassIndex
.
INTEGER
:
case
ClassIndex
.
RATIONAL
:
case
ClassIndex
.
FLOAT
:
}
}
boolean
floats
=
false
;
double
f
=
0.0
;
/*
* Kahan-Babuska balancing compensated summation algorithm
* See http://link.springer.com/article/10.1007/s00607-005-0139-x
*/
double
x
=
0.0
,
t
;
if
(
lhs
instanceof
RubyFloat
lhsFloat
) {
if
(
rhs
instanceof
RubyNumeric
num
) {
f
=
lhsFloat
.
value
;
x
=
num
.
asDouble
(
context
);
floats
=
true
;
}
}
else
if
(
rhs
instanceof
RubyFloat
rhsFloat
) {
if
(
lhs
instanceof
RubyNumeric
num
) {
c
.
d
=
0.0
;
f
=
num
.
asDouble
(
context
);
x
=
rhsFloat
.
value
;
floats
=
true
;
}
}
if
(!
floats
)
return
sites
(
context
).
sum_op_plus
.
call
(
context
,
lhs
,
lhs
,
rhs
);
if
(
Double
.
isNaN
(
f
))
return
lhs
;
if
(
Double
.
isNaN
(
x
))
return
lhs
;
if
(
Double
.
isInfinite
(
x
)) {
if
(
Double
.
isInfinite
(
f
) &&
Math
.
signum
(
x
) !=
Math
.
signum
(
f
)) {
return
asFloat
(
context
,
RubyFloat
.
NAN
);
}
else
{
return
rhs
;
}
}
if
(
Double
.
isInfinite
(
f
))
return
lhs
;
// Kahan's compensated summation algorithm
View remainder of file in raw view
Footer
© 2026 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Community
Docs
Contact
You can’t perform that action at this time.
Back
|
FazBrowse Home
|
New Git URL