/* * (c) Copyright 2009 Hewlett-Packard Development Company, LP * All rights reserved. * [See end of file] */ package dev.g2; import org.openjena.atlas.iterator.IteratorConcat; import java.util.Collection; import java.util.Iterator; import org.openjena.atlas.lib.DS; public class TupleIndex //extends Index>> { // Top : Hash of T -> (T, T*) private Index>> map = new IndexImpl>>() ; public TupleIndex() {} public Index> get(T key1) { return map.get(key1) ; } public Collection get(T key1, T key2) { return map.get(key1).get(key2) ; } public Iterator keys() { return map.keys() ; } public void put(T key1, T key2, T value) { IndexImpl> x = map.get(key1) ; if ( x == null ) { x = new IndexImpl>() ; map.put(key1, x) ; } Collection z = x.get(key2) ; if ( z == null ) { z = DS.list() ; x.put(key2, z) ; } z.add(value) ; } public void remove(T key1, T key2) { Index> x = map.get(key1) ; if ( x == null ) return ; x.remove(key2) ; if ( x.size() == 0 ) map.remove(key1) ; } public Iterator flatten() { // Accumulating iterator. IteratorConcat all = new IteratorConcat() ; for ( Iterator iter = map.keys() ; iter.hasNext() ; ) { T k1 = iter.next(); Index> x = map.get(k1) ; for (Iterator iter2 = x.keys() ; iter.hasNext() ; ) { T k2 = iter2.next(); Collection y = x.get(k2) ; all.add(y.iterator()) ; } } return all ; } public int size() { return map.size() ; } public boolean isEmpty() { return map.isEmpty() ; } } /* * (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. */