]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/TableIntArraySet.java
Ignore multiple modelled tests via context menu action
[simantics/platform.git] / bundles / org.simantics.db.procore / src / org / simantics / db / procore / cluster / TableIntArraySet.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.procore.cluster;
13
14 import org.simantics.db.Resource;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.impl.ClusterI;
17 import org.simantics.db.impl.IntAllocatorI;
18 import org.simantics.db.impl.Modifier;
19 import org.simantics.db.impl.ResourceImpl;
20 import org.simantics.db.impl.graph.ReadGraphImpl;
21 import org.simantics.db.procedure.AsyncContextMultiProcedure;
22 import org.simantics.db.procedure.AsyncMultiProcedure;
23
24 final class TableIntArraySet {
25     public static final int HeaderSize = 1; 
26     private static final int SIZE_OFFSET = -1;
27     static int create(final int[] ints, IntAllocatorI allocator)
28     throws DatabaseException {
29         final int LENGTH = ints.length*2;
30         if (LENGTH <1)
31             throw new DatabaseException("Illegal argument to create TableIntArraySet.");
32         int newBase = allocator.allocate(LENGTH + HeaderSize) + HeaderSize;
33         int[] table = allocator.getTable();
34         table[newBase + SIZE_OFFSET] = -LENGTH;
35         for (int i=0; i<ints.length; ++i) {
36             if (0 == ints[i])
37                 throw new DatabaseException("Illegal value to create TableIntArraySet.");
38             table[newBase+i] = ints[i];
39         }
40         for (int i=ints.length; i<LENGTH; ++i) {
41             table[newBase+i] = 0;
42         }
43         return newBase;
44     }
45     static boolean isArraySet(int[] table, int base) {
46         return table[base + SIZE_OFFSET] < 0;
47     }
48     static class Ints {
49         int[] ints;
50         boolean found;
51         Ints() {
52             this.ints = null;
53             this.found = false;
54         }
55     }
56     static Ints getIntsIfValueNotFound(int[] table, int base, int aValue) {
57         final int REAL_SIZE = -table[base + SIZE_OFFSET];
58         assert(REAL_SIZE > 0);
59         Ints it = new Ints();
60         int i;
61         for (i=0; i<REAL_SIZE; ++i) {
62             if (0 == table[base+i])
63                 break;
64             else if (aValue == table[base+i]) {
65                 it.found = true;
66                 return it;
67             }
68         }
69         int size = i;
70         assert(size > 0);
71         it.ints = new int[size+1];
72         for (i=0; i<size; ++i)
73             it.ints[i] = table[base+i];
74         it.ints[i] = aValue;
75         return it;
76     }
77     /**
78      * @param table
79      * @param base
80      * @param object
81      * @param allocator
82      * @return new base if object was actually added.
83      */
84     static int addInt(int[] table, int base, final int object, IntAllocatorI allocator) {
85         final int size = -table[base + SIZE_OFFSET];
86         assert(size > 0);
87         int i;
88         for (i=0; i<size; ++i) {
89             if (object == table[base+i])
90                 return 0;
91             else if (0 == table[base+i])
92                 break;
93         }
94         if (i < size) {
95             assert(0 == table[base+i]);
96             table[base+i] = object;
97             return base;
98         }
99         final int newSize = size + 1;
100         int newBase = allocator.allocate(newSize + HeaderSize) + HeaderSize;
101         int[] newTable = allocator.getTable();
102         newTable[newBase + SIZE_OFFSET] = -newSize;
103         System.arraycopy(table, base, newTable, newBase, size);
104         newTable[newBase+size] = object;
105         return newBase;
106     }       
107     
108     static int removeInt(int[] table, int base, int object) {
109         final int size = -table[base + SIZE_OFFSET];
110         assert(size > 0);
111         int i;
112         for (i=0; i<size; ++i)
113             if (object == table[base+i])
114                 break;
115             else if (0 == table[base+i])
116                 return i; // not found
117         if (i == size)
118             return i; // not found
119         int end = size - 1;
120         for (;end>i; --end)
121                 if (0 != table[base + end])
122                         break;
123         table[base + i] = table[base + end];
124         table[base + end] = 0;
125         return end;
126     }
127
128     static int removeIntLast(int[] table, int base)
129     throws DatabaseException {
130         final int size = getSize(table, base);
131         if (size != 1)
132             throw new DatabaseException("Illegal call of TableIntArraySet.removeLastint");
133         int t = table[base];
134         table[base] = 0;
135         return t;
136     }
137
138     static int getSize(int[] table, int base) {
139         final int size = -table[base + SIZE_OFFSET];
140         assert(size > 0);
141         int i;
142         for (i=0; i<size; ++i)
143             if (0 == table[base+i])
144                 break;
145         return i;
146     }
147     
148     static int getAllocatedSize(int[] table, int base) {
149         final int size = -table[base + SIZE_OFFSET];
150         assert(size>0);
151         return size + HeaderSize;
152     }
153     
154     static void foreachInt(final int[] table, final int base, ReadGraphImpl graph, AsyncMultiProcedure<Resource> procedure, Modifier modifier) throws DatabaseException {
155         
156         final int size = -table[base + SIZE_OFFSET];
157         assert(size>0);
158         for (int i=0; i<size; ++i) {
159
160                 int pRef = table[base+i];
161             if (0 == pRef)
162                 break;
163             int key = modifier.execute(pRef);
164             procedure.execute(graph, new ResourceImpl(graph.getResourceSupport(), key));
165             
166         }
167         
168                 procedure.finished(graph);
169 //              graph.state.dec(0);
170         
171     }
172
173     static <C> void foreachInt(final int[] table, final int base, ReadGraphImpl graph, C context, AsyncContextMultiProcedure<C, Resource> procedure, Modifier modifier) throws DatabaseException {
174         
175         final int size = -table[base + SIZE_OFFSET];
176         assert(size>0);
177         for (int i=0; i<size; ++i) {
178
179                 int pRef = table[base+i];
180             if (0 == pRef)
181                 break;
182             int key = modifier.execute(pRef);
183             procedure.execute(graph, context, new ResourceImpl(graph.getResourceSupport(), key));
184             
185         }
186         
187                 procedure.finished(graph);
188 //              graph.state.dec(0);
189         
190     }
191     
192     static int getSingleInt(final int[] table, final int base, Modifier modifier) throws DatabaseException {
193
194         int result = 0;
195         
196         final int size = -table[base + SIZE_OFFSET];
197         assert(size>0);
198         for (int i=0; i<size; ++i) {
199
200                 int pRef = table[base+i];
201             if (0 == pRef)
202                 break;
203             int key = modifier.execute(pRef);
204             if(result == 0) result = key;
205             else result = -1;
206             
207         }
208
209         return result;
210         
211 //        if(result == -1) return 0;
212 //        else return result;
213         
214     }
215
216     static <Context> boolean foreachInt(final int[] table, final int base
217                 , final ClusterI.ObjectProcedure<Context> procedure, final Context context, final Modifier modifier) throws DatabaseException {
218         final int size = -table[base + SIZE_OFFSET];
219         assert(size>0);
220         for (int i=0; i<size; ++i) {
221             int pRef = table[base+i];
222             if (0 == pRef)
223                 break;
224             int key;
225             if (null == modifier)
226                 key = pRef;
227             else
228                 key = modifier.execute(pRef);
229             if (procedure.execute(context, key))
230                 return true; // loop broken by procedure
231         }
232         return false; // loop finished
233     }
234 }
235