Skip to content

Instantly share code, notes, and snippets.

@tyanmahou
Last active August 24, 2017 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyanmahou/aa0be23f6678b5fd260d3660c13f9df5 to your computer and use it in GitHub Desktop.
Save tyanmahou/aa0be23f6678b5fd260d3660c13f9df5 to your computer and use it in GitHub Desktop.
or , and

概要

val==0||val==1||…や val!=1&&val!=2&&…を少し楽に記述するための構造体

速度はさがりそー

#pragma once
#include<vector>
#include<algorithm>
template<class Type>
struct OR;
template<class Type>
struct AND;
///<summary>
///論理和
///</summary>
template<class Type>
struct OR
{
public:
std::vector<Type> elms;
OR(std::initializer_list<Type>&& args):
OR(args.begin(),args.end())
{}
template<class It>
OR(It first, It last)
{
for (auto it = first;it != last; ++it)
{
elms.emplace_back(*it);
}
std::sort(elms.begin(), elms.end());
std::unique(elms.begin(), elms.end());
}
explicit operator bool()
{
return std::any_of(elms.begin(), elms.end(), [](const Type& elm) {return elm; });
}
AND<Type> operator!()
{
std::vector<Type> v;
for (auto&& elm : elms)
{
v.emplace_back(!elm);
}
return AND<Type>(v.begin(),v.end());
}
};
template<class Type>
bool operator == (const Type& val,const OR<Type>& or)
{
return std::any_of(or.elms.begin(), or .elms.end(), [&val](const Type& elm) {return val == elm; });
}
template<class Type>
bool operator != (const Type& val, const OR<Type>& or )
{
return std::all_of(or.elms.begin(), or.elms.end(), [&val](const Type& elm) {return val != elm; });
}
//-----------------------------------------------------------------------------------------------------
///<summary>
///論理積
///</summary>
template<class Type>
struct AND
{
public:
std::vector<Type> elms;
AND(std::initializer_list<Type>&& args) :
AND(args.begin(), args.end())
{}
template<class It>
AND(It first, It last)
{
for (auto it = first; it != last; ++it)
{
elms.emplace_back(*it);
}
std::sort(elms.begin(), elms.end());
std::unique(elms.begin(), elms.end());
}
explicit operator bool()
{
return std::all_of(elms.begin(), elms.end(), [](const Type& elm) {return elm; });
}
OR<Type> operator!()
{
std::vector<Type> v;
for (auto&& elm : elms)
{
v.emplace_back(!elm);
}
return OR<Type>(v.begin(), v.end());
}
};
template<class Type>
bool operator == (const Type& val, const AND<Type>& and )
{
return std::all_of(and .elms.begin(), and .elms.end(), [&val](const Type& elm) {return val == elm; });
}
template<class Type>
bool operator != (const Type& val, const AND<Type>& and )
{
return std::any_of(and .elms.begin(), and .elms.end(), [&val](const Type& elm) {return val != elm; });
}
#include<iostream>
#include"LogicalOperation.h"
void main()
{
using namespace std;
//-----------------------------------
//operator==
for (int val = 0; val < 10; ++val)
{
if (val == OR<int>{0, 3, 5})
{
cout << val <<" ";
}
}
cout << endl;
//result 0 3 5
//-----------------------------------
//operator!=
for (int val = 0; val < 10; ++val)
{
if (val != OR<int>{0, 3, 5})
{
cout << val << " ";
}
}
cout << endl;
//result 1 2 4 6 7 8 9
//-----------------------------------
//operator bool
if (AND<bool>{true, false, true})
{
cout << "true";
}
else
{
cout << "false";
}
cout << endl;
//result false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment