diff options
| author | Cliff Click <cliffc@acm.org> | 2026-10-12 16:46:53 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-10-12 16:46:53 -0700 |
| commit | 0e94cdc0c481e9312d1b5033b3597c7c262ff878 (patch) | |
| tree | 2d77b8aa47dc406407ce7ccfe95b415a68016342 /chapter25/src/main/java/com/seaofnodes/simple/util | |
| parent | 1213e8e8f359c8a1abb05ee8a220d9c56bc36051 (diff) | |
| download | Simple-clinit.tar.gz Simple-clinit.zip | |
Check for cyclic class init before we emit codeclinit
Diffstat (limited to 'chapter25/src/main/java/com/seaofnodes/simple/util')
| -rw-r--r-- | chapter25/src/main/java/com/seaofnodes/simple/util/IntHashMap.java | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/chapter25/src/main/java/com/seaofnodes/simple/util/IntHashMap.java b/chapter25/src/main/java/com/seaofnodes/simple/util/IntHashMap.java index 55aaa9f9..b356ec31 100644 --- a/chapter25/src/main/java/com/seaofnodes/simple/util/IntHashMap.java +++ b/chapter25/src/main/java/com/seaofnodes/simple/util/IntHashMap.java @@ -17,6 +17,7 @@ public class IntHashMap { } public int size() { return _size; } + public boolean isEmpty() { return _size==0; } public void clear() { _size = 0; Arrays.fill(_es,_sentinel); @@ -29,7 +30,7 @@ public class IntHashMap { int old = _es[(slot<<1)+1]; if( old == _sentinel && val != _sentinel ) _size++; if( old != _sentinel && val == _sentinel ) _size--; - // Check cap + // Check cap, resize at 3/4 full if( _size > ((cap()>>1) + (cap()>>2)) ) { grow(); slot = probe(key); @@ -45,7 +46,7 @@ public class IntHashMap { return _es[(slot<<1)+1]; } - int probe( int key ) { + private int probe( int key ) { int mask = cap()-1; int slot = key & mask; while( _es[slot<<1] != _sentinel && _es[slot<<1] != key ) @@ -59,7 +60,28 @@ public class IntHashMap { _es = new int[_es.length<<1]; Arrays.fill(_es,_sentinel); for( int i=0; i<old.length; i+=2 ) - if( old[i] != _sentinel ) - put(old[i],old[i+1]); + if( old[i] != _sentinel ) { + int slot = probe(old[i]); + _es[ slot<<1 ] = old[i ]; + _es[(slot<<1)+1] = old[i+1]; + } + } + + // Just all the keys + public int[] keySet() { + int[] keys = new int[_size]; + int j=0; + for( int i=0; i<_es.length; i+=2 ) + if( _es[i] != _sentinel ) + keys[j++] = _es[i]; + return keys; + } + + @Override public String toString() { + SB sb = new SB().p("[ "); + for( int i=0; i<_es.length; i+=2 ) + if( _es[i] != _sentinel ) + sb.p('(').p(_es[i]).p(',').p(_es[i+1]).p(')').p(','); + return sb.unchar().p(']').toString(); } } |
