Line data Source code
1 : // Implementation of coupling::indexing::CellIndex 2 : 3 : namespace coupling { 4 : namespace indexing { 5 : 6 : template <unsigned int dim, IndexTrait... traits> 7 : template <IndexTrait... converted_traits> 8 9615200 : CellIndex<dim, traits...>::operator CellIndex<dim, converted_traits...>() const { 9 : 10 : // conversion: identical IndexType -> "copy constructor" 11 : if constexpr (std::is_same_v<CellIndex, CellIndex<dim, converted_traits...>>) { 12 : return CellIndex{_index}; 13 : } 14 : 15 : #if (COUPLING_MD_ERROR == COUPLING_MD_YES) 16 9615200 : if (!IndexingService<dim>::getInstance().isInitialized()) { 17 16 : throw std::runtime_error(std::string("coupling::indexing::CellIndex::operator CellIndex(): IndexingService not initialized! (I.e. you attempted to convert " 18 : "indices before defining their domains)")); 19 : } 20 : #endif 21 : 22 : // conversion: non-BaseIndex -> BaseIndex 23 : if constexpr (std::is_same_v<CellIndex<dim, converted_traits...>, BaseIndex<dim>>) { 24 : // convert to vector if neccessary, then add offset, then construct a 25 : // BaseIndex from that tarch::la::Vector 26 10832288 : return BaseIndex<dim>{convertToVector<dim, traits...>(*this) + CellIndex<dim, traits...>::lowerBoundary.get()}; 27 : } 28 : 29 : // conversion: BaseIndex -> non-BaseIndex 30 : if constexpr (std::is_same_v<CellIndex, BaseIndex<dim>>) { 31 : // convert to scalar if neccessary + construct CellIndex<dim, 32 : // converted_traits...> 33 : if constexpr ((TraitOperations::is_same<IndexTrait::vector>(converted_traits) or ...)) { 34 4901160 : return CellIndex<dim, converted_traits...>{(*this - CellIndex<dim, converted_traits...>::lowerBoundary).get()}; 35 : } else /* result index is not a vector */ { 36 : // get scalar version of i_vec by first constructing a BaseIndex and then 37 : // converting it 38 1293264 : const unsigned int i_sca{// Get BaseIndex (i.e. traits=vector) and convert that to scalar 39 3879792 : convertToScalar<dim, IndexTrait::vector, converted_traits...>(CellIndex<dim, IndexTrait::vector, converted_traits...>( 40 : (*this - CellIndex<dim, converted_traits...>::lowerBoundary).get()))}; 41 : 42 1293264 : return CellIndex<dim, converted_traits...>{i_sca}; 43 : } 44 : } 45 : 46 : // conversion: non-BaseIndex -> different non-BaseIndex 47 : // call this -> BaseIndex conversion 48 1925552 : BaseIndex<dim> b{*this}; 49 : // call BaseIndex -> target index type conversion 50 1925552 : return CellIndex<dim, converted_traits...>{b}; 51 : } 52 : 53 : // Overloading operator<< for CellIndex 54 64 : template <unsigned int dim, IndexTrait... traits> std::ostream& operator<<(std::ostream& os, const CellIndex<dim, traits...>& i) { 55 64 : os << i.get(); 56 64 : return os; 57 : } 58 : 59 : // Overloading arithmetic operators for CellIndex 60 : template <unsigned int dim, IndexTrait... traits> 61 : CellIndex<dim, traits...> operator+(const CellIndex<dim, traits...>& i1, const CellIndex<dim, traits...>& i2) { 62 : if constexpr (std::is_same_v<CellIndex<dim, traits...>, BaseIndex<dim>>) 63 : return BaseIndex<dim>{i1.get() + i2.get()}; 64 : else 65 : return BaseIndex<dim>{i1} + BaseIndex<dim>{i2}; 66 : } 67 : 68 : template <unsigned int dim, IndexTrait... traits> 69 2274260 : coupling::indexing::CellIndex<dim, traits...> operator-(const CellIndex<dim, traits...>& i1, const CellIndex<dim, traits...>& i2) { 70 : if constexpr (std::is_same_v<CellIndex<dim, traits...>, BaseIndex<dim>>) 71 9097040 : return BaseIndex<dim>{i1.get() - i2.get()}; 72 : else 73 : return BaseIndex<dim>{i1} - BaseIndex<dim>{i2}; 74 : } 75 : 76 : template <unsigned int dim, IndexTrait... traits> 77 : CellIndex<dim, traits...> operator+(const CellIndex<dim, traits...>& i1, const typename CellIndex<dim, traits...>::value_T& raw_i) { 78 : return CellIndex<dim, traits...>{i1.get() + raw_i}; 79 : } 80 : 81 : template <unsigned int dim, IndexTrait... traits> 82 : coupling::indexing::CellIndex<dim, traits...> operator-(const CellIndex<dim, traits...>& i1, const typename CellIndex<dim, traits...>::value_T& raw_i) { 83 : return CellIndex<dim, traits...>{i1.get() - raw_i}; 84 : } 85 : 86 : } // namespace indexing 87 : } // namespace coupling