--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.procore.cluster;\r
+\r
+import org.simantics.db.procore.cluster.LongHash.AllocatorI;\r
+\r
+public class ClusterObjectSet {\r
+ public static final int HeaderSize = LongHash.HeaderSize;\r
+ private final AllocatorI allocator;\r
+ private long[] longs;\r
+ private int hashBase;\r
+ public ClusterObjectSet(AllocatorI allocator) {\r
+ this.allocator = allocator;\r
+ longs = allocator.getLongs();\r
+ hashBase = allocator.getHashBase();\r
+ }\r
+ public ClusterObjectSet(AllocatorI allocator, int initialSize) {\r
+ this.allocator = allocator;\r
+ hashBase = LongHash.setUp(allocator, initialSize);\r
+ longs = allocator.getLongs();\r
+ }\r
+ public boolean add(long a) {\r
+ if (LongHash.add(allocator, a)) {\r
+ longs = allocator.getLongs();\r
+ hashBase = allocator.getHashBase();\r
+ return true;\r
+ }\r
+ return false;\r
+ }\r
+ public void clear() {\r
+ LongHash.clear(longs, hashBase);\r
+ }\r
+ public void compact() {\r
+ LongHash.compact(allocator);\r
+ longs = allocator.getLongs();\r
+ hashBase = allocator.getHashBase();\r
+ }\r
+ public boolean contains(long a) {\r
+ return LongHash.contains(longs, hashBase, a);\r
+ }\r
+ public boolean ensureSize(int size) {\r
+ if (LongHash.ensureSize(allocator, size)) {\r
+ longs = allocator.getLongs();\r
+ hashBase = allocator.getHashBase();\r
+ return true;\r
+ }\r
+ return false;\r
+ }\r
+ public int getCapacity() {\r
+ return LongHash.getRealSize(longs, hashBase);\r
+ }\r
+ public LongIterator getObjeccts() {\r
+ return new LongIterator(allocator);\r
+ }\r
+ public int getSize() {\r
+ return LongHash.getUsedSize(longs, hashBase);\r
+ }\r
+ public boolean isEmpty() {\r
+ return LongHash.isEmpty(longs, hashBase);\r
+ }\r
+ public boolean remove(long a) {\r
+ return LongHash.remove(longs, hashBase, a);\r
+ }\r
+ private static void test(boolean ensureOn, boolean dumpOn, final int addedLongs, final long longOffset) {\r
+ String eo = ensureOn ? "on" : "off";\r
+ System.out.println("********************************************");\r
+ System.out.println("Set test with ensure "+ eo);\r
+ long start = System.nanoTime();\r
+ AllocatorTest allocator = new AllocatorTest(null, 0);\r
+ ClusterObjectSet longSet = new ClusterObjectSet(allocator, 0);\r
+ \r
+ if (ensureOn) {\r
+ longSet.ensureSize(addedLongs);\r
+ }\r
+\r
+ for (long i = 0; i < addedLongs; ++i) {\r
+ if (!longSet.add(i + longOffset))\r
+ throw new Error("Add failed.");\r
+ }\r
+ \r
+ double end = (System.nanoTime() - start) / 1000000.0;\r
+ System.out.println("Elapsed time in milliseconds " + end + " for adding " + addedLongs + ".");\r
+\r
+ if (dumpOn)\r
+ allocator.dump();\r
+ \r
+ start = System.nanoTime();\r
+ for (long i = 0; i < addedLongs; ++i) {\r
+ if (!longSet.contains(i + longOffset))\r
+ throw new Error("Contain failed.");\r
+ }\r
+ end = (System.nanoTime() - start) / 1000000.0; \r
+ System.out.println("Elapsed time in milliseconds " + end + " for reading.");\r
+\r
+ start = System.nanoTime();\r
+ //int count = 0;\r
+ long next;\r
+ final long longSize = addedLongs + longOffset;\r
+ final long freeLong = LongHash.setFree();\r
+ LongIterator iterator = longSet.getObjeccts();\r
+ while (freeLong != (next = iterator.next())) {\r
+ //++count;\r
+ if (next < longOffset || next >= longSize)\r
+ throw new Error("Iterator failed.");\r
+ }\r
+ end = (System.nanoTime() - start) / 1000000.0; \r
+ System.out.println("Elapsed time in milliseconds " + end + " for looping.");\r
+\r
+ start = System.nanoTime();\r
+ for (long i = 0; i < addedLongs; ++i) {\r
+ if (!longSet.remove(i + longOffset))\r
+ throw new Error("Remove failed.");\r
+ } \r
+ end = (System.nanoTime() - start) / 1000000.0;\r
+ System.out.println("Elapsed time in milliseconds " + end + " for removing.");\r
+ \r
+ if (longSet.getSize() != 0)\r
+ throw new Error("Element count not zero.");\r
+\r
+ if (dumpOn)\r
+ allocator.dump();\r
+\r
+ System.out.println("Real hash space consumption " + longSet.getCapacity());\r
+ start = System.nanoTime();\r
+ longSet.compact();\r
+ end = (System.nanoTime() - start) / 1000000.0;\r
+ System.out.println("Elapsed time in " + end + " for compact.");\r
+ System.out.println("Real hash space consumption " + longSet.getCapacity());\r
+\r
+ if (dumpOn)\r
+ allocator.dump();\r
+\r
+ System.out.println("********************************************");\r
+ }\r
+ \r
+ private static class AllocatorTest implements AllocatorI {\r
+ private static final int HASH_OFFSET = 10;\r
+ private long[] longs;\r
+ private int hashBase;\r
+\r
+ AllocatorTest(long[] longs, int hashBase) {\r
+ }\r
+ @Override\r
+ public int allocate(int size) { // return hash base index\r
+ hashBase = HASH_OFFSET + LongHash.HeaderSize;\r
+ longs = new long[size + hashBase];\r
+ return HASH_OFFSET + LongHash.HeaderSize;\r
+ }\r
+\r
+ @Override\r
+ public final int getHashBase() {\r
+ return hashBase;\r
+ }\r
+\r
+ @Override\r
+ public final long[] getLongs() {\r
+ return longs;\r
+ }\r
+ \r
+ void dump() {\r
+ for(int i=0; i<longs.length; ++i)\r
+ System.out.println("longs[" + i + "]=" + longs[i]);\r
+ System.out.println("longs capacity=" + longs.length);\r
+ }\r
+ }\r
+ public static void main(String[] args) {\r
+ final boolean dumpOn = true;\r
+ final int addedLongs = 100; //00000;\r
+ final long longOffset = 100;\r
+ test(false, dumpOn, addedLongs, longOffset);\r
+ test(true, dumpOn, addedLongs, longOffset);\r
+ }\r
+ \r
+}\r