From dcdef83d0d8ad5fff8d3f15424f60c16aa814b15 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Robert=20Kl=C3=B6fkorn?= <robertk@dune-project.org>
Date: Sun, 22 May 2005 15:00:30 +0000
Subject: [PATCH] Implemented the EntityPointer support. Test only grid walks.
 Needs testing in adaptiv runs etc.

[[Imported from SVN: r2086]]
---
 grid/albertagrid.hh | 287 ++++++++++++++++++++++++++++++++------------
 1 file changed, 207 insertions(+), 80 deletions(-)

diff --git a/grid/albertagrid.hh b/grid/albertagrid.hh
index 7410cff12..37bdd3c95 100644
--- a/grid/albertagrid.hh
+++ b/grid/albertagrid.hh
@@ -44,13 +44,13 @@ namespace Dune {
   // own exception classes
   class AlbertaError   : public Exception {};
   class AlbertaIOError : public IOError {};
+
 }
 
 // contains a simple memory management for some componds of this grid
 #include "albertagrid/agmemory.hh"
 
 // contains the communication for parallel computing for this grid
-
 #include "albertagrid/agcommunicator.hh"
 #include "common/defaultindexsets.hh"
 
@@ -64,8 +64,9 @@ namespace Dune
   class AlbertaMarkerVector;
 
   template<int codim, int dim, class GridImp> class AlbertaGridEntity;
-  template<int codim, PartitionIteratorType pitype, class GridImp> class AlbertaGridLevelIterator;
+  template<int codim, PartitionIteratorType pitype, class GridImp> class AlbertaGridTreeIterator;
   template<class GridImp> class AlbertaGridLeafIterator;
+  template<int cd, class GridImp> class AlbertaGridEntityPointer;
 
   template <int mydim, int cdim, class GridImp> class AlbertaGridGeometry;
   template<class GridImp>         class AlbertaGridBoundaryEntity;
@@ -315,10 +316,20 @@ namespace Dune
       return this->realEntity.getElInfo();
     }
 
+    void removeElInfo ()
+    {
+      this->realEntity.removeElInfo();
+    }
+
     void setLevel ( int level )
     {
       this->realEntity.setLevel(level);
     }
+
+    void setEntity ( const AlbertaGridMakeableEntity<codim,dim,GridImp> & org)
+    {
+      this->realEntity.setEntity(org.realEntity);
+    }
   };
 
   //**********************************************************************
@@ -339,7 +350,7 @@ namespace Dune
     enum { dimworld = GridImp::dimensionworld };
     friend class AlbertaGrid < dim , dimworld >;
     friend class AlbertaGridEntity < 0, dim, GridImp>;
-    friend class AlbertaGridLevelIterator < cd, All_Partition,GridImp>;
+    friend class AlbertaGridTreeIterator < cd, All_Partition,GridImp>;
     friend class AlbertaGridMakeableEntity<cd,dim,GridImp>;
 
     typedef AlbertaGridMakeableGeometry<dim-cd,dimworld,GridImp> GeometryImp;
@@ -381,6 +392,8 @@ namespace Dune
     // needed for the LevelIterator and LeafIterator
     ALBERTA EL_INFO *getElInfo () const;
 
+    void removeElInfo() { elInfo_ = 0; }
+
     //! return the current face/edge or vertex number
     //! no interface method
     int getFEVnum () const;
@@ -390,6 +403,9 @@ namespace Dune
     void setElInfo (ALBERTA EL_INFO *elInfo, int face,
                     int edge, int vertex );
 
+    // same as setElInfo just with a entity given
+    void setEntity (const AlbertaGridEntity<cd,dim,GridImp> & org);
+
     // set level of entity
     void setLevel ( int newLevel );
 
@@ -456,7 +472,7 @@ namespace Dune
     friend class AlbertaMarkerVector;
     friend class AlbertaGridIntersectionIterator <GridImp>;
     friend class AlbertaGridHierarchicIterator <GridImp>;
