Changeset 6d35e4 for src/atom.cpp
- Timestamp:
- May 7, 2008, 1:38:13 PM (18 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, Candidate_v1.7.0, 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:
- 8d9c38
- Parents:
- db066d
- File:
-
- 1 edited
-
src/atom.cpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
src/atom.cpp
rdb066d r6d35e4 106 106 }; 107 107 108 /******************************** Functions for class AtomStackClass ********************************/109 110 /** Constructor of class AtomStackClass.111 */112 AtomStackClass::AtomStackClass(int dimension)113 {114 CurrentLastEntry = 0;115 CurrentFirstEntry = 0;116 NextFreeField = 0;117 EntryCount = dimension;118 StackList = (atom **) Malloc(sizeof(atom *)*EntryCount, "AtomStackClass::AtomStackClass: **StackList");119 };120 121 /** Destructor of class AtomStackClass.122 */123 AtomStackClass::~AtomStackClass()124 {125 Free((void **)&StackList, "AtomStackClass::AtomStackClass: **StackList");126 };127 128 /** Pushes an object onto the stack.129 * \param *object atom to be pushed on stack130 * \return true - sucess, false - failure, meaning stack field was occupied131 */132 bool AtomStackClass::Push(atom *object)133 {134 if (!IsFull()) { // check whether free field is really not occupied135 StackList[NextFreeField] = object;136 CurrentLastEntry = NextFreeField;137 NextFreeField = (NextFreeField + 1) % EntryCount; // step on to next free field138 return true;139 } else {140 cerr << "ERROR: Stack is full, " << "Stack: CurrentLastEntry " << CurrentLastEntry<< "\tCurrentFirstEntry " << CurrentFirstEntry << "\tNextFreeField " << NextFreeField << "\tEntryCount " << EntryCount << "!" << endl;141 return false;142 }143 };144 145 /** Pops first/oldest atom from stack.146 * First in, last out.147 * \return atom pointer from stack, NULL - if failure (no atom pointer in field)148 */149 atom *AtomStackClass::PopFirst()150 {151 atom *Walker = NULL;152 if (!IsEmpty()) {153 Walker = StackList[CurrentFirstEntry];154 if (Walker == NULL)155 cerr << "ERROR: Stack's field is empty!" << endl;156 StackList[CurrentFirstEntry] = NULL;157 if (CurrentFirstEntry != CurrentLastEntry) { // hasn't last item been popped as well?158 CurrentFirstEntry = (CurrentFirstEntry + 1) % EntryCount; // step back from current free field to last used (somehow modulo does not work in -1)159 } else {160 CurrentFirstEntry = (CurrentFirstEntry + 1) % EntryCount; // step back from current free field to last used (somehow modulo does not work in -1)161 CurrentLastEntry = CurrentFirstEntry;162 }163 } else164 cerr << "ERROR: Stack is empty!" << endl;165 return Walker;166 };167 168 /** Pops last atom from stack.169 * First in, first out.170 * \return atom pointer from stack, NULL - if failure (no atom pointer in field)171 */172 atom *AtomStackClass::PopLast()173 {174 atom *Walker = NULL;175 if (!IsEmpty()) {176 Walker = StackList[CurrentLastEntry];177 StackList[CurrentLastEntry] = NULL;178 if (Walker == NULL)179 cerr << "ERROR: Stack's field is empty!" << endl;180 NextFreeField = CurrentLastEntry;181 if (CurrentLastEntry != CurrentFirstEntry) // has there been more than one item on stack182 CurrentLastEntry = (CurrentLastEntry + (EntryCount-1)) % EntryCount; // step back from current free field to last (modulo does not work in -1, thus go EntryCount-1 instead)183 } else {184 cerr << "ERROR: Stack is empty!" << endl;185 }186 return Walker;187 };188 189 /** Removes a certain item from the stack.190 * Item is seeked between \a CurrentFirstEntry and \a CurrentLastEntry, if found, place in stack is NULL'd and191 * all subsequent items shifted by one position downward (with wrapping taken into account).192 * \param *ptr adress of item193 * \return true - item was removed, false - item was not found194 */195 bool AtomStackClass::RemoveItem(atom *ptr)196 {197 bool found = false;198 cout << Verbose(5) << "First " << CurrentFirstEntry<< "\tLast " << CurrentLastEntry<< "\tNext " << NextFreeField<< "\tCount " << EntryCount<< "." << endl;199 int i=CurrentFirstEntry;200 if (!IsEmpty())201 do {202 if (StackList[i] == ptr) { // if item found, remove203 cout << Verbose(5) << "Item " << *ptr << " was number " << i << " on stack, removing it." << endl;204 found = true;205 StackList[i] = NULL;206 }207 if ((found) && (StackList[i] != NULL)) { // means we have to shift (and not the removed item)208 if (i == 0) { // we are down to first item in stack, have to put onto last item209 cout << Verbose(5) << "Shifting item 0 to place " << EntryCount-1 << "." << endl;210 StackList[EntryCount-1] = StackList[0];211 } else {212 cout << Verbose(5) << "Shifting item " << i << " to place " << i-1 << "." << endl;213 StackList[i-1] = StackList[i];214 }215 }216 i=((i + 1) % EntryCount); // step on217 } while (i!=NextFreeField);218 else219 cerr << "ERROR: Stack is already empty!" << endl;220 if (found) {221 NextFreeField = CurrentLastEntry;222 if (CurrentLastEntry != CurrentFirstEntry) // has there been more than one item on stack223 CurrentLastEntry = (CurrentLastEntry + (EntryCount-1)) % EntryCount;224 }225 return found;226 };227 228 /** Test the functionality of the stack.229 * \param *out ofstream for debugging230 * \param *test one item to put on stack231 * \return true - all tests worked correctly232 */233 void AtomStackClass::TestImplementation(ofstream *out, atom *test)234 {235 atom *Walker = test;236 *out << Verbose(1) << "Testing the snake stack..." << endl;237 for (int i=0;i<EntryCount;i++) {238 *out << Verbose(2) << "Filling " << i << "th element of stack." << endl;239 Push(Walker);240 Walker=Walker->next;241 }242 *out << endl;243 Output(out);244 if (IsFull())245 *out << "Stack is full as supposed to be!" << endl;246 else247 *out << "ERROR: Stack is not as full as supposed to be!" << endl;248 //if (StackList[(EntryCount+1)/2] != NULL) {249 *out << "Removing element in the middle ..." << endl;250 RemoveItem(StackList[(EntryCount+1)/2]);251 Output(out);252 //}253 //if (StackList[CurrentFirstEntry] != NULL) {254 *out << "Removing first element ..." << endl;255 RemoveItem(StackList[CurrentFirstEntry]);256 Output(out);257 //}258 //if (StackList[CurrentLastEntry] != NULL) {259 *out << "Removing last element ..." << endl;260 RemoveItem(StackList[CurrentLastEntry]);261 Output(out);262 //}263 *out << "Clearing stack ... " << endl;264 ClearStack();265 Output(out);266 if (IsEmpty())267 *out << "Stack is empty as supposed to be!" << endl;268 else269 *out << "ERROR: Stack is not as empty as supposed to be!" << endl;270 *out << "done." << endl;271 };272 273 /** Puts contents of stack into ofstream \a *out.274 * \param *out ofstream for output275 */276 void AtomStackClass::Output(ofstream *out) const277 {278 *out << "Contents of Stack: ";279 for(int i=0;i<EntryCount;i++) {280 *out << "\t";281 if (i == CurrentFirstEntry)282 *out << " 1";283 if (i == CurrentLastEntry)284 *out << " "<< EntryCount;285 if (i == NextFreeField)286 *out << " F";287 *out << ": " << StackList[i];288 }289 *out << endl;290 };291 292 /** Checks whether stack is empty.293 * Simply checks whether AtomStackClass::NextFreeField is equal to AtomStackClass::CurrentFirstEntry and294 * AtomStackClass::CurrentFirstEntry is equal to AtomStackClass::CurrentLastEntry.295 * \return true - empty, false - not296 */297 bool AtomStackClass::IsEmpty()298 {299 return((NextFreeField == CurrentFirstEntry) && (CurrentLastEntry == CurrentFirstEntry));300 };301 302 /** Checks whether stack is full.303 * Simply checks whether AtomStackClass::NextFreeField is equal to AtomStackClass::CurrentFirstEntry and304 * AtomStackClass::CurrentFirstEntry is _not_ equal to AtomStackClass::CurrentLastEntry.305 * \return true - full, false - not306 */307 bool AtomStackClass::IsFull()308 {309 return((NextFreeField == CurrentFirstEntry) && (CurrentLastEntry != CurrentFirstEntry));310 };311 312 /** Returns number of items on stack.313 * Simply returns difference between AtomStackClass::Stacklist entry AtomStackClass::CurrentEntry-1.314 * \return number of items on stack315 * \warning will never return correct item count if stack is full, i.e. count would be AtomStackClass::EntryCount.316 */317 int AtomStackClass::ItemCount()318 {319 //cout << "Stack: CurrentLastEntry " << CurrentLastEntry<< "\tCurrentFirstEntry " << CurrentFirstEntry << "\tEntryCount " << EntryCount << "." << endl;320 return((NextFreeField + (EntryCount - CurrentFirstEntry)) % EntryCount);321 };322 323 /** Clears the stack from all atoms.324 * \return true - sucess, false - failure325 */326 void AtomStackClass::ClearStack()327 {328 for(int i=0;i<EntryCount; i++)329 StackList[i] = NULL;330 CurrentFirstEntry = 0;331 CurrentLastEntry = 0;332 NextFreeField = 0;333 };334
Note:
See TracChangeset
for help on using the changeset viewer.
