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 
00047   template<typename T>
00048     void copySetToVector(const std::set<T>& set_obj, std::vector<T>& vec)
00049     {
00050       vec.clear();
00051       vec.reserve(set_obj.size());
00052 
00053       typename std::set<T>::const_iterator
00054         s_iter = set_obj.begin(), s_end = set_obj.end();
00055       for(; s_iter != s_end; ++s_iter) {
00056         vec.push_back(*s_iter);
00057       }
00058     }
00059 
00063   template<typename MAP_TYPE>
00064     void copyKeysToArray(const MAP_TYPE& map_obj,
00065        unsigned lenList,
00066        int* list)
00067     {
00068       unsigned i = 0;
00069       typename MAP_TYPE::const_iterator
00070         iter = map_obj.begin(), iter_end = map_obj.end();
00071 
00072       for(; iter != iter_end; ++iter) {
00073         if (i == lenList) break;
00074         list[i++] = iter->first;
00075       }
00076     }
00077 
00081   template<typename MAP_TYPE>
00082     void copyKeysToVector(const MAP_TYPE& map_obj,
00083                           std::vector<int>& keyvector)
00084     {
00085       keyvector.resize(map_obj.size());
00086       if (map_obj.empty()) return;
00087       copyKeysToArray<MAP_TYPE>(map_obj, map_obj.size(), &keyvector[0]);
00088     }
00089 
00090   template<typename T, typename U>
00091     void copyMapOfSetsToVectorOfVectors(const std::map<T,std::set<U> >& mapset,
00092                                      std::vector<T>& keys,
00093                                      std::vector<std::vector<U> >& values)
00094     {
00095       typedef std::map<T,std::set<U> > mapsettype;
00096       keys.resize(mapset.size());
00097       values.resize(mapset.size());
00098       typename mapsettype::const_iterator
00099        ms_iter = mapset.begin(), ms_end = mapset.end();
00100       for(size_t i=0; ms_iter!=ms_end; ++ms_iter, ++i) {
00101         keys[i] = ms_iter->first;
00102         typename std::set<U>::const_iterator
00103           s_iter = ms_iter->second.begin(), s_end = ms_iter->second.end();
00104         values[i].resize(ms_iter->second.size());
00105         for(size_t j=0; s_iter!=s_end; ++s_iter, ++j) {
00106           values[i][j] = *s_iter;
00107         }
00108       }
00109     }
00110 
00115   template<typename MAP_TYPE>
00116     void copyToArrays(MAP_TYPE& map_obj,
00117           int lenList,
00118           int* keylist,
00119           int* vallist)
00120     {
00121       int i = 0;
00122       typename MAP_TYPE::iterator
00123         iter = map_obj.begin(),
00124              iter_end = map_obj.end();
00125 
00126       for(; iter != iter_end; ++iter) {
00127         if (i == lenList) break;
00128         keylist[i] = (*iter).first;
00129         vallist[i++] = (*iter).second;
00130       }
00131     }
00132 
00134   template<typename MAP_TYPE>
00135     void destroyValues(MAP_TYPE& map_obj)
00136     {
00137       typename MAP_TYPE::iterator
00138         m_iter = map_obj.begin(),
00139       m_end  = map_obj.end();
00140 
00141       for(; m_iter != m_end; ++m_iter) {
00142         delete (*m_iter).second;
00143       }
00144     }
00145 
00147   template<typename MAP_TYPE, typename SET_TYPE>
00148   void writeToStream(snl_fei::RaggedTable<MAP_TYPE,SET_TYPE>& table,
00149          FEI_OSTREAM& os,
00150          const char* lineprefix=NULL)
00151   {
00152     MAP_TYPE& map_obj = table.getMap();
00153     typename MAP_TYPE::iterator
00154       m_iter = map_obj.begin(),
00155       m_end = map_obj.end();
00156 
00157     for(; m_iter != m_end; ++m_iter) {
00158       if (lineprefix != NULL) {
00159         os << lineprefix;
00160       }
00161 
00162       os << " row "<<(*m_iter).first<<": ";
00163 
00164       typename SET_TYPE::const_iterator
00165         s_iter = (*m_iter).second->begin(),
00166                s_end = (*m_iter).second->end();
00167 
00168       for(; s_iter != s_end; ++s_iter) {
00169         os << *s_iter << " ";
00170       }
00171 
00172       os << FEI_ENDL;
00173     }
00174   }
00175 
00176   template<typename MAP_TYPE, typename SET_TYPE>
00177     void packRaggedTable(snl_fei::RaggedTable<MAP_TYPE,SET_TYPE>& table,
00178                        std::vector<int>& intdata)
00179     {
00180       MAP_TYPE& map_obj = table.getMap();
00181       int numRows = map_obj.size();
00182 
00183       typename MAP_TYPE::iterator
00184         m_iter = map_obj.begin(),
00185         m_end  = map_obj.end();
00186 
00187       int nnz = 0;
00188 
00189       for(; m_iter != m_end; ++m_iter) {
00190         typename MAP_TYPE::value_type m_pair = *m_iter;
00191 
00192         int rowLen = m_pair.second->size();
00193         nnz += rowLen;
00194       }
00195 
00196       intdata.resize(1+2*numRows+nnz);
00197       intdata[0] = numRows;
00198       int* rowNumbers = &intdata[1];
00199       int* rowLengths = rowNumbers+numRows;
00200       int* packedCols = rowLengths+numRows;
00201 
00202       m_iter = map_obj.begin();
00203       unsigned offset = 0;
00204       for(unsigned i=0; m_iter != m_end; ++m_iter, ++i) {
00205         typename MAP_TYPE::value_type m_pair = *m_iter;
00206         rowNumbers[i] = m_pair.first;
00207         rowLengths[i] = m_pair.second->size();
00208 
00209         int* colInds = &packedCols[offset];
00210         copySetToArray(*(m_pair.second), rowLengths[i], colInds);
00211         offset += rowLengths[i];
00212       }
00213     }
00214 
00217   template<typename MAP_TYPE, typename SET_TYPE>
00218   fei::SharedPtr<fei::SparseRowGraph> createSparseRowGraph(const std::vector<snl_fei::RaggedTable<MAP_TYPE,SET_TYPE>* >& tables)
00219     {
00220       int numRows = 0;
00221       int nnz = 0;
00222       fei::SharedPtr<fei::SparseRowGraph> srg(new fei::SparseRowGraph);
00223 
00224       for(unsigned i=0; i<tables.size(); ++i) {
00225         MAP_TYPE& map_obj = tables[i]->getMap();
00226         numRows += map_obj.size();
00227 
00228         typename MAP_TYPE::iterator
00229           m_iter = map_obj.begin(),
00230           m_end  = map_obj.end();
00231         for(; m_iter != m_end; ++m_iter) {
00232           typename MAP_TYPE::value_type m_pair = *m_iter;
00233           nnz += m_pair.second->size();
00234         }
00235       }
00236 
00237       srg->rowNumbers.resize(numRows);
00238       srg->rowOffsets.resize(numRows+1);
00239       srg->packedColumnIndices.resize(nnz);
00240 
00241       unsigned offset1 = 0;
00242       unsigned rowOffset = 0;
00243       for(unsigned i=0; i<tables.size(); ++i) {
00244         MAP_TYPE& map_obj = tables[i]->getMap();
00245 
00246         typename MAP_TYPE::iterator
00247           m_iter = map_obj.begin(),
00248           m_end  = map_obj.end();
00249         for(; m_iter != m_end; ++m_iter) {
00250           typename MAP_TYPE::value_type m_pair = *m_iter;
00251           srg->rowNumbers[offset1] = m_pair.first;
00252           int rowLen = m_pair.second->size();
00253           srg->rowOffsets[offset1++] = rowOffset;
00254           int* cols = &srg->packedColumnIndices[rowOffset];
00255           copySetToArray(*(m_pair.second), rowLen, cols);
00256           rowOffset += rowLen;
00257         }
00258       }
00259 
00260       srg->rowOffsets[offset1] = rowOffset;
00261 
00262       return(srg);
00263     }
00264 
00266   template<typename MAP_TYPE, typename SET_TYPE>
00267   void copyToSparseRowGraph(snl_fei::RaggedTable<MAP_TYPE,SET_TYPE>& table,
00268                             fei::SparseRowGraph& srg)
00269     {
00270       MAP_TYPE& map_obj = table.getMap();
00271       int numRows = map_obj.size();
00272 
00273       srg.rowNumbers.resize(numRows);
00274       srg.rowOffsets.resize(numRows+1);
00275 
00276       int* rowNumPtr = &(srg.rowNumbers[0]);
00277       int* rowOffsPtr = &(srg.rowOffsets[0]);
00278 
00279       typename MAP_TYPE::iterator
00280         m_iter = map_obj.begin(),
00281         m_end  = map_obj.end();
00282 
00283       int offset = 0;
00284       int nnz = 0;
00285 
00286       for(; m_iter != m_end; ++m_iter) {
00287         typename MAP_TYPE::value_type m_pair = *m_iter;
00288 
00289         rowNumPtr[offset] = m_pair.first;
00290         rowOffsPtr[offset++] = nnz;
00291         int rowLen = m_pair.second->size();
00292         nnz += rowLen;
00293       }
00294       rowOffsPtr[offset] = nnz;
00295 
00296       srg.packedColumnIndices.resize(nnz);
00297       int* colPtr = &(srg.packedColumnIndices[0]);
00298       offset = 0;
00299       m_iter = map_obj.begin();
00300       int i = 0;
00301       for(; m_iter != m_end; ++m_iter, ++i) {
00302         typename MAP_TYPE::value_type m_pair = *m_iter;
00303 
00304         int rowLen = rowOffsPtr[i+1]-rowOffsPtr[i];
00305         int* colInds = &(colPtr[offset]);
00306         copySetToArray(*(m_pair.second), rowLen, colInds);
00307         offset += rowLen;
00308       }
00309     }
00310 
00313   template<typename MAP_TYPE, typename SET_TYPE>
00314   fei::SharedPtr<fei::SparseRowGraph>
00315     createSparseRowGraph(snl_fei::RaggedTable<MAP_TYPE,SET_TYPE>& table)
00316     {
00317       fei::SharedPtr<fei::SparseRowGraph> srg(new fei::SparseRowGraph);
00318 
00319       copyToSparseRowGraph<MAP_TYPE, SET_TYPE>(table, *srg);
00320 
00321       return( srg );
00322     }
00323 
00325   template<typename MAP_TYPE, typename SET_TYPE>
00326   int countNonzeros(snl_fei::RaggedTable<MAP_TYPE,SET_TYPE>& table)
00327     {
00328       int nnz = 0;
00329       MAP_TYPE& map_obj = table.getMap();
00330       typename MAP_TYPE::iterator
00331         m_iter = map_obj.begin(),
00332                m_end = map_obj.end();
00333 
00334       for(; m_iter != m_end; ++m_iter) {
00335         nnz += (*m_iter).second->size();
00336       }
00337 
00338       return(nnz);
00339     }
00340 
00341 } //namespace fei
00342 
00343 #endif // _fei_TemplateUtils_hpp_
00344 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends
Generated on Wed Apr 13 10:08:24 2011 for FEI by  doxygen 1.6.3