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.exception.DatabaseException;
15 import org.simantics.db.impl.ClusterI;
16 import org.simantics.db.impl.IntAllocatorI;
17 import org.simantics.db.impl.Modifier;
19 final class TableIntArraySet2 {
20 public static final int HeaderSize = 1;
21 private static final int SIZE_OFFSET = -1;
22 static int create(final int[] keys, final int[] vals, IntAllocatorI allocator)
23 throws DatabaseException {
24 final int LENGTH = keys.length;
25 if (LENGTH < 1 || LENGTH != vals.length)
26 throw new DatabaseException("Illegal argument to create TableIntArraySet2.");
27 int newBase = allocator.allocate(LENGTH*2 + HeaderSize) + HeaderSize;
28 int[] table = allocator.getTable();
29 table[newBase + SIZE_OFFSET] = -LENGTH;
30 for (int i=0; i<LENGTH; ++i) {
32 throw new DatabaseException("Illegal value to create TableIntArraySet2.");
33 table[newBase + i*2] = keys[i];
34 table[newBase + i*2 + 1] = vals[i];
38 static boolean isArraySet(int[] table, int base) {
39 return table[base + SIZE_OFFSET] < 0;
49 static Tables getInts(int[] table, int base) {
50 final int REAL_SIZE = -table[base + SIZE_OFFSET];
51 assert(REAL_SIZE > 0);
52 Tables t = new Tables();
54 for (i=0; i<REAL_SIZE; ++i) {
55 int k = table[base + i*2];
61 t.keys = new int[size];
62 t.vals = new int[size];
63 for (i=0; i<size; ++i){
64 t.keys[i] = table[base + i*2];
65 t.vals[i] = table[base + i*2 + 1];
69 static int get(int[] table, int base, int aKey) {
70 final int REAL_SIZE = -table[base + SIZE_OFFSET];
71 assert(REAL_SIZE > 0);
72 for (int i=0; i<REAL_SIZE; ++i, base += 2) {
75 else if (aKey == table[base]) {
76 return table[base + 1];
86 * @return new base if object was actually added.
88 static int addInt(int[] table, int base, final int key, int val, IntAllocatorI allocator) {
89 final int size = -table[base + SIZE_OFFSET];
92 for (i=0; i<size; ++i) {
93 if (key == table[base + i*2]) {
94 if (val == table[base + i*2 + 1])
97 table[base + i*2 + 1] = val;
100 } else if (0 == table[base + i*2])
104 assert(0 == table[base + i*2]);
105 table[base + i*2] = key;
106 table[base + i*2 + 1] = val;
109 final int newSize = size + 1;
110 int newBase = allocator.allocate(newSize*2 + HeaderSize) + HeaderSize;
111 int[] newTable = allocator.getTable();
112 newTable[newBase + SIZE_OFFSET] = -newSize;
113 System.arraycopy(table, base, newTable, newBase, size*2);
114 newTable[newBase + size*2] = key;
115 newTable[newBase + size*2 + 1] = val;
119 static boolean removeInt(int[] table, int base, int key) {
120 final int size = -table[base + SIZE_OFFSET];
123 for (i=0; i<size; ++i)
124 if (key == table[base + i*2])
126 else if (0 == table[base + i*2])
127 return false; // not found
129 return false; // not found
132 if (0 != table[base + end*2])
134 table[base + i*2] = table[base + end*2];
135 table[base + i*2 + 1] = table[base + end*2 + 1];
136 table[base + end*2] = 0;
137 table[base + end*2 + 1] = 0;
141 static int getSize(int[] table, int base) {
142 final int size = -table[base + SIZE_OFFSET];
145 for (i=0; i<size; ++i)
146 if (0 == table[base + i*2])
151 static int getAllocatedSize(int[] table, int base) {
152 final int size = -table[base + SIZE_OFFSET];
154 return size + HeaderSize;
157 static <Context> boolean foreachInt(final int[] table, final int base
158 , final ClusterI.PredicateProcedure<Context> procedure, final Context context, final Modifier modifier) throws DatabaseException {
159 final int size = -table[base + SIZE_OFFSET];
161 for (int i=0; i<size; ++i) {
162 int pRef = table[base + i*2];
166 if (null == modifier)
169 key = modifier.execute(pRef);
170 if (procedure.execute(context, key, table[base + i*2 + 1]))
171 return true; // loop broken by procedure
173 return false; // loop finished