Changeset 2654

Show
Ignore:
Timestamp:
11/10/08 16:54:10 (2 months ago)
Author:
leo
Message:

lattice

Location:
XerialJ/trunk/xerial-core/src
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • XerialJ/trunk/xerial-core/src/main/java/org/xerial/core/XerialErrorCode.java

    r2652 r2654  
    3737    SYNTAX_ERROR, 
    3838    NOT_INITIALIZED, 
     39    UNSUPPORTED, 
    3940    // collection 
    4041    MISSING_ELEMENT, 
  • XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/BitVector.java

    r2652 r2654  
    149149    } 
    150150 
    151     public static BitVector newInstance(BitVector source, int additionlBitIndex) 
     151    public static BitVector newInstanceWithAnAdditionalBit(BitVector source, int bitIndexToAdd) 
    152152    { 
    153153        BitVector newInstance = new BitVector(source.bitVector, source.size); 
    154         newInstance.on(additionlBitIndex); 
     154        newInstance.on(bitIndexToAdd); 
    155155        return newInstance; 
    156156    } 
  • XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/graph/Lattice.java

    r2652 r2654  
    2525package org.xerial.util.graph; 
    2626 
     27import java.util.ArrayList; 
     28import java.util.HashMap; 
     29 
     30import org.xerial.core.XerialError; 
     31import org.xerial.core.XerialErrorCode; 
    2732import org.xerial.util.BitVector; 
    2833import org.xerial.util.IndexedSet; 
     
    3742public class Lattice<T> 
    3843{ 
    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; 
    4150 
    4251    public Lattice() 
    4352    { 
    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; 
    45118    } 
    46119 
    47120    public LatticeNode<T> emptyNode() 
    48121    { 
    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); 
    50128    } 
    51129 
     
    54132        return elementSet.getByID(elementID); 
    55133    } 
     134 
    56135} 
  • XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/graph/LatticeNode.java

    r2652 r2654  
    2626 
    2727import java.util.ArrayList; 
    28 import java.util.HashMap; 
    2928 
    3029import org.xerial.core.XerialError; 
     
    3433import org.xerial.util.StringUtil; 
    3534 
     35/** 
     36 * lattice node 
     37 *  
     38 * @author leo 
     39 *  
     40 * @param <T> 
     41 */ 
    3642public class LatticeNode<T> 
    3743{ 
    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; 
    4147 
    4248    public LatticeNode(Lattice<T> lattice, BitVector elementOnOffIndicator) 
     
    4652    } 
    4753 
     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 
    4870    public boolean contains(T element) 
    4971    { 
    50         int id = lattice.elementSet.getID(element); 
     72        int id = lattice.getElementID(element); 
    5173        if (id == IndexedSet.INVALID_ID) 
    5274            return false; 
    5375 
    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); 
    6377    } 
    6478 
    6579    public LatticeNode<T> next(T elementToAdd) 
    6680    { 
    67         if (linkToNextNode.containsKey(elementToAdd)) 
    68         { 
    69             return linkToNextNode.get(elementToAdd); 
    70         } 
     81        return lattice.next(this, elementToAdd); 
     82    } 
    7183 
    72         // Add new edge to a LatticeNode 
    73         // First, create a new bit vector that additionally set the target element ID 
    74         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    } 
    7688 
    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; 
    85107    } 
    86108 
  • XerialJ/trunk/xerial-core/src/test/java/org/xerial/util/graph/LatticeTest.java

    r2652 r2654  
    2525package org.xerial.util.graph; 
    2626 
     27import static org.junit.Assert.assertEquals; 
    2728import static org.junit.Assert.assertFalse; 
     29import static org.junit.Assert.assertNotSame; 
    2830import static org.junit.Assert.assertTrue; 
    2931 
     
    3133import org.junit.Before; 
    3234import org.junit.Test; 
     35import org.xerial.util.StopWatch; 
     36import org.xerial.util.log.Logger; 
    3337 
    3438public class LatticeTest 
    3539{ 
     40    private static Logger _logger = Logger.getLogger(LatticeTest.class); 
    3641 
    3742    @Before 
     
    5964        assertFalse(abNode.contains("C")); 
    6065 
     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 
    61108    } 
    62109