Maybe
(alga/maybe.hpp)
- View this file on GitHub
- Last update: 2020-09-19 12:18:12+09:00
- Include:
#include "alga/maybe.hpp"
- Link: 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
Required by
alga/monoid.hpp
RMQ<O(N),O(1)> (data_structure/RMQ.hpp)
RangeArgminQuery <O(N),O(1)> (data_structure/arg_rmq.hpp)
SparseTable (data_structure/sparse_table.hpp)
SWAG(Queue) (data_structure/swag.hpp)
区間加算 (functional/range_add_and_range_sum.hpp)
LCA <O(N),O(1)>(HL分解と同等の速さ) (graph_tree/lca.hpp)
双対セグメント木 (segment_tree/dual_segment_tree.hpp)
segment_tree/lazy_segment_tree.hpp
セグメント木 (segment_tree/segment_tree.hpp)
Verified with
data_structure/test/LC_RMQ.test.cpp
data_structure/test/LC_sparse_table.test.cpp
data_structure/test/LC_swag.test.cpp
graph_tree/test/LC_lca.test.cpp
segment_tree/test/AOJ_dual_segment_tree.test.cpp
segment_tree/test/AOJ_lazy_segment_tree.test.cpp
segment_tree/test/LC_segment_tree.test.cpp
Code
#pragma once
#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 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()));
};
}