// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

/** \file DualTraverse.h */

#ifndef AMDIS_DUALTRAVERSE_H
#define AMDIS_DUALTRAVERSE_H

#include "Traverse.h"
#include "Flag.h"
#include "MemoryManager.h"

namespace AMDiS {

  class Mesh;
  class ElInfo;

  // ====================================================================
  // === class DualTraverse =============================================
  // ====================================================================

  /** \brief
   * Parallel traversal of two meshes. 
   */
  class DualTraverse
  {
  public:
    MEMORY_MANAGED(DualTraverse);

    DualTraverse() 
      : fillSubElemInfo(false)
    {};

    ~DualTraverse() {};

    /** \brief
     * Start dual traversal
     */
    bool traverseFirst(Mesh *mesh1, 
		       Mesh *mesh2, 
		       int level1, 
		       int level2, 
		       Flag flag1,
		       Flag flag2,
		       ElInfo **elInfo1,
		       ElInfo **elInfo2,
		       ElInfo **elInfoSmall,
		       ElInfo **elInfoLarge);

    /** \brief
     * Get next ElInfo combination
     */
    bool traverseNext(ElInfo **elInfoNext1,
		      ElInfo **elInfoNext2,
		      ElInfo **elInfoSmall,
		      ElInfo **elInfoLarge);

    bool skipEl1(ElInfo *elInfo) {
      return false;
    }

    bool skipEl2(ElInfo *elInfo) {
      return false;
    }

    bool check(ElInfo **elInfo1,
	       ElInfo **elInfo2,
	       ElInfo **elInfoSmall,
	       ElInfo **elInfoLarge) 
    {
      prepareNextStep(elInfo1, elInfo2, elInfoSmall, elInfoLarge);
      return true;
    }

    inline void setFillSubElemInfo(bool b) {
      fillSubElemInfo = b;
    }
      
  protected:
    /** \brief
     * Determines smaller and larger element, determines which element(s) has to 
     * be incremented in the next step 
     */
    void prepareNextStep(ElInfo **elInfo1,
			 ElInfo **elInfo2,
			 ElInfo **elInfoSmall,
			 ElInfo **elInfoLarge);

    void fillSubElInfo(ElInfo *elInfo1, 
		       ElInfo *elInfo2,
		       ElInfo *elInfoSmall,
		       ElInfo *elInfoLarge);

  protected:
    /** \brief
     * stack for mesh 1
     */
    TraverseStack stack1;

    /** \brief
     * stack for mesh 2
     */
    TraverseStack stack2;

    /** \brief
     * used to determine whether all small elements belonging to the large
     * element are traversed.
     */
    double rest;

    /** \brief
     * true is element 1 should be incremented (set in prepareNextStep())
     */
    bool inc1;

    /** \brief
     * true is element 2 should be incremented (set in prepareNextStep())
     */
    bool inc2;

    /** \brief
     * for level traverse of mesh 1
     */
    int level1_;

    /** \brief
     * for level traverse of mesh 2
     */
    int level2_;

    /** \brief
     * for leaf element level traverse of mesh 1
     */
    bool callLeafElLevel1_;

    /** \brief
     * for leaf element level traverse of mesh 2
     */
    bool callLeafElLevel2_;

    bool fillSubElemInfo;
  };

}

#endif