:heavy_check_mark: SWAG(Queue)
(data_structure/swag.hpp)

Depends on

Verified with

Code

#pragma once
#include<stack>
#include<tuple>
#include<cmath>
#include"../alga/maybe.hpp"
/**
 * @brief SWAG(Queue)
 */

template<typename T,typename F>
class swag{
    std::stack<std::pair<T,T>>front,back;
    F f;
    public:
    swag(F f=F()):f(f){}
    inline int size(){
        return front.size()+back.size();
    }
    inline int empty(){
        return front.empty()&&back.empty();
    }
    void push(T val){
        if(back.empty()){
            back.emplace(val,val);
        }else{
            back.emplace(val,f(back.top().second,val));
        }
    }
    void pop(){
        if(front.empty()){
            while(!back.empty()){
                const T val=back.top().first;
                back.pop();
                if(front.empty())front.emplace(val,val);
                else front.emplace(val,f(val,front.top().second));
            }
        }
        front.pop();
    }
    maybe<T> fold(){
        if(front.empty()&&back.empty())return maybe<T>();
        else if(front.empty()||back.empty())return front.empty()?back.top().second:front.top().second;
        return f(front.top().second,back.top().second);
    }
};
#line 2 "data_structure/swag.hpp"
#include<stack>
#include<tuple>
#include<cmath>
#line 2 "alga/maybe.hpp"
#include<cassert>

/**
 * @brief Maybe
 * @see https://ja.wikipedia.org/wiki/%E3%83%A2%E3%83%8A%E3%83%89_(%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0)#Maybe%E3%83%A2%E3%83%8A%E3%83%89
 */

template<typename T>
struct maybe{
    bool _is_none;
    T val;
    maybe():_is_none(true){}
    maybe(T val):_is_none(false),val(val){}
    T unwrap()const{
        assert(!_is_none);
        return val;
    }
    T unwrap_or(T e)const{
        return _is_none?e:val;
    }
    bool is_none()const{return _is_none;}
    bool is_some()const{return !_is_none;}
};

template<typename T,typename F>
auto expand(F op){
    return [&op](const maybe<T>& s,const maybe<T>& t){
        if(s.is_none())return t;
        if(t.is_none())return s;
        return maybe<T>(op(s.unwrap(),t.unwrap()));
    };
}
#line 6 "data_structure/swag.hpp"
/**
 * @brief SWAG(Queue)
 */

template<typename T,typename F>
class swag{
    std::stack<std::pair<T,T>>front,back;
    F f;
    public:
    swag(F f=F()):f(f){}
    inline int size(){
        return front.size()+back.size();
    }
    inline int empty(){
        return front.empty()&&back.empty();
    }
    void push(T val){
        if(back.empty()){
            back.emplace(val,val);
        }else{
            back.emplace(val,f(back.top().second,val));
        }
    }
    void pop(){
        if(front.empty()){
            while(!back.empty()){
                const T val=back.top().first;
                back.pop();
                if(front.empty())front.emplace(val,val);
                else front.emplace(val,f(val,front.top().second));
            }
        }
        front.pop();
    }
    maybe<T> fold(){
        if(front.empty()&&back.empty())return maybe<T>();
        else if(front.empty()||back.empty())return front.empty()?back.top().second:front.top().second;
        return f(front.top().second,back.top().second);
    }
};
Back to top page