// GenFem - A high-level finite element library
// Copyright (C) 2010-2026 Eric Bechet
//
// See the LICENSE file for license information and contributions.
// Please report all bugs and problems to <bechet@cadxfem.org>.
//
// Initial design: Frederic Duboeuf (rev.694)


#ifndef _SAVED_UNKNOWNTERM_H_
#define _SAVED_UNKNOWNTERM_H_

#include <vector>
#include <iostream>
#include "MElement.h"
#include "genTerm.h"
#include "genFSpace.h"


template<class T> class SavedUnknownTerm : public genTerm<genTensor0<double>,1>
{
public :
  typedef typename TensorialTraits<genTensor0<double> >::ValType ValType;
  typedef typename ContainerTraits<ValType,1>::Container ContainerValType;
  typedef typename std::map< Dof, ValType > mapUnknownType;
  static const int Nsp=1;
protected :
  mapUnknownType mapUnknown;
  typename diffTerm<T,1>::ConstHandle fs;

public :
  SavedUnknownTerm() {}
  SavedUnknownTerm(const typename diffTerm<T,1>::ConstHandle &fs_) : fs(fs_) {}
  SavedUnknownTerm & operator= (const SavedUnknownTerm<T> &other)
  {
    if (&other != this)
    {
      mapUnknown = other.mapUnknown;
      fs = other.fs;
    }
    return *this;
  }
  virtual int getNumKeys(MElement* ele,int k=0) const {return fs->getNumKeys(ele,k);}
  virtual void getKeys(MElement* ele, std::vector<Dof> &keys, int k=0) const {fs->getKeys(ele,keys,k);}
  virtual int getIncidentSpaceTag(int k=0) const { return fs->getIncidentSpaceTag(k);}
  virtual void get(MElement* ele, int npts, IntPt* GP, std::vector<ContainerValType> &vvals) const;
  virtual void get(MElement* ele, int npts, IntPt* GP, ContainerValType &vals) const;
  virtual SavedUnknownTerm<T>* clone () const { return new SavedUnknownTerm<T>(fs);}

  virtual int size() const { return mapUnknown.size(); }
  virtual mapUnknownType::const_iterator begin() const { return mapUnknown.begin(); }
  virtual mapUnknownType::const_iterator end() const { return mapUnknown.end(); }
  virtual bool isAnUnknown(Dof &D) const { if( mapUnknown.find(D) != mapUnknown.end()) return true; return false;}
  virtual void set(MElement* ele, int nbdofs, std::vector<Dof> &D, ContainerValType &Dofvals);
  virtual void print(const char* name);
  virtual bool printfile(const std::string &fileName,
                         const std::ios_base::openmode mode = std::ios::trunc,  // mode = app, ate, truncate
                         const int precision = 5);  // format = fixed, scientific
};



#include "savedUnknownTerm.hpp"


#endif // _SAVED_UNKNOWNTERM_H_




