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.procore.cluster.LongHash.AllocatorI;
16 public class ClusterObjectSet {
17 public static final int HeaderSize = LongHash.HeaderSize;
18 private final AllocatorI allocator;
21 public ClusterObjectSet(AllocatorI allocator) {
22 this.allocator = allocator;
23 longs = allocator.getLongs();
24 hashBase = allocator.getHashBase();
26 public ClusterObjectSet(AllocatorI allocator, int initialSize) {
27 this.allocator = allocator;
28 hashBase = LongHash.setUp(allocator, initialSize);
29 longs = allocator.getLongs();
31 public boolean add(long a) {
32 if (LongHash.add(allocator, a)) {
33 longs = allocator.getLongs();
34 hashBase = allocator.getHashBase();
40 LongHash.clear(longs, hashBase);
42 public void compact() {
43 LongHash.compact(allocator);
44 longs = allocator.getLongs();
45 hashBase = allocator.getHashBase();
47 public boolean contains(long a) {
48 return LongHash.contains(longs, hashBase, a);
50 public boolean ensureSize(int size) {
51 if (LongHash.ensureSize(allocator, size)) {
52 longs = allocator.getLongs();
53 hashBase = allocator.getHashBase();
58 public int getCapacity() {
59 return LongHash.getRealSize(longs, hashBase);
61 public LongIterator getObjeccts() {
62 return new LongIterator(allocator);
64 public int getSize() {
65 return LongHash.getUsedSize(longs, hashBase);
67 public boolean isEmpty() {
68 return LongHash.isEmpty(longs, hashBase);
70 public boolean remove(long a) {
71 return LongHash.remove(longs, hashBase, a);
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);
82 longSet.ensureSize(addedLongs);
85 for (long i = 0; i < addedLongs; ++i) {
86 if (!longSet.add(i + longOffset))
87 throw new Error("Add failed.");
90 double end = (System.nanoTime() - start) / 1000000.0;
91 System.out.println("Elapsed time in milliseconds " + end + " for adding " + addedLongs + ".");
96 start = System.nanoTime();
97 for (long i = 0; i < addedLongs; ++i) {
98 if (!longSet.contains(i + longOffset))
99 throw new Error("Contain failed.");
101 end = (System.nanoTime() - start) / 1000000.0;
102 System.out.println("Elapsed time in milliseconds " + end + " for reading.");
104 start = System.nanoTime();
107 final long longSize = addedLongs + longOffset;
108 final long freeLong = LongHash.setFree();
109 LongIterator iterator = longSet.getObjeccts();
110 while (freeLong != (next = iterator.next())) {
112 if (next < longOffset || next >= longSize)
113 throw new Error("Iterator failed.");
115 end = (System.nanoTime() - start) / 1000000.0;
116 System.out.println("Elapsed time in milliseconds " + end + " for looping.");
118 start = System.nanoTime();
119 for (long i = 0; i < addedLongs; ++i) {
120 if (!longSet.remove(i + longOffset))
121 throw new Error("Remove failed.");
123 end = (System.nanoTime() - start) / 1000000.0;
124 System.out.println("Elapsed time in milliseconds " + end + " for removing.");
126 if (longSet.getSize() != 0)
127 throw new Error("Element count not zero.");
132 System.out.println("Real hash space consumption " + longSet.getCapacity());
133 start = System.nanoTime();
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());
142 System.out.println("********************************************");
145 private static class AllocatorTest implements AllocatorI {
146 private static final int HASH_OFFSET = 10;
147 private long[] longs;
148 private int hashBase;
150 AllocatorTest(long[] longs, int hashBase) {
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;
160 public final int getHashBase() {
165 public final long[] getLongs() {
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);
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);