Browse Source

ObjectIdSubclassMap: Use & rather than % for hashing

Bitwise and is faster than integer modulus operations, and since
the table size is always a power of 2, this is simple to use for
index operation.

Change-Id: I83d01e5c74fd9e910c633a98ea6f90b59092ba29
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
tags/v0.12.1
Shawn O. Pearce 13 years ago
parent
commit
47c2a3a98d
1 changed files with 13 additions and 4 deletions
  1. 13
    4
      org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectIdSubclassMap.java

+ 13
- 4
org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectIdSubclassMap.java View File

* type of subclass of ObjectId that will be stored in the map. * type of subclass of ObjectId that will be stored in the map.
*/ */
public class ObjectIdSubclassMap<V extends ObjectId> implements Iterable<V> { public class ObjectIdSubclassMap<V extends ObjectId> implements Iterable<V> {
private static final int INITIAL_TABLE_SIZE = 32;

private int size; private int size;


private int mask;

private V[] table; private V[] table;


/** Create an empty map. */ /** Create an empty map. */
public ObjectIdSubclassMap() { public ObjectIdSubclassMap() {
table = createArray(32);
initTable(INITIAL_TABLE_SIZE);
} }


/** Remove all entries from this map. */ /** Remove all entries from this map. */
public void clear() { public void clear() {
size = 0; size = 0;
table = createArray(32);
initTable(INITIAL_TABLE_SIZE);
} }


/** /**
} }


private final int index(final AnyObjectId id) { private final int index(final AnyObjectId id) {
return (id.w1 >>> 1) % table.length;
return id.w1 & mask;
} }


private void insert(final V newValue) { private void insert(final V newValue) {
final V[] oldTable = table; final V[] oldTable = table;
final int oldSize = table.length; final int oldSize = table.length;


table = createArray(2 * oldSize);
initTable(oldSize << 1);
for (int i = 0; i < oldSize; i++) { for (int i = 0; i < oldSize; i++) {
final V obj = oldTable[i]; final V obj = oldTable[i];
if (obj != null) if (obj != null)
} }
} }


private void initTable(int sz) {
mask = sz - 1;
table = createArray(sz);
}

@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private final V[] createArray(final int sz) { private final V[] createArray(final int sz) {
return (V[]) new ObjectId[sz]; return (V[]) new ObjectId[sz];

Loading…
Cancel
Save