85 lines
3 KiB
C++
85 lines
3 KiB
C++
#include <functional>
|
|
namespace pit{
|
|
template<typename InputType, typename InputIt, typename OutputType>
|
|
class lazy_transform{
|
|
private:
|
|
std::function<OutputType(InputType)> transformation;
|
|
InputIt start;
|
|
InputIt ending;
|
|
public:
|
|
class iterator{
|
|
private:
|
|
InputIt underlying;
|
|
std::function<OutputType(InputType)> transformation;
|
|
iterator(InputIt u,std::function<OutputType(InputType)> transformation ):underlying(u), transformation(transformation){}
|
|
public:
|
|
iterator operator+(std::size_t amount){
|
|
return iterator(this->underlying+amount);
|
|
}
|
|
void operator+=(std::size_t amount){
|
|
this->underlying += amount;
|
|
}
|
|
iterator operator-(std::size_t amount){
|
|
return iterator(this->underlying-amount);
|
|
}
|
|
void operator-=(std::size_t amount){
|
|
this->underlying -= amount;
|
|
}
|
|
bool operator==(iterator const& rhs){
|
|
return this->underlying == rhs.underlying;
|
|
}
|
|
OutputType operator*(){
|
|
return transformation(*underlying);
|
|
}
|
|
};
|
|
lazy_transform(InputIt start, InputIt end, std::function<OutputType(InputType)> transformation):start(start), ending(end), transformation(transformation){}
|
|
iterator begin(){
|
|
return iterator(start);
|
|
}
|
|
iterator end(){
|
|
return iterator(ending);
|
|
}
|
|
};
|
|
template<typename InputType, typename InputIt>
|
|
class lazy_filter{
|
|
private:
|
|
std::function<bool(InputType)> filter;
|
|
InputIt start;
|
|
InputIt ending;
|
|
public:
|
|
class iterator{
|
|
private:
|
|
InputIt underlying;
|
|
InputIt ending;
|
|
std::function<bool(InputType)> filter;
|
|
iterator(InputIt point, InputIt ending,std::function<bool(InputType)> filter ):underlying(u), filter(filter){}
|
|
public:
|
|
iterator operator+(std::size_t amount){
|
|
return iterator(this->underlying+amount);
|
|
}
|
|
void operator+=(std::size_t amount){
|
|
this->underlying += amount;
|
|
|
|
}
|
|
iterator operator-(std::size_t amount){
|
|
return iterator(this->underlying-amount);
|
|
}
|
|
void operator-=(std::size_t amount){
|
|
this->underlying -= amount;
|
|
}
|
|
bool operator==(iterator const& rhs){
|
|
return this->underlying == rhs.underlying;
|
|
}
|
|
InputType operator*(){
|
|
return *underlying;
|
|
}
|
|
};
|
|
lazy_filter(InputIt start, InputIt end, std::function<bool(InputType)> filter):start(start), ending(end), filter(filter){}
|
|
iterator begin(){
|
|
return iterator(start);
|
|
}
|
|
iterator end(){
|
|
return iterator(ending);
|
|
}
|
|
};
|
|
}
|