Changeset 2654
- Timestamp:
- 11/10/08 16:54:10 (2 months ago)
- Location:
- XerialJ/trunk/xerial-core/src
- Files:
-
- 5 modified
-
main/java/org/xerial/core/XerialErrorCode.java (modified) (1 diff)
-
main/java/org/xerial/util/BitVector.java (modified) (1 diff)
-
main/java/org/xerial/util/graph/Lattice.java (modified) (3 diffs)
-
main/java/org/xerial/util/graph/LatticeNode.java (modified) (3 diffs)
-
test/java/org/xerial/util/graph/LatticeTest.java (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
XerialJ/trunk/xerial-core/src/main/java/org/xerial/core/XerialErrorCode.java
r2652 r2654 37 37 SYNTAX_ERROR, 38 38 NOT_INITIALIZED, 39 UNSUPPORTED, 39 40 // collection 40 41 MISSING_ELEMENT, -
XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/BitVector.java
r2652 r2654 149 149 } 150 150 151 public static BitVector newInstance (BitVector source, int additionlBitIndex)151 public static BitVector newInstanceWithAnAdditionalBit(BitVector source, int bitIndexToAdd) 152 152 { 153 153 BitVector newInstance = new BitVector(source.bitVector, source.size); 154 newInstance.on( additionlBitIndex);154 newInstance.on(bitIndexToAdd); 155 155 return newInstance; 156 156 } -
XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/graph/Lattice.java
r2652 r2654 25 25 package org.xerial.util.graph; 26 26 27 import java.util.ArrayList; 28 import java.util.HashMap; 29 30 import org.xerial.core.XerialError; 31 import org.xerial.core.XerialErrorCode; 27 32 import org.xerial.util.BitVector; 28 33 import org.xerial.util.IndexedSet; … … 37 42 public class Lattice<T> 38 43 { 39 IndexedSet<T> elementSet = new IndexedSet<T>(); 40 IndexedSet<BitVector> latticeNodeSet = new IndexedSet<BitVector>(); 44 private IndexedSet<T> elementSet = new IndexedSet<T>(); 45 private IndexedSet<LatticeNode<T>> latticeNodeSet = new IndexedSet<LatticeNode<T>>(); 46 private ArrayList<HashMap<T, LatticeNode<T>>> outEdgeIndexTable = new ArrayList<HashMap<T, LatticeNode<T>>>(); 47 private ArrayList<HashMap<T, LatticeNode<T>>> inEdgeIndexTable = new ArrayList<HashMap<T, LatticeNode<T>>>(); 48 49 private final LatticeNode<T> emptySet; 41 50 42 51 public Lattice() 43 52 { 44 latticeNodeSet.add(new BitVector()); 53 emptySet = newLatticeNode(new BitVector()); 54 } 55 56 private HashMap<T, LatticeNode<T>> getOutEdgeIndex(int latticeNodeID) 57 { 58 assert latticeNodeID > 0; 59 return outEdgeIndexTable.get(latticeNodeID - 1); 60 } 61 62 private HashMap<T, LatticeNode<T>> getInEdgeIndex(int latticeNodeID) 63 { 64 assert latticeNodeID > 0; 65 return inEdgeIndexTable.get(latticeNodeID - 1); 66 } 67 68 LatticeNode<T> next(LatticeNode<T> currentNode, T element) 69 { 70 int currentLatticeNodeID = currentNode.getID(); 71 72 HashMap<T, LatticeNode<T>> outEdgeIndexOfTheCurrentNode = getOutEdgeIndex(currentLatticeNodeID); 73 LatticeNode<T> nextNode = outEdgeIndexOfTheCurrentNode.get(element); 74 if (nextNode != null) 75 return nextNode; 76 77 // create a new lattice node 78 LatticeNode<T> newLatticeNode = newLatticeNode(BitVector.newInstanceWithAnAdditionalBit(currentNode 79 .getElementOnOffIndicator(), elementSet.getIDwithAddition(element))); 80 81 // draw an in-edge from the current node (for back operation) 82 getInEdgeIndex(newLatticeNode.getID()).put(element, currentNode); 83 84 // draw an out-edge (labeled with the element value) from the current node to the new node 85 getOutEdgeIndex(currentLatticeNodeID).put(element, newLatticeNode); 86 87 return newLatticeNode; 88 } 89 90 LatticeNode<T> back(LatticeNode<T> currentNode, T element) 91 { 92 int currentLatticeNodeID = currentNode.getID(); 93 94 HashMap<T, LatticeNode<T>> inEdgeIndexOfTheCurrentNode = getInEdgeIndex(currentLatticeNodeID); 95 LatticeNode<T> prevNode = inEdgeIndexOfTheCurrentNode.get(element); 96 if (prevNode != null) 97 return prevNode; 98 else 99 throw new XerialError(XerialErrorCode.UNSUPPORTED, String.format( 100 "previous node must exist in the lattice. currentNode = %s, element = %s", currentNode, element)); 101 } 102 103 protected LatticeNode<T> newLatticeNode(BitVector bv) 104 { 105 LatticeNode<T> newLatticeNode = new LatticeNode<T>(this, bv); 106 // allocate an new ID for the lattice node 107 int newLatticeNodeID = latticeNodeSet.getIDwithAddition(newLatticeNode); 108 newLatticeNode.setID(newLatticeNodeID); 109 110 // create in/out edge indexes for the new node 111 inEdgeIndexTable.add(new HashMap<T, LatticeNode<T>>()); 112 outEdgeIndexTable.add(new HashMap<T, LatticeNode<T>>()); 113 114 assert (newLatticeNodeID == outEdgeIndexTable.size()); 115 assert (newLatticeNodeID == inEdgeIndexTable.size()); 116 117 return newLatticeNode; 45 118 } 46 119 47 120 public LatticeNode<T> emptyNode() 48 121 { 49 return new LatticeNode<T>(this, new BitVector()); 122 return emptySet; 123 } 124 125 public int getElementID(T element) 126 { 127 return elementSet.getID(element); 50 128 } 51 129 … … 54 132 return elementSet.getByID(elementID); 55 133 } 134 56 135 } -
XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/graph/LatticeNode.java
r2652 r2654 26 26 27 27 import java.util.ArrayList; 28 import java.util.HashMap;29 28 30 29 import org.xerial.core.XerialError; … … 34 33 import org.xerial.util.StringUtil; 35 34 35 /** 36 * lattice node 37 * 38 * @author leo 39 * 40 * @param <T> 41 */ 36 42 public class LatticeNode<T> 37 43 { 38 private final Lattice<T> lattice;39 private final BitVector elementOnOffIndicator;40 private final HashMap<T, LatticeNode<T>> linkToNextNode = new HashMap<T, LatticeNode<T>>();44 private int id = -1; 45 private final Lattice<T> lattice; 46 private final BitVector elementOnOffIndicator; 41 47 42 48 public LatticeNode(Lattice<T> lattice, BitVector elementOnOffIndicator) … … 46 52 } 47 53 54 public void setID(int id) 55 { 56 this.id = id; 57 } 58 59 public int getID() 60 { 61 assert id != -1; 62 return id; 63 } 64 65 BitVector getElementOnOffIndicator() 66 { 67 return elementOnOffIndicator; 68 } 69 48 70 public boolean contains(T element) 49 71 { 50 int id = lattice. elementSet.getID(element);72 int id = lattice.getElementID(element); 51 73 if (id == IndexedSet.INVALID_ID) 52 74 return false; 53 75 54 return elementOnOffIndicator.get(id - 1); 55 } 56 57 protected int getElementID(T element) 58 { 59 if (!lattice.elementSet.contains(element)) 60 lattice.elementSet.add(element); 61 62 return lattice.elementSet.getID(element); 76 return elementOnOffIndicator.get(id); 63 77 } 64 78 65 79 public LatticeNode<T> next(T elementToAdd) 66 80 { 67 if (linkToNextNode.containsKey(elementToAdd)) 68 { 69 return linkToNextNode.get(elementToAdd); 70 } 81 return lattice.next(this, elementToAdd); 82 } 71 83 72 // Add new edge to a LatticeNode73 // First, create a new bit vector that additionally set the target element ID74 int elementID = getElementID(elementToAdd);75 BitVector newIndicator = BitVector.newInstance(elementOnOffIndicator, elementID - 1);84 public LatticeNode<T> back(T elementToRemove) 85 { 86 return lattice.back(this, elementToRemove); 87 } 76 88 77 int latticeNodeID = lattice.latticeNodeSet.getID(newIndicator); 78 if (latticeNodeID == IndexedSet.INVALID_ID) 79 { 80 lattice.latticeNodeSet.add(newIndicator); 81 return new LatticeNode<T>(lattice, newIndicator); 82 } 83 else 84 return new LatticeNode<T>(lattice, lattice.latticeNodeSet.getByID(latticeNodeID)); 89 @SuppressWarnings("unchecked") 90 @Override 91 public boolean equals(Object obj) 92 { 93 if (!this.getClass().isInstance(obj)) 94 return false; 95 96 LatticeNode<T> other = (LatticeNode<T>) obj; 97 return this.lattice == other.lattice && this.elementOnOffIndicator.equals(other.elementOnOffIndicator); 98 } 99 100 @Override 101 public int hashCode() 102 { 103 int hashValue = 3; 104 hashValue += lattice.hashCode() * 31; 105 hashValue += elementOnOffIndicator.hashCode() * 31; 106 return hashValue % 1987; 85 107 } 86 108 -
XerialJ/trunk/xerial-core/src/test/java/org/xerial/util/graph/LatticeTest.java
r2652 r2654 25 25 package org.xerial.util.graph; 26 26 27 import static org.junit.Assert.assertEquals; 27 28 import static org.junit.Assert.assertFalse; 29 import static org.junit.Assert.assertNotSame; 28 30 import static org.junit.Assert.assertTrue; 29 31 … … 31 33 import org.junit.Before; 32 34 import org.junit.Test; 35 import org.xerial.util.StopWatch; 36 import org.xerial.util.log.Logger; 33 37 34 38 public class LatticeTest 35 39 { 40 private static Logger _logger = Logger.getLogger(LatticeTest.class); 36 41 37 42 @Before … … 59 64 assertFalse(abNode.contains("C")); 60 65 66 LatticeNode<String> acNode = aNode.next("C"); 67 assertTrue(acNode.contains("A")); 68 assertTrue(acNode.contains("C")); 69 assertFalse(acNode.contains("B")); 70 71 LatticeNode<String> acdNode = acNode.next("D"); 72 assertTrue(acdNode.contains("A")); 73 assertTrue(acdNode.contains("C")); 74 assertTrue(acdNode.contains("D")); 75 assertTrue(!acdNode.contains("B")); 76 77 LatticeNode<String> acdMinusDNode = acdNode.back("D"); 78 assertEquals(acNode, acdMinusDNode); 79 assertNotSame(acNode, acdNode); 80 assertEquals(acNode.getID(), acdMinusDNode.getID()); 81 82 LatticeNode<String> acdMinusDCNode = acdNode.back("D").back("C"); 83 assertEquals(aNode, acdMinusDCNode); 84 assertNotSame(abNode, acdMinusDCNode); 85 assertEquals(aNode.getID(), acdMinusDCNode.getID()); 86 87 } 88 89 @Test 90 public void latticePerformance() 91 { 92 Lattice<String> lattice = new Lattice<String>(); 93 LatticeNode<String> emptyNode = lattice.emptyNode(); 94 95 StopWatch timer = new StopWatch(); 96 int N = 10000; 97 98 timer.reset(); 99 LatticeNode<String> emptyNode2 = emptyNode.next("A").next("B").next("C").next("D").back("D").back("C") 100 .back("B").back("A"); 101 assertEquals(emptyNode, emptyNode2); 102 103 emptyNode2 = emptyNode.next("A").next("B").back("B").back("A"); 104 assertEquals(emptyNode, emptyNode2); 105 106 _logger.debug(timer.getElapsedTime()); 107 61 108 } 62 109


