Line data Source code
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 :
41 : /// \addtogroup ugbase_common_types
42 : /// \{
43 :
44 : /// removes the first occurance of the specified entry.
45 : /**
46 : * Runs in O(size).
47 : * copies all entries after the specified one to their predecessor.
48 : * Be sure that TType supports operator= and operator==
49 : * \return new size
50 : */
51 : template <class TType>
52 0 : int ArrayEraseEntry(TType* array, const TType& entry, size_t size)
53 : {
54 : // find the entry
55 : size_t i;
56 0 : for(i = 0; i < size; ++i)
57 : {
58 0 : if(array[i] == entry)
59 : break;
60 : }
61 :
62 : // proceed if the entry has been found
63 0 : if(i >= size)
64 0 : return (int)size;
65 :
66 : // copy elements
67 0 : for(; i < size - 1; ++i)
68 0 : array[i] = array[i+1];
69 :
70 : // done
71 0 : return (int)size - 1;
72 : }
73 :
74 : /// Swaps the first entry with the given value with the last entry in the list
75 : /**
76 : * Runs in O(size).
77 : * Iterates through all entries until the given one is found.
78 : * Be sure that TType supports operator= and operator==
79 : */
80 : template <class TType>
81 0 : void ArraySwapWithLast(TType* array, const TType& entry, size_t size)
82 : {
83 : using namespace std;
84 : // find the entry
85 : size_t i;
86 0 : for(i = 0; i < size; ++i){
87 0 : 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 0 : if(i + 1>= size)
93 : return;
94 :
95 : // swap elements
96 0 : swap(array[i], array[size - 1]);
97 : }
98 :
99 : /// replaces the first occurance of oldEntry with newEntry
100 : /**
101 : * Runs in O(size).
102 : * Be sure that TType supports operator= and operator==
103 : * \return true if oldEntry was found, false if not.
104 : */
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 0 : for(i = 0; i < size; ++i)
112 : {
113 0 : if(array[i] == oldEntry){
114 0 : array[i] = newEntry;
115 0 : return true;
116 : }
117 : }
118 :
119 : return false;
120 : }
121 :
122 : // end group ugbase_common_types
123 : /// \}
124 :
125 : }// end of namespace
126 :
127 : #endif
|