Reference documentation for deal.II version 8.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
named_data.h
1 // ---------------------------------------------------------------------
2 // @f$Id: named_data.h 30036 2013-07-18 16:55:32Z maier @f$
3 //
4 // Copyright (C) 2000 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__named_data_h
18 #define __deal2__named_data_h
19 
20 #include <deal.II/base/config.h>
22 #include <deal.II/base/subscriptor.h>
23 
24 #include <vector>
25 #include <algorithm>
26 
28 
29 
47 template <typename DATA>
48 class NamedData : public Subscriptor
49 {
50 public:
54  NamedData();
55 
61  template <typename DATA2>
63 
72  void add(DATA &v, const std::string &name);
73 
80  void add(const DATA &v, const std::string &name);
81 
91  template <typename DATA2>
92  void merge(NamedData<DATA2> &);
102  template <typename DATA2>
103  void merge(const NamedData<DATA2> &);
105 
110  unsigned int size() const;
112 
125  DATA &operator() (unsigned int i);
126 
128  const DATA &operator() (unsigned int i) const;
129 
131  const DATA &read (unsigned int i) const;
132 
134  const std::string &name(unsigned int i) const;
135 
137  unsigned int find(const std::string &name) const;
138 
140  bool is_const () const;
141 
143  template <class OUT>
144  void print(OUT &o) const;
146 
153  DeclException2(ExcNameMismatch, int, std::string,
154  << "Name at position " << arg1 << " is not equal to " << arg2);
155 
164  DeclException0(ExcConstantObject);
165 
166 private:
170  std::vector<DATA> data;
171 
173  std::vector<std::string> names;
174 };
175 
176 
186 {
187 public:
195  void add (const std::string &name);
196 
202  template <typename DATA>
203  void initialize(const NamedData<DATA> &data);
204 
211  unsigned int size() const;
212 
224  unsigned int operator() (unsigned int i) const;
225 
226 private:
230  std::vector<std::string> names;
236  std::vector<unsigned int> indices;
237 };
238 
239 
240 //----------------------------------------------------------------------//
241 
242 template<typename DATA>
243 inline
245  :
246  is_constant(false)
247 {}
248 
249 
250 template<typename DATA>
251 inline
252 void
253 NamedData<DATA>::add(DATA &v, const std::string &n)
254 {
255  Assert(!is_constant, ExcConstantObject());
256  names.push_back(n);
257  data.push_back(v);
258 }
259 
260 
261 template<typename DATA>
262 inline
263 void
264 NamedData<DATA>::add(const DATA &v, const std::string &n)
265 {
266  Assert(!is_constant, ExcConstantObject());
267  DATA &aux = const_cast<DATA &>(v);
268  data.push_back(aux);
269  names.push_back(n);
270  is_constant = true;
271 }
272 
273 
274 template<typename DATA>
275 template<typename DATA2>
276 inline
277 void
279 {
280  Assert(!is_constant, ExcConstantObject());
281 
282  for (unsigned int i=0; i<other.size(); ++i)
283  {
284  names.push_back(other.name(i));
285  data.push_back(other.read(i));
286  }
287  is_constant = other.is_const();
288 }
289 
290 
291 template<typename DATA>
292 template<typename DATA2>
293 inline
294 void
296 {
297  Assert(!is_constant, ExcConstantObject());
298  for (unsigned int i=0; i<other.size(); ++i)
299  {
300  names.push_back(other.name(i));
301  data.push_back(other(i));
302  }
303  is_constant = true;
304 }
305 
306 
307 
308 template<typename DATA>
309 template<typename DATA2>
310 inline
313 {
314  is_constant = false;
315  merge(other);
316  is_constant = other.is_const();
317  return *this;
318 }
319 
320 
321 
322 template<typename DATA>
323 inline
324 unsigned int
326 {
327  return data.size();
328 }
329 
330 
331 template<typename DATA>
332 inline
333 bool
335 {
336  return is_constant;
337 }
338 
339 
340 template<typename DATA>
341 inline
342 DATA &
344 {
345  Assert(!is_constant, ExcConstantObject());
346  AssertIndexRange(i, size());
347  return data[i];
348 }
349 
350 
351 template<typename DATA>
352 inline
353 const DATA &
354 NamedData<DATA>::operator() (unsigned int i) const
355 {
356  AssertIndexRange(i, size());
357  return data[i];
358 }
359 
360 
361 template<typename DATA>
362 inline
363 const DATA &
364 NamedData<DATA>::read (unsigned int i) const
365 {
366  AssertIndexRange(i, size());
367  return data[i];
368 }
369 
370 
371 template<typename DATA>
372 inline
373 const std::string &
374 NamedData<DATA>::name(unsigned int i) const
375 {
376  AssertIndexRange(i, size());
377  return names[i];
378 }
379 
380 
381 template<typename DATA>
382 inline
383 unsigned int
384 NamedData<DATA>::find (const std::string &name) const
385 {
386  const std::vector<std::string>::const_iterator
387  i = std::find(names.begin(), names.end(), name);
388  if (i == names.end())
389  return deal_II_numbers::invalid_unsigned_int;
390  return i - names.begin();
391 }
392 
393 
394 template<typename DATA>
395 template<class OUT>
396 inline
397 void
399 {
400  o << "NamedData:";
401  for (unsigned int i=0; i<size(); ++i)
402  o << ' ' << '\"' << names[i] << '\"';
403  o << std::endl;
404 }
405 
406 
407 inline
408 void
409 NamedSelection::add(const std::string &s)
410 {
411  names.push_back(s);
412 }
413 
414 
415 template <typename DATA>
416 inline void
418 {
419  indices.resize(names.size());
420  for (unsigned int i=0; i<names.size(); ++i)
421  indices[i] = data.find(names[i]);
422 }
423 
424 
425 inline
426 unsigned int
428 {
429  return names.size();
430 }
431 
432 
433 inline
434 unsigned int
435 NamedSelection::operator() (unsigned int i) const
436 {
437  Assert (indices.size() == names.size(), ExcNotInitialized());
438  AssertIndexRange(i, size());
439  return indices[i];
440 }
441 
442 
443 DEAL_II_NAMESPACE_CLOSE
444 
445 
446 #endif
const std::string & name(unsigned int i) const
Name of object at index.
Definition: named_data.h:374
DATA & operator()(unsigned int i)
Access to stored data object by index.
Definition: named_data.h:343
void add(DATA &v, const std::string &name)
Definition: named_data.h:253
void add(const std::string &name)
Definition: named_data.h:409
std::vector< unsigned int > indices
Definition: named_data.h:236
#define AssertIndexRange(index, range)
Definition: exceptions.h:888
unsigned int operator()(unsigned int i) const
Definition: named_data.h:435
void initialize(const NamedData< DATA > &data)
Definition: named_data.h:417
const DATA & read(unsigned int i) const
Read only access for a non-const object.
Definition: named_data.h:364
unsigned int size() const
Definition: named_data.h:427
#define Assert(cond, exc)
Definition: exceptions.h:299
std::vector< std::string > names
Names for the data.
Definition: named_data.h:173
unsigned int size() const
Number of stored data objects.
Definition: named_data.h:325
unsigned int find(const std::string &name) const
Find index of a named object.
Definition: named_data.h:384
void merge(NamedData< DATA2 > &)
Definition: named_data.h:278
bool is_const() const
Returns true if this object contains constant data.
Definition: named_data.h:334
bool is_constant
True if the object is to be treated constant.
Definition: named_data.h:168
std::vector< DATA > data
The actual data stored.
Definition: named_data.h:170
std::vector< std::string > names
Definition: named_data.h:230
::ExceptionBase & ExcNotInitialized()
DeclException0(ExcConstantObject)
DeclException2(ExcNameMismatch, int, std::string,<< "Name at position "<< arg1<< " is not equal to "<< arg2)
void print(OUT &o) const
List names of stored objects.
Definition: named_data.h:398
NamedData< DATA > & operator=(const NamedData< DATA2 > &other)
Definition: named_data.h:312