-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataField.java
47 lines (38 loc) · 880 Bytes
/
DataField.java
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
import java.util.ArrayList;
import java.util.List;
/**
* An abstract data field that keeps track of changed values
*/
public abstract class DataField {
private int[] data;
private List<Integer> changedFields;
public DataField(int size) {
data = new int[size];
reset();
}
public void reset() {
changedFields = new ArrayList<Integer>();
for(int i = 0; i < data.length; i++) {
data[i] = 0;
}
}
protected int get(int index) {
return data[index];
}
protected void set(int index, int value) {
touch(index);
data[index] = value;
}
private void touch(int index) {
if(changedFields.contains(index)) {
changedFields.remove(changedFields.indexOf(index));
}
changedFields.add(0, index);
}
public int[] getRawData() {
return data.clone();
}
public List<Integer> getChangedIndices() {
return new ArrayList<Integer>(changedFields);
}
}