source: src/Parser/DiscreteValue_impl.hpp@ 3308b6

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 3308b6 was e09d67, checked in by Frederik Heber <heber@…>, 14 years ago

Added class DiscreteValue that contains enums.

  • class has extensive unit test.
  • this should replace enums in MpqcParser_Parameters.
  • Property mode set to 100644
File size: 3.0 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 <vector>
17
18#include "CodePatterns/Assert.hpp"
19
20/** Constructor of class DiscreteValue.
21 */
22template <class T>
23DiscreteValue<T>::DiscreteValue() :
24 ValueSet(false)
25{}
26
27/** Constructor of class DiscreteValue.
28 */
29template <class T>
30DiscreteValue<T>::DiscreteValue(const std::vector<T> &_ValidValues) :
31 ValueSet(false),
32 ValidValues(_ValidValues)
33{}
34
35/** Destructor of class DiscreteValue.
36 */
37template <class T>
38DiscreteValue<T>::~DiscreteValue()
39{}
40
41/** Checks whether \a _value is a valid value.
42 * \param _value value to check for validity.
43 * \return true - \a _value is valid, false - is not
44 */
45template <class T>
46bool DiscreteValue<T>::isValid(const T &_value) const
47{
48 const size_t index = findIndexOfValue(_value);
49 if (index == (size_t)-1)
50 return false;
51 else
52 return true;
53}
54
55/** Internal function for finding the index of a desired value.
56 *
57 * \note As this is internal, we do not ASSERT value's presence, but return -1
58 * such that other functions may ASSERT on that.
59 *
60 * \param _value value to get the index of
61 * \return index such that ValidValues[index] == _value
62 */
63template <class T>
64const size_t DiscreteValue<T>::findIndexOfValue(const T &_value) const
65{
66 size_t index = 0;
67 const size_t max = ValidValues.size();
68 for (; index < max; ++index) {
69 if (ValidValues[index] == _value)
70 break;
71 }
72 if (index == max)
73 return (size_t)-1;
74 else
75 return index;
76}
77
78/** Adds another value to the valid ones.
79 *
80 * We check whether its already present, otherwise we throw an Assert::AssertionFailure.
81 *
82 * @param _value value to add
83 */
84template <class T>
85void DiscreteValue<T>::appendValidValue(const T &_value)
86{
87 ASSERT(!isValid(_value),
88 "DiscreteValue<>::appendValidValue() - value "+toString(_value)+" is already among the valid");
89 ValidValues.push_back(_value);
90}
91
92/** Returns all possible valid values.
93 *
94 * @return vector with all allowed values
95 */
96template <class T>
97const std::vector<T> &DiscreteValue<T>::getValidValues() const
98{
99 return ValidValues;
100}
101
102/** Sets the value.
103 *
104 * We check for its validity, otherwise we throw an Assert::AssertionFailure.
105 *
106 * @param _value const reference of value to set
107 */
108template <class T>
109void DiscreteValue<T>::set(const T &_value)
110{
111 const size_t index = findIndexOfValue(_value);
112 ASSERT(index != (size_t)-1,
113 "DiscreteValue<>::set() - value "+toString(_value)+" is not valid.");
114 if (!ValueSet)
115 ValueSet = true;
116 value = index;
117}
118
119/** Getter fot the set value.
120 *
121 * We check whether it has been set, otherwise we throw an Assert::AssertionFailure.
122 *
123 * @return set value
124 */
125template <class T>
126const T & DiscreteValue<T>::get() const
127{
128 ASSERT(ValueSet,
129 "DiscreteValue<>::get() - value has never been set.");
130 return ValidValues[value];
131}
132
133
134#endif /* DISCRETEVALUE_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.