Turned some helper classes into inner classes to avoid naming conflicts.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@76 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// $Id: HashIntMap.java,v 1.1 2001/03/01 18:47:01 mdb Exp $
|
// $Id: HashIntMap.java,v 1.2 2001/03/02 05:31:13 mdb Exp $
|
||||||
|
|
||||||
package com.samskivert.util;
|
package com.samskivert.util;
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ public class IntMap
|
|||||||
|
|
||||||
public IntMap (int buckets)
|
public IntMap (int buckets)
|
||||||
{
|
{
|
||||||
_buckets = new IntRecord[buckets];
|
_buckets = new Record[buckets];
|
||||||
}
|
}
|
||||||
|
|
||||||
public IntMap ()
|
public IntMap ()
|
||||||
@@ -33,17 +33,17 @@ public class IntMap
|
|||||||
public synchronized void put (int key, Object value)
|
public synchronized void put (int key, Object value)
|
||||||
{
|
{
|
||||||
int index = Math.abs(key)%_buckets.length;
|
int index = Math.abs(key)%_buckets.length;
|
||||||
IntRecord rec = _buckets[index];
|
Record rec = _buckets[index];
|
||||||
|
|
||||||
// either we start a new chain
|
// either we start a new chain
|
||||||
if (rec == null) {
|
if (rec == null) {
|
||||||
_buckets[index] = new IntRecord(key, value);
|
_buckets[index] = new Record(key, value);
|
||||||
_size++; // we're bigger
|
_size++; // we're bigger
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// or we replace an element in an existing chain
|
// or we replace an element in an existing chain
|
||||||
IntRecord prev = rec;
|
Record prev = rec;
|
||||||
for (; rec != null; rec = rec.next) {
|
for (; rec != null; rec = rec.next) {
|
||||||
if (rec.key == key) {
|
if (rec.key == key) {
|
||||||
rec.value = value; // we're not bigger
|
rec.value = value; // we're not bigger
|
||||||
@@ -53,14 +53,14 @@ public class IntMap
|
|||||||
}
|
}
|
||||||
|
|
||||||
// or we append it to this chain
|
// or we append it to this chain
|
||||||
prev.next = new IntRecord(key, value);
|
prev.next = new Record(key, value);
|
||||||
_size++; // we're bigger
|
_size++; // we're bigger
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized Object get (int key)
|
public synchronized Object get (int key)
|
||||||
{
|
{
|
||||||
int index = Math.abs(key)%_buckets.length;
|
int index = Math.abs(key)%_buckets.length;
|
||||||
for (IntRecord rec = _buckets[index]; rec != null; rec = rec.next) {
|
for (Record rec = _buckets[index]; rec != null; rec = rec.next) {
|
||||||
if (rec.key == key) {
|
if (rec.key == key) {
|
||||||
return rec.value;
|
return rec.value;
|
||||||
}
|
}
|
||||||
@@ -76,7 +76,7 @@ public class IntMap
|
|||||||
public synchronized Object remove (int key)
|
public synchronized Object remove (int key)
|
||||||
{
|
{
|
||||||
int index = Math.abs(key)%_buckets.length;
|
int index = Math.abs(key)%_buckets.length;
|
||||||
IntRecord rec = _buckets[index];
|
Record rec = _buckets[index];
|
||||||
|
|
||||||
// if there's no chain, there's no object
|
// if there's no chain, there's no object
|
||||||
if (rec == null) {
|
if (rec == null) {
|
||||||
@@ -91,7 +91,7 @@ public class IntMap
|
|||||||
}
|
}
|
||||||
|
|
||||||
// or maybe it's an element further down the chain
|
// or maybe it's an element further down the chain
|
||||||
for (IntRecord prev = rec; rec != null; rec = rec.next) {
|
for (Record prev = rec; rec != null; rec = rec.next) {
|
||||||
if (rec.key == key) {
|
if (rec.key == key) {
|
||||||
prev.next = rec.next;
|
prev.next = rec.next;
|
||||||
_size--;
|
_size--;
|
||||||
@@ -115,12 +115,79 @@ public class IntMap
|
|||||||
|
|
||||||
public Enumeration keys ()
|
public Enumeration keys ()
|
||||||
{
|
{
|
||||||
return new IntMapEnumerator(_buckets, true);
|
return new Enumerator(_buckets, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Enumeration elements ()
|
public Enumeration elements ()
|
||||||
{
|
{
|
||||||
return new IntMapEnumerator(_buckets, false);
|
return new Enumerator(_buckets, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static class Record
|
||||||
|
{
|
||||||
|
public Record next;
|
||||||
|
public int key;
|
||||||
|
public Object value;
|
||||||
|
|
||||||
|
public Record (int key, Object value)
|
||||||
|
{
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Enumerator implements Enumeration
|
||||||
|
{
|
||||||
|
public Enumerator (Record[] buckets, boolean returnKeys)
|
||||||
|
{
|
||||||
|
_buckets = buckets;
|
||||||
|
_index = buckets.length;
|
||||||
|
_returnKeys = returnKeys;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasMoreElements ()
|
||||||
|
{
|
||||||
|
// if we're pointing to an entry, we've got more entries
|
||||||
|
if (_record != null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// search backward through the buckets looking for the next
|
||||||
|
// non-empty hash chain
|
||||||
|
while (_index-- > 0) {
|
||||||
|
if ((_record = _buckets[_index]) != null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// found no non-empty hash chains, we're done
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object nextElement ()
|
||||||
|
{
|
||||||
|
// if we're not pointing to an entry, search for the next
|
||||||
|
// non-empty hash chain
|
||||||
|
if (_record == null) {
|
||||||
|
while ((_index-- > 0) &&
|
||||||
|
((_record = _buckets[_index]) == null));
|
||||||
|
}
|
||||||
|
|
||||||
|
// if we found a record, return it's value and move our record
|
||||||
|
// reference to it's successor
|
||||||
|
if (_record != null) {
|
||||||
|
Record r = _record;
|
||||||
|
_record = r.next;
|
||||||
|
return _returnKeys ? new Integer(r.key) : r.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new NoSuchElementException("IntMapEnumerator");
|
||||||
|
}
|
||||||
|
|
||||||
|
private int _index;
|
||||||
|
private Record _record;
|
||||||
|
private Record[] _buckets;
|
||||||
|
private boolean _returnKeys;
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static void main (String[] args)
|
// public static void main (String[] args)
|
||||||
@@ -148,72 +215,6 @@ public class IntMap
|
|||||||
// System.out.println("");
|
// System.out.println("");
|
||||||
// }
|
// }
|
||||||
|
|
||||||
private IntRecord[] _buckets;
|
private Record[] _buckets;
|
||||||
private int _size;
|
private int _size;
|
||||||
}
|
}
|
||||||
|
|
||||||
class IntRecord
|
|
||||||
{
|
|
||||||
public IntRecord next;
|
|
||||||
public int key;
|
|
||||||
public Object value;
|
|
||||||
|
|
||||||
public IntRecord (int key, Object value)
|
|
||||||
{
|
|
||||||
this.key = key;
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class IntMapEnumerator implements Enumeration
|
|
||||||
{
|
|
||||||
public IntMapEnumerator (IntRecord[] buckets, boolean returnKeys)
|
|
||||||
{
|
|
||||||
_buckets = buckets;
|
|
||||||
_index = buckets.length;
|
|
||||||
_returnKeys = returnKeys;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasMoreElements ()
|
|
||||||
{
|
|
||||||
// if we're pointing to an entry, we've got more entries
|
|
||||||
if (_record != null) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// search backward through the buckets looking for the next
|
|
||||||
// non-empty hash chain
|
|
||||||
while (_index-- > 0) {
|
|
||||||
if ((_record = _buckets[_index]) != null) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// found no non-empty hash chains, we're done
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object nextElement ()
|
|
||||||
{
|
|
||||||
// if we're not pointing to an entry, search for the next
|
|
||||||
// non-empty hash chain
|
|
||||||
if (_record == null) {
|
|
||||||
while ((_index-- > 0) && ((_record = _buckets[_index]) == null));
|
|
||||||
}
|
|
||||||
|
|
||||||
// if we found a record, return it's value and move our record
|
|
||||||
// reference to it's successor
|
|
||||||
if (_record != null) {
|
|
||||||
IntRecord r = _record;
|
|
||||||
_record = r.next;
|
|
||||||
return _returnKeys ? new Integer(r.key) : r.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new NoSuchElementException("IntMapEnumerator");
|
|
||||||
}
|
|
||||||
|
|
||||||
private int _index;
|
|
||||||
private IntRecord _record;
|
|
||||||
private IntRecord[] _buckets;
|
|
||||||
private boolean _returnKeys;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// $Id: IntIntMap.java,v 1.1 2001/03/01 18:47:01 mdb Exp $
|
// $Id: IntIntMap.java,v 1.2 2001/03/02 05:31:13 mdb Exp $
|
||||||
|
|
||||||
package com.samskivert.util;
|
package com.samskivert.util;
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ public class IntIntMap
|
|||||||
|
|
||||||
public IntIntMap (int buckets)
|
public IntIntMap (int buckets)
|
||||||
{
|
{
|
||||||
_buckets = new IntRecord[buckets];
|
_buckets = new Record[buckets];
|
||||||
}
|
}
|
||||||
|
|
||||||
public IntIntMap ()
|
public IntIntMap ()
|
||||||
@@ -34,17 +34,17 @@ public class IntIntMap
|
|||||||
public synchronized void put (int key, int value)
|
public synchronized void put (int key, int value)
|
||||||
{
|
{
|
||||||
int index = Math.abs(key)%_buckets.length;
|
int index = Math.abs(key)%_buckets.length;
|
||||||
IntRecord rec = _buckets[index];
|
Record rec = _buckets[index];
|
||||||
|
|
||||||
// either we start a new chain
|
// either we start a new chain
|
||||||
if (rec == null) {
|
if (rec == null) {
|
||||||
_buckets[index] = new IntRecord(key, value);
|
_buckets[index] = new Record(key, value);
|
||||||
_size++; // we're bigger
|
_size++; // we're bigger
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// or we replace an element in an existing chain
|
// or we replace an element in an existing chain
|
||||||
IntRecord prev = rec;
|
Record prev = rec;
|
||||||
for (; rec != null; rec = rec.next) {
|
for (; rec != null; rec = rec.next) {
|
||||||
if (rec.key == key) {
|
if (rec.key == key) {
|
||||||
rec.value = value; // we're not bigger
|
rec.value = value; // we're not bigger
|
||||||
@@ -54,14 +54,14 @@ public class IntIntMap
|
|||||||
}
|
}
|
||||||
|
|
||||||
// or we append it to this chain
|
// or we append it to this chain
|
||||||
prev.next = new IntRecord(key, value);
|
prev.next = new Record(key, value);
|
||||||
_size++; // we're bigger
|
_size++; // we're bigger
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized int get (int key)
|
public synchronized int get (int key)
|
||||||
{
|
{
|
||||||
int index = Math.abs(key)%_buckets.length;
|
int index = Math.abs(key)%_buckets.length;
|
||||||
for (IntRecord rec = _buckets[index]; rec != null; rec = rec.next) {
|
for (Record rec = _buckets[index]; rec != null; rec = rec.next) {
|
||||||
if (rec.key == key) {
|
if (rec.key == key) {
|
||||||
return rec.value;
|
return rec.value;
|
||||||
}
|
}
|
||||||
@@ -72,7 +72,7 @@ public class IntIntMap
|
|||||||
public boolean contains (int key)
|
public boolean contains (int key)
|
||||||
{
|
{
|
||||||
int index = Math.abs(key)%_buckets.length;
|
int index = Math.abs(key)%_buckets.length;
|
||||||
for (IntRecord rec = _buckets[index]; rec != null; rec = rec.next) {
|
for (Record rec = _buckets[index]; rec != null; rec = rec.next) {
|
||||||
if (rec.key == key) {
|
if (rec.key == key) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -83,7 +83,7 @@ public class IntIntMap
|
|||||||
public synchronized int remove (int key)
|
public synchronized int remove (int key)
|
||||||
{
|
{
|
||||||
int index = Math.abs(key)%_buckets.length;
|
int index = Math.abs(key)%_buckets.length;
|
||||||
IntRecord rec = _buckets[index];
|
Record rec = _buckets[index];
|
||||||
|
|
||||||
// if there's no chain, there's no object
|
// if there's no chain, there's no object
|
||||||
if (rec == null) {
|
if (rec == null) {
|
||||||
@@ -98,7 +98,7 @@ public class IntIntMap
|
|||||||
}
|
}
|
||||||
|
|
||||||
// or maybe it's an element further down the chain
|
// or maybe it's an element further down the chain
|
||||||
for (IntRecord prev = rec; rec != null; rec = rec.next) {
|
for (Record prev = rec; rec != null; rec = rec.next) {
|
||||||
if (rec.key == key) {
|
if (rec.key == key) {
|
||||||
prev.next = rec.next;
|
prev.next = rec.next;
|
||||||
_size--;
|
_size--;
|
||||||
@@ -122,12 +122,79 @@ public class IntIntMap
|
|||||||
|
|
||||||
public Enumeration keys ()
|
public Enumeration keys ()
|
||||||
{
|
{
|
||||||
return new IntIntMapEnumerator(_buckets, true);
|
return new Enumerator(_buckets, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Enumeration elements ()
|
public Enumeration elements ()
|
||||||
{
|
{
|
||||||
return new IntIntMapEnumerator(_buckets, false);
|
return new Enumerator(_buckets, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
class Record
|
||||||
|
{
|
||||||
|
public Record next;
|
||||||
|
public int key;
|
||||||
|
public int value;
|
||||||
|
|
||||||
|
public Record (int key, int value)
|
||||||
|
{
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Enumerator implements Enumeration
|
||||||
|
{
|
||||||
|
public Enumerator (Record[] buckets, boolean returnKeys)
|
||||||
|
{
|
||||||
|
_buckets = buckets;
|
||||||
|
_index = buckets.length;
|
||||||
|
_returnKeys = returnKeys;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasMoreElements ()
|
||||||
|
{
|
||||||
|
// if we're pointing to an entry, we've got more entries
|
||||||
|
if (_record != null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// search backward through the buckets looking for the next
|
||||||
|
// non-empty hash chain
|
||||||
|
while (_index-- > 0) {
|
||||||
|
if ((_record = _buckets[_index]) != null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// found no non-empty hash chains, we're done
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object nextElement ()
|
||||||
|
{
|
||||||
|
// if we're not pointing to an entry, search for the next
|
||||||
|
// non-empty hash chain
|
||||||
|
if (_record == null) {
|
||||||
|
while ((_index-- > 0) &&
|
||||||
|
((_record = _buckets[_index]) == null));
|
||||||
|
}
|
||||||
|
|
||||||
|
// if we found a record, return it's value and move our record
|
||||||
|
// reference to it's successor
|
||||||
|
if (_record != null) {
|
||||||
|
Record r = _record;
|
||||||
|
_record = r.next;
|
||||||
|
return new Integer(_returnKeys ? r.key : r.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new NoSuchElementException("IntIntMapEnumerator");
|
||||||
|
}
|
||||||
|
|
||||||
|
private int _index;
|
||||||
|
private Record _record;
|
||||||
|
private Record[] _buckets;
|
||||||
|
private boolean _returnKeys;
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static void main (String[] args)
|
// public static void main (String[] args)
|
||||||
@@ -154,72 +221,6 @@ public class IntIntMap
|
|||||||
// System.out.println("");
|
// System.out.println("");
|
||||||
// }
|
// }
|
||||||
|
|
||||||
private IntRecord[] _buckets;
|
private Record[] _buckets;
|
||||||
private int _size;
|
private int _size;
|
||||||
}
|
}
|
||||||
|
|
||||||
class IntRecord
|
|
||||||
{
|
|
||||||
public IntRecord next;
|
|
||||||
public int key;
|
|
||||||
public int value;
|
|
||||||
|
|
||||||
public IntRecord (int key, int value)
|
|
||||||
{
|
|
||||||
this.key = key;
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class IntIntMapEnumerator implements Enumeration
|
|
||||||
{
|
|
||||||
public IntIntMapEnumerator (IntRecord[] buckets, boolean returnKeys)
|
|
||||||
{
|
|
||||||
_buckets = buckets;
|
|
||||||
_index = buckets.length;
|
|
||||||
_returnKeys = returnKeys;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasMoreElements ()
|
|
||||||
{
|
|
||||||
// if we're pointing to an entry, we've got more entries
|
|
||||||
if (_record != null) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// search backward through the buckets looking for the next
|
|
||||||
// non-empty hash chain
|
|
||||||
while (_index-- > 0) {
|
|
||||||
if ((_record = _buckets[_index]) != null) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// found no non-empty hash chains, we're done
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object nextElement ()
|
|
||||||
{
|
|
||||||
// if we're not pointing to an entry, search for the next
|
|
||||||
// non-empty hash chain
|
|
||||||
if (_record == null) {
|
|
||||||
while ((_index-- > 0) && ((_record = _buckets[_index]) == null));
|
|
||||||
}
|
|
||||||
|
|
||||||
// if we found a record, return it's value and move our record
|
|
||||||
// reference to it's successor
|
|
||||||
if (_record != null) {
|
|
||||||
IntRecord r = _record;
|
|
||||||
_record = r.next;
|
|
||||||
return new Integer(_returnKeys ? r.key : r.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new NoSuchElementException("IntIntMapEnumerator");
|
|
||||||
}
|
|
||||||
|
|
||||||
private int _index;
|
|
||||||
private IntRecord _record;
|
|
||||||
private IntRecord[] _buckets;
|
|
||||||
private boolean _returnKeys;
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user