1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.procore.cluster;
\r
14 import org.simantics.db.procore.cluster.LongHash.AllocatorI;
\r
16 public class ClusterObjectSet {
\r
17 public static final int HeaderSize = LongHash.HeaderSize;
\r
18 private final AllocatorI allocator;
\r
19 private long[] longs;
\r
20 private int hashBase;
\r
21 public ClusterObjectSet(AllocatorI allocator) {
\r
22 this.allocator = allocator;
\r
23 longs = allocator.getLongs();
\r
24 hashBase = allocator.getHashBase();
\r
26 public ClusterObjectSet(AllocatorI allocator, int initialSize) {
\r
27 this.allocator = allocator;
\r
28 hashBase = LongHash.setUp(allocator, initialSize);
\r
29 longs = allocator.getLongs();
\r
31 public boolean add(long a) {
\r
32 if (LongHash.add(allocator, a)) {
\r
33 longs = allocator.getLongs();
\r
34 hashBase = allocator.getHashBase();
\r
39 public void clear() {
\r
40 LongHash.clear(longs, hashBase);
\r
42 public void compact() {
\r
43 LongHash.compact(allocator);
\r
44 longs = allocator.getLongs();
\r
45 hashBase = allocator.getHashBase();
\r
47 public boolean contains(long a) {
\r
48 return LongHash.contains(longs, hashBase, a);
\r
50 public boolean ensureSize(int size) {
\r
51 if (LongHash.ensureSize(allocator, size)) {
\r
52 longs = allocator.getLongs();
\r
53 hashBase = allocator.getHashBase();
\r
58 public int getCapacity() {
\r
59 return LongHash.getRealSize(longs, hashBase);
\r
61 public LongIterator getObjeccts() {
\r
62 return new LongIterator(allocator);
\r
64 public int getSize() {
\r
65 return LongHash.getUsedSize(longs, hashBase);
\r
67 public boolean isEmpty() {
\r
68 return LongHash.isEmpty(longs, hashBase);
\r
70 public boolean remove(long a) {
\r
71 return LongHash.remove(longs, hashBase, a);
\r
73 private static void test(boolean ensureOn, boolean dumpOn, final int addedLongs, final long longOffset) {
\r
74 String eo = ensureOn ? "on" : "off";
\r
75 System.out.println("********************************************");
\r
76 System.out.println("Set test with ensure "+ eo);
\r
77 long start = System.nanoTime();
\r
78 AllocatorTest allocator = new AllocatorTest(null, 0);
\r
79 ClusterObjectSet longSet = new ClusterObjectSet(allocator, 0);
\r
82 longSet.ensureSize(addedLongs);
\r
85 for (long i = 0; i < addedLongs; ++i) {
\r
86 if (!longSet.add(i + longOffset))
\r
87 throw new Error("Add failed.");
\r
90 double end = (System.nanoTime() - start) / 1000000.0;
\r
91 System.out.println("Elapsed time in milliseconds " + end + " for adding " + addedLongs + ".");
\r
96 start = System.nanoTime();
\r
97 for (long i = 0; i < addedLongs; ++i) {
\r
98 if (!longSet.contains(i + longOffset))
\r
99 throw new Error("Contain failed.");
\r
101 end = (System.nanoTime() - start) / 1000000.0;
\r
102 System.out.println("Elapsed time in milliseconds " + end + " for reading.");
\r
104 start = System.nanoTime();
\r
107 final long longSize = addedLongs + longOffset;
\r
108 final long freeLong = LongHash.setFree();
\r
109 LongIterator iterator = longSet.getObjeccts();
\r
110 while (freeLong != (next = iterator.next())) {
\r
112 if (next < longOffset || next >= longSize)
\r
113 throw new Error("Iterator failed.");
\r
115 end = (System.nanoTime() - start) / 1000000.0;
\r
116 System.out.println("Elapsed time in milliseconds " + end + " for looping.");
\r
118 start = System.nanoTime();
\r
119 for (long i = 0; i < addedLongs; ++i) {
\r
120 if (!longSet.remove(i + longOffset))
\r
121 throw new Error("Remove failed.");
\r
123 end = (System.nanoTime() - start) / 1000000.0;
\r
124 System.out.println("Elapsed time in milliseconds " + end + " for removing.");
\r
126 if (longSet.getSize() != 0)
\r
127 throw new Error("Element count not zero.");
\r
132 System.out.println("Real hash space consumption " + longSet.getCapacity());
\r
133 start = System.nanoTime();
\r
135 end = (System.nanoTime() - start) / 1000000.0;
\r
136 System.out.println("Elapsed time in " + end + " for compact.");
\r
137 System.out.println("Real hash space consumption " + longSet.getCapacity());
\r
142 System.out.println("********************************************");
\r
145 private static class AllocatorTest implements AllocatorI {
\r
146 private static final int HASH_OFFSET = 10;
\r
147 private long[] longs;
\r
148 private int hashBase;
\r
150 AllocatorTest(long[] longs, int hashBase) {
\r
153 public int allocate(int size) { // return hash base index
\r
154 hashBase = HASH_OFFSET + LongHash.HeaderSize;
\r
155 longs = new long[size + hashBase];
\r
156 return HASH_OFFSET + LongHash.HeaderSize;
\r
160 public final int getHashBase() {
\r
165 public final long[] getLongs() {
\r
170 for(int i=0; i<longs.length; ++i)
\r
171 System.out.println("longs[" + i + "]=" + longs[i]);
\r
172 System.out.println("longs capacity=" + longs.length);
\r
175 public static void main(String[] args) {
\r
176 final boolean dumpOn = true;
\r
177 final int addedLongs = 100; //00000;
\r
178 final long longOffset = 100;
\r
179 test(false, dumpOn, addedLongs, longOffset);
\r
180 test(true, dumpOn, addedLongs, longOffset);
\r