← Back to team overview

yade-dev team mailing list archive

[Branch ~yade-dev/yade/trunk] Rev 2967: - re-activate smart iterators in body container after fixing the begin() case.

 

------------------------------------------------------------
revno: 2967
committer: Bruno Chareyre <bruno.chareyre@xxxxxxxxxxx>
branch nick: trunk
timestamp: Wed 2011-11-23 21:02:42 +0100
message:
  - re-activate smart iterators in body container after fixing the begin() case.
  - also fix the parallel FOREACH macro by including a "continue" directive 
modified:
  core/BodyContainer.hpp
  pkg/dem/GlobalStiffnessTimeStepper.cpp


--
lp:yade
https://code.launchpad.net/~yade-dev/yade/trunk

Your team Yade developers is subscribed to branch lp:yade.
To unsubscribe from this branch go to https://code.launchpad.net/~yade-dev/yade/trunk/+edit-subscription
=== modified file 'core/BodyContainer.hpp'
--- core/BodyContainer.hpp	2011-11-23 16:48:27 +0000
+++ core/BodyContainer.hpp	2011-11-23 20:02:42 +0000
@@ -32,7 +32,7 @@
 	#endif
 #else
 	#if YADE_OPENMP
-		#define YADE_PARALLEL_FOREACH_BODY_BEGIN(b_,bodies) const Body::id_t _sz(bodies->size()); _Pragma("omp parallel for") for(Body::id_t _id=0; _id<_sz; _id++){ b_((*bodies)[_id]);
+		#define YADE_PARALLEL_FOREACH_BODY_BEGIN(b_,bodies) const Body::id_t _sz(bodies->size()); _Pragma("omp parallel for") for(Body::id_t _id=0; _id<_sz; _id++){ if(unlikely(!(*bodies)[_id]))  continue; b_((*bodies)[_id]);
 		#define YADE_PARALLEL_FOREACH_BODY_END() }
 	#else
 		#define YADE_PARALLEL_FOREACH_BODY_BEGIN(b,bodies) FOREACH(b,*(bodies)){
@@ -43,6 +43,7 @@
 /*
 Container of bodies implemented as flat std::vector. It handles body removal and
 intelligently reallocates free ids for newly added ones.
+The nested iterators and the specialized FOREACH_BODY macros above will silently skip null body pointers which may exist after removal. The null pointers can still be accessed via the [] operator. 
 
 Any alternative implementation should use the same API.
 */
@@ -66,17 +67,18 @@
 			ContainerT::iterator end;
 			smart_iterator& operator++() {
 				ContainerT::iterator::operator++();
-				while (!(this->operator*()) && end!=(*this)){
-					ContainerT::iterator::operator++();}
+				while (!(this->operator*()) && end!=(*this)) ContainerT::iterator::operator++();
 				return *this;}
 			smart_iterator operator++(int) {smart_iterator temp(*this); operator++(); return temp;}
 			smart_iterator& operator=(const ContainerT::iterator& rhs) {ContainerT::iterator::operator=(rhs); return *this;}
+			smart_iterator() {}
+			smart_iterator(const ContainerT::iterator& source) {(*this)=source;}
 		};
 
-		typedef ContainerT::iterator iterator;
-		typedef ContainerT::const_iterator const_iterator;
-// 		typedef smart_iterator iterator;
-// 		typedef const smart_iterator const_iterator;
+// 		typedef ContainerT::iterator iterator;
+// 		typedef ContainerT::const_iterator const_iterator;
+		typedef smart_iterator iterator;
+		typedef const smart_iterator const_iterator;
 
 		BodyContainer();
 		virtual ~BodyContainer();
@@ -85,15 +87,16 @@
 	
 		// mimick some STL api
 		void clear();
-		iterator begin() { return body.begin(); }
-		iterator end() { return body.end(); }
-		const_iterator begin() const { return body.begin(); }
-		const_iterator end() const { return body.end(); }
+		// by usning simple vector<>::iterator's, we can hit null bodies 
+// 		iterator begin() { return body.begin(); }
+// 		iterator end() { return body.end(); }
+// 		const_iterator begin() const { return body.begin(); }
+// 		const_iterator end() const { return body.end(); }
 // 		//with smart iterators
-// 		iterator begin() { iterator temp; temp=(body.begin()); temp.end=(body.end()); return temp;}
-// 		iterator end() { iterator temp; temp= body.end(); return temp;}
-// 		const_iterator begin() const { return begin();}
-// 		const_iterator end() const { return end();}
+		iterator begin() { iterator temp(body.begin()); temp.end=body.end(); return (*temp)?temp:++temp;}
+		iterator end() { iterator temp(body.end()); return temp;}
+		const_iterator begin() const { return begin();}
+		const_iterator end() const { return end();}
 
 		size_t size() const { return body.size(); }
 		shared_ptr<Body>& operator[](unsigned int id){ return body[id];}

=== modified file 'pkg/dem/GlobalStiffnessTimeStepper.cpp'
--- pkg/dem/GlobalStiffnessTimeStepper.cpp	2011-11-23 15:33:01 +0000
+++ pkg/dem/GlobalStiffnessTimeStepper.cpp	2011-11-23 20:02:42 +0000
@@ -73,7 +73,7 @@
 	BodyContainer::iterator biEnd = bodies->end();
 	for(  ; bi!=biEnd ; ++bi )
 	{
-		if (!*bi)  continue;
+// 		if (!*bi)  continue;
 		shared_ptr<Body> b = *bi;
 		if (b->isDynamic()) findTimeStepFromBody(b, ncb);
 	}


Follow ups