Skip to main content
Loading...

More C++ Posts

//Leif Messinger
//Compile with C++ 20
#include <iostream>
#include <ranges>
#include <vector>
#include <functional>
#include <cctype> //toupper
#include <cxxabi.h>

template <typename T>
void printType(){
    std::cout << abi::__cxa_demangle(typeid(T).name(), NULL, NULL, NULL) << std::endl;
}

template <typename T>
class Slicer{
    public:
        T begin_;
        T end_;
        T trueEnd;
        Slicer(T begin, T end): begin_(begin), end_(begin), trueEnd(end){}
        template<typename U>
        Slicer(U&& vec) : begin_(vec.begin()), end_(vec.begin()), trueEnd(vec.end()){}
        Slicer& finish(){
            begin_ = end_;
            end_ = trueEnd;
            return (*this);
        }
        Slicer& to(long int index){
            begin_ = end_;
            if(index > 0){
                end_ = (begin_ + index);
            }else{
                index *= -1;
                end_ = (trueEnd - index);
            }
            return (*this);
        }
        Slicer& operator[](long int index){
            return to(index);
        }
        T begin(){
            return this->begin_;
        }
        T end(){
            return this->end_;
        }
        Slicer& operator()(std::function<void(decltype(*begin_))> func) {
            for(decltype(*begin_) thing : (*this)){
                func(thing);
            }
            return (*this);
        }
};
static_assert(std::ranges::range< Slicer<std::vector<int>::const_iterator> >);

int main(){
    std::string vec = "abcdefghijklmnopqrstuvwxyz";
    Slicer<std::string::const_iterator> bruh(vec);
    
    //printType<decltype(bruh)>();

    bruh.to(3)([](char yeet){
        std::cout << yeet;
    })
    .to(-1)([](char yeet){
        std::cout << char(std::toupper(yeet));
    }).finish()([](char yeet){
        std::cout << yeet << yeet << yeet << yeet << yeet;
    });
    
    std::cout << std::endl << std::endl;
    
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
    Slicer<int*> arrSlicer(arr, arr + (sizeof(arr)/sizeof(int)));
    
    std::cout << "[";
    arrSlicer.to(-1)([](int yeet){
        std::cout << yeet << ", ";
    }).finish()([](int yeet){
        std::cout << yeet << "]" << std::endl;
    });

    return 0;
}