Chemical Data Processing Library C++ API - Version 1.4.0
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 
50  template <typename C, typename K = typename C::KeyType>
52  {
53 
54  public:
58  typedef C ContainerType;
59 
63  typedef K KeyType;
64 
68  typedef typename ContainerType::ValueType ValueType;
69 
73  typedef typename ContainerType::SizeType SizeType;
74 
78  typedef ValueType& Reference;
79 
83  typedef typename ContainerType::ConstReference ConstReference;
84 
88  typedef typename ContainerType::ArrayType ArrayType;
89 
96  cntnr(c), key(key) {}
97 
104  {
105  p.get();
106  set(p.value);
107 
108  return *this;
109  }
110 
120  template <typename D>
122  {
123  set(d);
124 
125  return *this;
126  }
127 
134  template <typename D>
136  {
137  get();
138  value += d;
139  set(value);
140 
141  return *this;
142  }
143 
150  template <typename D>
152  {
153  get();
154  value -= d;
155  set(value);
156 
157  return *this;
158  }
159 
166  template <typename D>
168  {
169  get();
170  value *= d;
171  set(value);
172 
173  return *this;
174  }
175 
182  template <typename D>
184  {
185  get();
186  value /= d;
187  set(value);
188 
189  return *this;
190  }
191 
198  template <typename D>
199  bool operator==(const D& d) const
200  {
201  get();
202 
203  return (value == d);
204  }
205 
212  template <typename D>
213  bool operator!=(const D& d) const
214  {
215  get();
216 
217  return (value != d);
218  }
219 
225  operator ConstReference() const
226  {
227  get();
228 
229  return value;
230  }
231 
232  private:
233  void set(const ValueType& v)
234  {
235  if (v == ValueType())
236  cntnr.getData().erase(key);
237 
238  else {
239  std::pair<typename ArrayType::iterator, bool> pos = cntnr.getData().insert(typename ArrayType::value_type(key, v));
240 
241  if (!pos.second)
242  pos.first->second = v;
243  }
244  }
245 
246  void get() const
247  {
248  typename ArrayType::const_iterator it = cntnr.getData().find(key);
249 
250  if (it == cntnr.getData().end())
251  value = ValueType();
252  else
253  value = it->second;
254  }
255 
256  ContainerType& cntnr;
257  KeyType key;
258  mutable ValueType value;
259  };
260 
265  template <typename C>
266  struct TypeTraits<SparseContainerElement<C> > : public TypeTraits<typename SparseContainerElement<C>::ValueType>
267  {};
268  } // namespace Math
269 } // namespace CDPL
270 
271 #endif // CDPL_MATH_SPARSECONTAINERELEMENT_HPP
Definition of various preprocessor macros for error checking.
Definition of type traits.
Proxy that exposes a single (key, value) entry of a sparse container as a writable reference.
Definition: SparseContainerElement.hpp:52
K KeyType
The key type used to address an entry of the container.
Definition: SparseContainerElement.hpp:63
SparseContainerElement & operator-=(const D &d)
Subtracts d from the value of the entry pointed to by this proxy.
Definition: SparseContainerElement.hpp:151
SparseContainerElement & operator/=(const D &d)
Divides the value of the entry pointed to by this proxy by d.
Definition: SparseContainerElement.hpp:183
ValueType & Reference
Mutable element value reference type.
Definition: SparseContainerElement.hpp:78
ContainerType::SizeType SizeType
The size type used by the container.
Definition: SparseContainerElement.hpp:73
ContainerType::ValueType ValueType
The value type stored in the container.
Definition: SparseContainerElement.hpp:68
SparseContainerElement & operator=(const SparseContainerElement &p)
Copies the value pointed to by p into the entry pointed to by this proxy.
Definition: SparseContainerElement.hpp:103
SparseContainerElement(ContainerType &c, KeyType key)
Constructs the proxy for the (key, value) entry of the sparse container c.
Definition: SparseContainerElement.hpp:95
bool operator!=(const D &d) const
Tests the value of the entry pointed to by this proxy for inequality with d.
Definition: SparseContainerElement.hpp:213
bool operator==(const D &d) const
Tests the value of the entry pointed to by this proxy for equality with d.
Definition: SparseContainerElement.hpp:199
SparseContainerElement & operator=(const D &d)
Assigns the value d to the entry pointed to by this proxy.
Definition: SparseContainerElement.hpp:121
SparseContainerElement & operator+=(const D &d)
Adds d to the value of the entry pointed to by this proxy.
Definition: SparseContainerElement.hpp:135
SparseContainerElement & operator*=(const D &d)
Multiplies the value of the entry pointed to by this proxy by d.
Definition: SparseContainerElement.hpp:167
C ContainerType
The sparse container type the proxy references.
Definition: SparseContainerElement.hpp:58
ContainerType::ArrayType ArrayType
Underlying associative-array type of the container (typically a std::map / std::unordered_map special...
Definition: SparseContainerElement.hpp:88
ContainerType::ConstReference ConstReference
Constant element value reference type (as defined by the container).
Definition: SparseContainerElement.hpp:83
constexpr unsigned int D
Specifies Hydrogen (Deuterium).
Definition: AtomType.hpp:62
constexpr unsigned int K
Specifies Potassium.
Definition: AtomType.hpp:157
constexpr unsigned int C
Specifies Carbon.
Definition: AtomType.hpp:92
constexpr unsigned int p
Specifies that the stereocenter has p configuration.
Definition: CIPDescriptor.hpp:121
The namespace of the Chemical Data Processing Library.
Primary traits template for scalar arithmetic value types.
Definition: TypeTraits.hpp:307