[jboss-svn-commits] JBL Code SVN: r6366 - in labs/jbossrules/trunk/drools-core/src: main/java/org/drools/util test/java/org/drools/util
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Sep 22 11:17:03 EDT 2006
Author: mark.proctor at jboss.com
Date: 2006-09-22 11:16:56 -0400 (Fri, 22 Sep 2006)
New Revision: 6366
Modified:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/AbstractHashTable.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactHashSet.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FieldIndexHashTable.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/util/FieldIndexEntryTest.java
Log:
JBRULES-498 Optimised HashMap impl
-Moved FactEntry to child class
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/AbstractHashTable.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/AbstractHashTable.java 2006-09-22 15:02:13 UTC (rev 6365)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/AbstractHashTable.java 2006-09-22 15:16:56 UTC (rev 6366)
@@ -5,25 +5,30 @@
import java.io.Serializable;
-public abstract class AbstractHashTable implements Serializable {
- static final int MAX_CAPACITY = 1 << 30;
+import org.drools.common.InternalFactHandle;
+import org.drools.util.FactHashSet.FactEntry;
- protected int size;
- protected int threshold;
- protected float loadFactor;
+public abstract class AbstractHashTable
+ implements
+ Serializable {
+ static final int MAX_CAPACITY = 1 << 30;
- protected boolean checkExists;
+ protected int size;
+ protected int threshold;
+ protected float loadFactor;
+
+ protected boolean checkExists;
protected ObjectComparator comparator;
-
- protected Entry[] table;
+ protected Entry[] table;
+
public AbstractHashTable() {
this( 16,
0.75f );
}
public AbstractHashTable(int capacity,
- float loadFactor) {
+ float loadFactor) {
this.loadFactor = loadFactor;
this.threshold = (int) (capacity * loadFactor);
this.table = new Entry[capacity];
@@ -31,15 +36,14 @@
this.checkExists = false;
}
-
- public void setCheckExists(boolean checkExists) {
- this.checkExists = checkExists;
- }
+ public void setCheckExists(boolean checkExists) {
+ this.checkExists = checkExists;
+ }
- public void setComparator(ObjectComparator comparator) {
- this.comparator = comparator;
- }
-
+ public void setComparator(ObjectComparator comparator) {
+ this.comparator = comparator;
+ }
+
protected void resize(int newCapacity) {
Entry[] oldTable = this.table;
int oldCapacity = oldTable.length;
@@ -60,7 +64,8 @@
while ( entry != null ) {
next = entry.getNext();
- int index = indexOf( entry.hashCode(), newTable.length );
+ int index = indexOf( entry.hashCode(),
+ newTable.length );
entry.setNext( newTable[index] );
newTable[index] = entry;
@@ -72,72 +77,73 @@
this.threshold = (int) (newCapacity * this.loadFactor);
}
-// public void add(Entry entry) {
-// int index = indexOf( entry.hashCode(), table.length );
-//
-//
-// boolean exists = false;
-//
-// // scan the linked entries to see if it exists
-// if ( !checkExists ) {
-// Entry current = this.table[index];
-// int hashCode = entry.hashCode();
-// while ( current != null ) {
-// if ( hashCode == current.hashCode() && entry.equals( current ) ) {
-// exists = true;
-// }
-// }
-// }
-//
-// if( exists == false ) {
-// entry.setNext( this.table[index] );
-// this.table[index] = entry;
-//
-// if ( this.size++ >= this.threshold ) {
-// resize( 2 * this.table.length );
-// }
-// }
-//
-// }
-//
-// public Entry get(Entry entry) {
-// int index = indexOf( entry.hashCode(), table.length );
-// Entry current = this.table[index];
-// while ( current != null ) {
-// if ( entry.hashCode() == current.hashCode() && entry.equals( current ) ) {
-// return current;
-// }
-// current = current.getNext();
-// }
-// return null;
-// }
-//
-// public Entry remove(Entry entry) {
-// int index = indexOf( entry.hashCode(), table.length );
-// Entry previous = this.table[index];
-// Entry current = previous;
-// int hashCode = entry.hashCode();
-// while ( current != null ) {
-// Entry next = current.getNext();
-// if ( hashCode == current.hashCode() && entry.equals( current ) ) {
-// if( previous == current ) {
-// this.table[index] = next;
-// previous.setNext( next );
-// }
-// current.setNext( null );
-// this.size--;
-// return current;
-// }
-// previous = current;
-// current = next;
-// }
-// return current;
-// }
+ // public void add(Entry entry) {
+ // int index = indexOf( entry.hashCode(), table.length );
+ //
+ //
+ // boolean exists = false;
+ //
+ // // scan the linked entries to see if it exists
+ // if ( !checkExists ) {
+ // Entry current = this.table[index];
+ // int hashCode = entry.hashCode();
+ // while ( current != null ) {
+ // if ( hashCode == current.hashCode() && entry.equals( current ) ) {
+ // exists = true;
+ // }
+ // }
+ // }
+ //
+ // if( exists == false ) {
+ // entry.setNext( this.table[index] );
+ // this.table[index] = entry;
+ //
+ // if ( this.size++ >= this.threshold ) {
+ // resize( 2 * this.table.length );
+ // }
+ // }
+ //
+ // }
+ //
+ // public Entry get(Entry entry) {
+ // int index = indexOf( entry.hashCode(), table.length );
+ // Entry current = this.table[index];
+ // while ( current != null ) {
+ // if ( entry.hashCode() == current.hashCode() && entry.equals( current ) ) {
+ // return current;
+ // }
+ // current = current.getNext();
+ // }
+ // return null;
+ // }
+ //
+ // public Entry remove(Entry entry) {
+ // int index = indexOf( entry.hashCode(), table.length );
+ // Entry previous = this.table[index];
+ // Entry current = previous;
+ // int hashCode = entry.hashCode();
+ // while ( current != null ) {
+ // Entry next = current.getNext();
+ // if ( hashCode == current.hashCode() && entry.equals( current ) ) {
+ // if( previous == current ) {
+ // this.table[index] = next;
+ // previous.setNext( next );
+ // }
+ // current.setNext( null );
+ // this.size--;
+ // return current;
+ // }
+ // previous = current;
+ // current = next;
+ // }
+ // return current;
+ // }
public Entry getBucket(int hashCode) {
- return this.table[ indexOf( hashCode, table.length ) ];
+ return this.table[indexOf( hashCode,
+ table.length )];
}
-
+
public Entry[] getTable() {
return this.table;
}
@@ -145,48 +151,103 @@
public int size() {
return this.size;
}
-
- protected int indexOf(int hashCode, int dataSize) {
+
+ protected int indexOf(int hashCode,
+ int dataSize) {
int index = hashCode % dataSize;
if ( index < 0 ) {
- index = index * -1;
+ index = index * -1;
}
return index;
}
-
+
public interface ObjectComparator {
- public boolean equal(Object object1, Object object2);
+ public boolean equal(Object object1,
+ Object object2);
}
-
- public static class InstanceEquals implements ObjectComparator {
- public static ObjectComparator INSTANCE = new InstanceEquals();
-
- public static ObjectComparator getInstance() {
- return INSTANCE;
- }
-
- private InstanceEquals() {
-
- }
-
- public boolean equal(Object object1, Object object2) {
- return object1 == object2;
- }
+
+ public static class InstanceEquals
+ implements
+ ObjectComparator {
+ public static ObjectComparator INSTANCE = new InstanceEquals();
+
+ public static ObjectComparator getInstance() {
+ return INSTANCE;
+ }
+
+ private InstanceEquals() {
+
+ }
+
+ public boolean equal(Object object1,
+ Object object2) {
+ return object1 == object2;
+ }
}
-
- public static class EqualityEquals implements ObjectComparator {
- public static ObjectComparator INSTANCE = new EqualityEquals();
-
- public static ObjectComparator getInstance() {
- return INSTANCE;
- }
-
- private EqualityEquals() {
-
- }
-
- public boolean equal(Object object1, Object object2) {
- return object1.equals( object2 );
- }
- }
+
+ public static class EqualityEquals
+ implements
+ ObjectComparator {
+ public static ObjectComparator INSTANCE = new EqualityEquals();
+
+ public static ObjectComparator getInstance() {
+ return INSTANCE;
+ }
+
+ private EqualityEquals() {
+
+ }
+
+ public boolean equal(Object object1,
+ Object object2) {
+ return object1.equals( object2 );
+ }
+ }
+
+ public static class FactEntry
+ implements
+ Entry {
+ public InternalFactHandle handle;
+
+ public int hashCode;
+
+ public Entry next;
+
+ public FactEntry(InternalFactHandle handle) {
+ this.handle = handle;
+ this.hashCode = handle.hashCode();
+ }
+
+ public FactEntry(InternalFactHandle handle,
+ int hashCode) {
+ this.handle = handle;
+ this.hashCode = hashCode;
+ }
+
+ public InternalFactHandle getFactHandle() {
+ return handle;
+ }
+
+ public Entry getNext() {
+ return this.next;
+ }
+
+ public void setNext(Entry next) {
+ this.next = next;
+ }
+
+ public int hashCode() {
+ return this.hashCode;
+ }
+
+ public boolean equals(Object object) {
+ if ( object == this ) {
+ return true;
+ }
+
+ // assumes we never have null or wrong class
+ FactEntry other = (FactEntry) object;
+ return this.handle.equals( other.handle );
+ }
+ }
}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactHashSet.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactHashSet.java 2006-09-22 15:02:13 UTC (rev 6365)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactHashSet.java 2006-09-22 15:16:56 UTC (rev 6366)
@@ -35,7 +35,7 @@
// We aren't checking the key exists, or it didn't find the key
FactEntry entry = new FactEntry( handle,
hashCode );
- entry.next = this.table[index];
+ entry.next = (FactEntry) this.table[index];
this.table[index] = entry;
if ( this.size++ >= this.threshold ) {
@@ -102,46 +102,4 @@
int dataSize) {
return hashCode & (dataSize - 1);
}
-
- public static class FactEntry
- implements
- Entry {
- private InternalFactHandle handle;
-
- private int hashCode;
-
- private Entry next;
-
- public FactEntry(InternalFactHandle handle,
- int hashCode) {
- this.handle = handle;
- this.hashCode = hashCode;
- }
-
- public InternalFactHandle getFactHandle() {
- return handle;
- }
-
- public Entry getNext() {
- return this.next;
- }
-
- public void setNext(Entry next) {
- this.next = next;
- }
-
- public int hashCode() {
- return this.hashCode;
- }
-
- public boolean equals(Object object) {
- if ( object == this ) {
- return true;
- }
-
- // assumes we never have null or wrong class
- FactEntry other = (FactEntry) object;
- return this.handle.equals( other.handle );
- }
- }
}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FieldIndexHashTable.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FieldIndexHashTable.java 2006-09-22 15:02:13 UTC (rev 6365)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FieldIndexHashTable.java 2006-09-22 15:16:56 UTC (rev 6366)
@@ -121,44 +121,6 @@
return entry;
}
- private static class FactEntry
- implements
- Entry {
- private InternalFactHandle handle;
-
- private Entry next;
-
- public FactEntry(InternalFactHandle handle) {
- this.handle = handle;
- }
-
- public InternalFactHandle getFactHandle() {
- return handle;
- }
-
- public Entry getNext() {
- return this.next;
- }
-
- public void setNext(Entry next) {
- this.next = next;
- }
-
- public int hashCode() {
- return this.handle.hashCode();
- }
-
- public boolean equals(Object object) {
- if ( object == this ) {
- return true;
- }
-
- // assumes we never have null or wrong class
- FactEntry other = (FactEntry) object;
- return this.handle.equals( other.handle );
- }
- }
-
public static class FieldIndexEntry
implements
Entry {
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/util/FieldIndexEntryTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/util/FieldIndexEntryTest.java 2006-09-22 15:02:13 UTC (rev 6365)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/util/FieldIndexEntryTest.java 2006-09-22 15:16:56 UTC (rev 6366)
@@ -4,6 +4,7 @@
import org.drools.base.ClassFieldExtractor;
import org.drools.common.DefaultFactHandle;
import org.drools.common.InternalFactHandle;
+import org.drools.util.AbstractHashTable.FactEntry;
import org.drools.util.FieldIndexHashTable.FieldIndexEntry;
import junit.framework.TestCase;
More information about the jboss-svn-commits
mailing list