Debian Codegraph @

current/structures/LongHeapSet.java

raw symbols SCIP
1 package structures;
2
3 /**
4 * Maintains a heap of unique values.
5 * @author Brian Bushnell
6 * @date July 6, 2016
7 *
8 */
9 public class LongHeapSet implements LongHeapSetInterface {
10
11 public LongHeapSet(int limit_){
12 limit=limit_;
13 heap=new LongHeap(limit, true);
14 set=new LongHashSet(limit*2);
15 }
16
17 @Override
18 public boolean add(long value){
19 assert(heap.size()==set.size());
20 if(heap.hasRoom()){
21 if(set.add(value)){
22 heap.add(value);
23 assert(heap.size()==set.size());
24 return true;
25 }
26 assert(heap.size()==set.size());
27 return false;
28 }
29
30 final long bottom=heap.peek();
31 if(value>bottom){
32 if(set.add(value)){
33 set.remove(bottom);
34 assert(set.size()<=limit);
35 heap.add(value);
36 assert(heap.size()<=limit);
37 assert(heap.size()==set.size());
38 return true;
39 }
40 }
41 assert(heap.size()==set.size());
42 return false;
43 }
44
45 @Override
46 public int increment(long key, int incr) {
47 return add(key) ? 1 : 0; //Not quite correct...
48 }
49
50 @Override
51 public void clear(){
52 assert(heap.size()==set.size()) : heap.size()+", "+set.size();
53 if(heap.size()<1){
54 assert(set.size()<1) : heap.size()+", "+set.size();
55 return;
56 }
57 heap.clear();
58 set.clear();
59 assert(heap.size()==set.size());
60 }
61
62 public void add(LongHeapSet b){
63 assert(heap.size()==set.size());
64 final long[] array=b.heap.array();
65 final int blen=b.heap.size();
66 for(int i=1; i<=blen; i++){
67 add(array[i]);
68 }
69 assert(heap.size()==set.size());
70 }
71
72 @Override
73 public int size(){
74 assert(heap.size()==set.size());
75 return heap.size();
76 }
77
78 @Override
79 public boolean contains(long key){return set.contains(key);}
80
81 @Override
82 public int capacity(){return heap.capacity();}
83 @Override
84 public boolean hasRoom(){return heap.hasRoom();}
85 @Override
86 public LongHeap heap(){return heap;}
87 @Override
88 public long peek(){return heap.peek();}
89
90 final int limit;
91 public LongHeap heap;
92 public LongHashSet set;
93
94 }