ug4
index_view.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-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 #ifndef __H__UG__LIB_ALGEBRA__INDEX_VIEW_H__
34 #define __H__UG__LIB_ALGEBRA__INDEX_VIEW_H__
35 
36 namespace ug
37 {
38 
39 
40 template<typename index_type>
41 class IndexView
42 {
43 public:
44  IndexView(const std::vector<index_type> &indices)
45  {
46  local_to_global = indices;
47  init();
48  }
49 
50  IndexView(index_type *indices, size_t size)
51  {
52  local_to_global.resize(size);
53  for(size_t i=0; i<size; i++)
54  local_to_global[i] = indices[i];
55  init();
56  }
57 
58  typedef std::vector<index_type> iterator;
59 
61  {
62  return local_to_global.begin();
63  }
64 
66  {
67  return local_to_global.end();
68  }
69 
70  bool is_in_view(index_type i)
71  {
72  // we need this function to be very fast, because it is used often in matrix-vector-multiplication
73  // this uses memory, but is faster than searching the local_to_global array, even with binary search
74  if(i >= is_in_locals.size()) return false;
75  return is_in_locals[i];
76  }
77 
78 private:
79  void init()
80  {
81  index_type maxindex = (*max_element(local_to_global.begin(), local_to_global.end()));
82  is_in_locals.clear();
83  is_in_locals.resize(maxindex+1, false);
84  for(size_t i=0; i<local_to_global.size(); i++)
85  is_in_locals[local_to_global[i]] = true;
86  }
87  std::vector<index_type> local_to_global;
88  std::vector<bool> is_in_locals;
89  //std::vector<int> global_to_lobal;
90 };
91 template<typename index_type>
93 {
94 public:
95  SliceIndexView(index_type from, index_type to) : m_from(from), m_to(to) { }
96 
97  struct iterator
98  {
99  iterator(index_type from) : i(from) { }
100  index_type i;
101 
102  bool operator != (const iterator &other) const { return i != other.i; }
103  bool operator == (const iterator &other) const { return i == other.i; }
104 
105  void operator ++() { ++i; }
106  int operator *() const { return i; }
107  };
108 
109  inline iterator begin()
110  {
111  return iterator(m_from);
112  }
113 
114  inline iterator end()
115  {
116  return iterator(m_to);
117  }
118 
119  bool is_in_view(index_type i)
120  {
121  return i >= m_from && i < m_to;
122  }
123 
124 private:
125  index_type m_from;
126  index_type m_to;
127 };
128 
129 /*
130 template<typename vector_t, typename view_t>
131 class VectorView
132 {
133  VectorView(vector_t &vector, view_t &view) : m_vector(vector), m_view(view)
134  { }
135 
136  typedef view_t::iterator index_iterator;
137  index_iterator begin() { return m_view.begin(); }
138  index_iterator end() { return m_view.end(); }
139 
140  typename vector_t::value_type operator[] (const index_iterator &it)
141  {
142  return vector_t::operator[] ( (*it) );
143  }
144 }*/
145 
146 
147 /*
149 template<typename vector_t>
150 inline void VecScaleAssign(vector_t &dest, double alpha1, const vector_t &v1)
151 {
152  for(dest::index_iterator it = dest.begin(); it != view.end(); ++it)
153  {
154  // so?
155  size_t i = (*it);
156  VecScaleAssign(dest[i], alpha1, v1[i]);
157  // oder so
158  VecScaleAssign(dest[it], alpha1, v1[it]);
159  }
160 }
161 
162 oder
163 
164 template<typename vector_t, typename view_t>
165 inline void VecScaleAssign(vector_t &dest, double alpha1, const vector_t &v1, const view_t &view)
166 {
167  for(view_t::index_iterator it = view.begin(); it != view.end(); ++it)
168  {
169  size_t i = (*it);
170  VecScaleAssign(dest[i], alpha1, v1[i]);
171  }
172 }
173 */
174 
175 
176 template<typename index_type>
178 {
179 public:
180  BlockSliceIndexView(index_type from, index_type to, index_type block) : m_from(from), m_to(to), m_block(block) { }
181 
182  struct iterator
183  {
184  iterator(index_type from, index_type block) : i(from), m_block(block) { }
185  index_type i;
186  index_type m_block;
187 
188  bool operator != (const iterator &other) const { return i != other.i; }
189  bool operator == (const iterator &other) const { return i == other.i; }
190 
191  void operator ++() { ++i; }
192  int index() const { return i; }
193  int block() const { return block; }
194  };
195 
196  inline iterator begin()
197  {
198  return iterator(m_from, m_block);
199  }
200 
201  inline iterator end()
202  {
203  return iterator(m_to, m_block);
204  }
205 
206  bool is_in_view(const iterator &i)
207  {
208  return is_in_view(i.i);
209  }
210  bool is_in_view(index_type i)
211  {
212  return i >= m_from && i < m_to;
213  }
214 
215 private:
216  index_type m_from;
217  index_type m_to;
218 };
219 
220 /*
221 template<typename vector_t, typename view_t>
222 class BlockVectorView
223 {
224  BlockVectorView(vector_t &vector, view_t &view) : m_vector(vector), m_view(view)
225  { }
226 
227  typedef view_t::iterator index_iterator;
228  index_iterator begin() { return m_view.begin(); }
229  index_iterator end() { return m_view.end(); }
230 
231  typename vector_t::value_type::value_type operator[] (const index_iterator &it)
232  {
233  return BlockRef(vector_t::operator[] ( it.index() ), it.block());
234  }
235 }*/
236 
238 /*template<typename vector_t>
239 inline void VecScaleAssign(vector_t &dest, double alpha1, const vector_t &v1)
240 {
241  for(dest::index_iterator it = dest.begin(); it != view.end(); ++it)
242  VecScaleAssign(dest[it], alpha1, v1[it]); // automatically on BlockRef(index, block) if used with BlockVectorView(vec, BlockSliceIndexView(10, 20, 2))
243 }
244 */
245 
246 } // namespace ug
247 
248 
249 #endif // __H__UG__LIB_ALGEBRA__INDEX_VIEW_H__
Definition: index_view.h:178
BlockSliceIndexView(index_type from, index_type to, index_type block)
Definition: index_view.h:180
index_type m_from
Definition: index_view.h:216
iterator end()
Definition: index_view.h:201
bool is_in_view(const iterator &i)
Definition: index_view.h:206
iterator begin()
Definition: index_view.h:196
index_type m_to
Definition: index_view.h:217
bool is_in_view(index_type i)
Definition: index_view.h:210
Definition: index_view.h:42
std::vector< bool > is_in_locals
Definition: index_view.h:88
IndexView(index_type *indices, size_t size)
Definition: index_view.h:50
iterator begin()
Definition: index_view.h:60
IndexView(const std::vector< index_type > &indices)
Definition: index_view.h:44
std::vector< index_type > iterator
Definition: index_view.h:58
std::vector< index_type > local_to_global
Definition: index_view.h:87
iterator end()
Definition: index_view.h:65
bool is_in_view(index_type i)
Definition: index_view.h:70
void init()
Definition: index_view.h:79
Definition: index_view.h:93
iterator end()
Definition: index_view.h:114
bool is_in_view(index_type i)
Definition: index_view.h:119
iterator begin()
Definition: index_view.h:109
SliceIndexView(index_type from, index_type to)
Definition: index_view.h:95
index_type m_from
Definition: index_view.h:125
index_type m_to
Definition: index_view.h:126
the ug namespace
Definition: index_view.h:183
iterator(index_type from, index_type block)
Definition: index_view.h:184
int block() const
Definition: index_view.h:193
index_type i
Definition: index_view.h:185
bool operator==(const iterator &other) const
Definition: index_view.h:189
void operator++()
Definition: index_view.h:191
index_type m_block
Definition: index_view.h:186
bool operator!=(const iterator &other) const
Definition: index_view.h:188
int index() const
Definition: index_view.h:192
Definition: index_view.h:98
index_type i
Definition: index_view.h:100
iterator(index_type from)
Definition: index_view.h:99
void operator++()
Definition: index_view.h:105
bool operator==(const iterator &other) const
Definition: index_view.h:103
bool operator!=(const iterator &other) const
Definition: index_view.h:102
int operator*() const
Definition: index_view.h:106