Search in sources :

Example 1 with BytesSuccinctBitVector

use of org.trie4j.bv.BytesSuccinctBitVector in project trie4j by takawitter.

the class SBVConcatTailArrayTest method test.

@Test
public void test() throws Exception {
    // 普通にSBVConcatTailArrayIndexBuilder使った場合と、
    // add毎にappendするTailArrayIndexBuilderを作ってそれを使った
    // 場合でbitvectorやcacheに差が出るか調べる
    TailPatriciaTrie org = new TailPatriciaTrie(new ConcatTailBuilder());
    new WikipediaTitles().insertTo(org);
    TailLOUDSTrie louds1 = new TailLOUDSTrie(org, new SBVConcatTailArrayAppendingBuilder());
    new WikipediaTitles().assertAllContains(louds1);
    BytesSuccinctBitVector sbv1 = (BytesSuccinctBitVector) ((SBVTailIndex) ((DefaultTailArray) louds1.getTailArray()).getTailIndex()).getSbv();
    TailLOUDSTrie louds2 = new TailLOUDSTrie(org, new SBVConcatTailArrayBuilder());
    new WikipediaTitles().assertAllContains(louds2);
    BytesSuccinctBitVector sbv2 = (BytesSuccinctBitVector) ((SBVTailIndex) ((DefaultTailArray) louds2.getTailArray()).getTailIndex()).getSbv();
    {
        int n = sbv1.size();
        System.out.println("sbv size: " + n);
        Assert.assertEquals(n, sbv2.size());
        for (int i = 0; i < n; i++) {
            Assert.assertEquals(i + "th bit", sbv1.get(i), sbv2.get(i));
        }
    }
    {
        int[] countCache1 = sbv1.getCountCache0();
        int[] countCache2 = sbv2.getCountCache0();
        int n = countCache1.length;
        System.out.println("countCache0 size should be: " + (sbv1.size() / 64 + 1));
        System.out.println("countCache0 size: " + n);
        //			Assert.assertEquals(n, countCache2.length);
        n = Math.min(countCache1.length, countCache2.length);
        for (int i = 0; i < n; i++) {
            Assert.assertEquals(i + "th index cache.", countCache1[i], countCache2[i]);
        }
    }
    {
        IntArray indexCache1 = sbv1.getIndexCache0();
        IntArray indexCache2 = sbv2.getIndexCache0();
        int n = indexCache1.size();
        System.out.println("indexCache0 size1: " + n);
        System.out.println("indexCache0 size2: " + indexCache2.size());
        //			Assert.assertEquals(n, countCache2.length);
        n = Math.min(indexCache1.size(), indexCache2.size());
        for (int i = 0; i < 10; i++) {
            System.out.print(indexCache1.get(i) + ", ");
        }
        System.out.println();
        for (int i = 0; i < 10; i++) {
            System.out.print(indexCache2.get(i) + ", ");
        }
        System.out.println();
        for (int i = 0; i < n; i++) {
            Assert.assertEquals(i + "th index cache.", indexCache1.get(i), indexCache2.get(i));
        }
    }
}
Also used : TailPatriciaTrie(org.trie4j.patricia.TailPatriciaTrie) TailLOUDSTrie(org.trie4j.louds.TailLOUDSTrie) IntArray(org.trie4j.util.IntArray) ConcatTailBuilder(org.trie4j.tail.builder.ConcatTailBuilder) BytesSuccinctBitVector(org.trie4j.bv.BytesSuccinctBitVector) WikipediaTitles(org.trie4j.test.WikipediaTitles) Test(org.junit.Test)

Example 2 with BytesSuccinctBitVector

use of org.trie4j.bv.BytesSuccinctBitVector in project trie4j by takawitter.

the class BitVectorExp method main.

