1 #ifndef _fei_TemplateUtils_hpp_
2 #define _fei_TemplateUtils_hpp_
12 #include <fei_iosfwd.hpp>
13 #include <fei_SparseRowGraph.hpp>
14 #include <snl_fei_RaggedTable.hpp>
15 #include <snl_fei_Utils.hpp>
17 #include <fei_ErrMacros.hpp>
29 template<
typename SET_TYPE>
34 int setsize = set_obj.size();
35 int len = setsize > lenList ? lenList : setsize;
37 typename SET_TYPE::const_iterator
38 s_iter = set_obj.begin();
39 for(
int i=0; i<len; ++i, ++s_iter) {
50 vec.reserve(set_obj.size());
52 typename std::set<T>::const_iterator
53 s_iter = set_obj.begin(), s_end = set_obj.end();
54 for(; s_iter != s_end; ++s_iter) {
55 vec.push_back(*s_iter);
62 template<
typename MAP_TYPE>
68 typename MAP_TYPE::const_iterator
69 iter = map_obj.begin(), iter_end = map_obj.end();
71 for(; iter != iter_end; ++iter) {
72 if (i == lenList)
break;
73 list[i++] = iter->first;
80 template<
typename MAP_TYPE>
82 std::vector<int>& keyvector)
84 keyvector.resize(map_obj.size());
85 if (map_obj.empty())
return;
86 copyKeysToArray<MAP_TYPE>(map_obj, map_obj.size(), &keyvector[0]);
89 template<
typename T,
typename U>
90 void copyMapOfSetsToVectorOfVectors(
const std::map<T,std::set<U> >& mapset,
92 std::vector<std::vector<U> >& values)
94 typedef std::map<T,std::set<U> > mapsettype;
95 keys.resize(mapset.size());
96 values.resize(mapset.size());
97 typename mapsettype::const_iterator
98 ms_iter = mapset.begin(), ms_end = mapset.end();
99 for(
size_t i=0; ms_iter!=ms_end; ++ms_iter, ++i) {
100 keys[i] = ms_iter->first;
101 typename std::set<U>::const_iterator
102 s_iter = ms_iter->second.begin(), s_end = ms_iter->second.end();
103 values[i].resize(ms_iter->second.size());
104 for(
size_t j=0; s_iter!=s_end; ++s_iter, ++j) {
105 values[i][j] = *s_iter;
114 template<
typename MAP_TYPE>
121 typename MAP_TYPE::iterator
122 iter = map_obj.begin(),
123 iter_end = map_obj.end();
125 for(; iter != iter_end; ++iter) {
126 if (i == lenList)
break;
127 keylist[i] = (*iter).first;
128 vallist[i++] = (*iter).second;
133 template<
typename MAP_TYPE>
136 typename MAP_TYPE::iterator
137 m_iter = map_obj.begin(),
138 m_end = map_obj.end();
140 for(; m_iter != m_end; ++m_iter) {
141 delete (*m_iter).second;
146 template<
typename MAP_TYPE,
typename SET_TYPE>
149 const char* lineprefix=NULL)
151 MAP_TYPE& map_obj = table.
getMap();
152 typename MAP_TYPE::iterator
153 m_iter = map_obj.begin(),
154 m_end = map_obj.end();
156 for(; m_iter != m_end; ++m_iter) {
157 if (lineprefix != NULL) {
161 os <<
" row "<<(*m_iter).first<<
": ";
163 typename SET_TYPE::const_iterator
164 s_iter = (*m_iter).second->begin(),
165 s_end = (*m_iter).second->end();
167 for(; s_iter != s_end; ++s_iter) {
168 os << *s_iter <<
" ";
175 template<
typename MAP_TYPE,
typename SET_TYPE>
177 std::vector<int>& intdata)
179 MAP_TYPE& map_obj = table.
getMap();
180 int numRows = map_obj.size();
182 typename MAP_TYPE::iterator
183 m_iter = map_obj.begin(),
184 m_end = map_obj.end();
188 for(; m_iter != m_end; ++m_iter) {
189 typename MAP_TYPE::value_type m_pair = *m_iter;
191 int rowLen = m_pair.second->size();
195 intdata.resize(1+2*numRows+nnz);
196 intdata[0] = numRows;
197 int* rowNumbers = &intdata[1];
198 int* rowLengths = rowNumbers+numRows;
199 int* packedCols = rowLengths+numRows;
201 m_iter = map_obj.begin();
203 for(
unsigned i=0; m_iter != m_end; ++m_iter, ++i) {
204 typename MAP_TYPE::value_type m_pair = *m_iter;
205 rowNumbers[i] = m_pair.first;
206 rowLengths[i] = m_pair.second->size();
208 int* colInds = &packedCols[offset];
210 offset += rowLengths[i];
216 template<
typename MAP_TYPE,
typename SET_TYPE>
223 for(
unsigned i=0; i<tables.size(); ++i) {
224 MAP_TYPE& map_obj = tables[i]->getMap();
225 numRows += map_obj.size();
227 typename MAP_TYPE::iterator
228 m_iter = map_obj.begin(),
229 m_end = map_obj.end();
230 for(; m_iter != m_end; ++m_iter) {
231 typename MAP_TYPE::value_type m_pair = *m_iter;
232 nnz += m_pair.second->size();
236 srg->rowNumbers.resize(numRows);
237 srg->rowOffsets.resize(numRows+1);
238 srg->packedColumnIndices.resize(nnz);
240 unsigned offset1 = 0;
241 unsigned rowOffset = 0;
242 for(
unsigned i=0; i<tables.size(); ++i) {
243 MAP_TYPE& map_obj = tables[i]->getMap();
245 typename MAP_TYPE::iterator
246 m_iter = map_obj.begin(),
247 m_end = map_obj.end();
248 for(; m_iter != m_end; ++m_iter) {
249 typename MAP_TYPE::value_type m_pair = *m_iter;
250 srg->rowNumbers[offset1] = m_pair.first;
251 int rowLen = m_pair.second->size();
252 srg->rowOffsets[offset1++] = rowOffset;
253 int* cols = &srg->packedColumnIndices[rowOffset];
259 srg->rowOffsets[offset1] = rowOffset;
265 template<
typename MAP_TYPE,
typename SET_TYPE>
269 MAP_TYPE& map_obj = table.
getMap();
270 int numRows = map_obj.size();
275 int* rowNumPtr = numRows>0 ? &(srg.
rowNumbers[0]): NULL;
278 typename MAP_TYPE::iterator
279 m_iter = map_obj.begin(),
280 m_end = map_obj.end();
285 for(; m_iter != m_end; ++m_iter) {
286 typename MAP_TYPE::value_type m_pair = *m_iter;
288 rowNumPtr[offset] = m_pair.first;
289 rowOffsPtr[offset++] = nnz;
290 int rowLen = m_pair.second->size();
293 rowOffsPtr[offset] = nnz;
298 m_iter = map_obj.begin();
300 for(; m_iter != m_end; ++m_iter, ++i) {
301 typename MAP_TYPE::value_type m_pair = *m_iter;
303 int rowLen = rowOffsPtr[i+1]-rowOffsPtr[i];
304 int* colInds = &(colPtr[offset]);
312 template<
typename MAP_TYPE,
typename SET_TYPE>
318 copyToSparseRowGraph<MAP_TYPE, SET_TYPE>(table, *srg);
324 template<
typename MAP_TYPE,
typename SET_TYPE>
328 MAP_TYPE& map_obj = table.
getMap();
329 typename MAP_TYPE::iterator
330 m_iter = map_obj.begin(),
331 m_end = map_obj.end();
333 for(; m_iter != m_end; ++m_iter) {
334 nnz += (*m_iter).second->size();
342 #endif // _fei_TemplateUtils_hpp_
void copySetToVector(const std::set< T > &set_obj, std::vector< T > &vec)
int countNonzeros(snl_fei::RaggedTable< MAP_TYPE, SET_TYPE > &table)
std::vector< int > rowNumbers
void copySetToArray(const SET_TYPE &set_obj, int lenList, int *list)
void writeToStream(snl_fei::RaggedTable< MAP_TYPE, SET_TYPE > &table, FEI_OSTREAM &os, const char *lineprefix=NULL)
void copyKeysToArray(const MAP_TYPE &map_obj, unsigned lenList, int *list)
std::vector< int > packedColumnIndices
std::vector< int > rowOffsets
void copyKeysToVector(const MAP_TYPE &map_obj, std::vector< int > &keyvector)
void copyToArrays(MAP_TYPE &map_obj, int lenList, int *keylist, int *vallist)
fei::SharedPtr< fei::SparseRowGraph > createSparseRowGraph(const std::vector< snl_fei::RaggedTable< MAP_TYPE, SET_TYPE > * > &tables)
void copyToSparseRowGraph(snl_fei::RaggedTable< MAP_TYPE, SET_TYPE > &table, fei::SparseRowGraph &srg)
void destroyValues(MAP_TYPE &map_obj)