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,59 +115,30 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static void main (String[] args)
|
protected static class Record
|
||||||
// {
|
|
||||||
// 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;
|
public Record next;
|
||||||
public int key;
|
public int key;
|
||||||
public Object value;
|
public Object value;
|
||||||
|
|
||||||
public IntRecord (int key, Object value)
|
public Record (int key, Object value)
|
||||||
{
|
{
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class IntMapEnumerator implements Enumeration
|
class Enumerator implements Enumeration
|
||||||
{
|
{
|
||||||
public IntMapEnumerator (IntRecord[] buckets, boolean returnKeys)
|
public Enumerator (Record[] buckets, boolean returnKeys)
|
||||||
{
|
{
|
||||||
_buckets = buckets;
|
_buckets = buckets;
|
||||||
_index = buckets.length;
|
_index = buckets.length;
|
||||||
@@ -198,13 +169,14 @@ class IntMapEnumerator implements Enumeration
|
|||||||
// if we're not pointing to an entry, search for the next
|
// if we're not pointing to an entry, search for the next
|
||||||
// non-empty hash chain
|
// non-empty hash chain
|
||||||
if (_record == null) {
|
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
|
// if we found a record, return it's value and move our record
|
||||||
// reference to it's successor
|
// reference to it's successor
|
||||||
if (_record != null) {
|
if (_record != null) {
|
||||||
IntRecord r = _record;
|
Record r = _record;
|
||||||
_record = r.next;
|
_record = r.next;
|
||||||
return _returnKeys ? new Integer(r.key) : r.value;
|
return _returnKeys ? new Integer(r.key) : r.value;
|
||||||
}
|
}
|
||||||
@@ -213,7 +185,36 @@ class IntMapEnumerator implements Enumeration
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int _index;
|
private int _index;
|
||||||
private IntRecord _record;
|
private Record _record;
|
||||||
private IntRecord[] _buckets;
|
private Record[] _buckets;
|
||||||
private boolean _returnKeys;
|
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;
|
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,58 +122,30 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static void main (String[] args)
|
class Record
|
||||||
// {
|
|
||||||
// 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;
|
public Record next;
|
||||||
public int key;
|
public int key;
|
||||||
public int value;
|
public int value;
|
||||||
|
|
||||||
public IntRecord (int key, int value)
|
public Record (int key, int value)
|
||||||
{
|
{
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class IntIntMapEnumerator implements Enumeration
|
class Enumerator implements Enumeration
|
||||||
{
|
{
|
||||||
public IntIntMapEnumerator (IntRecord[] buckets, boolean returnKeys)
|
public Enumerator (Record[] buckets, boolean returnKeys)
|
||||||
{
|
{
|
||||||
_buckets = buckets;
|
_buckets = buckets;
|
||||||
_index = buckets.length;
|
_index = buckets.length;
|
||||||
@@ -204,13 +176,14 @@ class IntIntMapEnumerator implements Enumeration
|
|||||||
// if we're not pointing to an entry, search for the next
|
// if we're not pointing to an entry, search for the next
|
||||||
// non-empty hash chain
|
// non-empty hash chain
|
||||||
if (_record == null) {
|
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
|
// if we found a record, return it's value and move our record
|
||||||
// reference to it's successor
|
// reference to it's successor
|
||||||
if (_record != null) {
|
if (_record != null) {
|
||||||
IntRecord r = _record;
|
Record r = _record;
|
||||||
_record = r.next;
|
_record = r.next;
|
||||||
return new Integer(_returnKeys ? r.key : r.value);
|
return new Integer(_returnKeys ? r.key : r.value);
|
||||||
}
|
}
|
||||||
@@ -219,7 +192,35 @@ class IntIntMapEnumerator implements Enumeration
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int _index;
|
private int _index;
|
||||||
private IntRecord _record;
|
private Record _record;
|
||||||
private IntRecord[] _buckets;
|
private Record[] _buckets;
|
||||||
private boolean _returnKeys;
|
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