summaryrefslogtreecommitdiff
path: root/lib/sorting.h
blob: be5a7e26149dcbde478ab28073e226dafc9bbf94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#pragma once

#include <functional>
#include <glm/fwd.hpp>
#include <type_traits>

template<typename T> struct PtrSorter {
	bool
	operator()(const T & a, const T & b) const
	{
		return *a < *b;
	}
};

template<typename T, auto M> struct PtrMemberSorter : public PtrSorter<T> {
	using MT = std::decay_t<decltype((*T {}).*M)>;
	using is_transparent = MT;

	using PtrSorter<T>::operator();

	bool
	operator()(const MT & a, const T & b) const
	{
		return a < (*b).*M;
	}

	bool
	operator()(const T & a, const MT & b) const
	{
		return (*a).*M < b;
	}
};

template<auto Proj> struct SortedBy {
	auto
	operator()(const auto & left, const auto & right) const
	{
		return (std::invoke(Proj, left) < std::invoke(Proj, right));
	}
};

struct CompareBy {
	glm::length_t index;

	template<typename T>
	auto
	operator()(const T & a, const T & b) const
	{
		return get(a) < get(b);
	}

	template<typename T>
	auto
	get(const T & a) const
	{
		return a[index];
	}
};