ug4
index_list_util.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
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 #ifndef __H__UG_index_list_util
34 #define __H__UG_index_list_util
35 
36 #include <cctype>
37 #include <cstring>
38 #include <vector>
39 #include <sstream>
40 
41 namespace ug {
42 
44 template <class ind_t>
45 std::string IndexListToRangeString (const std::vector<ind_t>& inds)
46 {
47  using namespace std;
48  stringstream ss;
49 
50  for(size_t i = 0; i < inds.size(); ++i)
51  {
52  if(i > 0)
53  ss << ",";
54 
55  const ind_t first = inds[i];
56  ind_t cur = first;
57  while((i + 1 < inds.size()) && inds[i+1] == cur + 1){
58  ++cur;
59  ++i;
60  }
61 
62  if(first == cur)
63  ss << first;
64  else if(first + 1 == cur)
65  ss << first << "," << cur;
66  else
67  ss << first << "-" << cur;
68  }
69 
70  return ss.str();
71 }
72 
73 
75 template <class ind_t>
76 void RangeStringToIndexList (std::vector<ind_t>& indsOut, const char* rangeString)
77 {
78  indsOut.clear();
79 
80  const char* cur = rangeString;
81 
82  bool readingRange = false;
83  ind_t lastInd = 0;
84 
85  while(*cur) {
86  ind_t ind = 0;
87  while(*cur && *cur != ',' && *cur != '-'){
88  if(isdigit(*cur))
89  ind = ind * 10 + *cur - '0';
90  ++cur;
91  }
92 
93  if(readingRange){
94  readingRange = false;
95  for(ind_t i = lastInd; i <= ind; ++i)
96  indsOut.push_back (i);
97  }
98  else{
99  switch(*cur){
100  case 0: indsOut.push_back(ind); break;
101  case ',': indsOut.push_back(ind); break;
102  case '-': lastInd = ind; readingRange = true; break;
103  }
104  }
105 
106  if(*cur)
107  ++cur;
108  }
109 }
110 
111 
112 }// end of namespace
113 
114 #endif //__H__UG_index_list_util
Definition: smart_pointer.h:814
the ug namespace
void RangeStringToIndexList(std::vector< ind_t > &indsOut, const char *rangeString)
Definition: index_list_util.h:76
std::string IndexListToRangeString(const std::vector< ind_t > &inds)
Definition: index_list_util.h:45