Debian Codegraph @

current/structures/IntMap.java

raw symbols SCIP
1 package structures;
2 import java.util.Arrays;
3
4 import shared.Tools;
5
6
7 public class IntMap {
8
9 public static void main(String[] args){
10
11 }
12
13
14 public IntMap(int from, int to){
15 reset(from, to);
16 }
17
18
19 public int get(int key){
20 assert(key>=min && key<=max);
21 return array[key-min];
22 }
23
24
25 public boolean containsKey(int key){
26 assert(key>=min && key<=max);
27 return array[key-min]!=INVALID;
28 }
29
30
31 public int increment(int key){
32 return increment(key, 1);
33 }
34
35
36 public int increment(int key, int value){
37 assert(key>=min && key<=max);
38 int index=key-min;
39 int old=array[index];
40 int v2=(int)Tools.min(Integer.MAX_VALUE, old+(long)value);
41 assert(array[index]!=INVALID);
42 array[index]=v2;
43 return v2;
44 }
45
46
47 public int put(int key, int value){
48 assert(key>=min && key<=max);
49 assert(value!=INVALID);
50 int index=key-min;
51 int old=array[index];
52 array[index]=value;
53 return old;
54 }
55
56
57 public int remove(int key){
58 assert(key>=min && key<=max);
59 int index=key-min;
60 int old=array[index];
61 array[index]=INVALID;
62 return old;
63 }
64
65
66 public int size(){
67 int sum=0;
68 for(int i=0; i<array.length; i++){
69 if(array[i]!=INVALID){sum++;}
70 }
71 return sum;
72 }
73
74
75 public int[] keys(){
76 int[] r=new int[size()];
77 for(int i=0, j=0; j<r.length; i++){
78 if(array[i]!=INVALID){
79 r[j]=(min+i);
80 j++;
81 }
82 }
83 return r;
84 }
85
86
87 public int[] values(){
88 int[] r=new int[size()];
89 for(int i=0, j=0; j<r.length; i++){
90 if(array[i]!=INVALID){
91 r[j]=array[i];
92 j++;
93 }
94 }
95 return r;
96 }
97
98
99 public void clear(){
100 Arrays.fill(array, INVALID);
101 }
102
103
104 public void reset(int from, int to){
105 min=from;
106 max=to;
107 assert(max>=min);
108 assert(((long)max)-((long)min)<Integer.MAX_VALUE);
109
110 int size=max-min+1;
111 if(array==null || array.length<size){
112 array=new int[size];
113 }
114 clear();
115 }
116
117
118 public int min;
119 public int max;
120 public int[] array;
121
122 private static final int INVALID=Integer.MIN_VALUE;
123
124 }