]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/org/simantics/db/procore/cluster/ClusterObjectSet.java
Fail safe import fixes made by Antti
[simantics/platform.git] / bundles / org.simantics.db.procore / src / org / simantics / db / procore / cluster / ClusterObjectSet.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.procore.cluster.LongHash.AllocatorI;
15
16 public class ClusterObjectSet {
17         public static final int HeaderSize = LongHash.HeaderSize;
18         private final AllocatorI allocator;
19         private long[] longs;
20         private int hashBase;
21         public ClusterObjectSet(AllocatorI allocator) {
22                 this.allocator = allocator;
23                 longs = allocator.getLongs();
24                 hashBase = allocator.getHashBase();
25         }
26         public ClusterObjectSet(AllocatorI allocator, int initialSize) {
27                 this.allocator = allocator;
28                 hashBase = LongHash.setUp(allocator, initialSize);
29                 longs = allocator.getLongs();
30         }
31         public boolean add(long a) {
32                 if (LongHash.add(allocator, a)) {
33                         longs = allocator.getLongs();
34                         hashBase = allocator.getHashBase();
35                         return true;
36                 }
37                 return false;
38         }
39         public void clear() {
40                 LongHash.clear(longs, hashBase);
41         }
42         public void compact() {
43                 LongHash.compact(allocator);
44                 longs = allocator.getLongs();
45                 hashBase = allocator.getHashBase();
46         }
47         public boolean contains(long a) {
48                 return LongHash.contains(longs, hashBase, a);
49         }
50         public boolean ensureSize(int size) {
51                 if (LongHash.ensureSize(allocator, size)) {
52                         longs = allocator.getLongs();
53                         hashBase = allocator.getHashBase();
54                         return true;
55                 }
56                 return false;
57         }
58         public int getCapacity() {
59                 return LongHash.getRealSize(longs, hashBase);
60         }
61         public LongIterator getObjeccts() {
62                 return new LongIterator(allocator);
63         }
64         public int getSize() {
65                 return LongHash.getUsedSize(longs, hashBase);
66         }
67         public boolean isEmpty() {
68                 return LongHash.isEmpty(longs, hashBase);
69         }
70         public boolean remove(long a) {
71                 return LongHash.remove(longs, hashBase, a);
72         }
73     private static void test(boolean ensureOn, boolean dumpOn, final int addedLongs, final long longOffset) {
74         String eo = ensureOn ? "on" : "off";
75         System.out.println("********************************************");
76         System.out.println("Set test with ensure "+ eo);
77         long start = System.nanoTime();
78         AllocatorTest allocator = new AllocatorTest(null, 0);
79         ClusterObjectSet longSet = new ClusterObjectSet(allocator, 0);
80         
81         if (ensureOn) {
82                 longSet.ensureSize(addedLongs);
83         }
84
85         for (long i = 0; i < addedLongs; ++i) {
86                 if (!longSet.add(i + longOffset))
87                         throw new Error("Add failed.");
88         }
89         
90         double end = (System.nanoTime() - start) / 1000000.0;
91         System.out.println("Elapsed time in milliseconds " + end + " for adding " + addedLongs + ".");
92
93         if (dumpOn)
94                 allocator.dump();
95         
96         start = System.nanoTime();
97         for (long i = 0; i < addedLongs; ++i) {
98                 if (!longSet.contains(i + longOffset))
99                         throw new Error("Contain failed.");
100         }
101         end = (System.nanoTime() - start) / 1000000.0; 
102         System.out.println("Elapsed time in milliseconds " + end + " for reading.");
103
104         start = System.nanoTime();
105         //int count = 0;
106         long next;
107         final long longSize = addedLongs + longOffset;
108         final long freeLong = LongHash.setFree();
109         LongIterator iterator = longSet.getObjeccts();
110         while (freeLong != (next = iterator.next())) {
111                 //++count;
112                 if (next < longOffset || next >= longSize)
113                         throw new Error("Iterator failed.");
114         }
115         end = (System.nanoTime() - start) / 1000000.0; 
116         System.out.println("Elapsed time in milliseconds " + end + " for looping.");
117
118         start = System.nanoTime();
119         for (long i = 0; i < addedLongs; ++i) {
120                 if (!longSet.remove(i + longOffset))
121                         throw new Error("Remove failed.");
122         } 
123         end = (System.nanoTime() - start) / 1000000.0;
124         System.out.println("Elapsed time in milliseconds " + end + " for removing.");
125         
126         if (longSet.getSize() != 0)
127                 throw new Error("Element count not zero.");
128
129         if (dumpOn)
130                 allocator.dump();
131
132         System.out.println("Real hash space consumption " + longSet.getCapacity());
133         start = System.nanoTime();
134         longSet.compact();
135         end = (System.nanoTime() - start) / 1000000.0;
136         System.out.println("Elapsed time in      " + end + " for compact.");
137         System.out.println("Real hash space consumption " + longSet.getCapacity());
138
139         if (dumpOn)
140                 allocator.dump();
141
142         System.out.println("********************************************");
143     }
144         
145     private static class AllocatorTest implements AllocatorI {
146         private static final int HASH_OFFSET = 10;
147         private long[] longs;
148         private int hashBase;
149
150         AllocatorTest(long[] longs, int hashBase) {
151         }
152         @Override
153                 public int allocate(int size) { // return hash base index
154                         hashBase = HASH_OFFSET + LongHash.HeaderSize;
155                         longs = new long[size + hashBase];
156                         return HASH_OFFSET + LongHash.HeaderSize;
157                 }
158
159                 @Override
160                 public final int getHashBase() {
161                         return hashBase;
162                 }
163
164                 @Override
165                 public final long[] getLongs() {
166                         return longs;
167                 }
168         
169             void dump() {
170                 for(int i=0; i<longs.length; ++i)
171                     System.out.println("longs[" + i + "]=" + longs[i]);
172                 System.out.println("longs capacity=" + longs.length);
173             }
174   }
175     public static void main(String[] args) {
176         final boolean dumpOn = true;
177         final int addedLongs = 100; //00000;
178         final long longOffset = 100;
179         test(false, dumpOn, addedLongs, longOffset);
180         test(true, dumpOn, addedLongs, longOffset);
181     }
182    
183 }