MFEM  v3.0
 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.googlecode.com.
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 
17 namespace mfem
18 {
19 
21 template <class A, class B>
22 class Pair
23 {
24 public:
25  A one;
26  B two;
27 };
28 
30 template <class A, class B>
31 int ComparePairs (const void *_p, const void *_q);
32 
34 template <class A, class B>
35 void SortPairs (Pair<A, B> *pairs, int size);
36 
37 
38 template <class A, class B, class C>
39 class Triple
40 {
41 public:
42  A one;
43  B two;
44  C three;
45 };
46 
47 template <class A, class B, class C>
48 int CompareTriple (const void *_p, const void *_q)
49 {
50  const Triple<A, B, C> *p, *q;
51 
52  p = static_cast< const Triple<A, B, C>* >(_p);
53  q = static_cast< const Triple<A, B, C>* >(_q);
54 
55  if (p -> one < q -> one) return -1;
56  if (q -> one < p -> one) return +1;
57  if (p -> two < q -> two) return -1;
58  if (q -> two < p -> two) return +1;
59  if (p -> three < q -> three) return -1;
60  if (q -> three < p -> three) return +1;
61  return 0;
62 }
63 
64 template <class A, class B, class C>
65 void SortTriple (Triple<A, B, C> *triples, int size)
66 {
67  if (size > 0)
68  qsort (triples, size, sizeof(Triple<A, B, C>), CompareTriple<A, B, C>);
69 }
70 
71 }
72 
73 #endif
int ComparePairs(const void *_p, const void *_q)
Compare the first element of the pairs.
Definition: sort_pairs.cpp:20
int CompareTriple(const void *_p, const void *_q)
Definition: sort_pairs.hpp:48
A pair of objects.
Definition: sort_pairs.hpp:22
void SortTriple(Triple< A, B, C > *triples, int size)
Definition: sort_pairs.hpp:65
void SortPairs(Pair< A, B > *pairs, int size)
Sort with respect to the first element.
Definition: sort_pairs.cpp:33