]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/IntSet.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / IntSet.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.query;
13
14 import gnu.trove.procedure.TIntProcedure;
15
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.Iterator;
19 import java.util.Set;
20
21 import org.simantics.db.Resource;
22 import org.simantics.db.ResourceSet;
23 import org.simantics.db.impl.ResourceImpl;
24 import org.simantics.db.impl.support.ResourceSupport;
25
26
27 final public class IntSet implements ResourceSet {
28
29     final private ResourceSupport support;
30
31     public int[] data;
32
33     /** the index after the last entry in the list */
34     public int sizeOrData;
35
36     /** the default capacity for new lists */
37     protected static final int DEFAULT_CAPACITY = 3;
38
39     public static final int NO_DATA = -1;
40
41     public IntSet() {
42         support = null;
43         data = null;
44         sizeOrData = NO_DATA;
45     }
46
47     public IntSet(QuerySupport support) {
48         this.support = support.getSupport();
49         data = null;
50         sizeOrData = NO_DATA;
51     }
52
53     public IntSet(QuerySupport support, int value) {
54         this.support = support.getSupport();
55         data = null;
56         sizeOrData = value;
57     }
58
59     @Override
60     public int hashCode() {
61         return 31 * sizeOrData + 41 * Arrays.hashCode(data);
62     }
63     
64     @Override
65     public boolean equals(Object object) {
66         if (this == object)
67             return true;
68         else if (object == null)
69             return false;
70         else if (IntSet.class != object.getClass())
71             return false;
72         IntSet r = (IntSet)object;
73 //        System.out.println("equals " + this + " vs. " + r);
74         return sizeOrData == r.sizeOrData && Arrays.equals(data, r.data);
75     }
76
77
78     /**
79      * Returns the number of values in the list.
80      *
81      * @return the number of values in the list.
82      */
83     public int size() {
84         return data != null ? sizeOrData : (sizeOrData != NO_DATA ? 1 : 0);
85     }
86
87     /**
88      * Tests whether this list contains any values.
89      *
90      * @return true if the list is empty.
91      */
92     public boolean isEmpty() {
93         return sizeOrData == NO_DATA;
94     }
95
96     // modifying
97
98     /**
99      * Adds <tt>val</tt> to the end of the list, growing as needed.
100      *
101      * @param val an <code>int</code> value
102      */
103     public boolean add(int val) {
104         if(data == null) {
105             if(sizeOrData == val) return false;
106             if(sizeOrData == NO_DATA) {
107                 sizeOrData = val;
108                 return true;
109             } else {
110                 data = new int[DEFAULT_CAPACITY];
111                 data[0] = sizeOrData;
112                 data[1] = val;
113                 sizeOrData = 2;
114                 return true;
115             }
116         } else {
117             for(int i=0;i<sizeOrData;i++) if(data[i] == val) return false;
118             if(data.length == sizeOrData) {
119                 int newCap = data.length << 1;
120                 int[] tmp = new int[newCap];
121                 System.arraycopy(data, 0, tmp, 0, data.length);
122                 data = tmp;
123                 data[sizeOrData++] = val;
124             } else {
125                 data[sizeOrData++] = val;
126             }
127             return true;
128         }
129     }
130
131     public void forEach(TIntProcedure proc) {
132         if(data == null) {
133             if(sizeOrData != NO_DATA) proc.execute(sizeOrData);
134         } else {
135             for(int i = 0;i < sizeOrData ; i++) proc.execute(data[i]);
136         }
137     }
138     
139     public boolean contains(int val) {
140         if(data == null) {
141             return sizeOrData == val;
142         } else {
143             for(int i = 0;i < sizeOrData ; i++) if(data[i] == val) return true;
144         }
145         return false;
146     }
147
148     public void trim() {
149         if(data != null && sizeOrData < data.length) {
150             int newCap = sizeOrData;
151             int[] tmp = new int[newCap];
152             System.arraycopy(data, 0, tmp, 0, newCap);
153             data = tmp;
154             sizeOrData = newCap;
155         }
156     }
157
158     @Override
159     public boolean add(Resource e) {
160         return add(((ResourceImpl)e).id);
161     }
162
163     @Override
164     public boolean addAll(Collection<? extends Resource> c) {
165         boolean ret = false;
166         for (Resource r : c) {
167                 if (add(r))
168                         ret = true;
169         }
170         return ret;
171     }
172
173     @Override
174     public void clear() {
175         sizeOrData = NO_DATA;
176         data = null;
177     }
178
179     @Override
180     public boolean contains(Object o) {
181         assert(o != null);
182         return contains(((ResourceImpl)o).id);
183     }
184
185     @Override
186     public boolean containsAll(Collection<?> c) {
187         for (Object o : c) {
188                 if (!contains(o))
189                         return false;
190         }
191         return true;
192     }
193
194     @Override
195     public Iterator<Resource> iterator() {
196
197         class ArraySetIterator implements Iterator<Resource> {
198
199             int next = 0;
200
201             @Override
202             public boolean hasNext() {
203                 return next < size();
204             }
205
206             @Override
207             public Resource next() {
208                 if(size() == 1) {
209                     next++;
210                     return new ResourceImpl(support, sizeOrData);
211                 } else {
212                     return new ResourceImpl(support, data[next++]);
213                 }
214             }
215
216             @Override
217             public void remove() {
218                 throw new UnsupportedOperationException();
219             }
220
221         }
222
223         return new ArraySetIterator();
224
225     }
226
227     @Override
228     public boolean remove(Object o) {
229         throw new UnsupportedOperationException();
230     }
231
232     @Override
233     public boolean removeAll(Collection<?> c) {
234         throw new UnsupportedOperationException();
235     }
236
237     @Override
238     public boolean retainAll(Collection<?> c) {
239         throw new UnsupportedOperationException();
240     }
241
242     @Override
243     public Object[] toArray() {
244         throw new UnsupportedOperationException();
245     }
246
247     @Override
248     public <T> T[] toArray(T[] a) {
249         throw new UnsupportedOperationException();
250     }
251
252     @Override
253     public String toString() {
254         return "IntSet " + sizeOrData + " " + Arrays.toString(data);
255     }
256     
257     @Override
258     public boolean disjoint(Set<Resource> other) {
259         if(other instanceof ResourceSet) {
260             ResourceSet rs = (ResourceSet)other;
261             if(data == null) {
262                 return !rs.contains(sizeOrData);
263             } else {
264                 for(int i = 0;i < sizeOrData ; i++) 
265                     if(rs.contains(data[i])) return false;
266                 return true;
267             }
268         } else {
269             if(data == null) {
270                 return !other.contains(sizeOrData);
271             } else {
272                 for(int i = 0;i < sizeOrData ; i++) 
273                     if(other.contains(data[i])) return false;
274                 return true;
275             }
276         }
277     }
278
279 }