added a bunch of stuff, repeat needs to change a bit to actually repeat forever

This commit is contained in:
Pagwin 2024-04-08 15:58:43 -04:00
parent a7e6d1d8de
commit c624c91d8b
3 changed files with 142 additions and 0 deletions

85
include/pit/algorithms.h Normal file
View file

@ -0,0 +1,85 @@
#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);
}
};
}

View file

@ -0,0 +1,57 @@
#include <cstdlib>
namespace pit{
template<typename T>
class passthrough{
T underlying;
public:
passthrough<T>(T underlying): underlying(underlying){}
passthrough<T>(passthrough<T>& from): underlying(from.underlying){}
passthrough<T> operator+(std::size_t amount){
return passthrough<T>(this->underlying+amount);
}
void operator+=(std::size_t amount){
this->underlying += amount;
}
passthrough<T> operator-(std::size_t amount){
return passthrough<T>(this->underlying-amount);
}
void operator-=(std::size_t amount){
this->underlying -= amount;
}
T const& operator*(){
return this->underlying;
}
bool operator==(passthrough<T> const& rhs){
return this->underlying == rhs.underlying;
}
};
template<typename T>
class range{
private:
T beginning;
T ending;
public:
range<T>(T begin, T end):beginning(begin), ending(end){}
passthrough<T> begin(){
return passthrough<T>(this->beginning);
}
passthrough<T> end(){
return passthrough<T>(this->ending);
}
};
template<typename T>
class repeat{
T val;
public:
// noops
repeat<T> operator+(std::size_t ){}
repeat<T> operator-(std::size_t ){}
void operator+=(std::size_t ){}
void operator-=(std::size_t ){}
bool operator==(repeat const& rhs){return true;}
T const& operator*(){
return this->val;
}
};
}