1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.query;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.Iterator;
19 import org.simantics.db.Resource;
20 import org.simantics.db.ResourceSet;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.impl.ResourceImpl;
23 import org.simantics.db.impl.graph.ReadGraphImpl;
24 import org.simantics.db.impl.support.ResourceSupport;
26 import gnu.trove.procedure.TIntProcedure;
29 final public class IntSet implements ResourceSet {
31 final private ResourceSupport support;
35 /** the index after the last entry in the list */
36 public int sizeOrData;
38 /** the default capacity for new lists */
39 protected static final int DEFAULT_CAPACITY = 3;
41 public static final int NO_DATA = -1;
43 private static final Object[] EMPTY_ARRAY = new Object[0];
45 public static IntSet EMPTY = new IntSet();
51 public IntSet(QuerySupport support) {
52 this.support = support.getSupport();
57 public IntSet(QuerySupport support, int value) {
58 this.support = support.getSupport();
64 public int hashCode() {
65 return 31 * sizeOrData + 41 * Arrays.hashCode(data);
69 public boolean equals(Object object) {
72 else if (object == null)
74 else if (IntSet.class != object.getClass())
76 IntSet r = (IntSet)object;
77 // System.out.println("equals " + this + " vs. " + r);
78 return sizeOrData == r.sizeOrData && Arrays.equals(data, r.data);
83 * Returns the number of values in the list.
85 * @return the number of values in the list.
88 return data != null ? sizeOrData : (sizeOrData != NO_DATA ? 1 : 0);
92 * Tests whether this list contains any values.
94 * @return true if the list is empty.
96 public boolean isEmpty() {
97 return sizeOrData == NO_DATA;
103 * Adds <tt>val</tt> to the end of the list, growing as needed.
105 * @param val an <code>int</code> value
107 public boolean add(int val) {
109 if(sizeOrData == val) return false;
110 if(sizeOrData == NO_DATA) {
114 data = new int[DEFAULT_CAPACITY];
115 data[0] = sizeOrData;
121 for(int i=0;i<sizeOrData;i++) if(data[i] == val) return false;
122 if(data.length == sizeOrData) {
123 int newCap = data.length << 1;
124 int[] tmp = new int[newCap];
125 System.arraycopy(data, 0, tmp, 0, data.length);
127 data[sizeOrData++] = val;
129 data[sizeOrData++] = val;
135 public void forEach(TIntProcedure proc) {
137 if(sizeOrData != NO_DATA) proc.execute(sizeOrData);
139 for(int i = 0;i < sizeOrData ; i++) proc.execute(data[i]);
143 public boolean contains(int val) {
145 return sizeOrData == val;
147 for(int i = 0;i < sizeOrData ; i++) if(data[i] == val) return true;
153 if(data != null && sizeOrData < data.length) {
154 int newCap = sizeOrData;
155 int[] tmp = new int[newCap];
156 System.arraycopy(data, 0, tmp, 0, newCap);
163 public boolean add(Resource e) {
164 return add(((ResourceImpl)e).id);
168 public boolean addAll(Collection<? extends Resource> c) {
170 for (Resource r : c) {
178 public void clear() {
179 sizeOrData = NO_DATA;
184 public boolean contains(Object o) {
186 return contains(((ResourceImpl)o).id);
190 public boolean containsAll(Collection<?> c) {
199 public Iterator<Resource> iterator() {
201 class ArraySetIterator implements Iterator<Resource> {
206 public boolean hasNext() {
207 return next < size();
211 public Resource next() {
214 return new ResourceImpl(support, sizeOrData);
216 return new ResourceImpl(support, data[next++]);
221 public void remove() {
222 throw new UnsupportedOperationException();
227 return new ArraySetIterator();
232 public boolean remove(Object o) {
233 throw new UnsupportedOperationException();
237 public boolean removeAll(Collection<?> c) {
238 throw new UnsupportedOperationException();
242 public boolean retainAll(Collection<?> c) {
243 throw new UnsupportedOperationException();
247 public Object[] toArray() {
249 Object[] result = new Object[sizeOrData];
250 for(int i=0;i<sizeOrData;++i)
251 result[i] = new ResourceImpl(support, data[i]);
254 else if(sizeOrData == NO_DATA)
257 return new Object[] { new ResourceImpl(support, sizeOrData) };
261 public <T> T[] toArray(T[] a) {
262 throw new UnsupportedOperationException();
266 public String toString() {
267 return "IntSet " + sizeOrData + " " + Arrays.toString(data);
271 public boolean disjoint(Set<Resource> other) {
272 if(other instanceof ResourceSet) {
273 ResourceSet rs = (ResourceSet)other;
275 return !rs.contains(sizeOrData);
277 for(int i = 0;i < sizeOrData ; i++)
278 if(rs.contains(data[i])) return false;
283 return !other.contains(sizeOrData);
285 for(int i = 0;i < sizeOrData ; i++)
286 if(other.contains(data[i])) return false;
292 public void forEach(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException {
294 for (int i=0;i<sizeOrData;++i)
295 procedure.execute(graph, data[i]);
296 } else if(sizeOrData == NO_DATA) {
298 procedure.execute(graph, sizeOrData);
300 procedure.finished(graph);