Chemical Data Processing Library C++ API - Version 1.2.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 
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
Definition of various preprocessor macros for error checking.
Definition of type traits.
Definition: SparseContainerElement.hpp:42
K KeyType
Definition: SparseContainerElement.hpp:46
SparseContainerElement & operator-=(const D &d)
Definition: SparseContainerElement.hpp:84
SparseContainerElement & operator/=(const D &d)
Definition: SparseContainerElement.hpp:104
ValueType & Reference
Definition: SparseContainerElement.hpp:49
ContainerType::SizeType SizeType
Definition: SparseContainerElement.hpp:48
ContainerType::ValueType ValueType
Definition: SparseContainerElement.hpp:47
SparseContainerElement & operator=(const SparseContainerElement &p)
Definition: SparseContainerElement.hpp:57
SparseContainerElement(ContainerType &c, KeyType key)
Definition: SparseContainerElement.hpp:53
bool operator!=(const D &d) const
Definition: SparseContainerElement.hpp:123
bool operator==(const D &d) const
Definition: SparseContainerElement.hpp:115
SparseContainerElement & operator=(const D &d)
Definition: SparseContainerElement.hpp:66
SparseContainerElement & operator+=(const D &d)
Definition: SparseContainerElement.hpp:74
SparseContainerElement & operator*=(const D &d)
Definition: SparseContainerElement.hpp:94
C ContainerType
Definition: SparseContainerElement.hpp:45
ContainerType::ArrayType ArrayType
Definition: SparseContainerElement.hpp:51
ContainerType::ConstReference ConstReference
Definition: SparseContainerElement.hpp:50
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.
Definition: TypeTraits.hpp:171