Loading [MathJax]/extensions/tex2jax.js
ug4
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sort_util.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2015: G-CSC, Goethe University Frankfurt
3 * Author: Martin Rupp
4 *
5 * This file is part of UG4.
6 *
7 * UG4 is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License version 3 (as published by the
9 * Free Software Foundation) with the following additional attribution
10 * requirements (according to LGPL/GPL v3 §7):
11 *
12 * (1) The following notice must be displayed in the Appropriate Legal Notices
13 * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 *
15 * (2) The following notice must be displayed at a prominent place in the
16 * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 *
18 * (3) The following bibliography is recommended for citation and must be
19 * preserved in all covered files:
20 * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 * parallel geometric multigrid solver on hierarchically distributed grids.
22 * Computing and visualization in science 16, 4 (2013), 151-164"
23 * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 * flexible software system for simulating pde based models on high performance
25 * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 */
32
33#include <utility>
34
35#ifndef __H__SORT_UTIL__
36#define __H__SORT_UTIL__
37
40
41template<typename TIndex, typename TValue>
43{
45 SortStruct(TIndex &i, TValue &v) : index(i), value(v) { }
46
47 void set_value(TValue &val)
48 {
49 value = val;
50 }
51
52 TValue &get_value()
53 {
54 return value;
55 }
56
57 bool operator < (const SortStruct<TIndex, TValue> &other) const
58 {
59 return value < other.value;
60 }
61
62 TIndex index;
63 TValue value;
64};
65
66
67
68template<typename TCompareValues, bool bReverse>
70{
71public:
72 CompareIndicesByClass(const TCompareValues &compareValues) : m_compareValues(compareValues) { }
73
74 template<typename TIndex>
75 bool operator () (const TIndex &i, const TIndex &j) const
76 {
77 if(bReverse)
78 return m_compareValues[i] > m_compareValues[j];
79 else
80 return m_compareValues[i] < m_compareValues[j];
81 }
82private:
83 const TCompareValues &m_compareValues;
84};
85
86template<typename TCompareValues, typename TCompare>
88{
89public:
90 CompareIndicesByClass2(const TCompareValues &compareValues, TCompare comp) : m_compareValues(compareValues), m_comp(comp) { }
91
92 template<typename TIndex>
93 bool operator () (const TIndex &i, const TIndex &j) const
94 {
96 }
97private:
98 const TCompareValues &m_compareValues;
99 TCompare m_comp;
100};
101
103// CompareIndicesBy
113template<typename TCompareValues>
118
119template<typename TCompareValues>
124
128template<typename TCompareValues, typename TCompare>
129inline CompareIndicesByClass2<TCompareValues, TCompare> CompareIndicesBy(const TCompareValues &values, TCompare comp)
130{
132}
133
134
144template<typename TCompareValues>
145void GetSortedIndices(std::vector<size_t> &indices, const TCompareValues &values, bool bReverse=false)
146{
147 indices.resize(values.size());
148 for(size_t i=0; i<indices.size(); i++) indices[i] = i;
149 if(bReverse)
150 std::sort(indices.begin(), indices.end(), CompareIndicesByClass<TCompareValues, true>(values));
151 else
152 std::sort(indices.begin(), indices.end(), CompareIndicesByClass<TCompareValues, false>(values));
153}
154
164template<typename TCompareValues, typename TCompare>
165void GetSortedIndices(std::vector<size_t> &indices, const TCompareValues &values, TCompare comp)
166{
167 indices.resize(values.size());
168 for(size_t i=0; i<indices.size(); i++) indices[i] = i;
169 std::sort(indices.begin(), indices.end(), CompareIndicesByClass2<TCompareValues, TCompare>(values, comp));
170}
171
172template<typename TCompareValues, typename TCompare>
173std::vector<size_t> GetSortedIndices(const TCompareValues &values, TCompare comp)
174{
175 std::vector<size_t> indices(values.size());
176 GetSortedIndices(indices, values, comp);
177 return indices;
178}
179
180template<typename TCompareValues>
181std::vector<size_t> GetSortedIndices(const TCompareValues &values)
182{
183 std::vector<size_t> indices(values.size());
184 GetSortedIndices(indices, values);
185 return indices;
186}
187
188
189
190inline bool boolstrcmp(const char *a, const char *b)
191{
192 return strcmp(a, b) < 0;
193}
194
195inline bool boolstrcmpReversed(const char *a, const char *b)
196{
197 return strcmp(a, b) > 0;
198}
199
200// end group ugbase_common_util
202
203#endif
204
205
Definition sort_util.h:88
const TCompareValues & m_compareValues
Definition sort_util.h:98
bool operator()(const TIndex &i, const TIndex &j) const
Definition sort_util.h:93
CompareIndicesByClass2(const TCompareValues &compareValues, TCompare comp)
Definition sort_util.h:90
TCompare m_comp
Definition sort_util.h:99
Definition sort_util.h:70
CompareIndicesByClass(const TCompareValues &compareValues)
Definition sort_util.h:72
const TCompareValues & m_compareValues
Definition sort_util.h:83
bool operator()(const TIndex &i, const TIndex &j) const
Definition sort_util.h:75
bool boolstrcmpReversed(const char *a, const char *b)
Definition sort_util.h:195
void GetSortedIndices(std::vector< size_t > &indices, const TCompareValues &values, bool bReverse=false)
Definition sort_util.h:145
bool boolstrcmp(const char *a, const char *b)
Definition sort_util.h:190
CompareIndicesByClass< TCompareValues, true > CompareIndicesReversedBy(const TCompareValues &values)
Definition sort_util.h:120
CompareIndicesByClass< TCompareValues, false > CompareIndicesBy(const TCompareValues &values)
Definition sort_util.h:114
Definition sort_util.h:43
SortStruct()
Definition sort_util.h:44
TIndex index
Definition sort_util.h:62
SortStruct(TIndex &i, TValue &v)
Definition sort_util.h:45
TValue & get_value()
Definition sort_util.h:52
void set_value(TValue &val)
Definition sort_util.h:47
TValue value
Definition sort_util.h:63
bool operator<(const SortStruct< TIndex, TValue > &other) const
Definition sort_util.h:57