Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __deal2__named_data_h
00013 #define __deal2__named_data_h
00014
00015 #include <deal.II/base/config.h>
00016 #include <deal.II/base/exceptions.h>
00017 #include <deal.II/base/subscriptor.h>
00018
00019 #include <vector>
00020 #include <algorithm>
00021
00022 DEAL_II_NAMESPACE_OPEN
00023
00024
00042 template <typename DATA>
00043 class NamedData : public Subscriptor
00044 {
00045 public:
00049 NamedData();
00050
00056 template <typename DATA2>
00057 NamedData<DATA>& operator = (const NamedData<DATA2>& other);
00058
00067 void add(DATA& v, const std::string& name);
00068
00075 void add(const DATA& v, const std::string& name);
00076
00086 template <typename DATA2>
00087 void merge(NamedData<DATA2>&);
00097 template <typename DATA2>
00098 void merge(const NamedData<DATA2>&);
00100
00105
00106 unsigned int size() const;
00107
00120 DATA& operator() (unsigned int i);
00121
00123 const DATA& operator() (unsigned int i) const;
00124
00126 const DATA& read (unsigned int i) const;
00127
00129 const std::string& name(unsigned int i) const;
00130
00132 unsigned int find(const std::string& name) const;
00133
00135 bool is_const () const;
00136
00138 template <class OUT>
00139 void print(OUT& o) const;
00141
00148 DeclException2(ExcNameMismatch, int, std::string,
00149 << "Name at position " << arg1 << " is not equal to " << arg2);
00150
00159 DeclException0(ExcConstantObject);
00160
00161 private:
00163 bool is_constant;
00165 std::vector<DATA> data;
00166
00168 std::vector<std::string> names;
00169 };
00170
00171
00180 class NamedSelection
00181 {
00182 public:
00190 void add (const std::string& name);
00191
00197 template <typename DATA>
00198 void initialize(const NamedData<DATA>& data);
00199
00206 unsigned int size() const;
00207
00219 unsigned int operator() (unsigned int i) const;
00220
00221 private:
00225 std::vector<std::string> names;
00231 std::vector<unsigned int> indices;
00232 };
00233
00234
00235
00236
00237 template<typename DATA>
00238 inline
00239 NamedData<DATA>::NamedData()
00240 :
00241 is_constant(false)
00242 {}
00243
00244
00245 template<typename DATA>
00246 inline
00247 void
00248 NamedData<DATA>::add(DATA& v, const std::string& n)
00249 {
00250 Assert(!is_constant, ExcConstantObject());
00251 names.push_back(n);
00252 data.push_back(v);
00253 }
00254
00255
00256 template<typename DATA>
00257 inline
00258 void
00259 NamedData<DATA>::add(const DATA& v, const std::string& n)
00260 {
00261 Assert(!is_constant, ExcConstantObject());
00262 DATA& aux = const_cast<DATA&>(v);
00263 data.push_back(aux);
00264 names.push_back(n);
00265 is_constant = true;
00266 }
00267
00268
00269 template<typename DATA>
00270 template<typename DATA2>
00271 inline
00272 void
00273 NamedData<DATA>::merge(NamedData<DATA2>& other)
00274 {
00275 Assert(!is_constant, ExcConstantObject());
00276
00277 for (unsigned int i=0;i<other.size();++i)
00278 {
00279 names.push_back(other.name(i));
00280 data.push_back(other.read(i));
00281 }
00282 is_constant = other.is_const();
00283 }
00284
00285
00286 template<typename DATA>
00287 template<typename DATA2>
00288 inline
00289 void
00290 NamedData<DATA>::merge(const NamedData<DATA2>& other)
00291 {
00292 Assert(!is_constant, ExcConstantObject());
00293 for (unsigned int i=0;i<other.size();++i)
00294 {
00295 names.push_back(other.name(i));
00296 data.push_back(other(i));
00297 }
00298 is_constant = true;
00299 }
00300
00301
00302
00303 template<typename DATA>
00304 template<typename DATA2>
00305 inline
00306 NamedData<DATA>&
00307 NamedData<DATA>::operator= (const NamedData<DATA2>& other)
00308 {
00309 is_constant = false;
00310 merge(other);
00311 is_constant = other.is_const();
00312 return *this;
00313 }
00314
00315
00316
00317 template<typename DATA>
00318 inline
00319 unsigned int
00320 NamedData<DATA>::size() const
00321 {
00322 return data.size();
00323 }
00324
00325
00326 template<typename DATA>
00327 inline
00328 bool
00329 NamedData<DATA>::is_const() const
00330 {
00331 return is_constant;
00332 }
00333
00334
00335 template<typename DATA>
00336 inline
00337 DATA&
00338 NamedData<DATA>::operator() (unsigned int i)
00339 {
00340 Assert(!is_constant, ExcConstantObject());
00341 AssertIndexRange(i, size());
00342 return data[i];
00343 }
00344
00345
00346 template<typename DATA>
00347 inline
00348 const DATA&
00349 NamedData<DATA>::operator() (unsigned int i) const
00350 {
00351 AssertIndexRange(i, size());
00352 return data[i];
00353 }
00354
00355
00356 template<typename DATA>
00357 inline
00358 const DATA&
00359 NamedData<DATA>::read (unsigned int i) const
00360 {
00361 AssertIndexRange(i, size());
00362 return data[i];
00363 }
00364
00365
00366 template<typename DATA>
00367 inline
00368 const std::string&
00369 NamedData<DATA>::name(unsigned int i) const
00370 {
00371 AssertIndexRange(i, size());
00372 return names[i];
00373 }
00374
00375
00376 template<typename DATA>
00377 inline
00378 unsigned int
00379 NamedData<DATA>::find (const std::string& name) const
00380 {
00381 const std::vector<std::string>::const_iterator
00382 i = std::find(names.begin(), names.end(), name);
00383 if (i == names.end())
00384 return deal_II_numbers::invalid_unsigned_int;
00385 return i - names.begin();
00386 }
00387
00388
00389 template<typename DATA>
00390 template<class OUT>
00391 inline
00392 void
00393 NamedData<DATA>::print(OUT& o) const
00394 {
00395 o << "NamedData:";
00396 for (unsigned int i=0;i<size();++i)
00397 o << ' ' << '\"' << names[i] << '\"';
00398 o << std::endl;
00399 }
00400
00401
00402 inline
00403 void
00404 NamedSelection::add(const std::string& s)
00405 {
00406 names.push_back(s);
00407 }
00408
00409
00410 template <typename DATA>
00411 inline void
00412 NamedSelection::initialize(const NamedData<DATA>& data)
00413 {
00414 indices.resize(names.size());
00415 for (unsigned int i=0;i<names.size();++i)
00416 indices[i] = data.find(names[i]);
00417 }
00418
00419
00420 inline
00421 unsigned int
00422 NamedSelection::size() const
00423 {
00424 return names.size();
00425 }
00426
00427
00428 inline
00429 unsigned int
00430 NamedSelection::operator() (unsigned int i) const
00431 {
00432 Assert (indices.size() == names.size(), ExcNotInitialized());
00433 AssertIndexRange(i, size());
00434 return indices[i];
00435 }
00436
00437
00438 DEAL_II_NAMESPACE_CLOSE
00439
00440
00441 #endif
00442