[MOAB-dev] r5407 - MOAB/trunk/itaps/imesh

jvporter at wisc.edu jvporter at wisc.edu
Fri Feb 17 17:50:19 CST 2012


Author: jvporter
Date: 2012-02-17 17:50:17 -0600 (Fri, 17 Feb 2012)
New Revision: 5407

Modified:
   MOAB/trunk/itaps/imesh/MBIter.hpp
Log:
Add a step_iterator helper function to improve efficiency of the iMesh_stepIter function


Modified: MOAB/trunk/itaps/imesh/MBIter.hpp
===================================================================
--- MOAB/trunk/itaps/imesh/MBIter.hpp	2012-02-17 21:17:41 UTC (rev 5406)
+++ MOAB/trunk/itaps/imesh/MBIter.hpp	2012-02-17 23:50:17 UTC (rev 5407)
@@ -54,13 +54,60 @@
     }
 };
 
+// step_iterator will safely step forward N steps in a iterator. We specialize
+// for random-access iterators (vectors and Ranges) so that they perform better.
 
+template <typename T>
+ErrorCode step_iterator(T &curr, const T &end, int num_steps, bool &at_end) 
+{
+  if (0 > num_steps) return MB_FAILURE;
+
+  while (num_steps && curr != end) {
+    num_steps--;
+    curr++;
+  }
+  at_end = (curr == end);
+  return MB_SUCCESS;
+}
+
+template <typename T>
+ErrorCode step_iterator(typename std::vector<T>::const_iterator &curr,
+                        const typename std::vector<T>::const_iterator &end,
+                        int num_steps, bool &at_end) 
+{
+  if (0 > num_steps) return MB_FAILURE;
+
+  assert(curr <= end); // Sanity check
+  at_end = (end - curr <= num_steps);
+
+  if (at_end)
+    curr = end;
+  else
+    curr += num_steps;
+  return MB_SUCCESS;
+}
+
+ErrorCode step_iterator(Range::const_iterator &curr, 
+                        const Range::const_iterator &end, int num_steps,
+                        bool &at_end) 
+{
+  if (0 > num_steps) return MB_FAILURE;
+
+  at_end = (end - curr <= num_steps);
+
+  if (at_end)
+    curr = end;


More information about the moab-dev mailing list