source: src/Parameters/DiscreteValue_impl.hpp@ 30ebdd

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 30ebdd was c513b7, checked in by Michael Ankele <ankele@…>, 13 years ago

removed Converter from Parameters

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