While poking around I noticed that the remove implementation had a minor

inefficiency in that if the first record in a chain did not match it ended
up checking it once more prior to moving on to the next record in the chain.
Cleaned up to be similar to HashIntMap's remove implementation.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@1527 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray
2004-11-11 22:14:09 +00:00
parent 1da24589d8
commit b2de8370a1
@@ -109,31 +109,23 @@ public class IntIntMap
protected int removeImpl (int key)
{
int index = Math.abs(key)%_buckets.length;
Record rec = _buckets[index];
Record prev = null;
// if there's no chain, there's no object
if (rec == null) {
return -1;
}
// go through the chain looking for a match
for (Record rec = _buckets[index]; rec != null; rec = rec.next) {
if (rec.key == key) {
if (prev == null) {
_buckets[index] = rec.next;
} else {
prev.next = rec.next;
}
_size--;
return rec.value;
}
prev = rec;
}
// maybe it's the first one in this chain
if (rec.key == key) {
_buckets[index] = rec.next;
_size--;
return rec.value;
}
// or maybe it's an element further down the chain
for (Record prev = rec; rec != null; rec = rec.next) {
if (rec.key == key) {
prev.next = rec.next;
_size--;
return rec.value;
}
prev = rec;
}
return -1;
return -1; // not found
}
public void clear ()