ug4
array_util.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2015: 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__UTIL__ARRAY_UTIL__
34 #define __H__UTIL__ARRAY_UTIL__
35 
36 #include <algorithm>
37 
38 namespace ug
39 {
40 
43 
45 
51 template <class TType>
52 int ArrayEraseEntry(TType* array, const TType& entry, size_t size)
53 {
54 // find the entry
55  size_t i;
56  for(i = 0; i < size; ++i)
57  {
58  if(array[i] == entry)
59  break;
60  }
61 
62 // proceed if the entry has been found
63  if(i >= size)
64  return (int)size;
65 
66 // copy elements
67  for(; i < size - 1; ++i)
68  array[i] = array[i+1];
69 
70 // done
71  return (int)size - 1;
72 }
73 
75 
80 template <class TType>
81 void ArraySwapWithLast(TType* array, const TType& entry, size_t size)
82 {
83  using namespace std;
84 // find the entry
85  size_t i;
86  for(i = 0; i < size; ++i){
87  if(array[i] == entry)
88  break;
89  }
90 
91 // proceed if the entry has been found and if it is not already the last
92  if(i + 1>= size)
93  return;
94 
95 // swap elements
96  swap(array[i], array[size - 1]);
97 }
98 
100 
105 template <class TType>
106 bool ArrayReplaceEntry(TType* array, const TType& newEntry,
107  const TType& oldEntry, size_t size)
108 {
109 // find the entry
110  size_t i;
111  for(i = 0; i < size; ++i)
112  {
113  if(array[i] == oldEntry){
114  array[i] = newEntry;
115  return true;
116  }
117  }
118 
119  return false;
120 }
121 
122 // end group ugbase_common_types
124 
125 }// end of namespace
126 
127 #endif
void ArraySwapWithLast(TType *array, const TType &entry, size_t size)
Swaps the first entry with the given value with the last entry in the list.
Definition: array_util.h:81
bool ArrayReplaceEntry(TType *array, const TType &newEntry, const TType &oldEntry, size_t size)
replaces the first occurance of oldEntry with newEntry
Definition: array_util.h:106
int ArrayEraseEntry(TType *array, const TType &entry, size_t size)
removes the first occurance of the specified entry.
Definition: array_util.h:52
Definition: smart_pointer.h:814
the ug namespace