FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
jruby/core/src/main/java/org/jruby/RubyHash.java at master · jruby/jruby · GitHub
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
/
RubyHash.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
1321 lines (1082 loc) · 46.1 KB
Breadcrumbs
jruby
/
core
/
src
/
main
/
java
/
org
/
jruby
/
RubyHash.java
Copy path
File metadata and controls
1321 lines (1082 loc) · 46.1 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) 2001 Chad Fowler <chadfowler@chadfowler.com>
* Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
* Copyright (C) 2001-2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
* Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
* Copyright (C) 2004-2006 Thomas E Enebo <enebo@acm.org>
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
* Copyright (C) 2005 Charles O Nutter <headius@headius.com>
* Copyright (C) 2006 Ola Bini <Ola.Bini@ki.se>
* Copyright (C) 2006 Tim Azzopardi <tim@tigerfive.com>
* Copyright (C) 2006 Miguel Covarrubias <mlcovarrubias@gmail.com>
* Copyright (C) 2007 MenTaLguY <mental@rydia.net>
*
* 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
.
JRubyClass
;
import
org
.
jruby
.
anno
.
JRubyMethod
;
import
org
.
jruby
.
api
.
Create
;
import
org
.
jruby
.
api
.
Error
;
import
org
.
jruby
.
runtime
.
Block
;
import
org
.
jruby
.
runtime
.
ClassIndex
;
import
org
.
jruby
.
runtime
.
ThreadContext
;
import
org
.
jruby
.
runtime
.
builtin
.
IRubyObject
;
import
org
.
jruby
.
runtime
.
marshal
.
MarshalDumper
;
import
org
.
jruby
.
runtime
.
marshal
.
MarshalLoader
;
import
org
.
jruby
.
util
.
TypeConverter
;
import
org
.
jruby
.
util
.
io
.
RubyInputStream
;
import
org
.
jruby
.
util
.
io
.
RubyOutputStream
;
import
java
.
io
.
IOException
;
import
java
.
util
.
Collection
;
import
java
.
util
.
Map
;
import
java
.
util
.
Set
;
import
static
org
.
jruby
.
api
.
Access
.
arrayClass
;
import
static
org
.
jruby
.
api
.
Access
.
hashClass
;
import
static
org
.
jruby
.
api
.
Convert
.
asBoolean
;
import
static
org
.
jruby
.
api
.
Define
.
defineClass
;
import
static
org
.
jruby
.
api
.
Error
.*;
import
static
org
.
jruby
.
runtime
.
Visibility
.
PRIVATE
;
// Design overview:
//
// RubyHash is implemented as hash table with a singly-linked list of
// RubyHash.RubyHashEntry objects for each bucket. RubyHashEntry objects
// are also kept in a doubly-linked list which reflects their insertion
// order and is used for iteration. For simplicity, this latter list is
// circular; a dummy RubyHashEntry, RubyHash.head, is used to mark the
// ends of the list.
//
// When an entry is removed from the table, it is also removed from the
// doubly-linked list. However, while the reference to the previous
// RubyHashEntry is cleared (to mark the entry as dead), the reference
// to the next RubyHashEntry is preserved so that iterators are not
// invalidated: any iterator with a reference to a dead entry can climb
// back up into the list of live entries by chasing next references until
// it finds a live entry (or head).
//
// Ordinarily, this scheme would require O(N) time to clear a hash (since
// each RubyHashEntry would need to be visited and unlinked from the
// iteration list), but RubyHash also maintains a generation count. Every
// time the hash is cleared, the doubly-linked list is simply discarded and
// the generation count incremented. Iterators check to see whether the
// generation count has changed; if it has, they reset themselves back to
// the new start of the list.
//
// This design means that iterators are never invalidated by changes to the
// hashtable, and they do not need to modify the structure during their
// lifecycle.
//
/** Implementation of the Hash class.
*
* Concurrency: no synchronization is required among readers, but
* all users must synchronize externally with writers.
*
*/
@
JRubyClass
(
name
=
"Hash"
,
include
=
"Enumerable"
,
overrides
= {
RubyHashLinkedBuckets
.
class
})
public
class
RubyHash
extends
RubyObject
implements
Map
{
public
static
final
int
DEFAULT_INSPECT_STR_SIZE
=
20
;
/**
* This field is used as part of the state of RubyHashLinkedBuckets, but when supporting legacy constructors it will
* point at a delegated instance. Once all consumers of the deprecated constructor have been migrated, we move this
* field back into the subclasses and eliminate all delegated methods.
*/
protected
Object
state
;
protected
static
final
byte
COMPARE_BY_IDENTITY
= 0b00000001;
protected
static
final
byte
RUBY2_KEYWORD
= 0b00000010;
protected
static
final
byte
PROCDEFAULT_HASH
= 0b00000100;
protected
static
final
RubyHashEntry
NULL_ENTRY
=
new
RubyHashEntry
();
@
Deprecated
(
since
=
"10.0.3.0"
)
public
static
final
RubyHashEntry
NO_ENTRY
=
NULL_ENTRY
;
@
SuppressWarnings
(
"removal"
)
public
static
RubyClass
createHashClass
(
ThreadContext
context
,
RubyClass
Object
,
RubyModule
Enumerable
) {
return
defineClass
(
context
,
"Hash"
,
Object
, (
runtime
,
klass
) ->
new
RubyHashLinkedBuckets
(
runtime
,
klass
)).
reifiedClass
(
RubyHash
.
class
).
kindOf
(
new
RubyModule
.
JavaClassKindOf
(
RubyHash
.
class
)).
classIndex
(
ClassIndex
.
HASH
).
include
(
context
,
Enumerable
).
defineMethods
(
context
,
RubyHash
.
class
);
}
@
Override
public
ClassIndex
getNativeClassIndex
() {
return
ClassIndex
.
HASH
;
}
/** rb_hash_s_create
*
*/
@
JRubyMethod
(
name
=
"[]"
,
rest
=
true
,
meta
=
true
)
public
static
IRubyObject
create
(
ThreadContext
context
,
IRubyObject
recv
,
IRubyObject
[]
args
,
Block
block
) {
if
(
args
.
length
==
1
) {
IRubyObject
tmp
=
TypeConverter
.
convertToTypeWithCheck
(
args
[
0
],
hashClass
(
context
),
"to_hash"
);
if
(!
tmp
.
isNil
())
return
new
RubyHashLinkedBuckets
(
context
.
runtime
, (
RubyClass
)
recv
, (
RubyHash
)
tmp
);
var
Array
=
arrayClass
(
context
);
final
IRubyObject
nil
=
context
.
nil
;
tmp
=
TypeConverter
.
convertToTypeWithCheck
(
args
[
0
],
Array
,
"to_ary"
);
if
(
tmp
!=
nil
) {
RubyHash
hash
= (
RubyHash
) ((
RubyClass
)
recv
).
allocate
(
context
);
var
arr
= (
RubyArray
<?>)
tmp
;
for
(
int
i
=
0
,
j
=
arr
.
getLength
();
i
<
j
;
i
++) {
IRubyObject
e
=
arr
.
entry
(
i
);
IRubyObject
v
=
TypeConverter
.
convertToTypeWithCheck
(
e
,
Array
,
"to_ary"
);
IRubyObject
key
;
IRubyObject
val
=
nil
;
if
(
v
==
nil
) {
throw
argumentError
(
context
,
"wrong element type "
+
e
.
getMetaClass
() +
" at "
+
i
+
" (expected array)"
);
}
switch
(((
RubyArray
<?>)
v
).
getLength
()) {
default
:
throw
argumentError
(
context
,
"invalid number of elements ("
+ ((
RubyArray
<?>)
v
).
getLength
() +
" for 1..2)"
);
case
2
:
val
= ((
RubyArray
<?>)
v
).
entry
(
1
);
case
1
:
key
= ((
RubyArray
<?>)
v
).
entry
(
0
);
hash
.
fastASetCheckString
(
context
.
runtime
,
key
,
val
);
}
}
return
hash
;
}
}
if
((
args
.
length
&
1
) !=
0
)
throw
argumentError
(
context
,
"odd number of arguments for Hash"
);
RubyHash
hash
= (
RubyHash
) ((
RubyClass
)
recv
).
allocate
(
context
);
for
(
int
i
=
0
;
i
<
args
.
length
;
i
+=
2
)
hash
.
fastASetCheckString
(
context
.
runtime
,
args
[
i
],
args
[
i
+
1
]);
return
hash
;
}
@
JRubyMethod
(
name
=
"try_convert"
,
meta
=
true
)
public
static
IRubyObject
try_convert
(
ThreadContext
context
,
IRubyObject
recv
,
IRubyObject
args
) {
return
TypeConverter
.
convertToTypeWithCheck
(
args
,
hashClass
(
context
),
"to_hash"
);
}
/** rb_hash_new
*
*/
@
Deprecated
(
since
=
"10.1.1.0"
)
public
static
final
RubyHash
newHash
(
Ruby
runtime
) {
return
RubyHashLinkedBuckets
.
newLBHash
(
runtime
);
}
/** rb_hash_new
*
*/
@
SuppressWarnings
(
"deprecation"
)
public
static
final
RubyHash
newSmallHash
(
Ruby
runtime
) {
return
RubyHashLinkedBuckets
.
newLBHash
(
runtime
,
1
);
}
public
static
RubyHash
newKwargs
(
Ruby
runtime
,
String
key
,
IRubyObject
value
) {
return
newHash
(
runtime
,
runtime
.
newSymbol
(
key
),
value
);
}
public
static
RubyHash
newHash
(
Ruby
runtime
,
IRubyObject
key
,
IRubyObject
value
) {
RubyHash
kwargs
=
newSmallHash
(
runtime
);
kwargs
.
fastASetSmall
(
key
,
value
);
return
kwargs
;
}
@
SuppressWarnings
(
"deprecation"
)
public
static
RubyHash
newHash
(
Ruby
runtime
,
IRubyObject
defaultValue
) {
return
RubyHashLinkedBuckets
.
newLBHash
(
runtime
,
defaultValue
);
}
@
SuppressWarnings
(
"deprecation"
)
public
static
RubyHash
newHash
(
Ruby
runtime
,
int
buckets
) {
return
RubyHashLinkedBuckets
.
newLBHash
(
runtime
,
buckets
);
}
@
SuppressWarnings
(
"deprecation"
)
public
static
RubyHash
newHash
(
Ruby
runtime
,
IRubyObject
defaultValue
,
int
buckets
) {
return
RubyHashLinkedBuckets
.
newLBHash
(
runtime
,
defaultValue
,
buckets
);
}
/** rb_hash_new
*
*/
public
static
final
RubyHash
newHash
(
Ruby
runtime
,
Map
valueMap
,
IRubyObject
defaultValue
) {
assert
defaultValue
!=
null
;
return
new
RubyHashLinkedBuckets
(
runtime
,
valueMap
,
defaultValue
);
}
// Only constructor that does not delegate
protected
RubyHash
(
Ruby
runtime
,
RubyClass
klass
,
boolean
objectSpace
,
int
unused
) {
super
(
runtime
,
klass
,
objectSpace
);
// ensure only subclasses call this constructor
assert
this
.
getClass
() !=
RubyHash
.
class
;
}
// Delegated constructor, to be hidden and returned to normal super constructor once no longer in use
@
Deprecated
(
since
=
"10.0.6.0"
,
forRemoval
=
true
)
public
RubyHash
(
Ruby
runtime
,
RubyClass
klass
) {
super
(
runtime
,
klass
);
// ensure no subclasses call this constructor
assert
getClass
() ==
RubyHash
.
class
;
setDelegate
(
this
);
}
// Delegated constructor, to be hidden and returned to normal super constructor once no longer in use
@
Deprecated
(
since
=
"10.0.3.0"
)
public
RubyHash
(
Ruby
runtime
,
RubyClass
klass
,
boolean
objectSpace
) {
super
(
runtime
,
klass
,
objectSpace
);
// ensure no subclasses call this constructor
assert
getClass
() ==
RubyHash
.
class
;
this
.
setDelegate
(
new
RubyHashLinkedBuckets
(
runtime
,
klass
,
objectSpace
));
}
// Delegated constructor, to be hidden and returned to normal super constructor once no longer in use
@
Deprecated
(
since
=
"10.0.6.0"
,
forRemoval
=
true
)
public
RubyHash
(
Ruby
runtime
,
int
buckets
) {
super
(
runtime
,
runtime
.
getHash
());
// ensure no subclasses call this constructor
assert
getClass
() ==
RubyHash
.
class
;
this
.
setDelegate
(
RubyHashLinkedBuckets
.
newLBHash
(
runtime
,
buckets
));
}
// Delegated constructor, to be hidden and returned to normal super constructor once no longer in use
@
Deprecated
(
since
=
"10.0.6.0"
,
forRemoval
=
true
)
public
RubyHash
(
Ruby
runtime
) {
super
(
runtime
,
runtime
.
getHash
());
// ensure no subclasses call this constructor
assert
getClass
() ==
RubyHash
.
class
;
this
.
setDelegate
(
RubyHashLinkedBuckets
.
newLBHash
(
runtime
));
}
// Delegated constructor, to be hidden and returned to normal super constructor once no longer in use
@
Deprecated
(
since
=
"10.0.6.0"
,
forRemoval
=
true
)
public
RubyHash
(
Ruby
runtime
,
IRubyObject
defaultValue
) {
super
(
runtime
,
runtime
.
getHash
());
// ensure no subclasses call this constructor
assert
getClass
() ==
RubyHash
.
class
;
this
.
setDelegate
(
RubyHashLinkedBuckets
.
newLBHash
(
runtime
,
defaultValue
));
}
// Delegated constructor, to be hidden and returned to normal super constructor once no longer in use
@
Deprecated
(
since
=
"10.0.6.0"
,
forRemoval
=
true
)
public
RubyHash
(
Ruby
runtime
,
IRubyObject
defaultValue
,
int
buckets
) {
super
(
runtime
,
runtime
.
getHash
());
// ensure no subclasses call this constructor
assert
getClass
() ==
RubyHash
.
class
;
this
.
setDelegate
(
RubyHashLinkedBuckets
.
newLBHash
(
runtime
,
defaultValue
,
buckets
));
}
// Delegated constructor, to be hidden and returned to normal super constructor once no longer in use
@
Deprecated
(
since
=
"10.0.3.0"
,
forRemoval
=
true
)
public
RubyHash
(
Ruby
runtime
,
Map
valueMap
,
IRubyObject
defaultValue
) {
super
(
runtime
,
runtime
.
getHash
());
// ensure no subclasses call this constructor
assert
getClass
() ==
RubyHash
.
class
;
this
.
setDelegate
(
RubyHashLinkedBuckets
.
newHash
(
runtime
,
valueMap
,
defaultValue
));
}
/* ============================
* Here are hash internals
* (This could be extracted to a separate class but it's not too large though)
* ============================
*/
@
Deprecated
(
since
=
"10.0.3.0"
)
public
static
final
int
MRI_PRIMES
[] = {
8
+
3
,
16
+
3
,
32
+
5
,
64
+
3
,
128
+
3
,
256
+
27
,
512
+
9
,
1024
+
9
,
2048
+
5
,
4096
+
3
,
8192
+
27
,
16384
+
43
,
32768
+
3
,
65536
+
45
,
131072
+
29
,
262144
+
3
,
524288
+
21
,
1048576
+
7
,
2097152
+
17
,
4194304
+
15
,
8388608
+
9
,
16777216
+
43
,
33554432
+
35
,
67108864
+
15
,
134217728
+
29
,
268435456
+
3
,
536870912
+
11
,
1073741824
+
85
,
0
};
private
RubyHash
getDelegate
() {
return
(
RubyHash
)
state
;
}
private
void
setDelegate
(
RubyHash
state
) {
this
.
state
=
state
;
}
public
static
class
RubyHashEntryLink
{
RubyHash
.
RubyHashEntryLink
prevAdded
;
RubyHash
.
RubyHashEntryLink
nextAdded
;
int
hash
() {
// should never be called
throw
new
RuntimeException
(
"hash() should never be called on RubyHashEntryLink"
);
}
public
IRubyObject
key
() {
// should never be called
throw
new
RuntimeException
(
"key() should never be called on RubyHashEntryLink"
);
}
IRubyObject
value
() {
// should never be called
throw
new
RuntimeException
(
"value() should never be called on RubyHashEntryLink"
);
}
IRubyObject
value
(
IRubyObject
value
) {
// should never be called
throw
new
RuntimeException
(
"value(IRubyObject) should never be called on RubyHashEntryLink"
);
}
public
boolean
isLive
() {
// should never be called
throw
new
RuntimeException
(
"value() should never be called on RubyHashEntryLink"
);
}
}
public
static
final
class
RubyHashEntry
extends
RubyHashEntryLink
implements
Map
.
Entry
{
private
final
IRubyObject
key
;
private
IRubyObject
value
;
private
RubyHashEntry
next
;
private
final
int
hash
;
RubyHashEntry
() {
key
=
NEVER
;
hash
= -
1
;
}
public
RubyHashEntry
(
int
h
,
IRubyObject
k
,
IRubyObject
v
,
RubyHashEntry
e
,
RubyHashEntryLink
head
) {
key
=
k
;
value
(
v
);
next
(
e
);
hash
=
h
;
if
(
head
!=
null
) {
RubyHashEntryLink
prevAdded
=
head
.
prevAdded
;
RubyHashEntryLink
nextAdded
=
head
;
this
.
prevAdded
=
prevAdded
;
prevAdded
.
nextAdded
=
this
;
this
.
nextAdded
=
nextAdded
;
nextAdded
.
prevAdded
=
this
;
}
}
public
RubyHashEntry
(
RubyHashEntry
oldEntry
,
int
newHash
) {
this
.
hash
=
newHash
;
this
.
key
=
oldEntry
.
key
();
this
.
value
(
oldEntry
.
value
());
this
.
next
(
oldEntry
.
next
());
// prevAdded is never null
RubyHashEntryLink
prevAdded
=
oldEntry
.
prevAdded
;
this
.
prevAdded
=
prevAdded
;
prevAdded
.
nextAdded
=
this
;
RubyHashEntryLink
nextAdded
=
oldEntry
.
nextAdded
;
if
(
nextAdded
!=
null
) {
this
.
nextAdded
=
nextAdded
;
nextAdded
.
prevAdded
=
this
;
}
}
public
void
detach
() {
if
(
prevAdded
!=
null
) {
prevAdded
.
nextAdded
=
nextAdded
;
nextAdded
.
prevAdded
=
prevAdded
;
prevAdded
=
null
;
}
}
public
boolean
isLive
() {
return
prevAdded
!=
null
;
}
@
Override
public
Object
getKey
() {
return
key
();
}
@
Override
public
Object
getValue
() {
return
value
();
}
@
Override
public
Object
setValue
(
Object
value
) {
IRubyObject
oldValue
=
this
.
value
();
if
(
value
instanceof
IRubyObject
) {
this
.
value
((
IRubyObject
)
value
);
}
else
{
throw
new
UnsupportedOperationException
(
"directEntrySet() doesn't support setValue for non IRubyObject instance entries, convert them manually or use entrySet() instead"
);
}
return
oldValue
;
}
@
Override
public
boolean
equals
(
Object
other
){
if
(!(
other
instanceof
RubyHashEntry
))
return
false
;
RubyHashEntry
otherEntry
= (
RubyHashEntry
)
other
;
return
(
key
() ==
otherEntry
.
key
() ||
key
().
eql
(
otherEntry
.
key
())) &&
(
value
() ==
otherEntry
.
value
() ||
value
().
equals
(
otherEntry
.
value
()));
}
@
Override
public
int
hashCode
(){
return
key
().
hashCode
() ^
value
().
hashCode
();
}
public
IRubyObject
key
() {
return
key
;
}
IRubyObject
value
() {
return
value
;
}
IRubyObject
value
(
IRubyObject
value
) {
IRubyObject
oldValue
=
this
.
value
;
this
.
value
=
value
;
return
oldValue
;
}
RubyHashEntry
next
() {
return
next
;
}
void
next
(
RubyHashEntry
next
) {
this
.
next
=
next
;
}
int
hash
() {
return
hash
;
}
@
Deprecated
(
since
=
"10.1.1.0"
)
public
Object
getJavaifiedKey
(){
return
key
.
toJava
(
Object
.
class
);
}
@
Deprecated
(
since
=
"10.1.1.0"
)
public
Object
getJavaifiedValue
() {
return
value
.
toJava
(
Object
.
class
);
}
}
// put implementation
public
IRubyObject
internalPut
(
final
IRubyObject
key
,
final
IRubyObject
value
) {
return
getDelegate
().
internalPut
(
key
,
value
);
}
boolean
internalPutIfNoKey
(
final
IRubyObject
key
,
final
IRubyObject
value
) {
return
getDelegate
().
internalPutIfNoKey
(
key
,
value
);
}
// get implementation
protected
IRubyObject
internalGet
(
IRubyObject
key
) {
return
getDelegate
().
internalGet
(
key
);
}
@
Deprecated
(
since
=
"10.0.3.0"
)
RubyHashEntry
getEntry
(
IRubyObject
key
) {
return
getDelegate
().
getEntry
(
key
);
}
public
interface
VisitorWithStateI
{
void
visit
(
ThreadContext
context
,
RubyHash
self
,
IRubyObject
key
,
IRubyObject
value
,
int
index
);
}
public
static
abstract
class
VisitorWithState
<
T
> {
public
abstract
void
visit
(
ThreadContext
context
,
RubyHash
self
,
IRubyObject
key
,
IRubyObject
value
,
int
index
,
T
state
);
}
public
static
abstract
class
Visitor
extends
VisitorWithState
{
public
void
visit
(
ThreadContext
context
,
RubyHash
self
,
IRubyObject
key
,
IRubyObject
value
,
int
index
,
Object
state
) {
visit
(
key
,
value
);
}
public
abstract
void
visit
(
IRubyObject
key
,
IRubyObject
value
);
}
public
<
T
>
void
visitAll
(
ThreadContext
context
,
VisitorWithStateI
visitor
) {
getDelegate
().
visitAll
(
context
,
visitor
);
}
public
<
T
>
void
visitAll
(
ThreadContext
context
,
VisitorWithState
visitor
,
T
state
) {
this
.
getDelegate
().
visitAll
(
context
,
visitor
,
state
);
}
protected
<
T
>
void
visitLimited
(
ThreadContext
context
,
RubyHash
.
VisitorWithState
visitor
,
long
size
,
T
state
) {
this
.
getDelegate
().
visitLimited
(
context
,
visitor
,
size
,
state
);
}
public
<
T
>
boolean
allSymbols
() {
return
getDelegate
().
allSymbols
();
}
/* ============================
* End of hash internals
* ============================
*/
/* ================
* Instance Methods
* ================
*/
@
JRubyMethod
(
visibility
=
PRIVATE
,
keywords
=
true
)
public
IRubyObject
initialize
(
ThreadContext
context
,
final
Block
block
) {
return
getDelegate
().
initialize
(
context
,
block
);
}
@
JRubyMethod
(
visibility
=
PRIVATE
,
keywords
=
true
)
public
IRubyObject
initialize
(
ThreadContext
context
,
IRubyObject
_default
,
final
Block
block
) {
return
getDelegate
().
initialize
(
context
,
_default
,
block
);
}
@
JRubyMethod
(
visibility
=
PRIVATE
,
keywords
=
true
)
public
IRubyObject
initialize
(
ThreadContext
context
,
IRubyObject
_default
,
IRubyObject
hash
,
final
Block
block
) {
return
getDelegate
().
initialize
(
context
,
_default
,
hash
,
block
);
}
@
JRubyMethod
(
name
=
"default"
)
public
IRubyObject
default_value_get
(
ThreadContext
context
) {
return
getDelegate
().
default_value_get
(
context
);
}
@
JRubyMethod
(
name
=
"default"
)
public
IRubyObject
default_value_get
(
ThreadContext
context
,
IRubyObject
arg
) {
return
getDelegate
().
default_value_get
(
context
,
arg
);
}
@
JRubyMethod
(
name
=
"default="
)
public
IRubyObject
default_value_set
(
ThreadContext
context
,
final
IRubyObject
defaultValue
) {
return
getDelegate
().
default_value_set
(
context
,
defaultValue
);
}
@
JRubyMethod
public
IRubyObject
default_proc
(
ThreadContext
context
) {
return
getDelegate
().
default_proc
(
context
);
}
@
JRubyMethod
(
name
=
"default_proc="
)
public
IRubyObject
set_default_proc
(
ThreadContext
context
,
IRubyObject
proc
) {
return
getDelegate
().
set_default_proc
(
context
,
proc
);
}
public
void
modify
() {
getDelegate
().
modify
();
}
@
JRubyMethod
(
name
=
"inspect"
)
public
IRubyObject
inspect
(
ThreadContext
context
) {
return
getDelegate
().
inspect
(
context
);
}
@
JRubyMethod
(
name
= {
"size"
,
"length"
})
public
RubyFixnum
rb_size
(
ThreadContext
context
) {
return
getDelegate
().
rb_size
(
context
);
}
@
JRubyMethod
(
name
=
"empty?"
)
public
RubyBoolean
empty_p
(
ThreadContext
context
) {
return
getDelegate
().
empty_p
(
context
);
}
@
JRubyMethod
(
name
=
"to_a"
)
@
Override
public
RubyArray
to_a
(
ThreadContext
context
) {
return
getDelegate
().
to_a
(
context
);
}
@
JRubyMethod
(
name
=
"to_s"
)
public
IRubyObject
to_s
(
ThreadContext
context
) {
return
getDelegate
().
to_s
(
context
);
}
@
JRubyMethod
(
name
=
"rehash"
)
public
RubyHash
rehash
(
ThreadContext
context
) {
return
getDelegate
().
rehash
(
context
);
}
@
JRubyMethod
(
name
=
"to_hash"
)
public
RubyHash
to_hash
(
ThreadContext
context
) {
return
getDelegate
().
to_hash
(
context
);
}
@
JRubyMethod
public
RubyHash
to_h
(
ThreadContext
context
,
Block
block
) {
return
getDelegate
().
to_h
(
context
,
block
);
}
protected
RubyHash
to_h_block
(
ThreadContext
context
,
Block
block
) {
return
getDelegate
().
to_h_block
(
context
,
block
);
}
@
Override
public
RubyHash
convertToHash
() {
return
getDelegate
().
convertToHash
();
}
public
void
fastASet
(
IRubyObject
key
,
IRubyObject
value
) {
getDelegate
().
fastASet
(
key
,
value
);
}
public
void
fastASetSmall
(
IRubyObject
key
,
IRubyObject
value
) {
getDelegate
().
fastASetSmall
(
key
,
value
);
}
// MRI: rb_hash_set_pair, fast/small version
public
void
fastASetSmallPair
(
ThreadContext
context
,
IRubyObject
_pair
) {
getDelegate
().
fastASetSmallPair
(
context
,
_pair
);
}
public
void
fastASetCheckString
(
Ruby
runtime
,
IRubyObject
key
,
IRubyObject
value
) {
getDelegate
().
fastASetCheckString
(
runtime
,
key
,
value
);
}
public
void
fastASetSmallCheckString
(
Ruby
runtime
,
IRubyObject
key
,
IRubyObject
value
) {
getDelegate
().
fastASetSmallCheckString
(
runtime
,
key
,
value
);
}
public
void
fastASet
(
Ruby
runtime
,
IRubyObject
key
,
IRubyObject
value
,
boolean
prepareString
) {
getDelegate
().
fastASet
(
runtime
,
key
,
value
,
prepareString
);
}
public
void
fastASetSmall
(
Ruby
runtime
,
IRubyObject
key
,
IRubyObject
value
,
boolean
prepareString
) {
getDelegate
().
fastASetSmall
(
runtime
,
key
,
value
,
prepareString
);
}
/**
* Set a key/value pair into this hash.
*
* @param context the current thread context
* @param key the key
* @param value the value
* @return the value set
*/
@
JRubyMethod
(
name
= {
"[]="
,
"store"
})
public
IRubyObject
op_aset
(
ThreadContext
context
,
IRubyObject
key
,
IRubyObject
value
) {
return
getDelegate
().
op_aset
(
context
,
key
,
value
);
}
// returns null when not found to avoid unnecessary getRuntime().getNil() call
public
IRubyObject
fastARef
(
IRubyObject
key
) {
return
getDelegate
().
fastARef
(
key
);
}
public
RubyBoolean
compare
(
final
ThreadContext
context
,
VisitorWithState
<
RubyHash
>
visitor
,
IRubyObject
other
,
boolean
eql
) {
return
getDelegate
().
compare
(
context
,
visitor
,
other
,
eql
);
}
@
Override
@
JRubyMethod
(
name
=
"=="
)
public
IRubyObject
op_equal
(
final
ThreadContext
context
,
IRubyObject
other
) {
return
getDelegate
().
op_equal
(
context
,
other
);
}
@
JRubyMethod
(
name
=
"eql?"
)
public
IRubyObject
op_eql
(
final
ThreadContext
context
,
IRubyObject
other
) {
return
getDelegate
().
op_eql
(
context
,
other
);
}
@
JRubyMethod
(
name
=
"[]"
)
public
IRubyObject
op_aref
(
ThreadContext
context
,
IRubyObject
key
) {
return
getDelegate
().
op_aref
(
context
,
key
);
}
@
JRubyMethod
(
name
=
"<"
)
public
IRubyObject
op_lt
(
ThreadContext
context
,
IRubyObject
other
) {
return
getDelegate
().
op_lt
(
context
,
other
);
}
@
JRubyMethod
(
name
=
"<="
)
public
IRubyObject
op_le
(
ThreadContext
context
,
IRubyObject
other
) {
return
getDelegate
().
op_le
(
context
,
other
);
}
@
JRubyMethod
(
name
=
">"
)
public
IRubyObject
op_gt
(
ThreadContext
context
,
IRubyObject
other
) {
return
getDelegate
().
op_gt
(
context
,
other
);
}
@
JRubyMethod
(
name
=
">="
)
public
IRubyObject
op_ge
(
ThreadContext
context
,
IRubyObject
other
) {
return
getDelegate
().
op_ge
(
context
,
other
);
}
// MRI: rb_hash_hash
@
JRubyMethod
(
name
=
"hash"
)
public
RubyFixnum
hash
(
ThreadContext
context
) {
return
getDelegate
().
hash
(
context
);
}
public
IRubyObject
fetch
(
ThreadContext
context
,
IRubyObject
[]
args
,
Block
block
) {
return
getDelegate
().
fetch
(
context
,
args
,
block
);
}
@
JRubyMethod
(
rest
=
true
)
public
IRubyObject
except
(
ThreadContext
context
,
IRubyObject
[]
keys
) {
return
getDelegate
().
except
(
context
,
keys
);
}
@
JRubyMethod
public
IRubyObject
fetch
(
ThreadContext
context
,
IRubyObject
key
,
Block
block
) {
return
getDelegate
().
fetch
(
context
,
key
,
block
);
}
@
JRubyMethod
public
IRubyObject
fetch
(
ThreadContext
context
,
IRubyObject
key
,
IRubyObject
_default
,
Block
block
) {
return
getDelegate
().
fetch
(
context
,
key
,
_default
,
block
);
}
@
JRubyMethod
(
name
= {
"has_key?"
,
"key?"
,
"include?"
,
"member?"
})
public
RubyBoolean
has_key_p
(
ThreadContext
context
,
IRubyObject
key
) {
return
getDelegate
().
has_key_p
(
context
,
key
);
}
/**
* A Java API to test the presence of a (Ruby) key in the Hash
*
* @param key the native (Ruby) key
* @return true if the hash contains the provided key
*/
public
boolean
hasKey
(
IRubyObject
key
) {
return
getDelegate
().
hasKey
(
key
);
}
@
JRubyMethod
(
name
= {
"has_value?"
,
"value?"
})
public
RubyBoolean
has_value_p
(
ThreadContext
context
,
IRubyObject
expected
) {
return
getDelegate
().
has_value_p
(
context
,
expected
);
}
public
RubyHash
eachCommon
(
final
ThreadContext
context
,
final
Block
block
) {
return
getDelegate
().
eachCommon
(
context
,
block
);
}
@
JRubyMethod
(
name
= {
"each"
,
"each_pair"
})
public
IRubyObject
each
(
final
ThreadContext
context
,
final
Block
block
) {
return
getDelegate
().
each
(
context
,
block
);
}
public
RubyHash
each_pairCommon
(
final
ThreadContext
context
,
final
Block
block
) {
return
getDelegate
().
each_pairCommon
(
context
,
block
);
}
public
RubyHash
each_valueCommon
(
final
ThreadContext
context
,
final
Block
block
) {
return
getDelegate
().
each_valueCommon
(
context
,
block
);
}
@
JRubyMethod
public
IRubyObject
each_value
(
final
ThreadContext
context
,
final
Block
block
) {
return
getDelegate
().
each_value
(
context
,
block
);
}
public
RubyHash
each_keyCommon
(
final
ThreadContext
context
,
final
Block
block
) {
return
getDelegate
().
each_keyCommon
(
context
,
block
);
}
@
JRubyMethod
public
IRubyObject
each_key
(
final
ThreadContext
context
,
final
Block
block
) {
return
getDelegate
().
each_key
(
context
,
block
);
}
@
JRubyMethod
(
name
=
"transform_keys"
,
rest
=
true
)
public
IRubyObject
transform_keys
(
final
ThreadContext
context
,
IRubyObject
[]
args
,
final
Block
block
) {
return
getDelegate
().
transform_keys
(
context
,
args
,
block
);
}
@
JRubyMethod
(
name
=
"transform_values"
)
public
IRubyObject
transform_values
(
final
ThreadContext
context
,
final
Block
block
) {
return
getDelegate
().
transform_values
(
context
,
block
);
}
@
JRubyMethod
(
name
=
"transform_keys!"
,
rest
=
true
)
public
IRubyObject
transform_keys_bang
(
final
ThreadContext
context
,
IRubyObject
[]
args
,
final
Block
block
) {
return
getDelegate
().
transform_keys_bang
(
context
,
args
,
block
);
}
@
JRubyMethod
(
name
=
"transform_values!"
)
public
IRubyObject
transform_values_bang
(
final
ThreadContext
context
,
final
Block
block
) {
return
getDelegate
().
transform_values_bang
(
context
,
block
);
}
@
JRubyMethod
(
name
=
"select!"
,
alias
=
"filter!"
)
public
IRubyObject
select_bang
(
final
ThreadContext
context
,
final
Block
block
) {
return
getDelegate
().
select_bang
(
context
,
block
);
}
@
JRubyMethod
public
IRubyObject
keep_if
(
final
ThreadContext
context
,
final
Block
block
) {
return
getDelegate
().
keep_if
(
context
,
block
);
}
public
boolean
keep_ifCommon
(
final
ThreadContext
context
,
final
Block
block
) {
return
getDelegate
().
keep_ifCommon
(
context
,
block
);
}
@
JRubyMethod
public
IRubyObject
key
(
ThreadContext
context
,
IRubyObject
expected
) {
return
getDelegate
().
key
(
context
,
expected
);
}
@
JRubyMethod
(
name
=
"keys"
)
public
RubyArray
keys
(
final
ThreadContext
context
) {
return
getDelegate
().
keys
(
context
);
}
public
RubyArray
keys
() {
return
getDelegate
().
keys
();
}
@
JRubyMethod
(
name
=
"values"
)
public
RubyArray
values
(
final
ThreadContext
context
) {
return
getDelegate
().
values
(
context
);
}
public
RubyArray
rb_values
(
ThreadContext
context
) {
return
getDelegate
().
rb_values
(
context
);
}
public
static
final
VisitorWithState
<
RubyArray
>
StoreValueVisitor
=
new
VisitorWithState
<
RubyArray
>() {
@
Override
public
void
visit
(
ThreadContext
context
,
RubyHash
self
,
IRubyObject
key
,
IRubyObject
value
,
int
index
,
RubyArray
values
) {
values
.
storeInternal
(
context
,
index
,
value
);
}
};
// like RubyHash.StoreValueVisitor but 'unsafe' - user needs to assure array capacity and adjust length
static
final
VisitorWithState
SetValueVisitor
=
new
VisitorWithState
<
RubyArray
>() {
public
void
visit
(
ThreadContext
context
,
RubyHash
self
,
IRubyObject
key
,
IRubyObject
val
,
int
index
,
RubyArray
target
) {
target
.
eltInternalSet
(
index
,
val
);
}
};
@
JRubyMethod
(
name
=
"shift"
)
public
IRubyObject
shift
(
ThreadContext
context
) {
return
getDelegate
().
shift
(
context
);
}
public
boolean
fastDelete
(
IRubyObject
key
) {
return
getDelegate
().
fastDelete
(
key
);
}
@
JRubyMethod
public
IRubyObject
delete
(
ThreadContext
context
,
IRubyObject
key
,
Block
block
) {
return
getDelegate
().
delete
(
context
,
key
,
block
);
}
public
IRubyObject
delete
(
IRubyObject
key
) {
return
getDelegate
().
delete
(
key
);
}
public
IRubyObject
delete
(
ThreadContext
context
,
IRubyObject
key
) {
return
getDelegate
().
delete
(
context
,
key
);
}
@
JRubyMethod
(
name
=
"select"
,
alias
=
"filter"
)
public
IRubyObject
select
(
final
ThreadContext
context
,
final
Block
block
) {
return
getDelegate
().
select
(
context
,
block
);
}
@
JRubyMethod
(
name
=
"slice"
,
rest
=
true
)
public
RubyHash
slice
(
final
ThreadContext
context
,
final
IRubyObject
[]
args
) {
return
getDelegate
().
slice
(
context
,
args
);
}
public
RubyHash
delete_ifInternal
(
ThreadContext
context
,
Block
block
) {
return
getDelegate
().
delete_ifInternal
(
context
,
block
);
}
@
JRubyMethod
public
IRubyObject
delete_if
(
final
ThreadContext
context
,
final
Block
block
) {
return
getDelegate
().
delete_if
(
context
,
block
);
}
public
RubyHash
rejectInternal
(
ThreadContext
context
,
Block
block
) {
return
getDelegate
().
rejectInternal
(
context
,
block
);
}
@
JRubyMethod
public
IRubyObject
reject
(
final
ThreadContext
context
,
final
Block
block
) {
return
getDelegate
().
reject
(
context
,
block
);
}
public
IRubyObject
reject_bangInternal
(
ThreadContext
context
,
Block
block
) {
return
getDelegate
().
reject_bangInternal
(
context
,
block
);
}
@
JRubyMethod
(
name
=
"reject!"
)
public
IRubyObject
reject_bang
(
final
ThreadContext
context
,
final
Block
block
) {
return
getDelegate
().
reject_bang
(
context
,
block
);
}
@
JRubyMethod
(
name
=
"clear"
)
public
RubyHash
rb_clear
(
ThreadContext
context
) {
return
getDelegate
().
rb_clear
(
context
);
}
@
JRubyMethod
(
name
=
"invert"
)
public
RubyHash
invert
(
final
ThreadContext
context
) {
return
getDelegate
().
invert
(
context
);
}
@
JRubyMethod
(
name
= {
"merge!"
,
"update"
},
rest
=
true
)
public
RubyHash
merge_bang
(
ThreadContext
context
,
IRubyObject
[]
others
,
Block
block
) {
return
getDelegate
().
merge_bang
(
context
,
others
,
block
);
}
public
void
addAll
(
ThreadContext
context
,
RubyHash
otherHash
) {
getDelegate
().
addAll
(
context
,
otherHash
);
}
@
JRubyMethod
(
rest
=
true
)
public
RubyHash
merge
(
ThreadContext
context
,
IRubyObject
[]
others
,
Block
block
) {
return
getDelegate
().
merge
(
context
,
others
,
block
);
}
@
JRubyMethod
(
name
=
"initialize_copy"
,
visibility
=
PRIVATE
)
public
RubyHash
initialize_copy
(
ThreadContext
context
,
IRubyObject
other
) {
return
getDelegate
().
initialize_copy
(
context
,
other
);
}
@
JRubyMethod
(
name
=
"replace"
)
public
RubyHash
replace
(
final
ThreadContext
context
,
IRubyObject
other
) {
return
getDelegate
().
replace
(
context
,
other
);
}
@
JRubyMethod
(
name
=
"values_at"
,
rest
=
true
)
public
RubyArray
values_at
(
ThreadContext
context
,
IRubyObject
[]
args
) {
return
getDelegate
().
values_at
(
context
,
args
);
}
@
JRubyMethod
(
name
=
"fetch_values"
,
rest
=
true
)
public
RubyArray
fetch_values
(
ThreadContext
context
,
IRubyObject
[]
args
,
Block
block
) {
return
getDelegate
().
fetch_values
(
context
,
args
,
block
);
}
@
JRubyMethod
(
name
=
"assoc"
)
public
IRubyObject
assoc
(
final
ThreadContext
context
,
final
IRubyObject
obj
) {
return
getDelegate
().
assoc
(
context
,
obj
);
}
@
JRubyMethod
(
name
=
"rassoc"
)
public
IRubyObject
rassoc
(
final
ThreadContext
context
,
final
IRubyObject
obj
) {
return
getDelegate
().
rassoc
(
context
,
obj
);
}
@
JRubyMethod
public
IRubyObject
flatten
(
ThreadContext
context
) {
return
getDelegate
().
flatten
(
context
);
}
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL