2006-06-20 23:41:12 +02:00
|
|
|
// kelondroIntBytesMap.java
|
2006-06-18 13:33:26 +02:00
|
|
|
// (C) 2006 by Michael Peter Christen; mc@anomic.de, Frankfurt a. M., Germany
|
2006-06-20 23:41:12 +02:00
|
|
|
// first published 08.06.2006 on http://www.anomic.de
|
2006-06-18 13:33:26 +02:00
|
|
|
//
|
|
|
|
// $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
|
|
|
|
// $LastChangedRevision: 1986 $
|
|
|
|
// $LastChangedBy: orbiter $
|
|
|
|
//
|
|
|
|
// LICENSE
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
package de.anomic.kelondro;
|
|
|
|
|
2007-04-19 15:37:02 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashSet;
|
2006-10-16 17:04:16 +02:00
|
|
|
import java.util.Iterator;
|
2007-04-19 15:37:02 +02:00
|
|
|
import java.util.Random;
|
2006-06-30 01:01:42 +02:00
|
|
|
|
2006-10-16 17:04:16 +02:00
|
|
|
public class kelondroIntBytesMap {
|
2006-06-18 13:33:26 +02:00
|
|
|
|
2007-03-21 14:26:18 +01:00
|
|
|
private kelondroRowSet index;
|
2006-10-16 17:04:16 +02:00
|
|
|
|
2006-06-20 23:41:12 +02:00
|
|
|
public kelondroIntBytesMap(int payloadSize, int initSize) {
|
2006-12-06 04:02:57 +01:00
|
|
|
index = new kelondroRowSet(new kelondroRow("Cardinal key-4 {b256}, byte[] payload-" + payloadSize, kelondroNaturalOrder.naturalOrder, 0), initSize);
|
2006-10-16 17:04:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int size() {
|
2007-03-21 14:26:18 +01:00
|
|
|
return index.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
public long memoryNeededForGrow() {
|
|
|
|
return index.memoryNeededForGrow();
|
2006-06-18 13:33:26 +02:00
|
|
|
}
|
|
|
|
|
2006-06-20 23:41:12 +02:00
|
|
|
public byte[] getb(int ii) {
|
2006-10-16 17:04:16 +02:00
|
|
|
kelondroRow.Entry indexentry;
|
2007-03-21 14:26:18 +01:00
|
|
|
indexentry = index.get(kelondroNaturalOrder.encodeLong((long) ii, 4));
|
2006-06-18 13:33:26 +02:00
|
|
|
if (indexentry == null) return null;
|
|
|
|
return indexentry.getColBytes(1);
|
|
|
|
}
|
|
|
|
|
2006-10-16 17:04:16 +02:00
|
|
|
public void addb(int ii, byte[] value) {
|
|
|
|
kelondroRow.Entry newentry;
|
2007-03-21 14:26:18 +01:00
|
|
|
newentry = index.row().newEntry();
|
|
|
|
newentry.setCol(0, (long) ii);
|
|
|
|
newentry.setCol(1, value);
|
|
|
|
index.addUnique(newentry);
|
2006-10-16 17:04:16 +02:00
|
|
|
}
|
|
|
|
|
2006-06-20 23:41:12 +02:00
|
|
|
public byte[] putb(int ii, byte[] value) {
|
2006-10-16 17:04:16 +02:00
|
|
|
kelondroRow.Entry newentry;
|
2007-03-21 14:26:18 +01:00
|
|
|
newentry = index.row().newEntry();
|
|
|
|
newentry.setCol(0, (long) ii);
|
|
|
|
newentry.setCol(1, value);
|
|
|
|
kelondroRow.Entry oldentry = index.put(newentry);
|
|
|
|
if (oldentry == null) return null;
|
|
|
|
return oldentry.getColBytes(1);
|
2006-06-18 13:33:26 +02:00
|
|
|
}
|
2006-10-16 02:27:25 +02:00
|
|
|
|
2006-06-20 23:41:12 +02:00
|
|
|
public byte[] removeb(int ii) {
|
2007-03-21 14:26:18 +01:00
|
|
|
if (index.size() == 0) return null;
|
|
|
|
kelondroRow.Entry indexentry = index.remove(kelondroNaturalOrder.encodeLong((long) ii, 4));
|
|
|
|
if (indexentry == null) return null;
|
|
|
|
return indexentry.getColBytes(1);
|
2006-10-16 17:04:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] removeoneb() {
|
2007-03-21 14:26:18 +01:00
|
|
|
if (index.size() == 0) return null;
|
|
|
|
kelondroRow.Entry indexentry = index.removeOne();
|
|
|
|
if (indexentry == null) return null;
|
|
|
|
return indexentry.getColBytes(1);
|
2006-10-16 17:04:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Iterator rows() {
|
2007-03-21 14:26:18 +01:00
|
|
|
return index.rows(true, null);
|
2006-10-16 17:04:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void flush() {
|
2006-10-26 15:50:50 +02:00
|
|
|
if (index instanceof kelondroRowSet) {
|
2007-03-14 09:55:05 +01:00
|
|
|
((kelondroRowSet) index).sort();
|
2007-03-08 17:15:40 +01:00
|
|
|
((kelondroRowSet) index).trim(true);
|
2006-10-26 15:50:50 +02:00
|
|
|
}
|
2006-10-16 17:04:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public kelondroProfile profile() {
|
|
|
|
return index.profile();
|
2006-06-18 13:33:26 +02:00
|
|
|
}
|
|
|
|
|
2006-06-30 01:01:42 +02:00
|
|
|
public static void main(String[] args) {
|
2007-04-19 15:37:02 +02:00
|
|
|
boolean assertEnabled = false;
|
|
|
|
assert assertEnabled = true;
|
|
|
|
System.out.println((assertEnabled) ? "asserts are enabled" : "enable asserts with 'java -ea'; not enabled yet");
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
long randomstart = 0;
|
|
|
|
Random random = new Random(randomstart);
|
|
|
|
long r;
|
|
|
|
Long R;
|
|
|
|
int p, rc = 0;
|
|
|
|
ArrayList ra = new ArrayList();
|
|
|
|
HashSet jcontrol = new HashSet();
|
|
|
|
kelondroIntBytesMap kcontrol = new kelondroIntBytesMap(1, 0);
|
|
|
|
for (int i = 0; i < 1000000; i++) {
|
|
|
|
r = Math.abs(random.nextLong() % 10000);
|
|
|
|
//System.out.println("add " + r);
|
|
|
|
jcontrol.add(new Long(r));
|
|
|
|
kcontrol.putb((int) r, "x".getBytes());
|
|
|
|
if (random.nextLong() % 5 == 0) ra.add(new Long(r));
|
|
|
|
if ((ra.size() > 0) && (random.nextLong() % 7 == 0)) {
|
|
|
|
rc++;
|
|
|
|
p = Math.abs(random.nextInt()) % ra.size();
|
|
|
|
R = (Long) ra.get(p);
|
|
|
|
//System.out.println("remove " + R.longValue());
|
|
|
|
jcontrol.remove(R);
|
|
|
|
kcontrol.removeb((int) R.longValue());
|
|
|
|
assert kcontrol.removeb((int) R.longValue()) == null;
|
|
|
|
}
|
|
|
|
assert jcontrol.size() == kcontrol.size();
|
|
|
|
}
|
|
|
|
System.out.println("removed: " + rc + ", size of jcontrol set: "
|
|
|
|
+ jcontrol.size() + ", size of kcontrol set: "
|
|
|
|
+ kcontrol.size());
|
|
|
|
System.out.println("Time: "
|
|
|
|
+ ((System.currentTimeMillis() - start) / 1000) + " seconds");
|
|
|
|
}
|
2006-06-30 01:01:42 +02:00
|
|
|
|
2006-06-18 13:33:26 +02:00
|
|
|
}
|