FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
jruby/src/org/jruby/RubyEnumerable.java at serializable_procs · MSNexploder/jruby · GitHub
MSNexploder
/
jruby
Public
forked from
jruby/jruby
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
jruby
/
src
/
org
/
jruby
/
RubyEnumerable.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
1550 lines (1323 loc) · 67.3 KB
Breadcrumbs
jruby
/
src
/
org
/
jruby
/
RubyEnumerable.java
Copy path
File metadata and controls
1550 lines (1323 loc) · 67.3 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: CPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public
* License Version 1.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/cpl-v10.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 CPL, 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 CPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package
org
.
jruby
;
import
java
.
util
.
ArrayList
;
import
static
org
.
jruby
.
RubyEnumerator
.
enumeratorize
;
import
java
.
util
.
Comparator
;
import
java
.
util
.
Arrays
;
import
java
.
util
.
Collections
;
import
java
.
util
.
List
;
import
java
.
util
.
concurrent
.
atomic
.
AtomicInteger
;
import
org
.
jruby
.
anno
.
JRubyMethod
;
import
org
.
jruby
.
anno
.
JRubyModule
;
import
org
.
jruby
.
common
.
IRubyWarnings
.
ID
;
import
org
.
jruby
.
exceptions
.
JumpException
;
import
org
.
jruby
.
javasupport
.
util
.
RuntimeHelpers
;
import
org
.
jruby
.
runtime
.
Arity
;
import
org
.
jruby
.
runtime
.
Block
;
import
org
.
jruby
.
runtime
.
BlockBody
;
import
org
.
jruby
.
runtime
.
CallBlock
;
import
org
.
jruby
.
runtime
.
BlockCallback
;
import
org
.
jruby
.
runtime
.
CallBlock19
;
import
org
.
jruby
.
runtime
.
ThreadContext
;
import
org
.
jruby
.
runtime
.
builtin
.
IRubyObject
;
import
org
.
jruby
.
util
.
TypeConverter
;
/**
* The implementation of Ruby's Enumerable module.
*/
@
JRubyModule
(
name
=
"Enumerable"
)
public
class
RubyEnumerable
{
public
static
RubyModule
createEnumerableModule
(
Ruby
runtime
) {
RubyModule
enumModule
=
runtime
.
defineModule
(
"Enumerable"
);
runtime
.
setEnumerable
(
enumModule
);
enumModule
.
defineAnnotatedMethods
(
RubyEnumerable
.
class
);
return
enumModule
;
}
public
static
IRubyObject
callEach
(
Ruby
runtime
,
ThreadContext
context
,
IRubyObject
self
,
BlockCallback
callback
) {
return
RuntimeHelpers
.
invoke
(
context
,
self
,
"each"
,
CallBlock
.
newCallClosure
(
self
,
runtime
.
getEnumerable
(),
Arity
.
noArguments
(),
callback
,
context
));
}
public
static
IRubyObject
callEach19
(
Ruby
runtime
,
ThreadContext
context
,
IRubyObject
self
,
BlockCallback
callback
) {
return
RuntimeHelpers
.
invoke
(
context
,
self
,
"each"
,
CallBlock19
.
newCallClosure
(
self
,
runtime
.
getEnumerable
(),
Arity
.
noArguments
(),
callback
,
context
));
}
public
static
IRubyObject
callEach
(
Ruby
runtime
,
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
[]
args
,
BlockCallback
callback
) {
return
RuntimeHelpers
.
invoke
(
context
,
self
,
"each"
,
args
,
CallBlock
.
newCallClosure
(
self
,
runtime
.
getEnumerable
(),
Arity
.
noArguments
(),
callback
,
context
));
}
private
static
void
checkContext
(
ThreadContext
firstContext
,
ThreadContext
secondContext
,
String
name
) {
if
(
firstContext
!=
secondContext
) {
throw
secondContext
.
getRuntime
().
newThreadError
(
"Enumerable#"
+
name
+
" cannot be parallelized"
);
}
}
public
static
IRubyObject
checkArgs
(
Ruby
runtime
,
IRubyObject
[]
largs
) {
return
largs
.
length
==
0
?
runtime
.
getNil
() :
largs
[
0
];
}
@
JRubyMethod
(
name
=
"count"
,
frame
=
true
)
public
static
IRubyObject
count
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
ThreadContext
localContext
=
context
;
final
int
result
[];
if
(
block
.
isGiven
()) {
result
=
new
int
[] {
0
};
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
checkContext
(
localContext
,
ctx
,
"count"
);
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
if
(
block
.
yield
(
ctx
,
larg
).
isTrue
()) {
result
[
0
]++;
}
return
runtime
.
getNil
();
}
});
}
else
{
if
(
self
.
respondsTo
(
"size"
))
return
self
.
callMethod
(
context
,
"size"
);
result
=
new
int
[] {
0
};
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
checkContext
(
localContext
,
ctx
,
"count"
);
result
[
0
]++;
return
runtime
.
getNil
();
}
});
}
return
RubyFixnum
.
newFixnum
(
runtime
,
result
[
0
]);
}
@
JRubyMethod
(
name
=
"count"
,
frame
=
true
)
public
static
IRubyObject
count
(
ThreadContext
context
,
IRubyObject
self
,
final
IRubyObject
arg
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
ThreadContext
localContext
=
context
;
final
int
result
[] =
new
int
[] {
0
};
if
(
block
.
isGiven
())
runtime
.
getWarnings
().
warn
(
ID
.
BLOCK_UNUSED
,
"given block not used"
);
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
block
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
checkContext
(
localContext
,
ctx
,
"count"
);
if
(
larg
.
equals
(
arg
))
result
[
0
]++;
return
runtime
.
getNil
();
}
});
return
RubyFixnum
.
newFixnum
(
runtime
,
result
[
0
]);
}
@
JRubyMethod
(
name
=
"cycle"
,
frame
=
true
)
public
static
IRubyObject
cycle
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
if
(!
block
.
isGiven
())
return
enumeratorize
(
context
.
getRuntime
(),
self
,
"cycle"
);
return
cycleCommon
(
context
,
self
, -
1
,
block
);
}
@
JRubyMethod
(
name
=
"cycle"
,
frame
=
true
)
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
enumeratorize
(
context
.
getRuntime
(),
self
,
"cycle"
,
arg
);
long
times
=
RubyNumeric
.
num2long
(
arg
);
if
(
times
<=
0
)
return
context
.
getRuntime
().
getNil
();
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
.
getRuntime
();
final
RubyArray
result
=
runtime
.
newArray
();
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
synchronized
(
result
) {
result
.
append
(
larg
);
}
block
.
yield
(
ctx
,
larg
);
return
runtime
.
getNil
();
}
});
int
len
=
result
.
size
();
if
(
len
==
0
)
return
runtime
.
getNil
();
while
(
nv
<
0
||
0
< --
nv
) {
for
(
int
i
=
0
;
i
<
len
;
i
++) {
block
.
yield
(
context
,
result
.
eltInternal
(
i
));
}
}
return
runtime
.
getNil
();
}
@
JRubyMethod
(
name
=
"take"
)
public
static
IRubyObject
take
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
n
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
long
len
=
RubyNumeric
.
num2long
(
n
);
if
(
len
<
0
)
throw
runtime
.
newArgumentError
(
"attempt to take negative size"
);
if
(
len
==
0
)
return
runtime
.
newEmptyArray
();
final
RubyArray
result
=
runtime
.
newArray
();
try
{
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
long
i
=
len
;
// Atomic ?
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
synchronized
(
result
) {
result
.
append
(
larg
);
if
(--
i
==
0
)
throw
JumpException
.
SPECIAL_JUMP
;
}
return
runtime
.
getNil
();
}
});
}
catch
(
JumpException
.
SpecialJump
sj
) {}
return
result
;
}
@
JRubyMethod
(
name
=
"take_while"
,
frame
=
true
)
public
static
IRubyObject
take_while
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
if
(!
block
.
isGiven
())
return
enumeratorize
(
context
.
getRuntime
(),
self
,
"take_while"
);
final
Ruby
runtime
=
context
.
getRuntime
();
final
RubyArray
result
=
runtime
.
newArray
();
try
{
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
if
(!
block
.
yield
(
ctx
,
larg
).
isTrue
())
throw
JumpException
.
SPECIAL_JUMP
;
synchronized
(
result
) {
result
.
append
(
larg
);
}
return
runtime
.
getNil
();
}
});
}
catch
(
JumpException
.
SpecialJump
sj
) {}
return
result
;
}
@
JRubyMethod
(
name
=
"drop"
)
public
static
IRubyObject
drop
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
n
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
long
len
=
RubyNumeric
.
num2long
(
n
);
if
(
len
<
0
)
throw
runtime
.
newArgumentError
(
"attempt to drop negative size"
);
final
RubyArray
result
=
runtime
.
newArray
();
try
{
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
long
i
=
len
;
// Atomic ?
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
synchronized
(
result
) {
if
(
i
==
0
) {
result
.
append
(
larg
);
}
else
{
--
i
;
}
}
return
runtime
.
getNil
();
}
});
}
catch
(
JumpException
.
SpecialJump
sj
) {}
return
result
;
}
@
JRubyMethod
(
name
=
"drop_while"
,
frame
=
true
)
public
static
IRubyObject
drop_while
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
if
(!
block
.
isGiven
())
return
enumeratorize
(
context
.
getRuntime
(),
self
,
"drop_while"
);
final
Ruby
runtime
=
context
.
getRuntime
();
final
RubyArray
result
=
runtime
.
newArray
();
try
{
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
boolean
memo
=
false
;
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
if
(!
memo
&& !
block
.
yield
(
ctx
,
larg
).
isTrue
())
memo
=
true
;
if
(
memo
)
synchronized
(
result
) {
result
.
append
(
larg
);
}
return
runtime
.
getNil
();
}
});
}
catch
(
JumpException
.
SpecialJump
sj
) {}
return
result
;
}
@
JRubyMethod
(
name
=
"first"
)
public
static
IRubyObject
first
(
ThreadContext
context
,
IRubyObject
self
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
ThreadContext
localContext
=
context
;
final
IRubyObject
[]
holder
=
new
IRubyObject
[]{
runtime
.
getNil
()};
try
{
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
checkContext
(
localContext
,
ctx
,
"first"
);
holder
[
0
] =
larg
;
throw
JumpException
.
SPECIAL_JUMP
;
}
});
}
catch
(
JumpException
.
SpecialJump
sj
) {}
return
holder
[
0
];
}
@
JRubyMethod
(
name
=
"first"
)
public
static
IRubyObject
first
(
ThreadContext
context
,
IRubyObject
self
,
final
IRubyObject
num
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
RubyArray
result
=
runtime
.
newArray
();
final
ThreadContext
localContext
=
context
;
final
int
firstCount
=
RubyNumeric
.
fix2int
(
num
);
if
(
firstCount
<
0
) {
throw
runtime
.
newArgumentError
(
"negative index"
);
}
else
if
(
firstCount
==
0
) {
return
result
;
}
try
{
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
private
int
iter
=
RubyNumeric
.
fix2int
(
num
);
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
checkContext
(
localContext
,
ctx
,
"first"
);
result
.
append
(
larg
);
if
(
iter
-- ==
1
) {
throw
JumpException
.
SPECIAL_JUMP
;
}
return
runtime
.
getNil
();
}
});
}
catch
(
JumpException
.
SpecialJump
sj
) {}
return
result
;
}
@
JRubyMethod
(
name
= {
"to_a"
,
"entries"
},
compat
=
CompatVersion
.
RUBY1_8
)
public
static
IRubyObject
to_a
(
ThreadContext
context
,
IRubyObject
self
) {
Ruby
runtime
=
context
.
getRuntime
();
RubyArray
result
=
runtime
.
newArray
();
callEach
(
runtime
,
context
,
self
,
new
AppendBlockCallback
(
runtime
,
result
));
return
result
;
}
@
JRubyMethod
(
name
= {
"to_a"
,
"entries"
},
rest
=
true
,
compat
=
CompatVersion
.
RUBY1_8
)
public
static
IRubyObject
to_a
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
[]
args
) {
Ruby
runtime
=
context
.
getRuntime
();
RubyArray
result
=
runtime
.
newArray
();
RuntimeHelpers
.
invoke
(
context
,
self
,
"each"
,
args
,
CallBlock
.
newCallClosure
(
self
,
runtime
.
getEnumerable
(),
Arity
.
OPTIONAL
,
new
AppendBlockCallback
(
runtime
,
result
),
context
));
return
result
;
}
@
JRubyMethod
(
name
= {
"to_a"
,
"entries"
},
compat
=
CompatVersion
.
RUBY1_9
)
public
static
IRubyObject
to_a19
(
ThreadContext
context
,
IRubyObject
self
) {
Ruby
runtime
=
context
.
getRuntime
();
RubyArray
result
=
runtime
.
newArray
();
callEach
(
runtime
,
context
,
self
,
new
AppendBlockCallback
(
runtime
,
result
));
result
.
infectBy
(
self
);
return
result
;
}
@
JRubyMethod
(
name
= {
"to_a"
,
"entries"
},
rest
=
true
,
compat
=
CompatVersion
.
RUBY1_9
)
public
static
IRubyObject
to_a19
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
[]
args
) {
Ruby
runtime
=
context
.
getRuntime
();
RubyArray
result
=
runtime
.
newArray
();
RuntimeHelpers
.
invoke
(
context
,
self
,
"each"
,
args
,
CallBlock
.
newCallClosure
(
self
,
runtime
.
getEnumerable
(),
Arity
.
OPTIONAL
,
new
AppendBlockCallback
(
runtime
,
result
),
context
));
result
.
infectBy
(
self
);
return
result
;
}
@
JRubyMethod
(
name
=
"sort"
,
frame
=
true
)
public
static
IRubyObject
sort
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
Ruby
runtime
=
context
.
getRuntime
();
RubyArray
result
=
runtime
.
newArray
();
callEach
(
runtime
,
context
,
self
,
new
AppendBlockCallback
(
runtime
,
result
));
result
.
sort_bang
(
context
,
block
);
return
result
;
}
public
static
IRubyObject
sort_byCommon
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
ThreadContext
localContext
=
context
;
// MUST NOT be used across threads
if
(
self
instanceof
RubyArray
) {
RubyArray
selfArray
= (
RubyArray
)
self
;
final
IRubyObject
[][]
valuesAndCriteria
=
new
IRubyObject
[
selfArray
.
size
()][
2
];
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
AtomicInteger
i
=
new
AtomicInteger
(
0
);
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
IRubyObject
[]
myVandC
=
valuesAndCriteria
[
i
.
getAndIncrement
()];
myVandC
[
0
] =
larg
;
myVandC
[
1
] =
block
.
yield
(
ctx
,
larg
);
return
runtime
.
getNil
();
}
});
Arrays
.
sort
(
valuesAndCriteria
,
new
Comparator
<
IRubyObject
[]>() {
public
int
compare
(
IRubyObject
[]
o1
,
IRubyObject
[]
o2
) {
return
RubyFixnum
.
fix2int
(
o1
[
1
].
callMethod
(
localContext
,
"<=>"
,
o2
[
1
]));
}
});
IRubyObject
dstArray
[] =
new
IRubyObject
[
selfArray
.
size
()];
for
(
int
i
=
0
;
i
<
dstArray
.
length
;
i
++) {
dstArray
[
i
] =
valuesAndCriteria
[
i
][
0
];
}
return
runtime
.
newArrayNoCopy
(
dstArray
);
}
else
{
final
List
<
IRubyObject
[]>
valuesAndCriteria
=
new
ArrayList
<
IRubyObject
[]>();
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
IRubyObject
[]
myVandC
=
new
IRubyObject
[
2
];
myVandC
[
0
] =
larg
;
myVandC
[
1
] =
block
.
yield
(
ctx
,
larg
);
valuesAndCriteria
.
add
(
myVandC
);
return
runtime
.
getNil
();
}
});
Collections
.
sort
(
valuesAndCriteria
,
new
Comparator
<
IRubyObject
[]>() {
public
int
compare
(
IRubyObject
[]
o1
,
IRubyObject
[]
o2
) {
return
RubyFixnum
.
fix2int
(
o1
[
1
].
callMethod
(
localContext
,
"<=>"
,
o2
[
1
]));
}
});
IRubyObject
dstArray
[] =
new
IRubyObject
[
valuesAndCriteria
.
size
()];
for
(
int
i
=
0
;
i
<
dstArray
.
length
;
i
++) {
dstArray
[
i
] =
valuesAndCriteria
.
get
(
i
)[
0
];
}
return
runtime
.
newArrayNoCopy
(
dstArray
);
}
}
@
JRubyMethod
(
name
=
"sort_by"
,
frame
=
true
)
public
static
IRubyObject
sort_by
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
block
.
isGiven
() ?
sort_byCommon
(
context
,
self
,
block
) :
enumeratorize
(
context
.
getRuntime
(),
self
,
"sort_by"
);
}
@
JRubyMethod
(
name
=
"grep"
,
required
=
1
,
frame
=
true
)
public
static
IRubyObject
grep
(
ThreadContext
context
,
IRubyObject
self
,
final
IRubyObject
pattern
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
RubyArray
result
=
runtime
.
newArray
();
if
(
block
.
isGiven
()) {
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
ctx
.
setRubyFrameDelta
(
ctx
.
getRubyFrameDelta
()+
2
);
if
(
pattern
.
callMethod
(
ctx
,
"==="
,
larg
).
isTrue
()) {
IRubyObject
value
=
block
.
yield
(
ctx
,
larg
);
synchronized
(
result
) {
result
.
append
(
value
);
}
}
ctx
.
setRubyFrameDelta
(
ctx
.
getRubyFrameDelta
()-
2
);
return
runtime
.
getNil
();
}
});
}
else
{
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
if
(
pattern
.
callMethod
(
ctx
,
"==="
,
larg
).
isTrue
()) {
synchronized
(
result
) {
result
.
append
(
larg
);
}
}
return
runtime
.
getNil
();
}
});
}
return
result
;
}
public
static
IRubyObject
detectCommon
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
detectCommon
(
context
,
self
,
null
,
block
);
}
public
static
IRubyObject
detectCommon
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
ifnone
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
IRubyObject
result
[] =
new
IRubyObject
[] {
null
};
final
ThreadContext
localContext
=
context
;
try
{
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
checkContext
(
localContext
,
ctx
,
"detect/find"
);
if
(
block
.
yield
(
ctx
,
larg
).
isTrue
()) {
result
[
0
] =
larg
;
throw
JumpException
.
SPECIAL_JUMP
;
}
return
runtime
.
getNil
();
}
});
}
catch
(
JumpException
.
SpecialJump
sj
) {
return
result
[
0
];
}
return
ifnone
!=
null
?
ifnone
.
callMethod
(
context
,
"call"
) :
runtime
.
getNil
();
}
@
JRubyMethod
(
name
=
"detect"
,
frame
=
true
)
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
,
self
,
block
) :
enumeratorize
(
context
.
getRuntime
(),
self
,
"detect"
);
}
@
JRubyMethod
(
name
=
"detect"
,
frame
=
true
)
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
,
null
,
block
);
return
block
.
isGiven
() ?
detectCommon
(
context
,
self
,
ifnone
,
block
) :
enumeratorize
(
context
.
getRuntime
(),
self
,
"detect"
,
ifnone
);
}
// FIXME: Custom Array enumeratorize should be made for all of these methods which skip Array without a supplied block.
@
JRubyMethod
(
name
=
"find"
,
frame
=
true
)
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
,
self
,
block
) :
enumeratorize
(
context
.
getRuntime
(),
self
,
"find"
);
}
@
JRubyMethod
(
name
=
"find"
,
frame
=
true
)
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
,
self
,
ifnone
,
block
) :
enumeratorize
(
context
.
getRuntime
(),
self
,
"find"
,
ifnone
);
}
@
JRubyMethod
(
name
=
"find_index"
,
frame
=
true
)
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
,
self
,
block
) :
enumeratorize
(
context
.
getRuntime
(),
self
,
"find_index"
);
}
@
JRubyMethod
(
name
=
"find_index"
,
frame
=
true
)
public
static
IRubyObject
find_index
(
ThreadContext
context
,
IRubyObject
self
,
final
IRubyObject
cond
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
getRuntime
();
if
(
block
.
isGiven
())
runtime
.
getWarnings
().
warn
(
ID
.
BLOCK_UNUSED
,
"given block not used"
);
if
(
self
instanceof
RubyArray
)
return
((
RubyArray
)
self
).
find_index
(
context
,
cond
);
return
find_indexCommon
(
context
,
self
,
cond
);
}
public
static
IRubyObject
find_indexCommon
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
long
result
[] =
new
long
[] {
0
};
try
{
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
if
(
block
.
yield
(
ctx
,
larg
).
isTrue
())
throw
JumpException
.
SPECIAL_JUMP
;
result
[
0
]++;
return
runtime
.
getNil
();
}
});
}
catch
(
JumpException
.
SpecialJump
sj
) {
return
RubyFixnum
.
newFixnum
(
runtime
,
result
[
0
]);
}
return
runtime
.
getNil
();
}
public
static
IRubyObject
find_indexCommon
(
ThreadContext
context
,
IRubyObject
self
,
final
IRubyObject
cond
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
long
result
[] =
new
long
[] {
0
};
try
{
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
if
(
larg
.
equals
(
cond
))
throw
JumpException
.
SPECIAL_JUMP
;
result
[
0
]++;
return
runtime
.
getNil
();
}
});
}
catch
(
JumpException
.
SpecialJump
sj
) {
return
RubyFixnum
.
newFixnum
(
runtime
,
result
[
0
]);
}
return
runtime
.
getNil
();
}
public
static
IRubyObject
selectCommon
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
RubyArray
result
=
runtime
.
newArray
();
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
if
(
block
.
yield
(
ctx
,
larg
).
isTrue
()) {
synchronized
(
result
) {
result
.
append
(
larg
);
}
}
return
runtime
.
getNil
();
}
});
return
result
;
}
@
JRubyMethod
(
name
=
"select"
,
frame
=
true
)
public
static
IRubyObject
select
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
block
.
isGiven
() ?
selectCommon
(
context
,
self
,
block
) :
enumeratorize
(
context
.
getRuntime
(),
self
,
"select"
);
}
@
JRubyMethod
(
name
=
"find_all"
,
frame
=
true
)
public
static
IRubyObject
find_all
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
block
.
isGiven
() ?
selectCommon
(
context
,
self
,
block
) :
enumeratorize
(
context
.
getRuntime
(),
self
,
"find_all"
);
}
public
static
IRubyObject
rejectCommon
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
RubyArray
result
=
runtime
.
newArray
();
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
if
(!
block
.
yield
(
ctx
,
larg
).
isTrue
()) {
synchronized
(
result
) {
result
.
append
(
larg
);
}
}
return
runtime
.
getNil
();
}
});
return
result
;
}
@
JRubyMethod
(
name
=
"reject"
,
frame
=
true
)
public
static
IRubyObject
reject
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
block
.
isGiven
() ?
rejectCommon
(
context
,
self
,
block
) :
enumeratorize
(
context
.
getRuntime
(),
self
,
"reject"
);
}
@
JRubyMethod
(
name
= {
"collect"
,
"map"
},
frame
=
true
,
compat
=
CompatVersion
.
RUBY1_8
)
public
static
IRubyObject
collect
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
RubyArray
result
=
runtime
.
newArray
();
if
(
block
.
isGiven
()) {
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
IRubyObject
value
=
block
.
yield
(
ctx
,
larg
);
synchronized
(
result
) {
result
.
append
(
value
);
}
return
runtime
.
getNil
();
}
});
}
else
{
callEach
(
runtime
,
context
,
self
,
new
AppendBlockCallback
(
runtime
,
result
));
}
return
result
;
}
@
JRubyMethod
(
name
= {
"collect"
},
frame
=
true
,
compat
=
CompatVersion
.
RUBY1_9
)
public
static
IRubyObject
collect19
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
collectCommon19
(
context
,
self
,
block
,
"collect"
);
}
@
JRubyMethod
(
name
= {
"map"
},
frame
=
true
,
compat
=
CompatVersion
.
RUBY1_9
)
public
static
IRubyObject
map19
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
collectCommon19
(
context
,
self
,
block
,
"map"
);
}
private
static
IRubyObject
collectCommon19
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
,
String
methodName
) {
final
Ruby
runtime
=
context
.
getRuntime
();
if
(
block
.
isGiven
()) {
final
RubyArray
result
=
runtime
.
newArray
();
callEach19
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
IRubyObject
value
=
block
.
yield
(
ctx
,
larg
);
synchronized
(
result
) {
result
.
append
(
value
);
}
return
runtime
.
getNil
();
}
});
return
result
;
}
else
{
return
enumeratorize
(
runtime
,
self
,
methodName
);
}
}
public
static
IRubyObject
collectCommon
(
ThreadContext
context
,
Ruby
runtime
,
IRubyObject
self
,
RubyArray
result
,
final
Block
block
,
BlockCallback
blockCallback
) {
callEach
(
runtime
,
context
,
self
,
blockCallback
);
return
result
;
}
@
JRubyMethod
(
name
= {
"flat_map"
},
frame
=
true
,
compat
=
CompatVersion
.
RUBY1_9
)
public
static
IRubyObject
flat_map19
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
flatMapCommon19
(
context
,
self
,
block
,
"flat_map"
);
}
@
JRubyMethod
(
name
= {
"collect_concat"
},
frame
=
true
,
compat
=
CompatVersion
.
RUBY1_9
)
public
static
IRubyObject
collect_concat19
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
flatMapCommon19
(
context
,
self
,
block
,
"collect_concat"
);
}
private
static
IRubyObject
flatMapCommon19
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
,
String
methodName
) {
final
Ruby
runtime
=
context
.
getRuntime
();
if
(
block
.
isGiven
()) {
final
RubyArray
ary
=
runtime
.
newArray
();
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
IRubyObject
i
=
block
.
yield
(
ctx
,
larg
);
IRubyObject
tmp
=
i
.
checkArrayType
();
synchronized
(
ary
) {
if
(
tmp
.
isNil
()) {
ary
.
append
(
i
);
}
else
{
ary
.
concat
(
tmp
);
}
}
return
runtime
.
getNil
();
}
});
return
ary
;
}
else
{
return
enumeratorize
(
runtime
,
self
,
methodName
);
}
}
public
static
IRubyObject
injectCommon
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
init
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
IRubyObject
result
[] =
new
IRubyObject
[] {
init
};
final
ThreadContext
localContext
=
context
;
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
checkContext
(
localContext
,
ctx
,
"inject"
);
result
[
0
] =
result
[
0
] ==
null
?
larg
:
block
.
yieldArray
(
ctx
,
runtime
.
newArray
(
result
[
0
],
larg
),
null
,
null
);
return
runtime
.
getNil
();
}
});
return
result
[
0
] ==
null
?
runtime
.
getNil
() :
result
[
0
];
}
@
JRubyMethod
(
name
= {
"inject"
,
"reduce"
},
frame
=
true
)
public
static
IRubyObject
inject
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
injectCommon
(
context
,
self
,
null
,
block
);
}
@
JRubyMethod
(
name
= {
"inject"
,
"reduce"
},
frame
=
true
)
public
static
IRubyObject
inject
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
arg
,
final
Block
block
) {
return
block
.
isGiven
() ?
injectCommon
(
context
,
self
,
arg
,
block
) :
inject
(
context
,
self
,
null
,
arg
,
block
);
}
@
JRubyMethod
(
name
= {
"inject"
,
"reduce"
},
frame
=
true
)
public
static
IRubyObject
inject
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
init
,
IRubyObject
method
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
getRuntime
();
if
(
block
.
isGiven
())
runtime
.
getWarnings
().
warn
(
ID
.
BLOCK_UNUSED
,
"given block not used"
);
final
String
methodId
=
method
.
asJavaString
();
final
IRubyObject
result
[] =
new
IRubyObject
[] {
init
};
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
result
[
0
] =
result
[
0
] ==
null
?
larg
:
result
[
0
].
callMethod
(
ctx
,
methodId
,
larg
);
return
runtime
.
getNil
();
}
});
return
result
[
0
] ==
null
?
runtime
.
getNil
() :
result
[
0
];
}
public
static
IRubyObject
partitionCommon
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
RubyArray
arr_true
=
runtime
.
newArray
();
final
RubyArray
arr_false
=
runtime
.
newArray
();
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
if
(
block
.
yield
(
ctx
,
larg
).
isTrue
()) {
synchronized
(
arr_true
) {
arr_true
.
append
(
larg
);
}
}
else
{
synchronized
(
arr_false
) {
arr_false
.
append
(
larg
);
}
}
return
runtime
.
getNil
();
}
});
return
runtime
.
newArray
(
arr_true
,
arr_false
);
}
@
JRubyMethod
(
name
=
"partition"
,
frame
=
true
)
public
static
IRubyObject
partition
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
return
block
.
isGiven
() ?
partitionCommon
(
context
,
self
,
block
) :
enumeratorize
(
context
.
getRuntime
(),
self
,
"partition"
);
}
private
static
class
EachWithIndex
implements
BlockCallback
{
private
int
index
=
0
;
private
final
Block
block
;
private
final
Ruby
runtime
;
public
EachWithIndex
(
ThreadContext
ctx
,
Block
block
) {
this
.
block
=
block
;
this
.
runtime
=
ctx
.
getRuntime
();
}
public
IRubyObject
call
(
ThreadContext
context
,
IRubyObject
[]
iargs
,
Block
block
) {
switch
(
iargs
.
length
) {
case
0
:
// FIXME: Does this ever happen?
case
1
:
this
.
block
.
call
(
context
,
checkArgs
(
runtime
,
iargs
),
runtime
.
newFixnum
(
index
++));
break
;
case
2
:
this
.
block
.
call
(
context
,
runtime
.
newArrayNoCopy
(
iargs
),
runtime
.
newFixnum
(
index
++));
break
;
}
return
runtime
.
getNil
();
}
}
public
static
IRubyObject
each_with_indexCommon
(
ThreadContext
context
,
IRubyObject
self
,
Block
block
) {
callEach
(
context
.
getRuntime
(),
context
,
self
,
new
EachWithIndex
(
context
,
block
));
return
self
;
}
public
static
IRubyObject
each_with_indexCommon19
(
ThreadContext
context
,
IRubyObject
self
,
Block
block
,
IRubyObject
[]
args
) {
callEach
(
context
.
getRuntime
(),
context
,
self
,
args
,
new
EachWithIndex
(
context
,
block
));
return
self
;
}
@
JRubyMethod
(
name
=
"each_with_index"
,
frame
=
true
,
compat
=
CompatVersion
.
RUBY1_8
)
public
static
IRubyObject
each_with_index
(
ThreadContext
context
,
IRubyObject
self
,
Block
block
) {
return
block
.
isGiven
() ?
each_with_indexCommon
(
context
,
self
,
block
) :
enumeratorize
(
context
.
getRuntime
(),
self
,
"each_with_index"
);
}
@
JRubyMethod
(
name
=
"each_with_index"
,
frame
=
true
,
rest
=
true
,
compat
=
CompatVersion
.
RUBY1_9
)
public
static
IRubyObject
each_with_index19
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
[]
args
,
Block
block
) {
return
block
.
isGiven
() ?
each_with_indexCommon19
(
context
,
self
,
block
,
args
) :
enumeratorize
(
context
.
getRuntime
(),
self
,
"each_with_index"
,
args
);
}
@
JRubyMethod
(
name
=
"enum_with_index"
,
frame
=
true
)
public
static
IRubyObject
enum_with_index
(
ThreadContext
context
,
IRubyObject
self
,
Block
block
) {
return
block
.
isGiven
() ?
each_with_indexCommon
(
context
,
self
,
block
) :
enumeratorize
(
context
.
getRuntime
(),
self
,
"enum_with_index"
);
}
@
JRubyMethod
(
name
=
"each_entry"
,
frame
=
true
,
rest
=
true
,
compat
=
CompatVersion
.
RUBY1_9
)
public
static
IRubyObject
each_entry
(
ThreadContext
context
,
final
IRubyObject
self
,
final
IRubyObject
[]
args
,
final
Block
block
) {
return
block
.
isGiven
() ?
each_entryCommon
(
context
,
self
,
args
,
block
) :
enumeratorize
(
context
.
getRuntime
(),
self
,
"each_entry"
,
args
);
}
private
static
IRubyObject
each_entryCommon
(
ThreadContext
context
,
final
IRubyObject
self
,
final
IRubyObject
[]
args
,
final
Block
block
) {
callEach
(
context
.
getRuntime
(),
context
,
self
,
args
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
return
block
.
yieldSpecific
(
ctx
,
checkArgs
(
ctx
.
getRuntime
(),
largs
));
}
});
return
self
;
}
@
JRubyMethod
(
name
=
"reverse_each"
,
frame
=
true
)
public
static
IRubyObject
reverse_each
(
ThreadContext
context
,
IRubyObject
self
,
Block
block
) {
return
block
.
isGiven
() ?
reverse_eachInternal
(
context
,
self
,
to_a
(
context
,
self
),
block
) :
enumeratorize
(
context
.
getRuntime
(),
self
,
"reverse_each"
);
}
@
JRubyMethod
(
name
=
"reverse_each"
,
frame
=
true
,
rest
=
true
)
public
static
IRubyObject
reverse_each
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
[]
args
,
Block
block
) {
return
block
.
isGiven
() ?
reverse_eachInternal
(
context
,
self
,
to_a
(
context
,
self
,
args
),
block
) :
enumeratorize
(
context
.
getRuntime
(),
self
,
"reverse_each"
,
args
);
}
private
static
IRubyObject
reverse_eachInternal
(
ThreadContext
context
,
IRubyObject
self
,
IRubyObject
obj
,
Block
block
) {
((
RubyArray
)
obj
).
reverse_each
(
context
,
block
);
return
self
;
}
@
JRubyMethod
(
name
= {
"include?"
,
"member?"
},
required
=
1
,
frame
=
true
)
public
static
IRubyObject
include_p
(
ThreadContext
context
,
IRubyObject
self
,
final
IRubyObject
arg
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
ThreadContext
localContext
=
context
;
try
{
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
checkContext
(
localContext
,
ctx
,
"include?/member?"
);
if
(
RubyObject
.
equalInternal
(
ctx
,
larg
,
arg
)) {
throw
JumpException
.
SPECIAL_JUMP
;
}
return
runtime
.
getNil
();
}
});
}
catch
(
JumpException
.
SpecialJump
sj
) {
return
runtime
.
getTrue
();
}
return
runtime
.
getFalse
();
}
@
JRubyMethod
(
name
=
"max"
,
frame
=
true
)
public
static
IRubyObject
max
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
getRuntime
();
final
IRubyObject
result
[] =
new
IRubyObject
[] {
null
};
final
ThreadContext
localContext
=
context
;
if
(
block
.
isGiven
()) {
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
checkContext
(
localContext
,
ctx
,
"max{}"
);
if
(
result
[
0
] ==
null
||
RubyComparable
.
cmpint
(
ctx
,
block
.
yieldArray
(
ctx
,
runtime
.
newArray
(
larg
,
result
[
0
]),
null
,
null
),
larg
,
result
[
0
]) >
0
) {
result
[
0
] =
larg
;
}
return
runtime
.
getNil
();
}
});
}
else
{
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
IRubyObject
larg
=
checkArgs
(
runtime
,
largs
);
synchronized
(
result
) {
if
(
result
[
0
] ==
null
||
RubyComparable
.
cmpint
(
ctx
,
larg
.
callMethod
(
ctx
,
"<=>"
,
result
[
0
]),
larg
,
result
[
0
]) >
0
) {
result
[
0
] =
larg
;
}
}
return
runtime
.
getNil
();
}
});
}
return
result
[
0
] ==
null
?
runtime
.
getNil
() :
result
[
0
];
}
@
JRubyMethod
(
name
=
"max_by"
,
frame
=
true
)
public
static
IRubyObject
max_by
(
ThreadContext
context
,
IRubyObject
self
,
final
Block
block
) {
final
Ruby
runtime
=
context
.
getRuntime
();
if
(!
block
.
isGiven
())
return
enumeratorize
(
runtime
,
self
,
"max_by"
);
final
IRubyObject
result
[] =
new
IRubyObject
[] {
runtime
.
getNil
() };
final
ThreadContext
localContext
=
context
;
callEach
(
runtime
,
context
,
self
,
new
BlockCallback
() {
IRubyObject
memo
=
null
;
public
IRubyObject
call
(
ThreadContext
ctx
,
IRubyObject
[]
largs
,
Block
blk
) {
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL