From c4a1a637c0e99993f3cf083c96f63d27caa55ba0 Mon Sep 17 00:00:00 2001 From: ray Date: Mon, 9 May 2005 21:01:36 +0000 Subject: [PATCH] Reimplemented increment() so that it hashes once for already-contained keys rather than three times. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1644 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/IntIntMap.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/IntIntMap.java b/projects/samskivert/src/java/com/samskivert/util/IntIntMap.java index 805afa36..86e046c6 100644 --- a/projects/samskivert/src/java/com/samskivert/util/IntIntMap.java +++ b/projects/samskivert/src/java/com/samskivert/util/IntIntMap.java @@ -80,13 +80,8 @@ public class IntIntMap public int get (int key) { - int index = Math.abs(key)%_buckets.length; - for (Record rec = _buckets[index]; rec != null; rec = rec.next) { - if (rec.key == key) { - return rec.value; - } - } - return -1; + Record rec = locateRecord(key); + return (rec == null) ? -1 : rec.value; } /** @@ -96,22 +91,31 @@ public class IntIntMap */ public void increment (int key, int amount) { - if (contains(key)) { - put(key, get(key) + amount); - } else { + Record rec = locateRecord(key); + if (rec == null) { put(key, amount); + } else { + rec.value += amount; } } public boolean contains (int key) + { + return (null != locateRecord(key)); + } + + /** + * Internal method to locate the record for the specified key. + */ + protected Record locateRecord (int key) { int index = Math.abs(key)%_buckets.length; for (Record rec = _buckets[index]; rec != null; rec = rec.next) { if (rec.key == key) { - return true; + return rec; } } - return false; + return null; } public int remove (int key)