MFEM  v4.6.0 Finite element discretization library
sort_pairs.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2023, Lawrence Livermore National Security, LLC. Produced
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11
12 #ifndef MFEM_SORT_PAIRS
13 #define MFEM_SORT_PAIRS
14
15 #include "../config/config.hpp"
16 #include <algorithm>
17
18 namespace mfem
19 {
20
21 /// A pair of objects
22 template <class A, class B>
23 class Pair
24 {
25 public:
26  A one;
27  B two;
28
29  Pair() = default;
30
31  Pair(const A &one, const B &two) : one(one), two(two) {}
32 };
33
34 /// @brief Comparison operator for class Pair, based on the first element only.
35 template <class A, class B>
36 bool operator<(const Pair<A,B> &p, const Pair<A,B> &q)
37 {
38  return (p.one < q.one);
39 }
40
41 /// @brief Equality operator for class Pair, based on the first element only.
42 template <class A, class B>
43 bool operator==(const Pair<A,B> &p, const Pair<A,B> &q)
44 {
45  return (p.one == q.one);
46 }
47
48 /// Sort an array of Pairs with respect to the first element
49 template <class A, class B>
50 void SortPairs (Pair<A, B> *pairs, int size)
51 {
52  std::sort(pairs, pairs + size);
53 }
54
55 /// A triple of objects
56 template <class A, class B, class C>
57 class Triple
58 {
59 public:
60  A one;
61  B two;
62  C three;
63
64  Triple() = default;
65
66  Triple(const A &one, const B &two, const C &three)
67  : one(one), two(two), three(three) { }
68 };
69
70 /// @brief Lexicographic comparison operator for class Triple.
71 template <class A, class B, class C>
72 bool operator<(const Triple<A,B,C> &p, const Triple<A,B,C> &q)
73 {
74  return (p.one < q.one ||
75  (!(q.one < p.one) &&
76  (p.two < q.two || (!(q.two < p.two) && p.three < q.three))));
77 }
78
79 /// @brief Lexicographic sort for arrays of class Triple.
80 template <class A, class B, class C>
81 void SortTriple (Triple<A, B, C> *triples, int size)
82 {
83  std::sort(triples, triples + size);
84 }
85
86 }
87
88 #endif
A triple of objects.
Definition: sort_pairs.hpp:57
Triple(const A &one, const B &two, const C &three)
Definition: sort_pairs.hpp:66
Pair(const A &one, const B &two)
Definition: sort_pairs.hpp:31
Triple()=default
A pair of objects.
Definition: sort_pairs.hpp:23
Pair()=default
bool operator==(const Array< T > &LHS, const Array< T > &RHS)
Definition: array.hpp:332
void SortTriple(Triple< A, B, C > *triples, int size)
Lexicographic sort for arrays of class Triple.
Definition: sort_pairs.hpp:81
void SortPairs(Pair< A, B > *pairs, int size)
Sort an array of Pairs with respect to the first element.
Definition: sort_pairs.hpp:50