1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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.procore.cluster;
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;
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;
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) {
37 throw new DatabaseException("Illegal value to create TableIntArraySet.");
38 table[newBase+i] = ints[i];
40 for (int i=ints.length; i<LENGTH; ++i) {
45 static boolean isArraySet(int[] table, int base) {
46 return table[base + SIZE_OFFSET] < 0;
56 static Ints getIntsIfValueNotFound(int[] table, int base, int aValue) {
57 final int REAL_SIZE = -table[base + SIZE_OFFSET];
58 assert(REAL_SIZE > 0);
61 for (i=0; i<REAL_SIZE; ++i) {
62 if (0 == table[base+i])
64 else if (aValue == table[base+i]) {
71 it.ints = new int[size+1];
72 for (i=0; i<size; ++i)
73 it.ints[i] = table[base+i];
82 * @return new base if object was actually added.
84 static int addInt(int[] table, int base, final int object, IntAllocatorI allocator) {
85 final int size = -table[base + SIZE_OFFSET];
88 for (i=0; i<size; ++i) {
89 if (object == table[base+i])
91 else if (0 == table[base+i])
95 assert(0 == table[base+i]);
96 table[base+i] = object;
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;
108 static int removeInt(int[] table, int base, int object) {
109 final int size = -table[base + SIZE_OFFSET];
112 for (i=0; i<size; ++i)
113 if (object == table[base+i])
115 else if (0 == table[base+i])
116 return i; // not found
118 return i; // not found
121 if (0 != table[base + end])
123 table[base + i] = table[base + end];
124 table[base + end] = 0;
128 static int removeIntLast(int[] table, int base)
129 throws DatabaseException {
130 final int size = getSize(table, base);
132 throw new DatabaseException("Illegal call of TableIntArraySet.removeLastint");
138 static int getSize(int[] table, int base) {
139 final int size = -table[base + SIZE_OFFSET];
142 for (i=0; i<size; ++i)
143 if (0 == table[base+i])
148 static int getAllocatedSize(int[] table, int base) {
149 final int size = -table[base + SIZE_OFFSET];
151 return size + HeaderSize;
154 static void foreachInt(final int[] table, final int base, ReadGraphImpl graph, AsyncMultiProcedure<Resource> procedure, Modifier modifier) throws DatabaseException {
156 final int size = -table[base + SIZE_OFFSET];
158 for (int i=0; i<size; ++i) {
160 int pRef = table[base+i];
163 int key = modifier.execute(pRef);
164 procedure.execute(graph, new ResourceImpl(graph.getResourceSupport(), key));
168 procedure.finished(graph);
169 // graph.state.dec(0);
173 static <C> void foreachInt(final int[] table, final int base, ReadGraphImpl graph, C context, AsyncContextMultiProcedure<C, Resource> procedure, Modifier modifier) throws DatabaseException {
175 final int size = -table[base + SIZE_OFFSET];
177 for (int i=0; i<size; ++i) {
179 int pRef = table[base+i];
182 int key = modifier.execute(pRef);
183 procedure.execute(graph, context, new ResourceImpl(graph.getResourceSupport(), key));
187 procedure.finished(graph, context);
188 // graph.state.dec(0);
192 static int getSingleInt(final int[] table, final int base, Modifier modifier) throws DatabaseException {
196 final int size = -table[base + SIZE_OFFSET];
198 for (int i=0; i<size; ++i) {
200 int pRef = table[base+i];
203 int key = modifier.execute(pRef);
204 if(result == 0) result = key;
211 // if(result == -1) return 0;
212 // else return result;
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];
220 for (int i=0; i<size; ++i) {
221 int pRef = table[base+i];
225 if (null == modifier)
228 key = modifier.execute(pRef);
229 if (procedure.execute(context, key))
230 return true; // loop broken by procedure
232 return false; // loop finished