/* * (c) Copyright 2009 Hewlett-Packard Development Company, LP * All rights reserved. * [See end of file] */ package structure.radix; import java.util.ArrayList ; import java.util.Iterator ; import java.util.List ; import org.junit.runner.JUnitCore ; import org.junit.runner.Result ; import org.openjena.atlas.junit.TextListener2 ; import org.openjena.atlas.lib.Bytes ; import org.openjena.atlas.logging.Log ; public class RadixRun { public static void main(String ...argv) { Log.setLog4j() ; Log.enable(RadixTree.class) ; RadixTree.logging = false ; //RadixTree.logging = true ; if ( false ) { JUnitCore runner = new org.junit.runner.JUnitCore() ; runner.addListener(new TextListener2(System.out)) ; //TestRadix.beforeClass() ; Result result = runner.run(TestRadix.class) ; //TestRadix.afterClass() ; System.exit(0) ; } List entries = new ArrayList() ; entries.add(new byte[]{1,4}) ; entries.add(new byte[]{1,2}) ; entries.add(new byte[]{1,2,3,4}) ; entries.add(new byte[]{1,2,8}) ; entries.add(new byte[]{1,2,3,4}) ; // repeat entries.add(new byte[]{1,5,8}) ; entries.add(new byte[]{0}) ; entries.add(new byte[]{}) ; RadixTree trie = new RadixTree() ; for ( byte[] arr : entries ) insert(trie, arr) ; RadixTree.logging = true ; Iterator iter = trie.iterator(new byte[]{1,2}, null) ; for ( ; iter.hasNext() ; ) { byte[] b = iter.next(); System.out.println(Bytes.asHex(b)) ; } System.exit(0) ; //RadixTree.logging = true ; //for ( int i = entries.size()-1 ; i >= 0 ; i-- ) for ( int i = 0 ; i < entries.size(); i++ ) { byte[] arr = entries.get(i) ; delete(trie, arr) ; } System.exit(0) ; } // static void search(RadixTree trie, byte[] key) // { // System.out.println("Search--'"+Bytes.asHex(key)+"'") ; // RadixNode node = trie.search(key) ; // System.out.println("Search>> "+node) ; // System.out.println() ; // } static void find(RadixTree trie, byte[] key) { System.out.println("Find --'"+Bytes.asHex(key)+"'") ; RadixNode node = trie.find(key) ; System.out.println("Find >> "+node) ; System.out.println() ; } static void insert(RadixTree trie, byte[] key) { System.out.println("Insert--'"+Bytes.asHex(key)+"'") ; boolean b2 = ( trie.find(key) == null ) ; boolean b = trie.insert(key) ; System.out.print(" >> "+b+" ["+b2+"]") ; System.out.print(": ") ; trie.printLeaves() ; //trie.print(); System.out.flush() ; if ( b != b2 ) System.err.println("Inconsistent (insert)") ; trie.check() ; } static void delete(RadixTree trie, byte[] key) { System.out.println("Delete--'"+Bytes.asHex(key)+"'") ; boolean b2 = ( trie.find(key) != null ) ; boolean b = trie.delete(key) ; System.out.print(" >> "+b+" ["+b2+"]") ; System.out.print(": ") ; trie.printLeaves() ; trie.print(); System.out.flush() ; if ( b != b2 ) System.err.println("Inconsistent (delete)") ; trie.check() ; } } /* * (c) Copyright 2009 Hewlett-Packard Development Company, LP * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */