- Timestamp:
- Jul 21, 2010, 1:55:18 PM (15 years ago)
- Branches:
- 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
- Children:
- a6d6a9
- Parents:
- 574ddc1
- Location:
- src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Formula.cpp
r574ddc1 rd03bb1 26 26 {} 27 27 28 Formula::Formula(const string &formula) : 29 numElements(0) 30 { 31 fromString(formula); 32 } 33 28 34 Formula::~Formula() 29 35 {} … … 42 48 } 43 49 return sstr.str(); 50 } 51 52 // quick function used for parsing 53 bool isInRange(pair<char,char> range,char character){ 54 return range.first<=character && character<=range.second; 55 } 56 57 void Formula::fromString(const std::string &formula) throw(ParseError){ 58 // some constants needed for parsing... Assumes ASCII, change if other encodings are used 59 static const pair<char,char> CapitalLetters = make_pair('A','Z'); 60 static const pair<char,char> SmallLetters = make_pair('a','z'); 61 static const pair<char,char> Numbers = make_pair('0','9'); 62 // clean the formula 63 clear(); 64 string::const_iterator end = formula.end(); // will be used frequently 65 for(string::const_iterator it=formula.begin();it!=end;){ 66 string shorthand; 67 // Atom names start with a capital letter 68 if(!isInRange(CapitalLetters,(*it))) 69 throw(ParseError(__FILE__,__LINE__)); 70 shorthand+=(*it++); 71 // the rest of the name follows 72 while(it!=end && isInRange(SmallLetters,(*it))) 73 shorthand+=(*it++); 74 // now we can count the occurences 75 int count = 0; 76 while(it!=end && isInRange(Numbers,(*it))) 77 count = (count*10) + ((*it++)-Numbers.first); 78 // one is implicit 79 count = (count!=0)?count:1; 80 // done, we can get the next one 81 addElements(shorthand,count); 82 } 44 83 } 45 84 … … 122 161 } 123 162 163 void Formula::addElements(const element *element,unsigned int count){ 164 ASSERT(element,"Invalid pointer in Formula::addElements(element*)"); 165 addElements(element->getNumber(),count); 166 } 167 168 void Formula::addElements(atomicNumber_t Z,unsigned int count){ 169 if(count==0) return; 170 ASSERT(Z>0,"Invalid atomic Number"); 171 ASSERT(World::getInstance().getPeriode()->FindElement(Z),"No Element with this number in Periodentafel"); 172 elementCounts.resize(Z); // No-op when we already have the right size 173 // might need to update number of elements 174 if(!elementCounts[Z-1]){ 175 numElements++; 176 } 177 elementCounts[Z-1]+=count; 178 } 179 180 void Formula::addElements(const string &shorthand,unsigned int count){ 181 element * element = World::getInstance().getPeriode()->FindElement(shorthand); 182 addElements(element,count); 183 } 184 124 185 const unsigned int &Formula::operator[](const element *element) const{ 125 186 ASSERT(element,"Invalid pointer in access of Formula"); … … 181 242 Formula::const_reverse_iterator Formula::rend() const{ 182 243 return const_reverse_iterator(begin()); 244 } 245 246 void Formula::clear(){ 247 elementCounts.clear(); 248 numElements = 0; 183 249 } 184 250 -
src/Formula.hpp
r574ddc1 rd03bb1 12 12 #include <iosfwd> 13 13 #include <iterator> 14 15 #include "Exceptions/ParseError.hpp" 14 16 15 17 #include "types.hpp" … … 53 55 54 56 Formula(); 57 Formula(const std::string&); 55 58 Formula(const Formula&); 56 59 virtual ~Formula(); … … 59 62 60 63 std::string toString() const; 64 void fromString(const std::string&) throw(ParseError); 61 65 bool checkOut(std::ostream*) const; 62 66 … … 73 77 void operator-=(atomicNumber_t); 74 78 void operator-=(const std::string&); 79 80 void addElements(const element*,unsigned int); 81 void addElements(atomicNumber_t,unsigned int); 82 void addElements(const std::string&,unsigned int); 75 83 76 84 // only const versions, because someone might try to increment a previously … … 93 101 const_reverse_iterator rend() const; 94 102 103 void clear(); 104 95 105 private: 96 106 mutable set_t elementCounts; // we might need to resize even at const places
Note:
See TracChangeset
for help on using the changeset viewer.