@@ -502,8 +502,6 @@ protected final void checkIterating() {
502
502
throw getRuntime ().newRuntimeError ("can't add a new key into hash during iteration" );
503
503
}
504
504
}
505
- // ------------------------------
506
- public static long collisions = 0 ;
507
505
508
506
// put implementation
509
507
@@ -512,26 +510,32 @@ private final void internalPut(final IRubyObject key, final IRubyObject value) {
512
510
}
513
511
514
512
private final void internalPutSmall (final IRubyObject key , final IRubyObject value ) {
515
- internalPutSmall (key , value , true );
513
+ internalPutNoResize (key , value , true );
516
514
}
517
515
518
516
protected void internalPut (final IRubyObject key , final IRubyObject value , final boolean checkForExisting ) {
519
517
checkResize ();
520
518
521
- internalPutSmall (key , value , checkForExisting );
519
+ internalPutNoResize (key , value , checkForExisting );
522
520
}
523
521
524
- protected void internalPutSmall (final IRubyObject key , final IRubyObject value , final boolean checkForExisting ) {
522
+ protected final IRubyObject internalJavaPut (final IRubyObject key , final IRubyObject value ) {
523
+ checkResize ();
524
+
525
+ return internalPutNoResize (key , value , true );
526
+ }
527
+
528
+ protected IRubyObject internalPutNoResize (final IRubyObject key , final IRubyObject value , final boolean checkForExisting ) {
525
529
final int hash = hashValue (key );
526
530
final int i = bucketIndex (hash , table .length );
527
531
528
- // if (table[i] != null) collisions++;
529
-
530
532
if (checkForExisting ) {
531
533
for (RubyHashEntry entry = table [i ]; entry != null ; entry = entry .next ) {
532
534
if (internalKeyExist (entry , hash , key )) {
535
+ IRubyObject existing = entry .value ;
533
536
entry .value = value ;
534
- return ;
537
+
538
+ return existing ;
535
539
}
536
540
}
537
541
}
@@ -540,6 +544,9 @@ protected void internalPutSmall(final IRubyObject key, final IRubyObject value,
540
544
541
545
table [i ] = new RubyHashEntry (hash , key , value , table [i ], head );
542
546
size ++;
547
+
548
+ // no existing entry
549
+ return null ;
543
550
}
544
551
545
552
// get implementation
@@ -1003,7 +1010,7 @@ protected void op_asetSmallForString(Ruby runtime, RubyString key, IRubyObject v
1003
1010
} else {
1004
1011
checkIterating ();
1005
1012
if (!key .isFrozen ()) key = (RubyString )key .dupFrozen ();
1006
- internalPutSmall (key , value , false );
1013
+ internalPutNoResize (key , value , false );
1007
1014
}
1008
1015
}
1009
1016
@@ -2049,8 +2056,9 @@ public Object get(Object key) {
2049
2056
2050
2057
@ Override
2051
2058
public Object put (Object key , Object value ) {
2052
- internalPut (JavaUtil .convertJavaToUsableRubyObject (getRuntime (), key ), JavaUtil .convertJavaToUsableRubyObject (getRuntime (), value ));
2053
- return value ;
2059
+ Ruby runtime = getRuntime ();
2060
+ IRubyObject existing = internalJavaPut (JavaUtil .convertJavaToUsableRubyObject (runtime , key ), JavaUtil .convertJavaToUsableRubyObject (runtime , value ));
2061
+ return existing == null ? null : existing .toJava (Object .class );
2054
2062
}
2055
2063
2056
2064
@ Override
@@ -2490,4 +2498,9 @@ public IRubyObject default_value_get(ThreadContext context, IRubyObject[] args)
2490
2498
throw context .runtime .newArgumentError (args .length , 1 );
2491
2499
}
2492
2500
}
2501
+
2502
+ @ Deprecated
2503
+ protected void internalPutSmall (final IRubyObject key , final IRubyObject value , final boolean checkForExisting ) {
2504
+ internalPutNoResize (key , value , checkForExisting );
2505
+ }
2493
2506
}
0 commit comments