fei_TemplateUtils.hpp

00001 #ifndef _fei_TemplateUtils_hpp_
00002 #define _fei_TemplateUtils_hpp_
00003 
00004 /*--------------------------------------------------------------------*/
00005 /*    Copyright 2005 Sandia Corporation.                              */
00006 /*    Under the terms of Contract DE-AC04-94AL85000, there is a       */
00007 /*    non-exclusive license for use of this work by or on behalf      */
00008 /*    of the U.S. Government.  Export of this program may require     */
00009 /*    a license from the United States Government.                    */
00010 /*--------------------------------------------------------------------*/
00011 
00012 #include <fei_iosfwd.hpp>
00013 #include <fei_SparseRowGraph.hpp>
00014 #include <snl_fei_RaggedTable.hpp>
00015 #include <snl_fei_Utils.hpp>
00016 
00017 #include <fei_ErrMacros.hpp>
00018 
00019 #include <set>
00020 #include <vector>
00021 #include <map>
00022 
00023 namespace fei {
00024 
00025   //------------------------------------------------------------------------
00029   template<typename SET_TYPE>
00030     void copySetToArray(const SET_TYPE& set_obj,
00031       int lenList,
00032       int* list)
00033     {
00034       int setsize = set_obj.size();
00035       int len = setsize > lenList ? lenList : setsize;
00036 
00037       typename SET_TYPE::const_iterator
00038   s_iter = set_obj.begin(),
00039   s_end = set_obj.end();
00040       for(int i=0; i<len; ++i, ++s_iter) {
00041   list[i] = *s_iter;
00042       }
00043     }
00044 
00048   template<typename MAP_TYPE>
00049     void copyKeysToArray(MAP_TYPE& map_obj,
00050        unsigned lenList,
00051        int* list)
00052     {
00053       unsigned i = 0;
00054       typename MAP_TYPE::iterator
00055   iter = map_obj.begin(),
00056   iter_end = map_obj.end();
00057 
00058       for(; iter != iter_end; ++iter) {
00059   if (i == lenList) break;
00060   list[i++] = (*iter).first;
00061       }
00062     }
00063 
00067   template<typename MAP_TYPE>
00068     void copyKeysToVector(MAP_TYPE& map_obj,
00069                           std::vector<int>& keyvector)
00070     {
00071       keyvector.resize(map_obj.size());
00072       if (map_obj.empty()) return;
00073       copyKeysToArray<MAP_TYPE>(map_obj, map_obj.size(), &keyvector[0]);
00074     }
00075 
00080   template<typename MAP_TYPE>
00081     void copyToArrays(MAP_TYPE& map_obj,
00082           int lenList,
00083           int* keylist,
00084           int* vallist)
00085     {
00086       int i = 0;
00087       typename MAP_TYPE::iterator
00088   iter = map_obj.begin(),
00089   iter_end = map_obj.end();
00090 
00091       for(; iter != iter_end; ++iter) {
00092   if (i == lenList) break;
00093   keylist[i] = (*iter).first;
00094   vallist[i++] = (*iter).second;
00095       }
00096     }
00097 
00099   template<typename MAP_TYPE>
00100     void destroyValues(MAP_TYPE& map_obj)
00101     {
00102       typename MAP_TYPE::iterator
00103   m_iter = map_obj.begin(),
00104   m_end  = map_obj.end();
00105 
00106       for(; m_iter != m_end; ++m_iter) {
00107   delete (*m_iter).second;
00108       }
00109     }
00110 
00112   template<typename MAP_TYPE, typename SET_TYPE>
00113   void writeToStream(snl_fei::RaggedTable<MAP_TYPE,SET_TYPE>& table,
00114          FEI_OSTREAM& os,
00115          const char* lineprefix=NULL)
00116   {
00117     MAP_TYPE& map_obj = table.getMap();
00118     typename MAP_TYPE::iterator
00119       m_iter = map_obj.begin(),
00120       m_end = map_obj.end();
00121 
00122     for(; m_iter != m_end; ++m_iter) {
00123       if (lineprefix != NULL) {
00124   os << lineprefix;
00125       }
00126 
00127       os << " row "<<(*m_iter).first<<": ";
00128 
00129       typename SET_TYPE::const_iterator
00130   s_iter = (*m_iter).second->begin(),
00131   s_end = (*m_iter).second->end();
00132 
00133       for(; s_iter != s_end; ++s_iter) {
00134   os << *s_iter << " ";
00135       }
00136 
00137       os << FEI_ENDL;
00138     }
00139   }
00140 
00141   template<typename MAP_TYPE, typename SET_TYPE>
00142   void packRaggedTable(snl_fei::RaggedTable<MAP_TYPE,SET_TYPE>& table,
00143                        std::vector<int>& intdata)
00144     {
00145       MAP_TYPE& map_obj = table.getMap();
00146       int numRows = map_obj.size();
00147 
00148       typename MAP_TYPE::iterator
00149         m_iter = map_obj.begin(),
00150         m_end  = map_obj.end();
00151 
00152       int nnz = 0;
00153 
00154       for(; m_iter != m_end; ++m_iter) {
00155         typename MAP_TYPE::value_type m_pair = *m_iter;
00156 
00157         int rowLen = m_pair.second->size();
00158         nnz += rowLen;
00159       }
00160 
00161       intdata.resize(1+2*numRows+nnz);
00162       intdata[0] = numRows;
00163       int* rowNumbers = &intdata[1];
00164       int* rowLengths = rowNumbers+numRows;
00165       int* packedCols = rowLengths+numRows;
00166 
00167       m_iter = map_obj.begin();
00168       unsigned offset = 0;
00169       for(unsigned i=0; m_iter != m_end; ++m_iter, ++i) {
00170         typename MAP_TYPE::value_type m_pair = *m_iter;
00171         rowNumbers[i] = m_pair.first;
00172         rowLengths[i] = m_pair.second->size();
00173 
00174         int* colInds = &packedCols[offset];
00175         copySetToArray(*(m_pair.second), rowLengths[i], colInds);
00176         offset += rowLengths[i];
00177       }
00178     }
00179 
00182   template<typename MAP_TYPE, typename SET_TYPE>
00183   fei::SharedPtr<fei::SparseRowGraph> createSparseRowGraph(const std::vector<snl_fei::RaggedTable<MAP_TYPE,SET_TYPE>* >& tables)
00184     {
00185       int numRows = 0;
00186       int nnz = 0;
00187       fei::SharedPtr<fei::SparseRowGraph> srg(new fei::SparseRowGraph);
00188 
00189       for(unsigned i=0; i<tables.size(); ++i) {
00190         MAP_TYPE& map_obj = tables[i]->getMap();
00191         numRows += map_obj.size();
00192 
00193         typename MAP_TYPE::iterator
00194           m_iter = map_obj.begin(),
00195           m_end  = map_obj.end();
00196         for(; m_iter != m_end; ++m_iter) {
00197           typename MAP_TYPE::value_type m_pair = *m_iter;
00198           nnz += m_pair.second->size();
00199         }
00200       }
00201 
00202       srg->rowNumbers.resize(numRows);
00203       srg->rowOffsets.resize(numRows+1);
00204       srg->packedColumnIndices.resize(nnz);
00205 
00206       unsigned offset1 = 0;
00207       unsigned rowOffset = 0;
00208       for(unsigned i=0; i<tables.size(); ++i) {
00209         MAP_TYPE& map_obj = tables[i]->getMap();
00210 
00211         typename MAP_TYPE::iterator
00212           m_iter = map_obj.begin(),
00213           m_end  = map_obj.end();
00214         for(; m_iter != m_end; ++m_iter) {
00215           typename MAP_TYPE::value_type m_pair = *m_iter;
00216           srg->rowNumbers[offset1] = m_pair.first;
00217           int rowLen = m_pair.second->size();
00218           srg->rowOffsets[offset1++] = rowOffset;
00219           int* cols = &srg->packedColumnIndices[rowOffset];
00220           copySetToArray(*(m_pair.second), rowLen, cols);
00221           rowOffset += rowLen;
00222         }
00223       }
00224 
00225       srg->rowOffsets[offset1] = rowOffset;
00226 
00227       return(srg);
00228     }
00229 
00231   template<typename MAP_TYPE, typename SET_TYPE>
00232   void copyToSparseRowGraph(snl_fei::RaggedTable<MAP_TYPE,SET_TYPE>& table,
00233                             fei::SparseRowGraph& srg)
00234     {
00235       MAP_TYPE& map_obj = table.getMap();
00236       int numRows = map_obj.size();
00237 
00238       srg.rowNumbers.resize(numRows);
00239       srg.rowOffsets.resize(numRows+1);
00240 
00241       int* rowNumPtr = &(srg.rowNumbers[0]);
00242       int* rowOffsPtr = &(srg.rowOffsets[0]);
00243 
00244       typename MAP_TYPE::iterator
00245         m_iter = map_obj.begin(),
00246         m_end  = map_obj.end();
00247 
00248       int offset = 0;
00249       int nnz = 0;
00250 
00251       for(; m_iter != m_end; ++m_iter) {
00252         typename MAP_TYPE::value_type m_pair = *m_iter;
00253 
00254         rowNumPtr[offset] = m_pair.first;
00255         rowOffsPtr[offset++] = nnz;
00256         int rowLen = m_pair.second->size();
00257         nnz += rowLen;
00258       }
00259       rowOffsPtr[offset] = nnz;
00260 
00261       srg.packedColumnIndices.resize(nnz);
00262       int* colPtr = &(srg.packedColumnIndices[0]);
00263       offset = 0;
00264       m_iter = map_obj.begin();
00265       int i = 0;
00266       for(; m_iter != m_end; ++m_iter, ++i) {
00267         typename MAP_TYPE::value_type m_pair = *m_iter;
00268 
00269         int rowLen = rowOffsPtr[i+1]-rowOffsPtr[i];
00270         int* colInds = &(colPtr[offset]);
00271         copySetToArray(*(m_pair.second), rowLen, colInds);
00272         offset += rowLen;
00273       }
00274     }
00275 
00278   template<typename MAP_TYPE, typename SET_TYPE>
00279   fei::SharedPtr<fei::SparseRowGraph>
00280     createSparseRowGraph(snl_fei::RaggedTable<MAP_TYPE,SET_TYPE>& table)
00281     {
00282       fei::SharedPtr<fei::SparseRowGraph> srg(new fei::SparseRowGraph);
00283 
00284       copyToSparseRowGraph<MAP_TYPE, SET_TYPE>(table, *srg);
00285 
00286       return( srg );
00287     }
00288 
00290   template<typename MAP_TYPE, typename SET_TYPE>
00291   int countNonzeros(snl_fei::RaggedTable<MAP_TYPE,SET_TYPE>& table)
00292     {
00293       int nnz = 0;
00294       MAP_TYPE& map_obj = table.getMap();
00295       typename MAP_TYPE::iterator
00296   m_iter = map_obj.begin(),
00297   m_end = map_obj.end();
00298 
00299       for(; m_iter != m_end; ++m_iter) {
00300   nnz += (*m_iter).second->size();
00301       }
00302 
00303       return(nnz);
00304     }
00305 
00306 } //namespace fei
00307 
00308 #endif // _fei_TemplateUtils_hpp_
00309 

Generated on Wed May 12 21:30:41 2010 for FEI by  doxygen 1.4.7