MFEM  v3.1
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
sort_pairs.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #ifndef MFEM_SORT_PAIRS
13 #define MFEM_SORT_PAIRS
14 
15 #include "../config/config.hpp"
16 #include <cstdlib>
17 
18 namespace mfem
19 {
20 
22 template <class A, class B>
23 class Pair
24 {
25 public:
26  A one;
27  B two;
28 };
29 
31 template <class A, class B>
32 int ComparePairs (const void *_p, const void *_q);
33 
35 template <class A, class B>
36 void SortPairs (Pair<A, B> *pairs, int size);
37 
38 
39 template <class A, class B, class C>
40 class Triple
41 {
42 public:
43  A one;
44  B two;
45  C three;
46 };
47 
48 template <class A, class B, class C>
49 int CompareTriple (const void *_p, const void *_q)
50 {
51  const Triple<A, B, C> *p, *q;
52 
53  p = static_cast< const Triple<A, B, C>* >(_p);
54  q = static_cast< const Triple<A, B, C>* >(_q);
55 
56  if (p -> one < q -> one) { return -1; }
57  if (q -> one < p -> one) { return +1; }
58  if (p -> two < q -> two) { return -1; }
59  if (q -> two < p -> two) { return +1; }
60  if (p -> three < q -> three) { return -1; }
61  if (q -> three < p -> three) { return +1; }
62  return 0;
63 }
64 
65 template <class A, class B, class C>
66 void SortTriple (Triple<A, B, C> *triples, int size)
67 {
68  if (size > 0)
69  {
70  qsort (triples, size, sizeof(Triple<A, B, C>), CompareTriple<A, B, C>);
71  }
72 }
73 
74 }
75 
76 #endif
int ComparePairs(const void *_p, const void *_q)
Compare the first element of the pairs.
Definition: sort_pairs.cpp:23
int CompareTriple(const void *_p, const void *_q)
Definition: sort_pairs.hpp:49
A pair of objects.
Definition: sort_pairs.hpp:23
void SortTriple(Triple< A, B, C > *triples, int size)
Definition: sort_pairs.hpp:66
void SortPairs(Pair< A, B > *pairs, int size)
Sort with respect to the first element.
Definition: sort_pairs.cpp:36