Changeset 3746aeb
- Timestamp:
- Mar 3, 2010, 5:47:40 PM (16 years ago)
- Children:
- 4938aa, f058ef
- Parents:
- 14d898
- Location:
- molecuilder/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
molecuilder/src/World.cpp
r14d898 r3746aeb 86 86 atom *World::createAtom(){ 87 87 OBSERVE; 88 atom *res = NewAtom(); 89 assert(!atoms.count(currAtomId)); 90 res->setId(currAtomId++); 88 atomId_t id = getNextAtomId(); 89 atom *res = NewAtom(id); 91 90 res->setWorld(this); 92 91 // store the atom by ID … … 97 96 int World::registerAtom(atom *atom){ 98 97 OBSERVE; 99 a ssert(!atoms.count(currAtomId));100 atom->setId( currAtomId++);98 atomId_t id = getNextAtomId(); 99 atom->setId(id); 101 100 atom->setWorld(this); 102 101 atoms[atom->getId()] = atom; … … 116 115 DeleteAtom(atom); 117 116 atoms.erase(id); 117 releaseAtomId(id); 118 } 119 120 bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){ 121 OBSERVE; 122 // in case this call did not originate from inside the atom, we redirect it, 123 // to also let it know that it has changed 124 if(!target){ 125 target = atoms[oldId]; 126 assert(target && "Atom with that ID not found"); 127 return target->changeId(newId); 128 } 129 else{ 130 if(reserveAtomId(newId)){ 131 atoms.erase(oldId); 132 atoms.insert(pair<atomId_t,atom*>(newId,target)); 133 return true; 134 } 135 else{ 136 return false; 137 } 138 } 118 139 } 119 140 … … 136 157 proc->signOff(this); 137 158 } 138 159 /******************************* IDManagement *****************************/ 160 161 atomId_t World::getNextAtomId(){ 162 // see if we can reuse some Id 163 if(atomIdPool.empty()){ 164 return currAtomId++; 165 } 166 else{ 167 // we give out the first ID from the pool 168 atomId_t id = *(atomIdPool.begin()); 169 atomIdPool.erase(id); 170 } 171 } 172 173 void World::releaseAtomId(atomId_t id){ 174 atomIdPool.insert(id); 175 // defragmentation of the pool 176 set<atomId_t>::reverse_iterator iter; 177 // go through all Ids in the pool that lie immediately below the border 178 while(!atomIdPool.empty() && *(atomIdPool.rbegin())==(currAtomId-1)){ 179 atomIdPool.erase(--currAtomId); 180 } 181 } 182 183 bool World::reserveAtomId(atomId_t id){ 184 if(id>=currAtomId ){ 185 // add all ids between the new one and current border as available 186 for(atomId_t pos=currAtomId; pos<id; ++pos){ 187 atomIdPool.insert(pos); 188 } 189 currAtomId=id+1; 190 return true; 191 } 192 else if(atomIdPool.count(id)){ 193 atomIdPool.erase(id); 194 return true; 195 } 196 else{ 197 // this ID could not be reserved 198 return false; 199 } 200 } 139 201 /******************************* Iterators ********************************/ 140 202 -
molecuilder/src/World.hpp
r14d898 r3746aeb 130 130 */ 131 131 void destroyAtom(atomId_t); 132 133 /** 134 * used when changing an atom Id. 135 * Unless you are calling this method from inside an atom don't fiddle with the third parameter. 136 * 137 * Return value indicates wether the change could be done or not. 138 */ 139 bool changeAtomId(atomId_t oldId, atomId_t newId, atom* target=0); 132 140 133 141 /** … … 226 234 227 235 private: 236 237 atomId_t getNextAtomId(); 238 void releaseAtomId(atomId_t); 239 bool reserveAtomId(atomId_t); 240 228 241 periodentafel *periode; 229 242 AtomSet atoms; 243 std::set<atomId_t> atomIdPool; //<!stores the pool for all available AtomIds below currAtomId 230 244 atomId_t currAtomId; //!< stores the next available Id for atoms 231 245 MoleculeSet molecules; -
molecuilder/src/atom.cpp
r14d898 r3746aeb 290 290 } 291 291 292 void atom::setId(int _id) { 292 bool atom::changeId(atomId_t newId){ 293 // first we move ourselves in the world 294 // the world lets us know if that succeeded 295 if(world->changeAtomId(id,newId,this)){ 296 id = newId; 297 return true; 298 } 299 else{ 300 return false; 301 } 302 } 303 304 void atom::setId(atomId_t _id) { 293 305 id=_id; 294 306 } … … 298 310 } 299 311 300 atom* NewAtom(){ 301 return new atom(); 302 } 303 304 void DeleteAtom(atom* atom){ 312 atom* NewAtom(atomId_t _id){ 313 atom * res =new atom(); 314 res->setId(_id); 315 return res; 316 } 317 318 void DeleteAtom(atom* atom){ 305 319 delete atom; 306 320 } -
molecuilder/src/atom.hpp
r14d898 r3746aeb 40 40 */ 41 41 class atom : public TesselPoint, public TrajectoryParticle, public GraphNode, public BondedParticle, public virtual ParticleInfo, public virtual AtomInfo { 42 friend atom* NewAtom( );42 friend atom* NewAtom(atomId_t); 43 43 friend void DeleteAtom(atom*); 44 44 public: … … 80 80 81 81 virtual int getId(); 82 virtual void setId(int); 82 virtual bool changeId(atomId_t newId); 83 84 /** 85 * this function sets the Id without notifying the world. Only use it, if the world has already 86 * gotten an ID for this Atom. 87 */ 88 virtual void setId(atomId_t); 89 83 90 protected: 84 91 /** … … 101 108 private: 102 109 World* world; 103 int id;110 atomId_t id; 104 111 }; 105 112 … … 109 116 * Use World::createAtom() instead. 110 117 */ 111 atom* NewAtom( );118 atom* NewAtom(atomId_t _id); 112 119 113 120 /**
Note:
See TracChangeset
for help on using the changeset viewer.
