File: IntMap.java

package info (click to toggle)
bbmap 39.20%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,024 kB
  • sloc: java: 312,743; sh: 18,099; python: 5,247; ansic: 2,074; perl: 96; makefile: 39; xml: 38
file content (124 lines) | stat: -rwxr-xr-x 2,068 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package structures;
import java.util.Arrays;

import shared.Tools;


public class IntMap {
	
	public static void main(String[] args){
		
	}
	
	
	public IntMap(int from, int to){
		reset(from, to);
	}
	
	
	public int get(int key){
		assert(key>=min && key<=max);
		return array[key-min];
	}
	
	
	public boolean containsKey(int key){
		assert(key>=min && key<=max);
		return array[key-min]!=INVALID;
	}
	
	
	public int increment(int key){
		return increment(key, 1);
	}
	
	
	public int increment(int key, int value){
		assert(key>=min && key<=max);
		int index=key-min;
		int old=array[index];
		int v2=(int)Tools.min(Integer.MAX_VALUE, old+(long)value);
		assert(array[index]!=INVALID);
		array[index]=v2;
		return v2;
	}
	
	
	public int put(int key, int value){
		assert(key>=min && key<=max);
		assert(value!=INVALID);
		int index=key-min;
		int old=array[index];
		array[index]=value;
		return old;
	}
	
	
	public int remove(int key){
		assert(key>=min && key<=max);
		int index=key-min;
		int old=array[index];
		array[index]=INVALID;
		return old;
	}
	
	
	public int size(){
		int sum=0;
		for(int i=0; i<array.length; i++){
			if(array[i]!=INVALID){sum++;}
		}
		return sum;
	}
	
	
	public int[] keys(){
		int[] r=new int[size()];
		for(int i=0, j=0; j<r.length; i++){
			if(array[i]!=INVALID){
				r[j]=(min+i);
				j++;
			}
		}
		return r;
	}
	
	
	public int[] values(){
		int[] r=new int[size()];
		for(int i=0, j=0; j<r.length; i++){
			if(array[i]!=INVALID){
				r[j]=array[i];
				j++;
			}
		}
		return r;
	}
	
	
	public void clear(){
		Arrays.fill(array, INVALID);
	}
	
	
	public void reset(int from, int to){
		min=from;
		max=to;
		assert(max>=min);
		assert(((long)max)-((long)min)<Integer.MAX_VALUE);
		
		int size=max-min+1;
		if(array==null || array.length<size){
			array=new int[size];
		}
		clear();
	}
	
	
	public int min;
	public int max;
	public int[] array;
	
	private static final int INVALID=Integer.MIN_VALUE;
	
}