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
This commit is contained in:
ray
2005-05-09 21:01:36 +00:00
parent f46967901e
commit c4a1a637c0
@@ -80,13 +80,8 @@ public class IntIntMap
public int get (int key) public int get (int key)
{ {
int index = Math.abs(key)%_buckets.length; Record rec = locateRecord(key);
for (Record rec = _buckets[index]; rec != null; rec = rec.next) { return (rec == null) ? -1 : rec.value;
if (rec.key == key) {
return rec.value;
}
}
return -1;
} }
/** /**
@@ -96,22 +91,31 @@ public class IntIntMap
*/ */
public void increment (int key, int amount) public void increment (int key, int amount)
{ {
if (contains(key)) { Record rec = locateRecord(key);
put(key, get(key) + amount); if (rec == null) {
} else {
put(key, amount); put(key, amount);
} else {
rec.value += amount;
} }
} }
public boolean contains (int key) 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; int index = Math.abs(key)%_buckets.length;
for (Record 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 rec;
} }
} }
return false; return null;
} }
public int remove (int key) public int remove (int key)