[jboss-svn-commits] JBL Code SVN: r6352 - labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Sep 22 07:58:49 EDT 2006


Author: mark.proctor at jboss.com
Date: 2006-09-22 07:58:46 -0400 (Fri, 22 Sep 2006)
New Revision: 6352

Added:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactHashSet.java
Removed:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactConstraintMap.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactEntry.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactIndexKey.java
Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FieldIndexHashTable.java
Log:
JBRULES-498 Optimised HashMap impl
-Added FactHashSet
-Fixes to FieldIndexHashTable

Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactConstraintMap.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactConstraintMap.java	2006-09-22 10:43:28 UTC (rev 6351)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactConstraintMap.java	2006-09-22 11:58:46 UTC (rev 6352)
@@ -1,147 +0,0 @@
-package org.drools.util;
-
-import org.drools.common.InternalFactHandle;
-import org.drools.spi.FieldExtractor;
-
-/***********************************/
-    public class FactConstraintMap {
-        static final int       MAX_CAPACITY = 1 << 30;
-
-        private int            size;
-        private int            threshold;
-        private float          loadFactor;
-
-        private Entry[]        table;
-
-        private int            fieldIndex;
-        private FieldExtractor extractor;
-
-        final int              PRIME        = 31;
-
-        private final int      startResult;
-
-        public FactConstraintMap(int fieldIndex,
-                                 FieldExtractor extractor) {
-            this( 16,
-                  0.75f,
-                  fieldIndex,
-                  extractor );
-        }
-
-        public FactConstraintMap(int capacity,
-                                 float loadFactor,
-                                 int fieldIndex,
-                                 FieldExtractor extractor) {
-            this.loadFactor = loadFactor;
-            this.threshold = (int) (capacity * loadFactor);
-            this.table = new TupleEntry[capacity];
-            this.fieldIndex = fieldIndex;
-            this.extractor = extractor;
-
-            this.startResult = PRIME + this.fieldIndex;
-        }
-
-        public void resize(int newCapacity) {
-            Entry[] oldTable = this.table;
-            int oldCapacity = oldTable.length;
-            if ( oldCapacity == MAX_CAPACITY ) {
-                this.threshold = Integer.MAX_VALUE;
-                return;
-            }
-
-            Entry[] newTable = new Entry[newCapacity];
-
-            for ( int i = 0; i < this.table.length; i++ ) {
-                Entry entry = this.table[i];
-                if ( entry == null ) {
-                    continue;
-                }
-                this.table[i] = null;
-                Entry next = null;
-                while ( entry != null ) {
-                    next = entry.getNext();
-
-                    int index = entry.hashCode() % newTable.length;
-                    entry.setNext( newTable[index] );
-                    newTable[index] = entry;
-
-                    entry = next;
-                }
-            }
-
-            this.table = newTable;
-            this.threshold = (int) (newCapacity * this.loadFactor);
-        }
-
-        public void add(InternalFactHandle handle) {
-            Object value = this.extractor.getValue( handle.getObject() );
-            int hashCode = PRIME * startResult + ((value == null) ? 0 : value.hashCode());
-
-//            FactEntry factEntry = new FactEntry( handle );
-//            getOrCreate( hashCode,
-//                         this.fieldIndex,
-//                         value ).add( factEntry );
-        }
-
-        public FieldIndexEntry getOrCreate(int hashCode,
-                                          int fieldIndex,
-                                          Object value) {
-            int index = hashCode % this.table.length;
-            FieldIndexEntry entry = (FieldIndexEntry) this.table[index];
-
-            while ( entry != null ) {
-                if ( hashCode == entry.hashCode() && value.equals( entry.getValue() ) ) {
-                    return entry;
-                }
-                entry = (FieldIndexEntry) entry.getNext();
-            }
-
-            if ( entry == null ) {
-                entry = new FieldIndexEntry( this.extractor,
-                                            fieldIndex,
-                                            hashCode );
-                entry.setNext( this.table[index] );
-                this.table[index] = entry;
-
-                if ( this.size++ >= this.threshold ) {
-                    resize( 2 * this.table.length );
-                }
-            }
-            return entry;
-        }
-
-        public Entry get(Entry entry) {
-            int index = entry.hashCode() % this.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(TupleEntry tuple) {
-            int index = tuple.hashCode() % this.table.length;
-            Entry current = this.table[index];
-            Entry previous = current;
-            if ( current != null ) {
-                if ( tuple.hashCode() == current.hashCode() && tuple.equals( current ) ) {
-                    previous.setNext( current.getNext() );
-                    return current;
-                }
-                previous = current;
-            }
-            return null;
-        }
-
-        public Entry getBucket(int hashCode) {
-            int index = hashCode % this.table.length;
-            return this.table[index];
-        }
-
-        public int size() {
-            return this.size;
-        }
-    }
\ No newline at end of file

Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactEntry.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactEntry.java	2006-09-22 10:43:28 UTC (rev 6351)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactEntry.java	2006-09-22 11:58:46 UTC (rev 6352)
@@ -1,33 +0,0 @@
-/**
- * 
- */
-package org.drools.util;
-
-import org.drools.common.InternalFactHandle;
-
-public class FactEntry extends BaseEntry {
-    private InternalFactHandle handle;
-
-    public FactEntry(InternalFactHandle handle) {
-        this.handle = handle;
-    }
-
-    public String toString() {
-        return this.handle.getObject().toString();
-    }
-
-    public InternalFactHandle getFactHandle() {
-        return this.handle;
-    }
-
-    public int hashCode() {
-        return this.handle.hashCode();
-    }
-
-    public boolean equals(Object object) {
-        // Inside the rete network we know that it will always be a TupleEntry
-        // We know it will never be null
-        FactEntry other = (FactEntry) object;
-        return (this.handle.getId() == other.handle.getId());
-    }
-}
\ No newline at end of file

Added: 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 10:43:28 UTC (rev 6351)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactHashSet.java	2006-09-22 11:58:46 UTC (rev 6352)
@@ -0,0 +1,151 @@
+/**
+ * 
+ */
+package org.drools.util;
+
+import org.drools.common.InternalFactHandle;
+
+public class FactHashSet extends AbstractHashTable {
+    public FactHashSet() {
+        this( 16,
+              0.75f );
+    }
+
+    public FactHashSet(int capacity,
+                         float loadFactor) {
+        super( capacity,
+               loadFactor );
+    }    
+
+    public boolean add(InternalFactHandle handle, boolean checkExists) {
+        int hashCode =  handle.hashCode();
+        int index = indexOf( hashCode,
+                             table.length );
+
+        // scan the linked entries to see if it exists
+        if ( checkExists ) {
+            FactEntry current = (FactEntry) this.table[index];
+            while ( current != null ) {
+                if ( hashCode == current.hashCode && handle.getId() == current.handle.getId() )  {
+                    return false;
+                }
+            }
+        }
+
+        // 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];
+        this.table[index] = entry;
+
+        if ( this.size++ >= this.threshold ) {
+            resize( 2 * this.table.length );
+        }
+        return true;
+    }
+
+    public boolean contains(InternalFactHandle handle) {
+        int hashCode = handle.hashCode();
+        int index = indexOf( hashCode,
+                             table.length );
+
+        FactEntry current = (FactEntry) this.table[index];
+        while ( current != null ) {
+            if ( hashCode == current.hashCode && handle.getId() == current.handle.getId() ) {
+                return false;
+            }
+            current = (FactEntry) current.getNext();
+        }
+        return true;
+    }
+
+    public boolean remove(InternalFactHandle handle) {
+        int hashCode = handle.hashCode();
+        int index = indexOf( hashCode,
+                             table.length );
+
+        FactEntry previous = (FactEntry) this.table[index];
+        FactEntry current = previous;
+        while ( current != null ) {
+            FactEntry next = (FactEntry) current.getNext();
+            if ( hashCode == current.hashCode && handle.getId() == current.handle.getId() ) {
+                if ( previous == current ) {
+                    this.table[index] = next;
+                } else {
+                    previous.setNext( next );
+                }
+                current.setNext( null );
+                this.size--;
+                return true;
+            }
+            previous = current;
+            current = next;
+        }
+        return false;
+    }
+    
+    public Entry getBucket(int hashCode) {
+        int h = hashCode;
+        h += ~(h << 9);
+        h ^= (h >>> 14);
+        h += (h << 4);
+        h ^= (h >>> 10);
+        
+        return this.table[ indexOf( h, table.length ) ];
+    }    
+    
+    public FactEntry[] getTable()  {
+        return (FactEntry[]) this.table;
+    }
+
+    public int hash(Object key) {
+        return key.hashCode();
+    }
+
+    protected int indexOf(int hashCode,
+                          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

Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactIndexKey.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactIndexKey.java	2006-09-22 10:43:28 UTC (rev 6351)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FactIndexKey.java	2006-09-22 11:58:46 UTC (rev 6352)
@@ -1,21 +0,0 @@
-/**
- * 
- */
-package org.drools.util;
-
-public class FactIndexKey {
-    public int    hashCode;
-    public int    fieldIndex;
-    public Object value;
-
-    public int hashCode() {
-        return this.hashCode;
-    }
-
-    public boolean equals(Object object) {
-        // Inside the rete network we know that it will always be a FactIndexEntry
-        // We know it will never be null
-        FieldIndexEntry entry = (FieldIndexEntry) object;
-        return this.fieldIndex == entry.fieldIndex && this.value.equals( entry.getValue() );
-    }
-}
\ 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 10:43:28 UTC (rev 6351)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/FieldIndexHashTable.java	2006-09-22 11:58:46 UTC (rev 6352)
@@ -15,7 +15,7 @@
     private final int      startResult;
 
     public FieldIndexHashTable(int fieldIndex,
-                         FieldExtractor extractor) {
+                               FieldExtractor extractor) {
         this( 16,
               0.75f,
               fieldIndex,
@@ -23,9 +23,9 @@
     }
 
     public FieldIndexHashTable(int capacity,
-                         float loadFactor,
-                         int fieldIndex,
-                         FieldExtractor extractor) {
+                               float loadFactor,
+                               int fieldIndex,
+                               FieldExtractor extractor) {
         super( capacity,
                loadFactor );
         this.fieldIndex = fieldIndex;
@@ -43,19 +43,20 @@
         Object value = this.extractor.getValue( handle.getObject() );
         int hashCode = PRIME * startResult + ((value == null) ? 0 : value.hashCode());
 
-        int index = indexOf( hashCode, table.length  );
-        
+        int index = indexOf( hashCode,
+                             table.length );
+
         // search the table for  the Entry, we need to track previous  and next, so if the 
         // Entry is empty after  its had the FactEntry removed, we must remove  it from the table
-        FieldIndexEntry previous = ( FieldIndexEntry ) this.table[index];        
+        FieldIndexEntry previous = (FieldIndexEntry) this.table[index];
         FieldIndexEntry current = previous;
         while ( current != null ) {
-            FieldIndexEntry next = ( FieldIndexEntry ) current.next;
+            FieldIndexEntry next = (FieldIndexEntry) current.next;
             if ( hashCode == current.hashCode && value.equals( current.getValue() ) ) {
                 current.remove( handle );
                 // If the FactEntryIndex is empty, then remove it from the hash map
-                if (  current.first ==  null  ) {
-                    if( previous  == current ) {
+                if ( current.first == null ) {
+                    if ( previous == current ) {
                         this.table[index] = next;
                         previous.next = next;
                     }
@@ -67,12 +68,13 @@
             previous = current;
             current = next;
         }
-    }   
+    }
 
     public FieldIndexEntry get(Object value) {
         int hashCode = PRIME * startResult + ((value == null) ? 0 : value.hashCode());
 
-        int index = indexOf( hashCode, table.length  );
+        int index = indexOf( hashCode,
+                             table.length );
         FieldIndexEntry entry = (FieldIndexEntry) this.table[index];
 
         while ( entry != null ) {
@@ -81,10 +83,9 @@
             }
             entry = (FieldIndexEntry) entry.getNext();
         }
-        
+
         return entry;
     }
-    
 
     /**
      * We use this method to aviod to table lookups for the same hashcode; which is what we would have to do if we did
@@ -95,7 +96,8 @@
      */
     public FieldIndexEntry getOrCreate(Object value) {
         int hashCode = PRIME * startResult + ((value == null) ? 0 : value.hashCode());
-        int index = indexOf( hashCode, table.length  );
+        int index = indexOf( hashCode,
+                             table.length );
         FieldIndexEntry entry = (FieldIndexEntry) this.table[index];
 
         while ( entry != null ) {
@@ -103,7 +105,7 @@
                 return entry;
             }
             entry = (FieldIndexEntry) entry.next;
-        }        
+        }
 
         if ( entry == null ) {
             entry = new FieldIndexEntry( this.extractor,
@@ -118,8 +120,48 @@
         }
         return entry;
     }
-    
-    public static class FieldIndexEntry implements 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 {
         private Entry                next;
         FactEntry                    first;
         private final int            hashCode;
@@ -132,8 +174,8 @@
             this.extractor = extractor;
             this.fieldIndex = fieldIndex;
             this.hashCode = hashCode;
-        }       
-        
+        }
+
         public Entry getNext() {
             return next;
         }
@@ -148,7 +190,7 @@
 
         public void add(InternalFactHandle handle) {
             FactEntry entry = new FactEntry( handle );
-            entry.setNext( this.first );
+            entry.next =  this.first;
             this.first = entry;
         }
 
@@ -156,10 +198,10 @@
             long id = handle.getId();
             FactEntry current = first;
             while ( current != null ) {
-                if ( current.getFactHandle().getId() == id ) {
+                if ( current.handle.getId() == id ) {
                     return current;
                 }
-                current = (FactEntry) current.getNext();
+                current = (FactEntry) current.next;
             }
             return null;
         }
@@ -170,14 +212,14 @@
             FactEntry previous = this.first;
             FactEntry current = previous;
             while ( current != null ) {
-                FactEntry next = (FactEntry) current.getNext();
-                if ( current.getFactHandle().getId() == id ) {
+                FactEntry next = (FactEntry) current.next;
+                if ( current.handle.getId() == id ) {
                     if ( this.first == current ) {
                         this.first = next;
                     } else {
-                        previous.setNext( next );
+                        previous.next =   next;
                     }
-                    current.setNext( null );
+                    current.next = null;
                     return current;
                 }
                 previous = current;
@@ -209,5 +251,5 @@
             FieldIndexEntry other = (FieldIndexEntry) object;
             return this.hashCode == other.hashCode && this.fieldIndex == other.fieldIndex;
         }
-    }    
+    }
 }
\ No newline at end of file




More information about the jboss-svn-commits mailing list