-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recurrent.h
56 lines (48 loc) · 1.57 KB
/
Recurrent.h
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
#pragma once
#include <memory>
#include <functional>
#include <DataStream.h>
template<typename DataT>
class RecurrentEmitter : public Emitter<DataT> {
public:
RecurrentEmitter(std::function<const DataT( const DataT )> recurrentFormula,
const DataT& startingState):
recurrentFormula(recurrentFormula),
startingState(startingState),
state(startingState)
{
}
RecurrentEmitter(const RecurrentEmitter& other):
recurrentFormula(other.recurrentFormula),
startingState(other.startingState),
state(other.state)
{
}
virtual const DataT EmitNext() {
const DataT oldState = state;
state = recurrentFormula(state);
return oldState;
}
virtual bool Ended() override {
return false;
}
virtual void Reset() {
state = startingState;
}
virtual std::unique_ptr<Emitter<DataT>> Copy() const
{ return std::make_unique<RecurrentEmitter>(recurrentFormula, startingState); }
virtual std::unique_ptr<Emitter<DataT>> CopyState() const
{ return std::make_unique<RecurrentEmitter>(*this); }
private:
std::function<const DataT( const DataT )> recurrentFormula;
const DataT startingState;
DataT state;
};
template<typename DataT>
class RecurrentContainer: public DataStream<DataT> {
public:
RecurrentContainer(std::function<const DataT( const DataT )> recurrentFormula, const DataT& startingState):
DataStream<DataT>(std::make_unique< const RecurrentEmitter<DataT> >(recurrentFormula, startingState).get())
{
}
};