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:
56  typedef C ContainerType;
58  typedef K KeyType;
60  typedef typename ContainerType::ValueType ValueType;
62  typedef typename ContainerType::SizeType SizeType;
64  typedef ValueType& Reference;
66  typedef typename ContainerType::ConstReference ConstReference;
68  typedef typename ContainerType::ArrayType ArrayType;
69 
76  cntnr(c), key(key) {}
77 
84  {
85  p.get();
86  set(p.value);
87 
88  return *this;
89  }
90 
100  template <typename D>
102  {
103  set(d);
104 
105  return *this;
106  }
107 
114  template <typename D>
116  {
117  get();
118  value += d;
119  set(value);
120 
121  return *this;
122  }
123 
130  template <typename D>
132  {
133  get();
134  value -= d;
135  set(value);
136 
137  return *this;
138  }
139 
146  template <typename D>
148  {
149  get();
150  value *= d;
151  set(value);
152 
153  return *this;
154  }
155 
162  template <typename D>
164  {
165  get();
166  value /= d;
167  set(value);
168 
169  return *this;
170  }
171 
178  template <typename D>
179  bool operator==(const D& d) const
180  {
181  get();
182 
183  return (value == d);
184  }
185 
192  template <typename D>
193  bool operator!=(const D& d) const
194  {
195  get();
196 
197  return (value != d);
198  }
199 
205  operator ConstReference() const
206  {
207  get();
208 
209  return value;
210  }
211 
212  private:
213  void set(const ValueType& v)
214  {
215  if (v == ValueType())
216  cntnr.getData().erase(key);
217 
218  else {
219  std::pair<typename ArrayType::iterator, bool> pos = cntnr.getData().insert(typename ArrayType::value_type(key, v));
220 
221  if (!pos.second)
222  pos.first->second = v;
223  }
224  }
225 
226  void get() const
227  {
228  typename ArrayType::const_iterator it = cntnr.getData().find(key);
229 
230  if (it == cntnr.getData().end())
231  value = ValueType();
232  else
233  value = it->second;
234  }
235 
236  ContainerType& cntnr;
237  KeyType key;
238  mutable ValueType value;
239  };
240 
245  template <typename C>
246  struct TypeTraits<SparseContainerElement<C> > : public TypeTraits<typename SparseContainerElement<C>::ValueType>
247  {};
248  } // namespace Math
249 } // namespace CDPL
250 
251 #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:58
SparseContainerElement & operator-=(const D &d)
Subtracts d from the value of the entry pointed to by this proxy.
Definition: SparseContainerElement.hpp:131
SparseContainerElement & operator/=(const D &d)
Divides the value of the entry pointed to by this proxy by d.
Definition: SparseContainerElement.hpp:163
ValueType & Reference
Mutable reference type to a value.
Definition: SparseContainerElement.hpp:64
ContainerType::SizeType SizeType
The size type used by the container.
Definition: SparseContainerElement.hpp:62
ContainerType::ValueType ValueType
The value type stored in the container.
Definition: SparseContainerElement.hpp:60
SparseContainerElement & operator=(const SparseContainerElement &p)
Copies the value pointed to by p into the entry pointed to by this proxy.
Definition: SparseContainerElement.hpp:83
SparseContainerElement(ContainerType &c, KeyType key)
Constructs the proxy for the (key, value) entry of the sparse container c.
Definition: SparseContainerElement.hpp:75
bool operator!=(const D &d) const
Tests the value of the entry pointed to by this proxy for inequality with d.
Definition: SparseContainerElement.hpp:193
bool operator==(const D &d) const
Tests the value of the entry pointed to by this proxy for equality with d.
Definition: SparseContainerElement.hpp:179
SparseContainerElement & operator=(const D &d)
Assigns the value d to the entry pointed to by this proxy.
Definition: SparseContainerElement.hpp:101
SparseContainerElement & operator+=(const D &d)
Adds d to the value of the entry pointed to by this proxy.
Definition: SparseContainerElement.hpp:115
SparseContainerElement & operator*=(const D &d)
Multiplies the value of the entry pointed to by this proxy by d.
Definition: SparseContainerElement.hpp:147
C ContainerType
The sparse container type the proxy references.
Definition: SparseContainerElement.hpp:56
ContainerType::ArrayType ArrayType
Underlying associative-array type of the container (typically a std::map / std::unordered_map special...
Definition: SparseContainerElement.hpp:68
ContainerType::ConstReference ConstReference
Constant reference type to a value (as defined by the container).
Definition: SparseContainerElement.hpp:66
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:285