01: /**
02: *
03: */package org.drools.util;
04:
05: import org.drools.common.InternalFactHandle;
06: import org.drools.reteoo.ReteTuple;
07: import org.drools.reteoo.TupleMemory;
08:
09: public class TupleHashTable extends AbstractHashTable implements
10: TupleMemory {
11: public TupleHashTable() {
12: this (16, 0.75f);
13: }
14:
15: public TupleHashTable(final int capacity, final float loadFactor) {
16: super (capacity, loadFactor);
17: }
18:
19: public Iterator iterator(final InternalFactHandle handle) {
20: return iterator();
21: }
22:
23: public void add(final ReteTuple tuple) {
24: final int hashCode = tuple.hashCode();
25: final int index = indexOf(hashCode, this .table.length);
26:
27: tuple.setNext(this .table[index]);
28: this .table[index] = tuple;
29:
30: if (this .size++ >= this .threshold) {
31: resize(2 * this .table.length);
32: }
33: }
34:
35: public ReteTuple get(final ReteTuple tuple) {
36: final int hashCode = tuple.hashCode();
37: final int index = indexOf(hashCode, this .table.length);
38:
39: ReteTuple current = (ReteTuple) this .table[index];
40: while (current != null) {
41: if (hashCode == current.hashCode() && tuple.equals(current)) {
42: return current;
43: }
44: current = (ReteTuple) current.getNext();
45: }
46: return null;
47: }
48:
49: public ReteTuple remove(final ReteTuple tuple) {
50: final int hashCode = tuple.hashCode();
51: final int index = indexOf(hashCode, this .table.length);
52:
53: ReteTuple previous = (ReteTuple) this .table[index];
54: ReteTuple current = previous;
55: while (current != null) {
56: final ReteTuple next = (ReteTuple) current.getNext();
57: if (hashCode == current.hashCode() && tuple.equals(current)) {
58: if (previous == current) {
59: this .table[index] = next;
60: } else {
61: previous.setNext(next);
62: }
63: current.setNext(null);
64: this .size--;
65: return current;
66: }
67: previous = current;
68: current = next;
69: }
70: return current;
71: }
72:
73: public Entry getBucket(final Object object) {
74: final int hashCode = object.hashCode();
75: final int index = indexOf(hashCode, this .table.length);
76:
77: return this .table[index];
78: }
79:
80: public boolean contains(final ReteTuple tuple) {
81: return (get(tuple) != null);
82: }
83:
84: public boolean isIndexed() {
85: return false;
86: }
87: }
|