From b2de8370a1b5377424a97547efde607d32804867 Mon Sep 17 00:00:00 2001 From: ray Date: Thu, 11 Nov 2004 22:14:09 +0000 Subject: [PATCH] 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 --- .../java/com/samskivert/util/IntIntMap.java | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/IntIntMap.java b/projects/samskivert/src/java/com/samskivert/util/IntIntMap.java index 45f391bf..e0b186f3 100644 --- a/projects/samskivert/src/java/com/samskivert/util/IntIntMap.java +++ b/projects/samskivert/src/java/com/samskivert/util/IntIntMap.java @@ -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 ()