1 #ifndef _fei_TemplateUtils_hpp_
2 #define _fei_TemplateUtils_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 s_end = set_obj.end();
40 for(
int i=0; i<len; ++i, ++s_iter) {
51 vec.reserve(set_obj.size());
53 typename std::set<T>::const_iterator
54 s_iter = set_obj.begin(), s_end = set_obj.end();
55 for(; s_iter != s_end; ++s_iter) {
56 vec.push_back(*s_iter);
63 template<
typename MAP_TYPE>
69 typename MAP_TYPE::const_iterator
70 iter = map_obj.begin(), iter_end = map_obj.end();
72 for(; iter != iter_end; ++iter) {
73 if (i == lenList)
break;
74 list[i++] = iter->first;
81 template<
typename MAP_TYPE>
83 std::vector<int>& keyvector)
85 keyvector.resize(map_obj.size());
86 if (map_obj.empty())
return;
87 copyKeysToArray<MAP_TYPE>(map_obj, map_obj.size(), &keyvector[0]);
90 template<
typename T,
typename U>
93 std::vector<std::vector<U> >& values)
95 typedef std::map<T,std::set<U> > mapsettype;
96 keys.resize(mapset.size());
97 values.resize(mapset.size());
98 typename mapsettype::const_iterator
99 ms_iter = mapset.begin(), ms_end = mapset.end();
100 for(
size_t i=0; ms_iter!=ms_end; ++ms_iter, ++i) {
101 keys[i] = ms_iter->first;
102 typename std::set<U>::const_iterator
103 s_iter = ms_iter->second.begin(), s_end = ms_iter->second.end();
104 values[i].resize(ms_iter->second.size());
105 for(
size_t j=0; s_iter!=s_end; ++s_iter, ++j) {
106 values[i][j] = *s_iter;
115 template<
typename MAP_TYPE>
122 typename MAP_TYPE::iterator
123 iter = map_obj.begin(),
124 iter_end = map_obj.end();
126 for(; iter != iter_end; ++iter) {
127 if (i == lenList)
break;
128 keylist[i] = (*iter).first;
129 vallist[i++] = (*iter).second;
134 template<
typename MAP_TYPE>
137 typename MAP_TYPE::iterator
138 m_iter = map_obj.begin(),
139 m_end = map_obj.end();
141 for(; m_iter != m_end; ++m_iter) {
142 delete (*m_iter).second;
147 template<
typename MAP_TYPE,
typename SET_TYPE>
150 const char* lineprefix=NULL)
152 MAP_TYPE& map_obj = table.
getMap();
153 typename MAP_TYPE::iterator
154 m_iter = map_obj.begin(),
155 m_end = map_obj.end();
157 for(; m_iter != m_end; ++m_iter) {
158 if (lineprefix != NULL) {
162 os <<
" row "<<(*m_iter).first<<
": ";
164 typename SET_TYPE::const_iterator
165 s_iter = (*m_iter).second->begin(),
166 s_end = (*m_iter).second->end();
168 for(; s_iter != s_end; ++s_iter) {
169 os << *s_iter <<
" ";
176 template<
typename MAP_TYPE,
typename SET_TYPE>
178 std::vector<int>& intdata)
180 MAP_TYPE& map_obj = table.
getMap();
181 int numRows = map_obj.size();
183 typename MAP_TYPE::iterator
184 m_iter = map_obj.begin(),
185 m_end = map_obj.end();
189 for(; m_iter != m_end; ++m_iter) {
190 typename MAP_TYPE::value_type m_pair = *m_iter;
192 int rowLen = m_pair.second->size();
196 intdata.resize(1+2*numRows+nnz);
197 intdata[0] = numRows;
198 int* rowNumbers = &intdata[1];
199 int* rowLengths = rowNumbers+numRows;
200 int* packedCols = rowLengths+numRows;
202 m_iter = map_obj.begin();
204 for(
unsigned i=0; m_iter != m_end; ++m_iter, ++i) {
205 typename MAP_TYPE::value_type m_pair = *m_iter;
206 rowNumbers[i] = m_pair.first;
207 rowLengths[i] = m_pair.second->size();
209 int* colInds = &packedCols[offset];
211 offset += rowLengths[i];
217 template<
typename MAP_TYPE,
typename SET_TYPE>
224 for(
unsigned i=0; i<tables.size(); ++i) {
225 MAP_TYPE& map_obj = tables[i]->getMap();
226 numRows += map_obj.size();
228 typename MAP_TYPE::iterator
229 m_iter = map_obj.begin(),
230 m_end = map_obj.end();
231 for(; m_iter != m_end; ++m_iter) {
232 typename MAP_TYPE::value_type m_pair = *m_iter;
233 nnz += m_pair.second->size();
241 unsigned offset1 = 0;
242 unsigned rowOffset = 0;
243 for(
unsigned i=0; i<tables.size(); ++i) {
244 MAP_TYPE& map_obj = tables[i]->getMap();
246 typename MAP_TYPE::iterator
247 m_iter = map_obj.begin(),
248 m_end = map_obj.end();
249 for(; m_iter != m_end; ++m_iter) {
250 typename MAP_TYPE::value_type m_pair = *m_iter;
252 int rowLen = m_pair.second->size();
266 template<
typename MAP_TYPE,
typename SET_TYPE>
270 MAP_TYPE& map_obj = table.
getMap();
271 int numRows = map_obj.size();
276 int* rowNumPtr = numRows>0 ? &(srg.
rowNumbers[0]): NULL;
279 typename MAP_TYPE::iterator
280 m_iter = map_obj.begin(),
281 m_end = map_obj.end();
286 for(; m_iter != m_end; ++m_iter) {
287 typename MAP_TYPE::value_type m_pair = *m_iter;
289 rowNumPtr[offset] = m_pair.first;
290 rowOffsPtr[offset++] = nnz;
291 int rowLen = m_pair.second->size();
294 rowOffsPtr[offset] = nnz;
299 m_iter = map_obj.begin();
301 for(; m_iter != m_end; ++m_iter, ++i) {
302 typename MAP_TYPE::value_type m_pair = *m_iter;
304 int rowLen = rowOffsPtr[i+1]-rowOffsPtr[i];
305 int* colInds = &(colPtr[offset]);
313 template<
typename MAP_TYPE,
typename SET_TYPE>
319 copyToSparseRowGraph<MAP_TYPE, SET_TYPE>(table, *srg);
325 template<
typename MAP_TYPE,
typename SET_TYPE>
329 MAP_TYPE& map_obj = table.
getMap();
330 typename MAP_TYPE::iterator
331 m_iter = map_obj.begin(),
332 m_end = map_obj.end();
334 for(; m_iter != m_end; ++m_iter) {
335 nnz += (*m_iter).second->size();
343 #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 copyMapOfSetsToVectorOfVectors(const std::map< T, std::set< U > > &mapset, std::vector< T > &keys, std::vector< std::vector< U > > &values)
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 packRaggedTable(snl_fei::RaggedTable< MAP_TYPE, SET_TYPE > &table, std::vector< int > &intdata)
void copyToSparseRowGraph(snl_fei::RaggedTable< MAP_TYPE, SET_TYPE > &table, fei::SparseRowGraph &srg)
void destroyValues(MAP_TYPE &map_obj)