Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 98 additions & 87 deletions Core/src/ca/uqac/lif/cep/functions/CumulativeFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,97 +22,108 @@

/**
* A function with memory.
*
*
* @author Sylvain Hallé
* @since 0.1
*/
public class CumulativeFunction<T> extends UnaryFunction<T, T>
{
/**
* The last value returned by the function
*/
private T m_lastValue;

/**
* The stateless binary function to apply on each call
*/
private BinaryFunction<T, T, T> m_function;

/**
* Instantiates a new cumulative function
* @param function The function to cumulate
*/
public CumulativeFunction(BinaryFunction<T, T, T> function)
{
super(function.getInputTypeLeft(), function.getOutputType());
m_function = function;
m_lastValue = m_function.getStartValue();
}

@Override
public T getValue(T x)
{
if (m_lastValue == null)
{
// If the function did not provide a start value, use the
// first given argument as the start value
m_lastValue = x;
return x;
public class CumulativeFunction<T> extends UnaryFunction<T, T> {
/**
* The last value returned by the function
*/
private T m_lastValue;

/**
* The start value of the function
*/

private T m_startValue;

/**
* The stateless binary function to apply on each call
*/
private BinaryFunction<T, T, T> m_function;

/**
* Instantiates a new cumulative function
*
* @param function The function to cumulate
*/
public CumulativeFunction(BinaryFunction<T, T, T> function) {
// If no start value is provided, we use the start value of the function that gived to the CumulativeFunction constructor
this(function, function.getStartValue());
}
T value = m_function.getValue(m_lastValue, x);
m_lastValue = value;
return value;
}

@Override
public void reset()
{
m_lastValue = m_function.getStartValue();
}

@Override
public CumulativeFunction<T> duplicate(boolean with_state)
{
CumulativeFunction<T> cf = new CumulativeFunction<T>(m_function.duplicate(with_state));
if (with_state)
{
cf.m_lastValue = m_lastValue;

/**
* Instantiates a new cumulative function with a start value
*
* @param function the function to cumulate
* @param startValue the start value of the function
*/
public CumulativeFunction(BinaryFunction<T, T, T> function, T startValue) {
super(function.getInputTypeLeft(), function.getOutputType());
m_function = function;
// We store the start value in case of reset
m_startValue = startValue;
m_lastValue = startValue;
}

@Override
public T getValue(T x) {
if (m_lastValue == null) {
// If the function did not provide a start value, use the
// first given argument as the start value
m_lastValue = x;
return x;
}
T value = m_function.getValue(m_lastValue, x);
m_lastValue = value;
return value;
}

@Override
public void reset() {
m_lastValue = this.m_startValue;
}

@Override
public CumulativeFunction<T> duplicate(boolean with_state) {
CumulativeFunction<T> cf = new CumulativeFunction<T>(m_function.duplicate(with_state));
if (with_state) {
cf.m_lastValue = m_lastValue;
}
return cf;
}

/**
* @since 0.10.2
*/
@Override
public Object printState() {
List<Object> list = new ArrayList<Object>(2);
list.add(m_function);
list.add(m_lastValue);
return list;
}

/**
* @since 0.10.2
*/
@SuppressWarnings("unchecked")
public CumulativeFunction<T> readState(Object o) {
List<Object> list = (List<Object>) o;
BinaryFunction<T, T, T> func = (BinaryFunction<T, T, T>) list.get(0);
CumulativeFunction<T> cf = new CumulativeFunction<T>(func);
cf.m_lastValue = (T) list.get(1);
return cf;
}

/**
* @return The last value
* @since 0.11
*/
/*@ pure @*/
public T getLastValue() {
return m_lastValue;
}
return cf;
}

/**
* @since 0.10.2
*/
@Override
public Object printState()
{
List<Object> list = new ArrayList<Object>(2);
list.add(m_function);
list.add(m_lastValue);
return list;
}

/**
* @since 0.10.2
*/
@SuppressWarnings("unchecked")
public CumulativeFunction<T> readState(Object o)
{
List<Object> list = (List<Object>) o;
BinaryFunction<T,T,T> func = (BinaryFunction<T,T,T>) list.get(0);
CumulativeFunction<T> cf = new CumulativeFunction<T>(func);
cf.m_lastValue = (T) list.get(1);
return cf;
}

/**
* @since 0.11
* @return The last value
*/
/*@ pure @*/ public T getLastValue()
{
return m_lastValue;
}

}