Debian Codegraph @

current/structures/LongHeapMap.java

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