[C++] 根据运算符获取对应枚举

// app::DepthTest::Func::Type
enum class Type : GLenum 
{
	Always = GL_ALWAYS,
	Never = GL_NEVER,
	Less = GL_LESS,
	Equal = GL_EQUAL,
	LEqual = GL_LEQUAL,
	Greater = GL_GREATER,
	NotEqual = GL_NOTEQUAL,
	GEqual = GL_GEQUAL,

	Default = Less,
};
#define APP_DEPTH_FUNC_OP(op)\
(([] {\
	struct { void operator##op (int) { } };/* X APP_DEPTH_FUNC_OP(== 1 && 2 ==) X */\
	static_assert(std::is_same_v<bool, decltype(0 op 0)>, "Comparison operator only.");\
	switch (((1 op 2) << 2) | ((3 op 3) << 1) | (5 op 4))\
	{\
	case 0b001: return app::DepthTest::Func::Type::Greater;\
	case 0b010: return app::DepthTest::Func::Type::Equal;\
	case 0b011: return app::DepthTest::Func::Type::GEqual;\
	case 0b100: return app::DepthTest::Func::Type::Less;\
	case 0b101: return app::DepthTest::Func::Type::NotEqual;\
	case 0b110: return app::DepthTest::Func::Type::LEqual;\
	}\
})())

用例:

APP_DEPTH_FUNC_OP(==) == Type::Equal;// (bool)true

[GLSL] HSV 转为 RGB

vec3 rgb(float h, float s, float v)
{
    int hi = int(mod(floor(h / 60), 6));
    float f = h / 60 - hi;
    float p = v * (1 - s);
    float q = v * (1 - f * s);
    float t = v * (1 - (1 - f) * s);
    switch(hi)
    {
    case 0:
        return vec3(v, t, p);
    case 1:
        return vec3(q, v, p);
    case 2:
        return vec3(p, v, t);
    case 3:
        return vec3(p, q, v);
    case 4:
        return vec3(t, p, v);
    case 5:
        return vec3(v, p, q);
    }
}

参考:颜色空间RGB与HSV(HSL)的转换_jiangxinyu的专栏-CSDN博客

[力扣] 算法 525 (C++)

525. 连续数组

class Solution {
	static inline int f(int x)
	{
		return 2 * x - 1;
	}
public:
	int findMaxLength(vector<int>& nums) {
		unordered_map<int, size_t> map;
		nums[0] = f(nums[0]);
		map.emplace(nums[0], 0);
		size_t ret = 0;
		for (size_t i = 1, size = nums.size(), tmp; i < size; ++i)
		{
			nums[i] = nums[i - 1] + f(nums[i]);
			if (nums[i] == 0 && (tmp = i + 1) > ret)
			{
				ret = tmp;
			}
			else
			{
				auto found = map.find(nums[i]);
				if (found == map.end())
				{
					map.emplace(nums[i], i);
				}
				else if ((tmp = i - found->second) > ret)
				{
					ret = tmp;
				}
			}
		}
		return ret;
	}
};