I think it would be a complete waste of time to use any form of HashMap
. I am guessing you are calculating multi-byte hashes of various data, these are already hash
es, there is no need to perform any more hashing on them.
Although you do not state it, I am guessing your hashes are byte
sequences. Clearly either a trie or a dawg would be ideal to store these.
I would suggest therefore you implement a trie/dawg
and use it to store all of the hashes in the first array. You could then use all of your computing power in parallel to lookup each element in your second array in this trie
. No locks would be required.
Added
Here's a simple Dawg
implementation I knocked together. It seems to work.
public class Dawg {
// All my children.
Dawg[] children = new Dawg[256];
// Am I a leaf.
boolean isLeaf = false;
// Add a new word.
public void add ( byte[] word ) {
// Finds its location, growing as necessary.
Dawg loc = find ( word, 0, true );
loc.isLeaf = true;
}
// String form.
public void add ( String word ) {
add(word.getBytes());
}
// Returns true if word is in the dawg.
public boolean contains ( byte [] word ) {
// Finds its location, no growing allowed.
Dawg d = find ( word, 0, false );
return d != null && d.isLeaf;
}
// String form.
public boolean contains ( String word ) {
return contains(word.getBytes());
}
// Find the Dawg - growing the tree as necessary if requested.
private Dawg find ( byte [] word, int i, boolean grow ) {
Dawg child = children[word[i]];
if ( child == null ) {
// Not present!
if ( grow ) {
// Grow the tree.
child = new Dawg();
children[word[i]] = child;
}
}
// Found it?
if ( child != null ) {
// More to find?
if ( i < word.length - 1 ) {
child = child.find(word, i+1, grow);
}
}
return child;
}
public static void main ( String[] args ) {
Dawg d = new Dawg();
d.add("H");
d.add("Hello");
d.add("World");
d.add("Hell");
System.out.println("Hello is "+(d.contains("Hello")?"in":"out"));
System.out.println("World is "+(d.contains("World")?"in":"out"));
System.out.println("Hell is "+(d.contains("Hell")?"in":"out"));
System.out.println("Hal is "+(d.contains("Hal")?"in":"out"));
System.out.println("Hel is "+(d.contains("Hel")?"in":"out"));
System.out.println("H is "+(d.contains("H")?"in":"out"));
}
}
Added
This could be a good start at a concurrent lock-free version. These things are notoriously difficult to test so I cannot guarantee this will work but to my mind it certainly should.
import java.util.concurrent.atomic.AtomicReferenceArray;
public class LFDawg {
// All my children.
AtomicReferenceArray<LFDawg> children = new AtomicReferenceArray<LFDawg> ( 256 );
// Am I a leaf.
boolean isLeaf = false;
// Add a new word.
public void add ( byte[] word ) {
// Finds its location, growing as necessary.
LFDawg loc = find( word, 0, true );
loc.isLeaf = true;
}
// String form.
public void add ( String word ) {
add( word.getBytes() );
}
// Returns true if word is in the dawg.
public boolean contains ( byte[] word ) {
// Finds its location, no growing allowed.
LFDawg d = find( word, 0, false );
return d != null && d.isLeaf;
}
// String form.
public boolean contains ( String word ) {
return contains( word.getBytes() );
}
// Find the Dawg - growing the tree as necessary if requested.
private LFDawg find ( byte[] word, int i, boolean grow ) {
LFDawg child = children.get( word[i] );
if ( child == null ) {
// Not present!
if ( grow ) {
// Grow the tree.
child = new LFDawg();
if ( !children.compareAndSet( word[i], null, child ) ) {
// Someone else got there before me. Get the one they set.
child = children.get( word[i] );
}
}
}
// Found it?
if ( child != null ) {
// More to find?
if ( i < word.length - 1 ) {
child = child.find( word, i + 1, grow );
}
}
return child;
}
public static void main ( String[] args ) {
LFDawg d = new LFDawg();
d.add( "H" );
d.add( "Hello" );
d.add( "World" );
d.add( "Hell" );
System.out.println( "Hello is " + ( d.contains( "Hello" ) ? "in" : "out" ) );
System.out.println( "World is " + ( d.contains( "World" ) ? "in" : "out" ) );
System.out.println( "Hell is " + ( d.contains( "Hell" ) ? "in" : "out" ) );
System.out.println( "Hal is " + ( d.contains( "Hal" ) ? "in" : "out" ) );
System.out.println( "Hel is " + ( d.contains( "Hel" ) ? "in" : "out" ) );
System.out.println( "H is " + ( d.contains( "H" ) ? "in" : "out" ) );
}
}
byte[]
. They are the result of a hash-function like SHA or MD5. No, the arrays aren't sorted. Sorting and merging them would need O(n log n) for sorting and O(n + m) for merging. I hoped for a higher efficiency. – Rave