source: src/Parser/Parameters/DiscreteValue_impl.hpp@ 7d98e7

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 7d98e7 was 498ddd, checked in by Frederik Heber <heber@…>, 13 years ago

Added operator==() and operator!=() to Continuous- and DiscreteValue.

  • ValueInterface is now inherited virtually.
  • this is preparatory for class Parameter which is a clonable ...Value.
  • we need comparators for the unit tests on that.
  • unit test added for comparators.
  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * DiscreteValues_impl.hpp
3 *
4 * Created on: Sep 28, 2011
5 * Author: heber
6 */
7
8#ifndef DISCRETEVALUE_IMPL_HPP_
9#define DISCRETEVALUE_IMPL_HPP_
10
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16#include <algorithm>
17#include <vector>
18
19#include <boost/any.hpp>
20
21#include "CodePatterns/Assert.hpp"
22
23#include "CodePatterns/Log.hpp"
24
25// static member
26template <class T> ConvertTo<T> DiscreteValue<T>::Converter;
27
28/** Constructor of class DiscreteValue.
29 */
30template <class T>
31DiscreteValue<T>::DiscreteValue() :
32 ValueSet(false)
33{}
34
35/** Constructor of class DiscreteValue with set of valid values.
36 *
37 * @param _ValidValues vector with all valid values
38 */
39template <class T>
40DiscreteValue<T>::DiscreteValue(const std::vector<T> &_ValidValues) :
41 ValueSet(false),
42 ValidValues(_ValidValues)
43{}
44
45/** Destructor of class DiscreteValue.
46 */
47template <class T>
48DiscreteValue<T>::~DiscreteValue()
49{}
50
51/** Checks whether \a _value is a valid value.
52 * \param _value value to check for validity.
53 * \return true - \a _value is valid, false - is not
54 */
55template <class T>
56bool DiscreteValue<T>::isValid(const std::string _value) const
57{
58 const T castvalue = Converter(_value);
59 return isValidValue(castvalue);
60}
61
62/** Compares this discrete value against another \a _instance.
63 *
64 * @param _instance other value to compare to
65 * @return true - if value and valid ranges are the same, false - else
66 */
67template <class T>
68bool DiscreteValue<T>::operator==(const DiscreteValue<T> &_instance) const
69{
70 bool status = true;
71 status = status && (ValidValues == _instance.ValidValues);
72 status = status && (ValueSet == _instance.ValueSet);
73 if (ValueSet && _instance.ValueSet)
74 status = status && (value == _instance.value);
75 return status;
76}
77
78
79/** Getter of value, returning string.
80 *
81 * @return string value
82 */
83template <class T>
84const std::string DiscreteValue<T>::get() const
85{
86 ASSERT(ValueSet,
87 "DiscreteValue<T>::get() - requesting unset value.");
88 return toString(getValue());
89}
90
91/** Setter of value for string
92 *
93 * @param _value string containing new value
94 */
95template <class T>
96void DiscreteValue<T>::set(const std::string _value)
97{
98 const T castvalue = Converter(_value);
99 setValue(castvalue);
100}
101
102
103/** Internal function for finding the index of a desired value.
104 *
105 * \note As this is internal, we do not ASSERT value's presence, but return -1
106 * such that other functions may ASSERT on that.
107 *
108 * \param _value value to get the index of
109 * \return index such that ValidValues[index] == _value
110 */
111template <class T>
112const size_t DiscreteValue<T>::findIndexOfValue(const T &_value) const
113{
114 size_t index = 0;
115 const size_t max = ValidValues.size();
116 for (; index < max; ++index) {
117 if (ValidValues[index] == _value)
118 break;
119 }
120 if (index == max)
121 return (size_t)-1;
122 else
123 return index;
124}
125
126/** Adds another value to the valid ones.
127 *
128 * We check whether its already present, otherwise we throw an Assert::AssertionFailure.
129 *
130 * @param _value value to add
131 */
132template <class T>
133void DiscreteValue<T>::appendValidValue(const T &_value)
134{
135 ASSERT(!isValidValue(_value),
136 "DiscreteValue<>::appendValidValue() - value "+toString(_value)+" is already among the valid");
137 ValidValues.push_back(_value);
138}
139
140/** Returns all possible valid values.
141 *
142 * @return vector with all allowed values
143 */
144template <class T>
145const std::vector<T> &DiscreteValue<T>::getValidValues() const
146{
147 return ValidValues;
148}
149
150/** Sets the value.
151 *
152 * We check for its validity, otherwise we throw an Assert::AssertionFailure.
153 *
154 * @param _value const reference of value to set
155 */
156template <class T>
157void DiscreteValue<T>::setValue(const T &_value)
158{
159 const size_t index = findIndexOfValue(_value);
160 ASSERT(index != (size_t)-1,
161 "DiscreteValue<>::set() - value "+toString(_value)+" is not valid.");
162 if (!ValueSet)
163 ValueSet = true;
164 value = index;
165}
166
167/** Getter for the set value.
168 *
169 * We check whether it has been set, otherwise we throw an Assert::AssertionFailure.
170 *
171 * @return set value
172 */
173template <class T>
174const T & DiscreteValue<T>::getValue() const
175{
176 ASSERT(ValueSet,
177 "DiscreteValue<>::get() - value has never been set.");
178 return ValidValues[value];
179}
180
181/** Checks whether \a _value is a valid value.
182 * \param _value value to check for validity.
183 * \return true - \a _value is valid, false - is not
184 */
185template <class T>
186bool DiscreteValue<T>::isValidValue(const T &_value) const
187{
188 typename ValidRange::const_iterator iter = std::find(ValidValues.begin(), ValidValues.end(), _value);
189 if (iter != ValidValues.end()) {
190 //std::cout << "Found " << _value << ":" << *iter << std::endl;
191 return true;
192 } else {
193 //std::cout << "Did not find " << _value << "." << std::endl;
194 return false;
195 }
196}
197
198#endif /* DISCRETEVALUE_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.