public static void main(String[] args) throws Exception {
    Trie trie = new PatriciaTrie();
    int c = 0;
    // You can download archive from http://dumps.wikimedia.org/jawiki/latest/
    LapTimer t = new LapTimer();
    for (String word : new WikipediaTitles()) {
        trie.insert(word);
        c++;
        if (c == maxCount)
            break;
    }
    t.lapMillis("trie building done. %d words.", c);
    final BytesSuccinctBitVector bv = new BytesSuccinctBitVector(5000000);
    final AtomicInteger nodeCount = new AtomicInteger();
    Algorithms.traverseByDepth(trie.getRoot(), new NodeVisitor() {

        @Override
        public boolean visit(Node node, int nest) {
            Node[] children = node.getChildren();
            if (children != null) {
                int n = node.getChildren().length;
                for (int i = 0; i < n; i++) {
                    bv.append(true);
                }
            }
            bv.append(false);
            nodeCount.incrementAndGet();
            return true;
        }
    });
    trie = null;
    t.lapMillis("done. %d nodes inserted. do rank and select", nodeCount.intValue());
    for (int i = 0; i < c; i += 100) {
        int count = bv.rank(i, true);
        bv.select(count, true);
    }
    t.lapMillis("done.");
    Thread.sleep(10000);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) Node(org.trie4j.Node) BytesSuccinctBitVector(org.trie4j.bv.BytesSuccinctBitVector) WikipediaTitles(org.trie4j.test.WikipediaTitles) Trie(org.trie4j.Trie) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) LapTimer(org.trie4j.test.LapTimer) NodeVisitor(org.trie4j.NodeVisitor)

Example 3 with BytesSuccinctBitVector

use of org.trie4j.bv.BytesSuccinctBitVector in project trie4j by takawitter.

the class TrieWriter method writeSuccinctBitVector.

public void writeSuccinctBitVector(SuccinctBitVector sbv) throws IOException {
    if (sbv instanceof BytesSuccinctBitVector) {
        dos.writeShort(TYPE_SBV_BYTES);
        writeBytesSuccinctBitVector((BytesSuccinctBitVector) sbv);
    } else if (sbv instanceof BytesRank0OnlySuccinctBitVector) {
        dos.writeShort(TYPE_SBV_RANK0ONLY);
        writeRank0OnlySuccinctBitVector((BytesRank0OnlySuccinctBitVector) sbv);
    } else if (sbv instanceof BytesRank1OnlySuccinctBitVector) {
        dos.writeShort(TYPE_SBV_RANK1ONLY);
        writeRank1OnlySuccinctBitVector((BytesRank1OnlySuccinctBitVector) sbv);
    } else if (sbv instanceof BytesRank1OnlySuccinctBitVector) {
        dos.writeShort(TYPE_SBV_LONGS);
        writeLongsSuccinctBitVector((LongsSuccinctBitVector) sbv);
    } else {
        throw new IOException("unknown sbv: " + sbv.getClass().getName());
    }
}
Also used : BytesRank0OnlySuccinctBitVector(org.trie4j.bv.BytesRank0OnlySuccinctBitVector) BytesSuccinctBitVector(org.trie4j.bv.BytesSuccinctBitVector) BytesRank1OnlySuccinctBitVector(org.trie4j.bv.BytesRank1OnlySuccinctBitVector) IOException(java.io.IOException) LongsSuccinctBitVector(org.trie4j.bv.LongsSuccinctBitVector)

Aggregations

BytesSuccinctBitVector (org.trie4j.bv.BytesSuccinctBitVector)3 WikipediaTitles (org.trie4j.test.WikipediaTitles)2 IOException (java.io.IOException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1 Node (org.trie4j.Node)1 NodeVisitor (org.trie4j.NodeVisitor)1 Trie (org.trie4j.Trie)1 BytesRank0OnlySuccinctBitVector (org.trie4j.bv.BytesRank0OnlySuccinctBitVector)1 BytesRank1OnlySuccinctBitVector (org.trie4j.bv.BytesRank1OnlySuccinctBitVector)1 LongsSuccinctBitVector (org.trie4j.bv.LongsSuccinctBitVector)1 TailLOUDSTrie (org.trie4j.louds.TailLOUDSTrie)1 PatriciaTrie (org.trie4j.patricia.PatriciaTrie)1 TailPatriciaTrie (org.trie4j.patricia.TailPatriciaTrie)1 ConcatTailBuilder (org.trie4j.tail.builder.ConcatTailBuilder)1 LapTimer (org.trie4j.test.LapTimer)1 IntArray (org.trie4j.util.IntArray)1