-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCompareFilterN.java
More file actions
98 lines (92 loc) · 3.08 KB
/
Copy pathCompareFilterN.java
File metadata and controls
98 lines (92 loc) · 3.08 KB
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
import java.util.Iterator;
/**
* ComparableFilterN is an abstract class that extends FilterN.
* ComparableFilterN works similarly to ComparableFilter, where
* an abstract compare() function is made to decide if the current
* input should be the next input, except
* it must keep track of how many inputs there has been since the
* most recent update of the output.
* When deciding the next output, it must check to see
* if there has been N inputs since the last update of the output,
* and if there has, it must use the abstract compare() function
* to find the next output.
* @author gmh73
*
* @param <T>
*/
public abstract class CompareFilterN<T> extends FilterN<T> implements Comparing<T>{
/**
* Makes a new CompareFilterN that remembers n values.
* @param n The number of outputs the filter remembers.
*/
public CompareFilterN(int n) {
super(n);
}
/**
* If the previous output is from an input for which compare returned true
* was < N inputs ago, the input is simply compared against the previous
* output. If the previous output was updated >= N inputs ago, the new
* output is found by checking each remembered input.
*/
@Override
protected void processInput(T input) {
//push the input into our buffer
buf.push(input);
//if its been N inputs since the last update of the output
if(inputsSinceNewOutput == N - 1){
//set the output based on our remembered N inputs
setOutput(getFilteredValue());
//reset the count
//inputsSinceNewOutput = 0;
}else{
//if the last output was null (this is the fist input)
//or the comparison test is passed for this input and the previous
//output
if(getPrevOutput() == null || compare(input, getPrevOutput())){
//set the output to this input and reset the count
setOutput(input);
inputsSinceNewOutput = 0;
}else{
//else increment the count since the last output update
inputsSinceNewOutput++;
}
}
}
/**
* An abstract compare function that will determine
* if the input passes a comparison test over
* the previous output.
* Whether or not left and right matters is determined
* when overridden. Ex. left compare right =?= right compare left
* Example: the compare may return left > right, or it might
* return left + right == 10
* @param left The left operand of the comparison
* @param right The right operand of the comparison
*/
public abstract boolean compare(T left, T right);
/**
* Iterates through the buffer of previous inputs
* to find the one/ oldest one that satisfies the comparison test
* @return returns the best candidate in memory.
* Note that "best" is abstract, determined by the compare function.
*/
private T getFilteredValue(){
Iterator<T> it = buf.iterator();
T val = it.next();
int count = N - 2;
inputsSinceNewOutput = N - 1;
//Check each stored input
while(it.hasNext()){
T temp = it.next();
//Test the current object against the last one to pass
//the test
if(compare(temp, val)){
//Set the last to pass the test
val = temp;
inputsSinceNewOutput = count;
}
count--;
}
return val;
}
}