[cig-commits] r13502 - cs/cigma/trunk/src

luis at geodynamics.org luis at geodynamics.org
Tue Dec 9 18:12:44 PST 2008


Author: luis
Date: 2008-12-09 18:12:44 -0800 (Tue, 09 Dec 2008)
New Revision: 13502

Added:
   cs/cigma/trunk/src/CellIterator.h
Log:
Iterator class for cells over a mesh part

Added: cs/cigma/trunk/src/CellIterator.h
===================================================================
--- cs/cigma/trunk/src/CellIterator.h	                        (rev 0)
+++ cs/cigma/trunk/src/CellIterator.h	2008-12-10 02:12:44 UTC (rev 13502)
@@ -0,0 +1,90 @@
+#ifndef __CIGMA_CELL_ITERATOR_H__
+#define __CIGMA_CELL_ITERATOR_H__
+
+#include "Cell.h"
+#include "MeshPart.h"
+
+namespace cigma
+{
+    class CellIterator;
+}
+
+/*
+ * Each iterator object carries its own Cell object.
+ */
+struct cigma::CellIterator
+{
+    //typedef Cell value_type;
+    typedef Cell& reference;
+    typedef Cell* pointer;
+    typedef const Cell& const_reference;
+    typedef const Cell* const_pointer;
+
+    CellIterator() : mesh(0), cell(0), e(0)
+    {
+    }
+
+    CellIterator(boost::shared_ptr<MeshPart>& m) : mesh(m), cell(0), e(0)
+    {
+        if (mesh)
+        {
+            cell = Cell::New(mesh->cell_type());
+            if (mesh->connect->n_cells() > 0)
+                mesh->loadCell(e, *cell);
+        }
+    }
+
+    CellIterator(const CellIterator& i) : mesh(i.mesh), cell(i.cell), e(i.e)
+    {
+    }
+
+    ~CellIterator()
+    {
+    }
+
+    Cell& operator*() const { return *(*cell); }
+    Cell* operator->() const { return *cell; }
+
+    CellIterator& operator++()
+    {
+        if (mesh)
+        {
+            const int nel = mesh->connect->n_cells();
+
+            e++;
+
+            if (e < nel)
+            {
+                mesh->getCell(e, *cell);
+            }
+            else
+            {
+                e = 0;
+                mesh.reset();
+            }
+        }
+    }
+
+    CellIterator operator++(int)
+    {
+        CellIterator tmp(*this);
+        ++(*this);
+        return tmp;
+    }
+
+    friend bool operator==(const CellIterator& x, const CellIterator& y)
+    {
+        return (x.e == y.e) && (*(x.mesh) == *(y.mesh));
+    }
+
+    friend bool operator!=(const CellIterator& x, const CellIterator& y)
+    {
+        return (x.e != y.e) || (*(x.mesh) != *(y.mesh));
+    }
+
+    boost::shared_ptr<MeshPart> mesh;
+    boost::shared_ptr<Cell> cell;
+    int e;
+};
+
+#endif



More information about the CIG-COMMITS mailing list