-    friend class AlbertaGridLevelIterator <0,All_Partition,GridImp>;
+    friend class AlbertaGridTreeIterator <0,All_Partition,GridImp>;
     friend class AlbertaGridMakeableEntity<0,dim,GridImp>;
   public:
     template <int cd>
@@ -517,7 +533,7 @@ namespace Dune
 
     //! Provide access to mesh entity i of given codimension. Entities
     //!  are numbered 0 ... count<cc>()-1
-    //template<int cc> void entity (AlbertaGridLevelIterator<cc,dim,dimworld>& it, int i);
+    //template<int cc> void entity (AlbertaGridTreeIterator<cc,dim,dimworld>& it, int i);
 
     /*! Intra-level access to intersection with neighboring elements.
        A neighbor is an entity of codimension 0
@@ -576,6 +592,7 @@ namespace Dune
 
     // needed for LevelIterator to compare
     ALBERTA EL_INFO *getElInfo () const;
+    void removeElInfo() { elInfo_ = 0; }
   private:
     // called from HierarchicIterator, because only this
     // class changes the level of the entity, otherwise level is set by
@@ -590,6 +607,9 @@ namespace Dune
                     int edge = 0,
                     int vertex = 0 );
 
+    // same as setElInfo just with a entity given
+    void setEntity (const AlbertaGridEntity<0,dim,GridImp> & org);
+
     //! make a new AlbertaGridEntity
     void makeDescription();
 
@@ -617,6 +637,76 @@ namespace Dune
 
   }; // end of AlbertaGridEntity codim = 0
 
+
+
+  //**********************************************************************
+  //
+  // --AlbertaGridEntityPointer
+  // --EntityPointer
+  // --EnPointer
+  /*!
+     Enables iteration over all entities of a given codimension and level of a grid.
+   */
+  template<int cd, class GridImp>
+  class AlbertaGridEntityPointer :
+    public EntityPointerDefault <cd, GridImp, AlbertaGridEntityPointer<cd,GridImp> >
+  {
+    enum { dim       = GridImp::dimension };
+    enum { dimworld  = GridImp::dimensionworld };
+
+    friend class AlbertaGridEntity<cd,dim,GridImp>;
+    friend class AlbertaGridEntity< 0,dim,GridImp>;
+    friend class AlbertaGrid < dim , dimworld >;
+
+  public:
+
+    typedef typename GridImp::template codim<cd>::Entity Entity;
+    typedef AlbertaGridMakeableEntity<cd,dim,GridImp> EntityImp;
+
+    //! typedef of my type
+    typedef AlbertaGridEntityPointer<cd,GridImp> AlbertaGridEntityPointerType;
+
+    //! Constructor for EntityPointer that points to an element
+    AlbertaGridEntityPointer(const GridImp & grid,
+                             int level,  ALBERTA EL_INFO *elInfo,int face,int edge,int vertex);
+
+    //! Constructor for EntityPointer that points to an element
+    AlbertaGridEntityPointer(const GridImp & grid, ALBERTA TRAVERSE_STACK * stack,
+                             int level,  ALBERTA EL_INFO *elInfo,int face,int edge,int vertex);
+
+    //! Constructor for EntityPointer init of Level- and LeafIterator
+    AlbertaGridEntityPointer(const GridImp & grid, int level , bool done);
+
+    //! make empty entity pointer (to be revised)
+    AlbertaGridEntityPointer(const AlbertaGridEntityPointerType & org);
+
+    //! Destructor
+    ~AlbertaGridEntityPointer();
+
+    //! equality
+    bool equals (const AlbertaGridEntityPointerType& i) const;
+
+    //! dereferencing
+    Entity & dereference () const ;
+
+    //! ask for level of entities
+    int level () const ;
+
+    //! has to be called when iterator is finished
+    void done ();
+
+  protected:
+    // reference to grid
+    const GridImp & grid_;
+
+    // entity that this EntityPointer points to
+    EntityImp * entity_;
+
+    //! flag for end iterators
+    bool done_;
+  };
+
+
   //**********************************************************************
   //
   // --AlbertaGridHierarchicIterator
@@ -632,6 +722,7 @@ namespace Dune
 
   template<class GridImp>
   class AlbertaGridHierarchicIterator :
+    public AlbertaGridEntityPointer<0,GridImp> ,
     public HierarchicIteratorDefault <GridImp,AlbertaGridHierarchicIterator>
   {
   public:
@@ -647,32 +738,29 @@ namespace Dune
     AlbertaGridHierarchicIterator(const GridImp &grid,
                                   int actLevel,int maxLevel);
 
+    //! the default Constructor
+    AlbertaGridHierarchicIterator(const AlbertaGridHierarchicIterator<GridImp> &org);
+
     //! increment
     void increment();
 
-    //! equality
-    bool equals (const AlbertaGridHierarchicIterator<GridImp>& i) const;
-
-    //! dereferencing
-    Entity& dereference() const;
-
   private:
-    //! know the grid were im comming from
-    const GridImp &grid_;
-
     //! the actual Level of this Hierarichic Iterator
     int level_;
 
     //! max level to go down
     int maxlevel_;
 
-    //! implement with virtual element
-    mutable EntityImp virtualEntity_;
+    //! reference to entity of entity pointer class
+    EntityImp & virtualEntity_;
 
     //! we need this for Albert traversal, and we need ManageTravStack, which
     //! does count References when copied
     ALBERTA ManageTravStack manageStack_;
 
+    //! true if iterator is end iterator
+    bool end_;
+
     //! The nessesary things for Albert
     ALBERTA EL_INFO * recursiveTraverse(ALBERTA TRAVERSE_STACK * stack);
 
@@ -750,6 +838,7 @@ namespace Dune
    */
   template<class GridImp>
   class AlbertaGridIntersectionIterator :
+    public AlbertaGridEntityPointer<0,GridImp> ,
     public IntersectionIteratorDefault <GridImp,AlbertaGridIntersectionIterator>
   {
     enum { dim      = GridImp::dimension };
@@ -774,8 +863,10 @@ namespace Dune
 
     //! increment
     void increment();
+
     //! equality
     bool equals (const AlbertaGridIntersectionIterator<GridImp>& i) const;
+
     //! access neighbor, dereferencing
     Entity & dereference() const;
 
@@ -861,8 +952,10 @@ namespace Dune
     //! calculate normal to current face
     void calcOuterNormal () const;
 
-    //! know the grid were im comming from
-    const GridImp *grid_;
+    /*
+       //! know the grid were im comming from
+       const GridImp *grid_;
+     */
 
     //! the actual level
     mutable int level_;
@@ -874,16 +967,16 @@ namespace Dune
     //! Most of the information can be generated from the ALBERTA EL_INFO
     //! therefore this element is only created on demand.
     mutable bool builtNeigh_;
-    mutable EntityImp *virtualEntity_;
+    EntityImp & virtualEntity_;
 
     //! pointer to the EL_INFO struct storing the real element information
     mutable ALBERTA EL_INFO * elInfo_;
 
     // for memory management
-    mutable typename GridImp::EntityProvider::ObjectEntity                *manageObj_;
-    mutable typename GridImp::IntersectionSelfProvider::ObjectEntity      *manageInterEl_;
-    mutable typename GridImp::IntersectionNeighProvider::ObjectEntity     *manageNeighEl_;
-    mutable typename GridImp::IntersectionBoundaryProvider::ObjectEntity  *manageBndEntity_;
+    //mutable typename GridImp::EntityProvider::ObjectEntity                *manageObj_;
+    //mutable typename GridImp::IntersectionSelfProvider::ObjectEntity      *manageInterEl_;
+    //mutable typename GridImp::IntersectionNeighProvider::ObjectEntity     *manageNeighEl_;
+    //mutable typename GridImp::IntersectionBoundaryProvider::ObjectEntity  *manageBndEntity_;
 
     //! pointer to element holding the self_local and self_global information.
     //! This element is created on demand.
@@ -896,9 +989,6 @@ namespace Dune
     //! BoundaryEntity
     mutable MakeableBndEntityType * boundaryEntity_;
 
-    //! defined in agmemory.hh
-    mutable typename ElInfoProvider::ObjectEntity *manageNeighInfo_;
-
     //! EL_INFO th store the information of the neighbor if needed
     mutable ALBERTA EL_INFO * neighElInfo_;
 
@@ -914,14 +1004,15 @@ namespace Dune
 
   //**********************************************************************
   //
-  // --AlbertaGridLevelIterator
+  // --AlbertaGridTreeIterator
   // --LevelIterator
   /*!
      Enables iteration over all entities of a given codimension and level of a grid.
    */
   template<int cd, PartitionIteratorType pitype, class GridImp>
-  class AlbertaGridLevelIterator :
-    public LevelIteratorDefault <cd,pitype,GridImp,AlbertaGridLevelIterator>
+  class AlbertaGridTreeIterator :
+    public AlbertaGridEntityPointer<cd,GridImp>
+    //public LevelIteratorDefault <cd,pitype,GridImp,AlbertaGridTreeIterator>
   {
     enum { dim = GridImp::dimension };
     friend class AlbertaGridEntity<2,dim,GridImp>;
@@ -929,41 +1020,38 @@ namespace Dune
     friend class AlbertaGridEntity<0,dim,GridImp>;
     friend class AlbertaGrid < dim , GridImp::dimensionworld >;
 
-    typedef AlbertaGridLevelIterator<cd,pitype,GridImp>  AlbertaGridLevelIteratorType;
+    typedef AlbertaGridTreeIterator<cd,pitype,GridImp>  AlbertaGridTreeIteratorType;
   public:
 
     typedef typename GridImp::template codim<cd>::Entity Entity;
     typedef AlbertaGridMakeableEntity<cd,dim,GridImp> EntityImp;
 
     //! Constructor making end iterator
-    AlbertaGridLevelIterator(const GridImp & grid, int
-                             travLevel, int proc, bool leafIt=false );
+    AlbertaGridTreeIterator(const AlbertaGridTreeIterator<cd,pitype,GridImp> & org );
+
+    //! Constructor making end iterator
+    AlbertaGridTreeIterator(const GridImp & grid, int
+                            travLevel, int proc, bool leafIt=false );
 
     //! Constructor making EntityPointer
-    AlbertaGridLevelIterator(const GridImp & grid,
-                             ALBERTA TRAVERSE_STACK * stack,
-                             int travLevel,
-                             ALBERTA EL_INFO *elInfo,
-                             int face=0,
-                             int edge=0,
-                             int vertex=0);
+    AlbertaGridTreeIterator(const GridImp & grid,
+                            ALBERTA TRAVERSE_STACK * stack,
+                            int travLevel,
+                            ALBERTA EL_INFO *elInfo,
+                            int face=0,
+                            int edge=0,
+                            int vertex=0);
 
     //! Constructor making begin iterator
-    AlbertaGridLevelIterator(const GridImp & grid,
-                             AlbertaMarkerVector * vec,
-                             int travLevel,
-                             int proc,
-                             bool leafIt=false);
+    AlbertaGridTreeIterator(const GridImp & grid,
+                            AlbertaMarkerVector * vec,
+                            int travLevel,
+                            int proc,
+                            bool leafIt=false);
 
     //! increment
     void increment();
     //! equality
-    bool equals(const AlbertaGridLevelIterator<cd,pitype,GridImp>& i) const;
-    //! dereferencing
-    Entity& dereference() const;
-
-    //! ask for level of entity
-    int level () const;
 
   private:
     // private Methods
@@ -992,16 +1080,14 @@ namespace Dune
     // search next macro el
     ALBERTA MACRO_EL * nextGhostMacro(ALBERTA MACRO_EL *mel);
 
-    //! the grid were it all comes from
-    const GridImp & grid_;
-
     //! level :)
     int level_;
 
     //! level :)
     int enLevel_;
 
-    mutable EntityImp virtualEntity_;
+    //! reference to entity of entity pointer class
+    EntityImp & virtualEntity_;
 
     // contains ALBERTA traversal stack
     ALBERTA ManageTravStack manageStack_;
@@ -1026,6 +1112,33 @@ namespace Dune
     const int proc_;
   };
 
+  //! --LevelIterator
+  //! the same as TreeIterator
+  template<int cd, PartitionIteratorType pitype, class GridImp>
+  class AlbertaGridLevelIterator :
+    public AlbertaGridTreeIterator<cd,pitype,GridImp> ,
+    public LevelIteratorDefault <cd,pitype,GridImp,AlbertaGridLevelIterator>
+  {
+  public:
+    typedef typename GridImp::template codim<cd>::Entity Entity;
+
+    //! Constructor making end iterator
+    AlbertaGridLevelIterator(const GridImp & grid, int level, int proc) :
+      AlbertaGridTreeIterator<cd,pitype,GridImp> (grid,level,proc)
+    {}
+
+    //! Constructor making begin iterator
+    AlbertaGridLevelIterator(const GridImp & grid,
+                             AlbertaMarkerVector * vec, int level, int proc) :
+      AlbertaGridTreeIterator<cd,pitype,GridImp> (grid,vec,level,proc)
+    {}
+
+    void increment ()
+    {
+      AlbertaGridTreeIterator<cd,pitype,GridImp>::increment();
+    }
+  };
+
   //**********************************************************************
   //
   //  AlbertaGridLeafIterator
@@ -1035,36 +1148,27 @@ namespace Dune
   //! LeafIterator which is just a hull for the LevelIterator
   template<class GridImp>
   class AlbertaGridLeafIterator :
+    public AlbertaGridTreeIterator<0,All_Partition,GridImp> ,
     public LeafIteratorDefault <GridImp,AlbertaGridLeafIterator>
   {
-    AlbertaGridLevelIterator<0,All_Partition,GridImp> it;
   public:
     typedef typename GridImp::template codim<0>::Entity Entity;
 
     //! Constructor making end iterator
     AlbertaGridLeafIterator(const GridImp & grid, int level, int proc) :
-      it(grid,level,proc,true) {}
+      AlbertaGridTreeIterator<0,All_Partition,GridImp> (grid,level,proc,true)
+    {}
 
     //! Constructor making begin iterator
     AlbertaGridLeafIterator(const GridImp & grid,
-                            AlbertaMarkerVector * vec,
-                            int level,
-                            int proc) :
-      it(grid,vec,level,proc,true) {}
+                            AlbertaMarkerVector * vec, int level, int proc) :
+      AlbertaGridTreeIterator<0,All_Partition,GridImp> (grid,vec,level,proc,true)
+    {}
 
-    //! increment
-    void increment() { it.increment(); }
-    //! equality
-    bool equals(const AlbertaGridLeafIterator<GridImp>& i) const
+    void increment ()
     {
-      return it.equals(i.it);
+      AlbertaGridTreeIterator<0,All_Partition,GridImp>::increment();
     }
-
-    //! dereferencing
-    Entity& dereference() const { return it.dereference(); }
-
-    //! ask for level of entity
-    int level () const { return it.level(); }
   };
 
   //**********************************************************************
@@ -1118,10 +1222,10 @@ namespace Dune
 
 
     // friends because of fillElInfo
-    friend class AlbertaGridLevelIterator<0,All_Partition,AlbertaGrid<dim,dimworld> >;
-    friend class AlbertaGridLevelIterator<1,All_Partition,AlbertaGrid<dim,dimworld> >;
-    friend class AlbertaGridLevelIterator<2,All_Partition,AlbertaGrid<dim,dimworld> >;
-    friend class AlbertaGridLevelIterator<3,All_Partition,AlbertaGrid<dim,dimworld> >;
+    friend class AlbertaGridTreeIterator<0,All_Partition,AlbertaGrid<dim,dimworld> >;
+    friend class AlbertaGridTreeIterator<1,All_Partition,AlbertaGrid<dim,dimworld> >;
+    friend class AlbertaGridTreeIterator<2,All_Partition,AlbertaGrid<dim,dimworld> >;
+    friend class AlbertaGridTreeIterator<3,All_Partition,AlbertaGrid<dim,dimworld> >;
     friend class AlbertaGridHierarchicIterator<AlbertaGrid<dim,dimworld> >;
 
     friend class AlbertaGridIntersectionIterator<AlbertaGrid<dim,dimworld> >;
@@ -1142,7 +1246,7 @@ namespace Dune
     typedef GridTraits<dim,dimworld,Dune::AlbertaGrid<dim,dimworld> ,
         AlbertaGridGeometry,AlbertaGridEntity,
         AlbertaGridBoundaryEntity,
-        AlbertaGridLevelIterator,
+        AlbertaGridEntityPointer,
         AlbertaGridLevelIterator,
         AlbertaGridIntersectionIterator,AlbertaGridHierarchicIterator,
         AlbertaGridLeafIterator>  Traits;
@@ -1286,6 +1390,7 @@ namespace Dune
     const HierarchicIndexSetType & hierarchicIndexSet () const { return hIndexSet_; }
     const LevelIndexSetType & levelIndexSet () const
     {
+      assert(false);
       if(!levelIndexSet_) levelIndexSet_ = new LevelIndexSetType (*this);
       return *levelIndexSet_;
     }
@@ -1418,16 +1523,29 @@ namespace Dune
     typedef AlbertaGridMakeableBoundaryEntity<const MyType> BoundaryImp;
 
   public:
-    typedef MemoryProvider< EntityImp > EntityProvider;
-    typedef MemoryProvider< GeometryImp > IntersectionSelfProvider;
-    typedef MemoryProvider< GeometryImp > IntersectionNeighProvider;
-    typedef MemoryProvider< BoundaryImp > IntersectionBoundaryProvider;
+    typedef AGMemoryProvider< EntityImp > EntityProvider;
+    typedef AGMemoryProvider< GeometryImp > IntersectionSelfProvider;
+    typedef AGMemoryProvider< GeometryImp > IntersectionNeighProvider;
+    typedef AGMemoryProvider< BoundaryImp > IntersectionBoundaryProvider;
 
     mutable EntityProvider entityProvider_;
     mutable IntersectionSelfProvider interSelfProvider_;
     mutable IntersectionNeighProvider interNeighProvider_;
     mutable IntersectionBoundaryProvider interBndProvider_;
 
+    template <int codim>
+    AlbertaGridMakeableEntity<codim,dim,const MyType> * getNewEntity (int level ) const
+    {
+      return new AlbertaGridMakeableEntity<codim,dim,const MyType> (*this,level);
+    }
+
+    template <int codim>
+    void freeEntity (AlbertaGridMakeableEntity<codim,dim,const MyType> * en) const
+    {
+      if(en) delete en;
+    }
+
+
   private:
     //*********************************************************************
     // organisation of the global index
@@ -1615,6 +1733,15 @@ namespace Dune
       assert(el);
       return (el->dof[i][0]);
     }
+
+    // codim = dim  means we get from dim-cd = 0
+    int getIndex ( const ALBERTA EL * el, int i , Int2Type<-1> fake ) const
+    {
+      assert(false);
+      DUNE_THROW(AlbertaError,"Error, wrong codimension!\n");
+      return -1;
+    }
+
   };
 
 
-- 
GitLab