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:
@@ -109,31 +109,23 @@ public class IntIntMap
|
|||||||
protected int removeImpl (int key)
|
protected int removeImpl (int key)
|
||||||
{
|
{
|
||||||
int index = Math.abs(key)%_buckets.length;
|
int index = Math.abs(key)%_buckets.length;
|
||||||
Record rec = _buckets[index];
|
Record prev = null;
|
||||||
|
|
||||||
// if there's no chain, there's no object
|
// go through the chain looking for a match
|
||||||
if (rec == null) {
|
for (Record rec = _buckets[index]; rec != null; rec = rec.next) {
|
||||||
return -1;
|
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
|
return -1; // not found
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clear ()
|
public void clear ()
|
||||||
|
|||||||
Reference in New Issue
Block a user