Last active
March 19, 2019 08:45
-
-
Save sotex/b4980ab3e3bb1eb6f0e086f8cbd44f52 to your computer and use it in GitHub Desktop.
C++一些不在标准库的方便使用的简单函数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __L_Y_M_convenient__ | |
#define __L_Y_M_convenient__ | |
#include <type_traits> | |
namespace convenient | |
{ | |
// 限制值范围 | |
template<typename T> | |
T clamp(T minval, T val, T maxval) | |
{ | |
return (minval > val)? minval : ((maxval < val)? maxval : val); | |
} | |
// 除法运算的结果向上取整 | |
template<typename T, typename U> | |
long long ceil_div(T divisor, U dividend) | |
{ | |
long long quotient = static_cats<long long>(divisor / dividend); | |
return (quotient*dividend < divisor)? (quotient + 1 ) : quotient; | |
} | |
// 向上对齐(用于字节对齐等,可以接受不同类型的参数) | |
template<typename T,typename U> | |
T algin(T val, U base) | |
{ | |
if(std::is_integral<T> && std::is_integral<U>) { | |
return ((val + base - 1) & (~(base - 1))); | |
} | |
return ceil_div(val,base) * base; | |
} | |
} | |
#endif //!__L_Y_M_convenient__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment