From 74e3959b80090aee65fab3eff4f7e6b744daafe9 Mon Sep 17 00:00:00 2001 From: kanschat Date: Sun, 13 Sep 2009 19:55:57 +0000 Subject: [PATCH] new NamedData and NamedSelection classes git-svn-id: https://svn.dealii.org/trunk@19452 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/include/base/named_data.h | 384 +++++++++++++++++++++++++ deal.II/doc/news/changes.h | 7 + 2 files changed, 391 insertions(+) create mode 100644 deal.II/base/include/base/named_data.h diff --git a/deal.II/base/include/base/named_data.h b/deal.II/base/include/base/named_data.h new file mode 100644 index 0000000000..84cdb07bca --- /dev/null +++ b/deal.II/base/include/base/named_data.h @@ -0,0 +1,384 @@ +//--------------------------------------------------------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2009 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//--------------------------------------------------------------------------- +#ifndef __deal2__named_data_h +#define __deal2__named_data_h + +#include +#include +#include + +#include + +DEAL_II_NAMESPACE_OPEN + + +/** + * This class is a collection of DATA objects. Each of the pointers + * has a name associated, enabling identification by this name rather + * than the index in the vector only. + * + * Note that it is the actual data stored in this object. Therefore, + * for storing vectors and other large objects, it should be + * considered to use SmartPointer or boost::shared_ptr for DATA. + * + * Objects of this kind have a smart way of treating constness: if a + * const data object is added or a const NamedData is supplied with + * merge(), the object will henceforth consider itself as const + * (#is_constant will be true). Thus, any subsequent modification will + * be illegal and ExcConstantObject will be raised in debug mode. + * + * @author Guido Kanschat, 2007, 2008, 2009 + */ +template +class NamedData : public Subscriptor +{ + public: + /** Standard constructor creating + * an empty object. + */ + NamedData(); + /** + * \name Adding members + */ +//@{ + /** + * Add a new data item to the end + * of the collection. + */ + void add(DATA& v, const std::string& name); + + /** + * Add a new constant data item + * to the end of the collection + * and make the collection + * constant. + */ + void add(const DATA& v, const std::string& name); + + /** + * Merge the data of another + * NamedData to the end of this + * object. + * + * If the other object had + * #is_constant set, so will have + * this object after merge. + */ + void merge(NamedData&); + /** + * Merge the data of another + * NamedData to the end of this + * object. + * + * After this operation, all + * data in this object will be + * treated as const. + */ + void merge(const NamedData&); +//@} + /** + * \name Accessing and querying + * contents + */ +//@{ +/// Number of stored data objects. + unsigned int size() const; + +/// Access to stored data object by index. + DATA& operator() (unsigned int i); + +/// Read-only access to stored data object by index. + const DATA& operator() (unsigned int i) const; + +/// Name of object at index. + const std::string& name(unsigned int i) const; + +/// Find index of a named object + unsigned int find(const std::string& name) const; + +/// Returns true if this object contains constant data + bool is_const () const; + +/// List names of stored objects + template + void print(OUT& o) const; +//@} + /** + * Exception indicating that a + * function expected a vector + * to have a certain name, but + * NamedData had a different + * name in that position. + */ + DeclException2(ExcNameMismatch, int, std::string, + << "Name at position " << arg1 << " is not equal to " << arg2); + + /** + * Exception indicating that read + * access to stored data was + * attempted although the + * NamedData object contains + * const data and #is_constant + * was true. + */ + DeclException0(ExcConstantObject); + + private: + /// True if the object is to be treated constant + bool is_constant; + /// The actual data stored + std::vector data; + + /// Names for the data + std::vector names; +}; + + +/** + * Select data from NamedData corresponding to the attached name. + * + * Given a list of names to search for (provided by add()), objects of + * this class provide an index list of the selected data. + * + * @author Guido Kanschat, 2009 + */ +class NamedSelection +{ + public: + /** + * Add a new name to be searched + * for in NamedData. + * + * @note Names will be added to + * the end of the current list. + */ + void add (const std::string& name); + + /** + * Create the index vector + * pointing into the NamedData + * object. + */ + template + void initialize(const NamedData& data); + + /** + * The number of names in this + * object. + */ + unsigned int size() const; + + /** + * Return the corresponding index + * in the NamedData object + * supplied to the last + * initialize(). + * + * Indices are in the same order + * as the calls to add(). + */ + unsigned int operator() (unsigned int i) const; + + private: + /** + * The selected names. + */ + std::vector names; + /** + * The index map generated by + * initialize() and accessed by + * operator(). + */ + std::vector indices; +}; + + +//----------------------------------------------------------------------// + +template +inline +NamedData::NamedData() + : + is_constant(false) +{} + + +template +inline +void +NamedData::add(DATA& v, const std::string& n) +{ + Assert(!is_constant, ExcConstantObject()); + names.push_back(n); + data.push_back(v); +} + + +template +inline +void +NamedData::add(const DATA& v, const std::string& n) +{ + Assert(!is_constant, ExcConstantObject()); + DATA& aux = const_cast(v); + data.push_back(aux); + names.push_back(n); + is_constant = true; +} + + +template +inline +void +NamedData::merge(NamedData& other) +{ + Assert(!is_constant, ExcConstantObject()); + for (unsigned int i=0;i +inline +void +NamedData::merge(const NamedData& other) +{ + Assert(!is_constant, ExcConstantObject()); + for (unsigned int i=0;i +inline +unsigned int +NamedData::size() const +{ + return data.size(); +} + + +template +inline +bool +NamedData::is_const() const +{ + return is_constant; +} + + +template +inline +DATA& +NamedData::operator() (unsigned int i) +{ + Assert(!is_constant, ExcConstantObject()); + AssertIndexRange(i, size()); + return data[i]; +} + + +template +inline +const DATA& +NamedData::operator() (unsigned int i) const +{ + AssertIndexRange(i, size()); + return data[i]; +} + + +template +inline +const std::string& +NamedData::name(unsigned int i) const +{ + AssertIndexRange(i, size()); + return names[i]; +} + + +template +inline +unsigned int +NamedData::find (const std::string& name) const +{ + const std::vector::const_iterator + i = std::find(names.begin(), names.end(), name); + if (i == names.end()) + return deal_II_numbers::invalid_unsigned_int; + return i - names.begin(); +} + + +template +template +inline +void +NamedData::print(OUT& o) const +{ + o << "NamedData:"; + for (unsigned int i=0;i +inline void +NamedSelection::initialize(const NamedData& data) +{ + indices.resize(names.size()); + for (unsigned int i=0;ibase
    + +
  1. New: classes NamedData and NamedSelection provide an interface to store and + retrieve data objects with name identifiers. +
    + (GK 2009/09/13) +

  2. +
  3. New: The Utilities::System::job_supports_mpi() can be used to query whether -- 2.39.5