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;
|
||||
|
||||
@@ -17,7 +17,7 @@ public class IntMap
|
||||
|
||||
public IntMap (int buckets)
|
||||
{
|
||||
_buckets = new IntRecord[buckets];
|
||||
_buckets = new Record[buckets];
|
||||
}
|
||||
|
||||
public IntMap ()
|
||||
@@ -33,17 +33,17 @@ public class IntMap
|
||||
public synchronized void put (int key, Object value)
|
||||
{
|
||||
int index = Math.abs(key)%_buckets.length;
|
||||
IntRecord rec = _buckets[index];
|
||||
Record rec = _buckets[index];
|
||||
|
||||
// either we start a new chain
|
||||
if (rec == null) {
|
||||
_buckets[index] = new IntRecord(key, value);
|
||||
_buckets[index] = new Record(key, value);
|
||||
_size++; // we're bigger
|
||||
return;
|
||||
}
|
||||
|
||||
// or we replace an element in an existing chain
|
||||
IntRecord prev = rec;
|
||||
Record prev = rec;
|
||||
for (; rec != null; rec = rec.next) {
|
||||
if (rec.key == key) {
|
||||
rec.value = value; // we're not bigger
|
||||
@@ -53,14 +53,14 @@ public class IntMap
|
||||
}
|
||||
|
||||
// or we append it to this chain
|
||||
prev.next = new IntRecord(key, value);
|
||||
prev.next = new Record(key, value);
|
||||
_size++; // we're bigger
|
||||
}
|
||||
|
||||
public synchronized Object get (int key)
|
||||
{
|
||||
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) {
|
||||
return rec.value;
|
||||
}
|
||||
@@ -76,7 +76,7 @@ public class IntMap
|
||||
public synchronized Object remove (int key)
|
||||
{
|
||||
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 (rec == null) {
|
||||
@@ -91,7 +91,7 @@ public class IntMap
|
||||
}
|
||||
|
||||
// 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) {
|
||||
prev.next = rec.next;
|
||||
_size--;
|
||||
@@ -115,59 +115,30 @@ public class IntMap
|
||||
|
||||
public Enumeration keys ()
|
||||
{
|
||||
return new IntMapEnumerator(_buckets, true);
|
||||
return new Enumerator(_buckets, true);
|
||||
}
|
||||
|
||||
public Enumeration elements ()
|
||||
{
|
||||
return new IntMapEnumerator(_buckets, false);
|
||||
return new Enumerator(_buckets, false);
|
||||
}
|
||||
|
||||
// public static void main (String[] args)
|
||||
// {
|
||||
// IntMap table = new IntMap();
|
||||
|
||||
// System.out.print("Inserting: ");
|
||||
// for (int i = 10; i < 100; i++) {
|
||||
// Integer value = new Integer(i);
|
||||
// table.put(i, value);
|
||||
// System.out.print("(" + i + "," + value + ")");
|
||||
// }
|
||||
// System.out.println("");
|
||||
|
||||
// System.out.print("Looking up: ");
|
||||
// for (int i = 10; i < 100; i++) {
|
||||
// System.out.print("(" + i + "," + table.get(i) + ")");
|
||||
// }
|
||||
// System.out.println("");
|
||||
|
||||
// System.out.print("Removing: ");
|
||||
// for (int i = 10; i < 100; i++) {
|
||||
// System.out.print("(" + i + "," + table.remove(i) + ")");
|
||||
// }
|
||||
// System.out.println("");
|
||||
// }
|
||||
|
||||
private IntRecord[] _buckets;
|
||||
private int _size;
|
||||
}
|
||||
|
||||
class IntRecord
|
||||
{
|
||||
public IntRecord next;
|
||||
protected static class Record
|
||||
{
|
||||
public Record next;
|
||||
public int key;
|
||||
public Object value;
|
||||
|
||||
public IntRecord (int key, Object value)
|
||||
public Record (int key, Object value)
|
||||
{
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class IntMapEnumerator implements Enumeration
|
||||
{
|
||||
public IntMapEnumerator (IntRecord[] buckets, boolean returnKeys)
|
||||
class Enumerator implements Enumeration
|
||||
{
|
||||
public Enumerator (Record[] buckets, boolean returnKeys)
|
||||
{
|
||||
_buckets = buckets;
|
||||
_index = buckets.length;
|
||||
@@ -198,13 +169,14 @@ class IntMapEnumerator implements Enumeration
|
||||
// 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));
|
||||
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 = _record;
|
||||
_record = r.next;
|
||||
return _returnKeys ? new Integer(r.key) : r.value;
|
||||
}
|
||||
@@ -213,7 +185,36 @@ class IntMapEnumerator implements Enumeration
|
||||
}
|
||||
|
||||
private int _index;
|
||||
private IntRecord _record;
|
||||
private IntRecord[] _buckets;
|
||||
private Record _record;
|
||||
private Record[] _buckets;
|
||||
private boolean _returnKeys;
|
||||
}
|
||||
|
||||
// public static void main (String[] args)
|
||||
// {
|
||||
// IntMap table = new IntMap();
|
||||
|
||||
// System.out.print("Inserting: ");
|
||||
// for (int i = 10; i < 100; i++) {
|
||||
// Integer value = new Integer(i);
|
||||
// table.put(i, value);
|
||||
// System.out.print("(" + i + "," + value + ")");
|
||||
// }
|
||||
// System.out.println("");
|
||||
|
||||
// System.out.print("Looking up: ");
|
||||
// for (int i = 10; i < 100; i++) {
|
||||
// System.out.print("(" + i + "," + table.get(i) + ")");
|
||||
// }
|
||||
// System.out.println("");
|
||||
|
||||
// System.out.print("Removing: ");
|
||||
// for (int i = 10; i < 100; i++) {
|
||||
// System.out.print("(" + i + "," + table.remove(i) + ")");
|
||||
// }
|
||||
// System.out.println("");
|
||||
// }
|
||||
|
||||
private Record[] _buckets;
|
||||
private int _size;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -18,7 +18,7 @@ public class IntIntMap
|
||||
|
||||
public IntIntMap (int buckets)
|
||||
{
|
||||
_buckets = new IntRecord[buckets];
|
||||
_buckets = new Record[buckets];
|
||||
}
|
||||
|
||||
public IntIntMap ()
|
||||
@@ -34,17 +34,17 @@ public class IntIntMap
|
||||
public synchronized void put (int key, int value)
|
||||
{
|
||||
int index = Math.abs(key)%_buckets.length;
|
||||
IntRecord rec = _buckets[index];
|
||||
Record rec = _buckets[index];
|
||||
|
||||
// either we start a new chain
|
||||
if (rec == null) {
|
||||
_buckets[index] = new IntRecord(key, value);
|
||||
_buckets[index] = new Record(key, value);
|
||||
_size++; // we're bigger
|
||||
return;
|
||||
}
|
||||
|
||||
// or we replace an element in an existing chain
|
||||
IntRecord prev = rec;
|
||||
Record prev = rec;
|
||||
for (; rec != null; rec = rec.next) {
|
||||
if (rec.key == key) {
|
||||
rec.value = value; // we're not bigger
|
||||
@@ -54,14 +54,14 @@ public class IntIntMap
|
||||
}
|
||||
|
||||
// or we append it to this chain
|
||||
prev.next = new IntRecord(key, value);
|
||||
prev.next = new Record(key, value);
|
||||
_size++; // we're bigger
|
||||
}
|
||||
|
||||
public synchronized int get (int key)
|
||||
{
|
||||
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) {
|
||||
return rec.value;
|
||||
}
|
||||
@@ -72,7 +72,7 @@ public class IntIntMap
|
||||
public boolean contains (int key)
|
||||
{
|
||||
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) {
|
||||
return true;
|
||||
}
|
||||
@@ -83,7 +83,7 @@ public class IntIntMap
|
||||
public synchronized int remove (int key)
|
||||
{
|
||||
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 (rec == null) {
|
||||
@@ -98,7 +98,7 @@ public class IntIntMap
|
||||
}
|
||||
|
||||
// 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) {
|
||||
prev.next = rec.next;
|
||||
_size--;
|
||||
@@ -122,58 +122,30 @@ public class IntIntMap
|
||||
|
||||
public Enumeration keys ()
|
||||
{
|
||||
return new IntIntMapEnumerator(_buckets, true);
|
||||
return new Enumerator(_buckets, true);
|
||||
}
|
||||
|
||||
public Enumeration elements ()
|
||||
{
|
||||
return new IntIntMapEnumerator(_buckets, false);
|
||||
return new Enumerator(_buckets, false);
|
||||
}
|
||||
|
||||
// public static void main (String[] args)
|
||||
// {
|
||||
// IntIntMap table = new IntIntMap();
|
||||
|
||||
// System.out.print("Inserting: ");
|
||||
// for (int i = 10; i < 100; i++) {
|
||||
// table.put(i, i);
|
||||
// System.out.print("(" + i + "," + i + ")");
|
||||
// }
|
||||
// System.out.println("");
|
||||
|
||||
// System.out.print("Looking up: ");
|
||||
// for (int i = 10; i < 100; i++) {
|
||||
// System.out.print("(" + i + "," + table.get(i) + ")");
|
||||
// }
|
||||
// System.out.println("");
|
||||
|
||||
// System.out.print("Removing: ");
|
||||
// for (int i = 10; i < 100; i++) {
|
||||
// System.out.print("(" + i + "," + table.remove(i) + ")");
|
||||
// }
|
||||
// System.out.println("");
|
||||
// }
|
||||
|
||||
private IntRecord[] _buckets;
|
||||
private int _size;
|
||||
}
|
||||
|
||||
class IntRecord
|
||||
{
|
||||
public IntRecord next;
|
||||
class Record
|
||||
{
|
||||
public Record next;
|
||||
public int key;
|
||||
public int value;
|
||||
|
||||
public IntRecord (int key, int value)
|
||||
public Record (int key, int value)
|
||||
{
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class IntIntMapEnumerator implements Enumeration
|
||||
{
|
||||
public IntIntMapEnumerator (IntRecord[] buckets, boolean returnKeys)
|
||||
class Enumerator implements Enumeration
|
||||
{
|
||||
public Enumerator (Record[] buckets, boolean returnKeys)
|
||||
{
|
||||
_buckets = buckets;
|
||||
_index = buckets.length;
|
||||
@@ -204,13 +176,14 @@ class IntIntMapEnumerator implements Enumeration
|
||||
// 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));
|
||||
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 = _record;
|
||||
_record = r.next;
|
||||
return new Integer(_returnKeys ? r.key : r.value);
|
||||
}
|
||||
@@ -219,7 +192,35 @@ class IntIntMapEnumerator implements Enumeration
|
||||
}
|
||||
|
||||
private int _index;
|
||||
private IntRecord _record;
|
||||
private IntRecord[] _buckets;
|
||||
private Record _record;
|
||||
private Record[] _buckets;
|
||||
private boolean _returnKeys;
|
||||
}
|
||||
|
||||
// public static void main (String[] args)
|
||||
// {
|
||||
// IntIntMap table = new IntIntMap();
|
||||
|
||||
// System.out.print("Inserting: ");
|
||||
// for (int i = 10; i < 100; i++) {
|
||||
// table.put(i, i);
|
||||
// System.out.print("(" + i + "," + i + ")");
|
||||
// }
|
||||
// System.out.println("");
|
||||
|
||||
// System.out.print("Looking up: ");
|
||||
// for (int i = 10; i < 100; i++) {
|
||||
// System.out.print("(" + i + "," + table.get(i) + ")");
|
||||
// }
|
||||
// System.out.println("");
|
||||
|
||||
// System.out.print("Removing: ");
|
||||
// for (int i = 10; i < 100; i++) {
|
||||
// System.out.print("(" + i + "," + table.remove(i) + ")");
|
||||
// }
|
||||
// System.out.println("");
|
||||
// }
|
||||
|
||||
private Record[] _buckets;
|
||||
private int _size;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user