Skip to content

Instantly share code, notes, and snippets.

Created August 1, 2012 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3230417 to your computer and use it in GitHub Desktop.
Save anonymous/3230417 to your computer and use it in GitHub Desktop.
BitSet are added
import java.util.* ;
class Node {
public int data;
public Node next;
public Node(int data, Node next) {
this.data = data; this.next = next;
}
}
class LongIntParallelHashMultimap {
Node[] l ;
BitSet bits = new BitSet(Integer.MAX_VALUE);
public LongIntParallelHashMultimap() {
l = new Node[Integer.MAX_VALUE];
}
public void load(){
}
public void put(int key, int value) {
bits.set(key , true) ; //bitset to find which linked list are filled up
// Assume ordering is significant, for now.
Node prev = l[key];
if (prev == null) {
l[key] = new Node(value, null);
} else {
while (prev.next != null) {
prev = prev.next;
}
prev.next = new Node(value, null);
}
}
public int[] get(int key) {
// Re-counting the values every time is...probably actually better than storing the counts.
// Based on the listed RAM constraints, we almost certainly cannot afford the overhead of
// the LinkedList objects.
// Anyway, with an average count around 3, this shouldn't be a significant hit.
int count = 0;
for (Node n = l[key]; n != null; n = n.next) {
count++;
}
int[] result = new int[count];
int i = 0;
for (Node n = l[key]; n != null; n = n.next) {
result[i++] = n.data;
}
return result;
}
public void save() {
}
}
public class Test{
public static void main(String args[]) {
try {
long startTime = System.nanoTime();
LongIntParallelHashMultimap lph = new LongIntParallelHashMultimap();
long endTime = System.nanoTime() - startTime;
System.out.println(endTime);
startTime = System.nanoTime();
for (int i= 0 ; i < 2500000 ; i++)
lph.put(i, i+100);
endTime = System.nanoTime() - startTime;
System.out.println(endTime);
startTime = System.nanoTime();
startTime = System.nanoTime();
for (int i= 0 ; i < 100 ; i++){
int k[] = lph.get(i);
System.out.print(k[0] + " ");
}
endTime = System.nanoTime() - startTime;
System.out.println(endTime);
startTime = System.nanoTime();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment