rexylib/include/rexy/utility.hpp

118 lines
3.1 KiB
C++

/**
This file is a part of rexy's general purpose library
Copyright (C) 2020 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef REXY_UTILITY_HPP
#define REXY_UTILITY_HPP
#include <utility> //forward, move
#include <cstdlib> //size_t
#include <type_traits>
#include "rexy.hpp"
namespace rexy{
namespace{
template<class T>
constexpr void swap(T& l, T& r)
noexcept(std::is_nothrow_move_assignable<T>::value &&
std::is_nothrow_move_constructible<T>::value)
{
T tmp = std::move(l);
l = std::move(r);
r = std::move(tmp);
}
template<class Iter1, class Iter2>
constexpr Iter2 swap_ranges(Iter1 start1, Iter1 end1, Iter2 start2)
noexcept(noexcept(swap(*start1, *start2)))
{
while(start1 != end1){
swap(start1++, start2++);
}
return start2;
}
template<class T, size_t N>
constexpr void swap(T (&l)[N], T (&r)[N])
noexcept(noexcept(swap(*l, *r)))
{
swap_ranges(l, l+N, r);
}
template<class T, class U = T>
constexpr T exchange(T& l, U&& r)
noexcept(std::is_nothrow_assignable<T,U&&>::value &&
std::is_nothrow_move_assignable<T>::value)
{
T old = std::move(l);
l = std::forward<U>(r);
return old;
}
template<class T>
constexpr const T& min(const T& l, const T& r)noexcept{
return l < r ? l : r;
}
template<class T, class Compare>
constexpr const T& min(const T& l, const T& r, Compare cmp)
noexcept(std::is_nothrow_invocable<Compare,const T&,const T&>::value)
{
return cmp(l, r) ? l : r;
}
template<class T>
constexpr const T& max(const T& l, const T& r)noexcept{
return l > r ? l : r;
}
template<class T, class Compare>
constexpr const T& max(const T& l, const T& r, Compare cmp)
noexcept(std::is_nothrow_invocable<Compare,const T&,const T&>::value)
{
return cmp(l, r) ? l : r;
}
template<class T>
constexpr size_t strlen(const T* c)noexcept{
size_t i = 0;
for(;c[i];++i);
return i;
}
template<class T>
constexpr int strcmp(const T* l, const T* r)noexcept{
for(;*l == *r && *l;++l, ++r);
return *l - *r;
}
template<class T, class Compare>
constexpr int strcmp(const T* l, const T* r, Compare cmp)noexcept{
for(;cmp(*l, *r) && *l;++l, ++r);
return *l - *r;
}
constexpr void memcpy(void* l, const void* r, size_t n){
char* ld = static_cast<char*>(l);
const char* rd = static_cast<const char*>(r);
for(size_t i = 0;i < n;++i){
ld[i] = rd[i];
}
}
template<class T>
constexpr T abs(const T& val){
return val > 0 ? val : -val;
}
}
}
#endif