FEI Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fei_TemplateUtils.hpp
Go to the documentation of this file.
1 #ifndef _fei_TemplateUtils_hpp_
2 #define _fei_TemplateUtils_hpp_
3 
4 /*--------------------------------------------------------------------*/
5 /* Copyright 2005 Sandia Corporation. */
6 /* Under the terms of Contract DE-AC04-94AL85000, there is a */
7 /* non-exclusive license for use of this work by or on behalf */
8 /* of the U.S. Government. Export of this program may require */
9 /* a license from the United States Government. */
10 /*--------------------------------------------------------------------*/
11 
12 #include <fei_iosfwd.hpp>
13 #include <fei_SparseRowGraph.hpp>
14 #include <snl_fei_RaggedTable.hpp>
15 #include <snl_fei_Utils.hpp>
16 
17 #include <fei_ErrMacros.hpp>
18 
19 #include <set>
20 #include <vector>
21 #include <map>
22 
23 namespace fei {
24 
25  //------------------------------------------------------------------------
29  template<typename SET_TYPE>
30  void copySetToArray(const SET_TYPE& set_obj,
31  int lenList,
32  int* list)
33  {
34  int setsize = set_obj.size();
35  int len = setsize > lenList ? lenList : setsize;
36 
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) {
41  list[i] = *s_iter;
42  }
43  }
44 
47  template<typename T>
48  void copySetToVector(const std::set<T>& set_obj, std::vector<T>& vec)
49  {
50  vec.clear();
51  vec.reserve(set_obj.size());
52 
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);
57  }
58  }
59 
63  template<typename MAP_TYPE>
64  void copyKeysToArray(const MAP_TYPE& map_obj,
65  unsigned lenList,
66  int* list)
67  {
68  unsigned i = 0;
69  typename MAP_TYPE::const_iterator
70  iter = map_obj.begin(), iter_end = map_obj.end();
71 
72  for(; iter != iter_end; ++iter) {
73  if (i == lenList) break;
74  list[i++] = iter->first;
75  }
76  }
77 
81  template<typename MAP_TYPE>
82  void copyKeysToVector(const MAP_TYPE& map_obj,
83  std::vector<int>& keyvector)
84  {
85  keyvector.resize(map_obj.size());
86  if (map_obj.empty()) return;
87  copyKeysToArray<MAP_TYPE>(map_obj, map_obj.size(), &keyvector[0]);
88  }
89 
90  template<typename T, typename U>
91  void copyMapOfSetsToVectorOfVectors(const std::map<T,std::set<U> >& mapset,
92  std::vector<T>& keys,
93  std::vector<std::vector<U> >& values)
94  {
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;
107  }
108  }
109  }
110 
115  template<typename MAP_TYPE>
116  void copyToArrays(MAP_TYPE& map_obj,
117  int lenList,
118  int* keylist,
119  int* vallist)
120  {
121  int i = 0;
122  typename MAP_TYPE::iterator
123  iter = map_obj.begin(),
124  iter_end = map_obj.end();
125 
126  for(; iter != iter_end; ++iter) {
127  if (i == lenList) break;
128  keylist[i] = (*iter).first;
129  vallist[i++] = (*iter).second;
130  }
131  }
132 
134  template<typename MAP_TYPE>
135  void destroyValues(MAP_TYPE& map_obj)
136  {
137  typename MAP_TYPE::iterator
138  m_iter = map_obj.begin(),
139  m_end = map_obj.end();
140 
141  for(; m_iter != m_end; ++m_iter) {
142  delete (*m_iter).second;
143  }
144  }
145 
147  template<typename MAP_TYPE, typename SET_TYPE>
149  FEI_OSTREAM& os,
150  const char* lineprefix=NULL)
151  {
152  MAP_TYPE& map_obj = table.getMap();
153  typename MAP_TYPE::iterator
154  m_iter = map_obj.begin(),
155  m_end = map_obj.end();
156 
157  for(; m_iter != m_end; ++m_iter) {
158  if (lineprefix != NULL) {
159  os << lineprefix;
160  }
161 
162  os << " row "<<(*m_iter).first<<": ";
163 
164  typename SET_TYPE::const_iterator
165  s_iter = (*m_iter).second->begin(),
166  s_end = (*m_iter).second->end();
167 
168  for(; s_iter != s_end; ++s_iter) {
169  os << *s_iter << " ";
170  }
171 
172  os << FEI_ENDL;
173  }
174  }
175 
176  template<typename MAP_TYPE, typename SET_TYPE>
178  std::vector<int>& intdata)
179  {
180  MAP_TYPE& map_obj = table.getMap();
181  int numRows = map_obj.size();
182 
183  typename MAP_TYPE::iterator
184  m_iter = map_obj.begin(),
185  m_end = map_obj.end();
186 
187  int nnz = 0;
188 
189  for(; m_iter != m_end; ++m_iter) {
190  typename MAP_TYPE::value_type m_pair = *m_iter;
191 
192  int rowLen = m_pair.second->size();
193  nnz += rowLen;
194  }
195 
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;
201 
202  m_iter = map_obj.begin();
203  unsigned offset = 0;
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();
208 
209  int* colInds = &packedCols[offset];
210  copySetToArray(*(m_pair.second), rowLengths[i], colInds);
211  offset += rowLengths[i];
212  }
213  }
214 
217  template<typename MAP_TYPE, typename SET_TYPE>
219  {
220  int numRows = 0;
221  int nnz = 0;
223 
224  for(unsigned i=0; i<tables.size(); ++i) {
225  MAP_TYPE& map_obj = tables[i]->getMap();
226  numRows += map_obj.size();
227 
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();
234  }
235  }
236 
237  srg->rowNumbers.resize(numRows);
238  srg->rowOffsets.resize(numRows+1);
239  srg->packedColumnIndices.resize(nnz);
240 
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();
245 
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;
251  srg->rowNumbers[offset1] = m_pair.first;
252  int rowLen = m_pair.second->size();
253  srg->rowOffsets[offset1++] = rowOffset;
254  int* cols = &srg->packedColumnIndices[rowOffset];
255  copySetToArray(*(m_pair.second), rowLen, cols);
256  rowOffset += rowLen;
257  }
258  }
259 
260  srg->rowOffsets[offset1] = rowOffset;
261 
262  return(srg);
263  }
264 
266  template<typename MAP_TYPE, typename SET_TYPE>
268  fei::SparseRowGraph& srg)
269  {
270  MAP_TYPE& map_obj = table.getMap();
271  int numRows = map_obj.size();
272 
273  srg.rowNumbers.resize(numRows);
274  srg.rowOffsets.resize(numRows+1);
275 
276  int* rowNumPtr = numRows>0 ? &(srg.rowNumbers[0]): NULL;
277  int* rowOffsPtr = &(srg.rowOffsets[0]);
278 
279  typename MAP_TYPE::iterator
280  m_iter = map_obj.begin(),
281  m_end = map_obj.end();
282 
283  int offset = 0;
284  int nnz = 0;
285 
286  for(; m_iter != m_end; ++m_iter) {
287  typename MAP_TYPE::value_type m_pair = *m_iter;
288 
289  rowNumPtr[offset] = m_pair.first;
290  rowOffsPtr[offset++] = nnz;
291  int rowLen = m_pair.second->size();
292  nnz += rowLen;
293  }
294  rowOffsPtr[offset] = nnz;
295 
296  srg.packedColumnIndices.resize(nnz);
297  int* colPtr = numRows>0 ? &(srg.packedColumnIndices[0]) : NULL;
298  offset = 0;
299  m_iter = map_obj.begin();
300  int i = 0;
301  for(; m_iter != m_end; ++m_iter, ++i) {
302  typename MAP_TYPE::value_type m_pair = *m_iter;
303 
304  int rowLen = rowOffsPtr[i+1]-rowOffsPtr[i];
305  int* colInds = &(colPtr[offset]);
306  copySetToArray(*(m_pair.second), rowLen, colInds);
307  offset += rowLen;
308  }
309  }
310 
313  template<typename MAP_TYPE, typename SET_TYPE>
316  {
318 
319  copyToSparseRowGraph<MAP_TYPE, SET_TYPE>(table, *srg);
320 
321  return( srg );
322  }
323 
325  template<typename MAP_TYPE, typename SET_TYPE>
327  {
328  int nnz = 0;
329  MAP_TYPE& map_obj = table.getMap();
330  typename MAP_TYPE::iterator
331  m_iter = map_obj.begin(),
332  m_end = map_obj.end();
333 
334  for(; m_iter != m_end; ++m_iter) {
335  nnz += (*m_iter).second->size();
336  }
337 
338  return(nnz);
339  }
340 
341 } //namespace fei
342 
343 #endif // _fei_TemplateUtils_hpp_
344 
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
#define FEI_OSTREAM
Definition: fei_iosfwd.hpp:24
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)
#define FEI_ENDL
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)