Changeset 6ac7ee for src/stackclass.hpp
- Timestamp:
- Feb 9, 2009, 5:24:10 PM (16 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:
- d8b94a
- Parents:
- 124df1
- git-author:
- Frederik Heber <heber@…> (02/09/09 15:55:37)
- git-committer:
- Frederik Heber <heber@…> (02/09/09 17:24:10)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/stackclass.hpp
-
Property mode
changed from
100644
to100755
r124df1 r6ac7ee 10 10 */ 11 11 template <typename T> class StackClass { 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 T *StackList;//!< the list containing the atom pointers29 int EntryCount;//!< number of entries in the stack30 int CurrentLastEntry;//!< Current last entry (newest item on stack)31 int CurrentFirstEntry;//!< Current first entry (oldest item on stack)32 int NextFreeField;//!< Current index of next free field12 public: 13 StackClass<T>(int dimension); 14 ~StackClass<T>(); 15 16 bool Push(T object); 17 T PopFirst(); 18 T PopLast(); 19 bool RemoveItem(T ptr); 20 void ClearStack(); 21 bool IsEmpty(); 22 bool IsFull(); 23 int ItemCount(); 24 void Output(ofstream *out) const; 25 void TestImplementation(ofstream *out, T test); 26 27 private: 28 T *StackList; //!< the list containing the atom pointers 29 int EntryCount; //!< number of entries in the stack 30 int CurrentLastEntry; //!< Current last entry (newest item on stack) 31 int CurrentFirstEntry; //!< Current first entry (oldest item on stack) 32 int NextFreeField; //!< Current index of next free field 33 33 }; 34 34 … … 37 37 template <typename T> StackClass<T>::StackClass(int dimension) 38 38 { 39 40 41 42 43 39 CurrentLastEntry = 0; 40 CurrentFirstEntry = 0; 41 NextFreeField = 0; 42 EntryCount = dimension; 43 StackList = (T *) Malloc(sizeof(T)*EntryCount, "StackClass::StackClass: **StackList"); 44 44 }; 45 45 … … 48 48 template <typename T> StackClass<T>::~StackClass() 49 49 { 50 50 Free((void **)&StackList, "StackClass::StackClass: **StackList"); 51 51 }; 52 52 … … 57 57 template <typename T> bool StackClass<T>::Push(T object) 58 58 { 59 if (!IsFull()) {// check whether free field is really not occupied60 61 62 63 64 65 66 67 59 if (!IsFull()) { // check whether free field is really not occupied 60 StackList[NextFreeField] = object; 61 CurrentLastEntry = NextFreeField; 62 NextFreeField = (NextFreeField + 1) % EntryCount; // step on to next free field 63 return true; 64 } else { 65 cerr << "ERROR: Stack is full, " << "Stack: CurrentLastEntry " << CurrentLastEntry<< "\tCurrentFirstEntry " << CurrentFirstEntry << "\tNextFreeField " << NextFreeField << "\tEntryCount " << EntryCount << "!" << endl; 66 return false; 67 } 68 68 }; 69 69 … … 74 74 template <typename T> T StackClass<T>::PopFirst() 75 75 { 76 77 78 79 80 81 82 83 84 85 86 87 88 89 cerr << "ERROR: Stack is empty!" << endl; 90 76 T Walker = NULL; 77 if (!IsEmpty()) { 78 Walker = StackList[CurrentFirstEntry]; 79 if (Walker == NULL) 80 cerr << "ERROR: Stack's field is empty!" << endl; 81 StackList[CurrentFirstEntry] = NULL; 82 if (CurrentFirstEntry != CurrentLastEntry) { // hasn't last item been popped as well? 83 CurrentFirstEntry = (CurrentFirstEntry + 1) % EntryCount; // step back from current free field to last used (somehow modulo does not work in -1) 84 } else { 85 CurrentFirstEntry = (CurrentFirstEntry + 1) % EntryCount; // step back from current free field to last used (somehow modulo does not work in -1) 86 CurrentLastEntry = CurrentFirstEntry; 87 } 88 } else 89 cerr << "ERROR: Stack is empty!" << endl; 90 return Walker; 91 91 }; 92 92 … … 97 97 template <typename T> T StackClass<T>::PopLast() 98 98 { 99 100 101 102 103 104 105 106 if (CurrentLastEntry != CurrentFirstEntry)// has there been more than one item on stack107 108 109 110 } 111 99 T Walker = NULL; 100 if (!IsEmpty()) { 101 Walker = StackList[CurrentLastEntry]; 102 StackList[CurrentLastEntry] = NULL; 103 if (Walker == NULL) 104 cerr << "ERROR: Stack's field is empty!" << endl; 105 NextFreeField = CurrentLastEntry; 106 if (CurrentLastEntry != CurrentFirstEntry) // has there been more than one item on stack 107 CurrentLastEntry = (CurrentLastEntry + (EntryCount-1)) % EntryCount; // step back from current free field to last (modulo does not work in -1, thus go EntryCount-1 instead) 108 } else { 109 cerr << "ERROR: Stack is empty!" << endl; 110 } 111 return Walker; 112 112 }; 113 113 … … 120 120 template <typename T> bool StackClass<T>::RemoveItem(T ptr) 121 121 { 122 123 124 125 126 127 if (StackList[i] == ptr) {// if item found, remove128 129 130 131 132 if ((found) && (StackList[i] != NULL)) {// means we have to shift (and not the removed item)133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 if (CurrentLastEntry != CurrentFirstEntry)// has there been more than one item on stack148 149 150 122 bool found = false; 123 cout << Verbose(5) << "First " << CurrentFirstEntry<< "\tLast " << CurrentLastEntry<< "\tNext " << NextFreeField<< "\tCount " << EntryCount<< "." << endl; 124 int i=CurrentFirstEntry; 125 if (!IsEmpty()) 126 do { 127 if (StackList[i] == ptr) { // if item found, remove 128 cout << Verbose(5) << "Item " << *ptr << " was number " << i << " on stack, removing it." << endl; 129 found = true; 130 StackList[i] = NULL; 131 } 132 if ((found) && (StackList[i] != NULL)) { // means we have to shift (and not the removed item) 133 if (i == 0) { // we are down to first item in stack, have to put onto last item 134 cout << Verbose(5) << "Shifting item 0 to place " << EntryCount-1 << "." << endl; 135 StackList[EntryCount-1] = StackList[0]; 136 } else { 137 cout << Verbose(5) << "Shifting item " << i << " to place " << i-1 << "." << endl; 138 StackList[i-1] = StackList[i]; 139 } 140 } 141 i=((i + 1) % EntryCount); // step on 142 } while (i!=NextFreeField); 143 else 144 cerr << "ERROR: Stack is already empty!" << endl; 145 if (found) { 146 NextFreeField = CurrentLastEntry; 147 if (CurrentLastEntry != CurrentFirstEntry) // has there been more than one item on stack 148 CurrentLastEntry = (CurrentLastEntry + (EntryCount-1)) % EntryCount; 149 } 150 return found; 151 151 }; 152 152 153 153 /** Test the functionality of the stack. 154 154 * \param *out ofstream for debugging 155 * \param *test one item to put on stack 155 * \param *test one item to put on stack 156 156 * \return true - all tests worked correctly 157 157 */ 158 158 template <typename T> void StackClass<T>::TestImplementation(ofstream *out, T test) 159 159 { 160 161 162 163 *out << Verbose(2) << "Filling " << i << "th element of stack." << endl; 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 *out << "Removing first element..." << endl;180 181 182 183 184 185 186 187 188 *out << "Clearing stack ... " << endl; 189 190 191 192 193 194 195 160 T Walker = test; 161 *out << Verbose(1) << "Testing the snake stack..." << endl; 162 for (int i=0;i<EntryCount;i++) { 163 *out << Verbose(2) << "Filling " << i << "th element of stack." << endl; 164 Push(Walker); 165 Walker=Walker->next; 166 } 167 *out << endl; 168 Output(out); 169 if (IsFull()) 170 *out << "Stack is full as supposed to be!" << endl; 171 else 172 *out << "ERROR: Stack is not as full as supposed to be!" << endl; 173 //if (StackList[(EntryCount+1)/2] != NULL) { 174 *out << "Removing element in the middle ..." << endl; 175 RemoveItem(StackList[(EntryCount+1)/2]); 176 Output(out); 177 //} 178 //if (StackList[CurrentFirstEntry] != NULL) { 179 *out << "Removing first element ..." << endl; 180 RemoveItem(StackList[CurrentFirstEntry]); 181 Output(out); 182 //} 183 //if (StackList[CurrentLastEntry] != NULL) { 184 *out << "Removing last element ..." << endl; 185 RemoveItem(StackList[CurrentLastEntry]); 186 Output(out); 187 //} 188 *out << "Clearing stack ... " << endl; 189 ClearStack(); 190 Output(out); 191 if (IsEmpty()) 192 *out << "Stack is empty as supposed to be!" << endl; 193 else 194 *out << "ERROR: Stack is not as empty as supposed to be!" << endl; 195 *out << "done." << endl; 196 196 }; 197 197 … … 201 201 template <typename T> void StackClass<T>::Output(ofstream *out) const 202 202 { 203 204 205 206 207 208 if(i == CurrentLastEntry)209 210 if (i ==NextFreeField)211 212 213 214 203 *out << "Contents of Stack: "; 204 for(int i=0;i<EntryCount;i++) { 205 *out << "\t"; 206 if (i == CurrentFirstEntry) 207 *out << " 1"; 208 if (i == CurrentLastEntry) 209 *out << " "<< EntryCount; 210 if (i == NextFreeField) 211 *out << " F"; 212 *out << ": " << StackList[i]; 213 } 214 *out << endl; 215 215 }; 216 216 … … 222 222 template <typename T> bool StackClass<T>::IsEmpty() 223 223 { 224 224 return((NextFreeField == CurrentFirstEntry) && (CurrentLastEntry == CurrentFirstEntry)); 225 225 }; 226 226 … … 232 232 template <typename T> bool StackClass<T>::IsFull() 233 233 { 234 234 return((NextFreeField == CurrentFirstEntry) && (CurrentLastEntry != CurrentFirstEntry)); 235 235 }; 236 236 … … 242 242 template <typename T> int StackClass<T>::ItemCount() 243 243 { 244 245 244 //cout << "Stack: CurrentLastEntry " << CurrentLastEntry<< "\tCurrentFirstEntry " << CurrentFirstEntry << "\tEntryCount " << EntryCount << "." << endl; 245 return((NextFreeField + (EntryCount - CurrentFirstEntry)) % EntryCount); 246 246 }; 247 247 … … 251 251 template <typename T> void StackClass<T>::ClearStack() 252 252 { 253 254 255 256 257 253 for(int i=EntryCount; i--;) 254 StackList[i] = NULL; 255 CurrentFirstEntry = 0; 256 CurrentLastEntry = 0; 257 NextFreeField = 0; 258 258 }; 259 259 -
Property mode
changed from
Note:
See TracChangeset
for help on using the changeset viewer.