Chemical Data Processing Library C++ API - Version 1.1.1
SparseContainerElement.hpp
Go to the documentation of this file.
1 /*
2  * SparseContainerElement.hpp
3  *
4  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
27 #ifndef CDPL_MATH_SPARSECONTAINERELEMENT_HPP
28 #define CDPL_MATH_SPARSECONTAINERELEMENT_HPP
29 
30 #include "CDPL/Math/Check.hpp"
31 #include "CDPL/Math/TypeTraits.hpp"
32 
33 
34 namespace CDPL
35 {
36 
37  namespace Math
38  {
39 
40  template <typename C, typename K = typename C::KeyType>
42  {
43 
44  public:
45  typedef C ContainerType;
46  typedef K KeyType;
47  typedef typename ContainerType::ValueType ValueType;
48  typedef typename ContainerType::SizeType SizeType;
49  typedef ValueType& Reference;
50  typedef typename ContainerType::ConstReference ConstReference;
51  typedef typename ContainerType::ArrayType ArrayType;
52 
54  cntnr(c), key(key) {}
55 
56  // Assignment
58  {
59  p.get();
60  set(p.value);
61 
62  return *this;
63  }
64 
65  template <typename D>
67  {
68  set(d);
69 
70  return *this;
71  }
72 
73  template <typename D>
75  {
76  get();
77  value += d;
78  set(value);
79 
80  return *this;
81  }
82 
83  template <typename D>
85  {
86  get();
87  value -= d;
88  set(value);
89 
90  return *this;
91  }
92 
93  template <typename D>
95  {
96  get();
97  value *= d;
98  set(value);
99 
100  return *this;
101  }
102 
103  template <typename D>
105  {
106  get();
107  value /= d;
108  set(value);
109 
110  return *this;
111  }
112 
113  // Comparison
114  template <typename D>
115  bool operator==(const D& d) const
116  {
117  get();
118 
119  return (value == d);
120  }
121 
122  template <typename D>
123  bool operator!=(const D& d) const
124  {
125  get();
126 
127  return (value != d);
128  }
129 
130  operator ConstReference() const
131  {
132  get();
133 
134  return value;
135  }
136 
137  private:
138  void set(const ValueType& v)
139  {
140  if (v == ValueType())
141  cntnr.getData().erase(key);
142 
143  else {
144  std::pair<typename ArrayType::iterator, bool> pos = cntnr.getData().insert(typename ArrayType::value_type(key, v));
145 
146  if (!pos.second)
147  pos.first->second = v;
148  }
149  }
150 
151  void get() const
152  {
153  typename ArrayType::const_iterator it = cntnr.getData().find(key);
154 
155  if (it == cntnr.getData().end())
156  value = ValueType();
157  else
158  value = it->second;
159  }
160 
161  ContainerType& cntnr;
162  KeyType key;
163  mutable ValueType value;
164  };
165 
166  template <typename C>
167  struct TypeTraits<SparseContainerElement<C> > : public TypeTraits<typename SparseContainerElement<C>::ValueType>
168  {};
169  } // namespace Math
170 } // namespace CDPL
171 
172 #endif // CDPL_MATH_SPARSECONTAINERELEMENT_HPP
CDPL::Chem::CIPDescriptor::p
const unsigned int p
Specifies that the stereocenter has p configuration.
Definition: CIPDescriptor.hpp:121
CDPL::Math::TypeTraits
Definition: TypeTraits.hpp:171
CDPL::Math::SparseContainerElement::KeyType
K KeyType
Definition: SparseContainerElement.hpp:46
CDPL::Math::SparseContainerElement::operator/=
SparseContainerElement & operator/=(const D &d)
Definition: SparseContainerElement.hpp:104
CDPL::Math::SparseContainerElement::SizeType
ContainerType::SizeType SizeType
Definition: SparseContainerElement.hpp:48
CDPL::Math::SparseContainerElement::ContainerType
C ContainerType
Definition: SparseContainerElement.hpp:45
CDPL::Math::SparseContainerElement
Definition: SparseContainerElement.hpp:42
CDPL::Math::SparseContainerElement::operator=
SparseContainerElement & operator=(const SparseContainerElement &p)
Definition: SparseContainerElement.hpp:57
CDPL::Math::SparseContainerElement::operator*=
SparseContainerElement & operator*=(const D &d)
Definition: SparseContainerElement.hpp:94
CDPL::Math::SparseContainerElement::ValueType
ContainerType::ValueType ValueType
Definition: SparseContainerElement.hpp:47
CDPL::Math::SparseContainerElement::operator==
bool operator==(const D &d) const
Definition: SparseContainerElement.hpp:115
CDPL::Math::SparseContainerElement::ConstReference
ContainerType::ConstReference ConstReference
Definition: SparseContainerElement.hpp:50
CDPL::Math::SparseContainerElement::ArrayType
ContainerType::ArrayType ArrayType
Definition: SparseContainerElement.hpp:51
CDPL::Math::SparseContainerElement::operator!=
bool operator!=(const D &d) const
Definition: SparseContainerElement.hpp:123
TypeTraits.hpp
Definition of type traits.
CDPL::Math::SparseContainerElement::SparseContainerElement
SparseContainerElement(ContainerType &c, KeyType key)
Definition: SparseContainerElement.hpp:53
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Math::SparseContainerElement::operator-=
SparseContainerElement & operator-=(const D &d)
Definition: SparseContainerElement.hpp:84
CDPL::Chem::AtomType::K
const unsigned int K
Specifies Potassium.
Definition: AtomType.hpp:157
CDPL::Math::SparseContainerElement::Reference
ValueType & Reference
Definition: SparseContainerElement.hpp:49
CDPL::Math::SparseContainerElement::operator+=
SparseContainerElement & operator+=(const D &d)
Definition: SparseContainerElement.hpp:74
CDPL::Chem::AtomType::D
const unsigned int D
Specifies Hydrogen (Deuterium).
Definition: AtomType.hpp:62
Check.hpp
Definition of various preprocessor macros for error checking.
CDPL::Chem::AtomType::C
const unsigned int C
Specifies Carbon.
Definition: AtomType.hpp:92
CDPL::Math::SparseContainerElement::operator=
SparseContainerElement & operator=(const D &d)
Definition: SparseContainerElement.hpp:66