Search code examples
c++templatesboostsparse-matrix

How to get iterators if template type is a template itself?


My first template class try for a simplified converter from some boost compressed_matrix to PETSc Sparse AIJ format but I am running into problems with the iterator typedef declerations at the very start. So T is compressed_matrix, naively using the below code does result in compile time errors, how should one define these iterators instead. I should read a bit more perhaps on this templates...

template <class T, class T1>
int converter<T, T1>::convertMBo2Pe( const T& A,
                           T1 A_ ){
    PetscErrorCode ierr;
    int cntNnz = 0;
    typedef T::iterator1 i1_t;
    typedef T::iterator2 i2_t;
    //int nnz[ooFelieMatrix.size1()];
    int nnz[A.size1()];
    unsigned ind=0; 
    //get information about the matrix

    double* vals = NULL;
    for (i1_t i1 = A.begin1(); i1 != A.end1(); ++i1)
    {
      nnz[ind] = distance(i1.begin(), i1.end()); 
      ind++; 
    }
    // create the matrix depending 
    // on the values of the nonzeros
    // on each row
    ierr = MatCreateSeqAIJ( PETSC_COMM_SELF, A.size1(), 
                                                  A.size2(), cntNnz, nnz, 
                                                  A_ );
    PetscInt rInd = 0, cInd=0; 
    PetscInt* rCount, dummy; 
    rCount = &dummy;
    // pointer to values in a row
    PetscScalar*   valsOfRowI = NULL;
    PetscInt*  colIndexOfRowI = NULL;
    PetscInt rC = 1;
    for(i1_t i1 = A.begin1(); i1 != A.end1(); ++i1)
    {
      // allocate space for the values of row I
      valsOfRowI     = new PetscScalar[nnz[rInd]];
      colIndexOfRowI = new PetscInt[nnz[rInd]];
      for(i2_t i2 = i1.begin(); i2 != i1.end(); ++i2)
      {
         colIndexOfRowI[cInd] = i2.index2();
         valsOfRowI[cInd]     = *i2;
         cInd++;
      }
      // setting one row each time
      *rCount = rInd;
      MatSetValues( A_, rC, rCount, nnz[rInd], 
                                  colIndexOfRowI, valsOfRowI,
                  INSERT_VALUES );
      // delete
      delete [] valsOfRowI;
      delete [] colIndexOfRowI;
      rInd++; cInd = 0;
    }
    //
    MatAssemblyBegin( A_, MAT_FINAL_ASSEMBLY );
    MatAssemblyEnd( A_, MAT_FINAL_ASSEMBLY );
    // return 
    return 0;   
}

Solution

  • You have to explicitly tell the compiler that T::iterator1 is a type name (as opposed to a static variable, or something along those lines).

    typedef typename T::iterator1 i1_t;