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