001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: HashedTripleBunch.java,v 1.19 2008/01/02 12:09:51 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.mem;
008:
009: import java.util.*;
010:
011: import com.hp.hpl.jena.graph.Triple;
012: import com.hp.hpl.jena.graph.query.*;
013: import com.hp.hpl.jena.util.iterator.*;
014:
015: public class HashedTripleBunch extends HashCommon implements
016: TripleBunch {
017: public HashedTripleBunch(TripleBunch b) {
018: super (nextSize((int) (b.size() / loadFactor)));
019: for (Iterator it = b.iterator(); it.hasNext();)
020: add((Triple) it.next());
021: changes = 0;
022: }
023:
024: public boolean contains(Triple t) {
025: return findSlot(t) < 0;
026: }
027:
028: protected int findSlotBySameValueAs(Triple key) {
029: int index = initialIndexFor(key);
030: while (true) {
031: Object current = keys[index];
032: if (current == null)
033: return index;
034: if (key.matches((Triple) current))
035: return ~index;
036: if (--index < 0)
037: index += capacity;
038: }
039: }
040:
041: public boolean containsBySameValueAs(Triple t) {
042: return findSlotBySameValueAs(t) < 0;
043: }
044:
045: /**
046: Answer the number of items currently in this TripleBunch.
047: @see com.hp.hpl.jena.mem.TripleBunch#size()
048: */
049: public int size() {
050: return size;
051: }
052:
053: /**
054: Answer the current capacity of this HashedTripleBunch; for testing purposes
055: only. [Note that the bunch is resized when it is more than half-occupied.]
056: */
057: public int currentCapacity() {
058: return capacity;
059: }
060:
061: public void add(Triple t) {
062: keys[findSlot(t)] = t;
063: changes += 1;
064: if (++size > threshold)
065: grow();
066: }
067:
068: protected void grow() {
069: Object[] oldContents = keys;
070: final int oldCapacity = capacity;
071: growCapacityAndThreshold();
072: Object[] newKeys = keys = new Triple[capacity];
073: for (int i = 0; i < oldCapacity; i += 1) {
074: Object t = oldContents[i];
075: if (t != null)
076: newKeys[findSlot(t)] = t;
077: }
078: }
079:
080: public void remove(Triple t) {
081: removeFrom(~findSlot(t));
082: changes += 1;
083: }
084:
085: public ExtendedIterator iterator() {
086: return iterator(NotifyEmpty.ignore);
087: }
088:
089: public ExtendedIterator iterator(final NotifyEmpty container) {
090: return keyIterator(container);
091: }
092:
093: public void app(Domain d, StageElement next, MatchOrBind s) {
094: int i = capacity, initialChanges = changes;
095: while (i > 0) {
096: if (changes > initialChanges)
097: throw new ConcurrentModificationException();
098: Object t = keys[--i];
099: if (t != null && s.matches((Triple) t))
100: next.run(d);
101: }
102: }
103: }
104:
105: /*
106: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
107: * All rights reserved.
108: *
109: * Redistribution and use in source and binary forms, with or without
110: * modification, are permitted provided that the following conditions
111: * are met:
112: * 1. Redistributions of source code must retain the above copyright
113: * notice, this list of conditions and the following disclaimer.
114: * 2. Redistributions in binary form must reproduce the above copyright
115: * notice, this list of conditions and the following disclaimer in the
116: * documentation and/or other materials provided with the distribution.
117: * 3. The name of the author may not be used to endorse or promote products
118: * derived from this software without specific prior written permission.
119: *
120: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
121: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
122: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
123: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
124: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
125: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
126: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
127: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
128: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
129: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
130: */
|