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  for(int i=0; i<len; ++i, ++s_iter) {
40  list[i] = *s_iter;
41  }
42  }
43 
46  template<typename T>
47  void copySetToVector(const std::set<T>& set_obj, std::vector<T>& vec)
48  {
49  vec.clear();
50  vec.reserve(set_obj.size());
51 
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);
56  }
57  }
58 
62  template<typename MAP_TYPE>
63  void copyKeysToArray(const MAP_TYPE& map_obj,
64  unsigned lenList,
65  int* list)
66  {
67  unsigned i = 0;
68  typename MAP_TYPE::const_iterator
69  iter = map_obj.begin(), iter_end = map_obj.end();
70 
71  for(; iter != iter_end; ++iter) {
72  if (i == lenList) break;
73  list[i++] = iter->first;
74  }
75  }
76 
80  template<typename MAP_TYPE>
81  void copyKeysToVector(const MAP_TYPE& map_obj,
82  std::vector<int>& keyvector)
83  {
84  keyvector.resize(map_obj.size());
85  if (map_obj.empty()) return;
86  copyKeysToArray<MAP_TYPE>(map_obj, map_obj.size(), &keyvector[0]);
87  }
88 
89  template<typename T, typename U>
90  void copyMapOfSetsToVectorOfVectors(const std::map<T,std::set<U> >& mapset,
91  std::vector<T>& keys,
92  std::vector<std::vector<U> >& values)
93  {
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;
106  }
107  }
108  }
109 
114  template<typename MAP_TYPE>
115  void copyToArrays(MAP_TYPE& map_obj,
116  int lenList,
117  int* keylist,
118  int* vallist)
119  {
120  int i = 0;
121  typename MAP_TYPE::iterator
122  iter = map_obj.begin(),
123  iter_end = map_obj.end();
124 
125  for(; iter != iter_end; ++iter) {
126  if (i == lenList) break;
127  keylist[i] = (*iter).first;
128  vallist[i++] = (*iter).second;
129  }
130  }
131 
133  template<typename MAP_TYPE>
134  void destroyValues(MAP_TYPE& map_obj)
135  {
136  typename MAP_TYPE::iterator
137  m_iter = map_obj.begin(),
138  m_end = map_obj.end();
139 
140  for(; m_iter != m_end; ++m_iter) {
141  delete (*m_iter).second;
142  }
143  }
144 
146  template<typename MAP_TYPE, typename SET_TYPE>
148  FEI_OSTREAM& os,
149  const char* lineprefix=NULL)
150  {
151  MAP_TYPE& map_obj = table.getMap();
152  typename MAP_TYPE::iterator
153  m_iter = map_obj.begin(),
154  m_end = map_obj.end();
155 
156  for(; m_iter != m_end; ++m_iter) {
157  if (lineprefix != NULL) {
158  os << lineprefix;
159  }
160 
161  os << " row "<<(*m_iter).first<<": ";
162 
163  typename SET_TYPE::const_iterator
164  s_iter = (*m_iter).second->begin(),
165  s_end = (*m_iter).second->end();
166 
167  for(; s_iter != s_end; ++s_iter) {
168  os << *s_iter << " ";
169  }
170 
171  os << FEI_ENDL;
172  }
173  }
174 
175  template<typename MAP_TYPE, typename SET_TYPE>
177  std::vector<int>& intdata)
178  {
179  MAP_TYPE& map_obj = table.getMap();
180  int numRows = map_obj.size();
181 
182  typename MAP_TYPE::iterator
183  m_iter = map_obj.begin(),
184  m_end = map_obj.end();
185 
186  int nnz = 0;
187 
188  for(; m_iter != m_end; ++m_iter) {
189  typename MAP_TYPE::value_type m_pair = *m_iter;
190 
191  int rowLen = m_pair.second->size();
192  nnz += rowLen;
193  }
194 
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;
200 
201  m_iter = map_obj.begin();
202  unsigned offset = 0;
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();
207 
208  int* colInds = &packedCols[offset];
209  copySetToArray(*(m_pair.second), rowLengths[i], colInds);
210  offset += rowLengths[i];
211  }
212  }
213 
216  template<typename MAP_TYPE, typename SET_TYPE>
218  {
219  int numRows = 0;
220  int nnz = 0;
222 
223  for(unsigned i=0; i<tables.size(); ++i) {
224  MAP_TYPE& map_obj = tables[i]->getMap();
225  numRows += map_obj.size();
226 
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();
233  }
234  }
235 
236  srg->rowNumbers.resize(numRows);
237  srg->rowOffsets.resize(numRows+1);
238  srg->packedColumnIndices.resize(nnz);
239 
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();
244 
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];
254  copySetToArray(*(m_pair.second), rowLen, cols);
255  rowOffset += rowLen;
256  }
257  }
258 
259  srg->rowOffsets[offset1] = rowOffset;
260 
261  return(srg);
262  }
263 
265  template<typename MAP_TYPE, typename SET_TYPE>
267  fei::SparseRowGraph& srg)
268  {
269  MAP_TYPE& map_obj = table.getMap();
270  int numRows = map_obj.size();
271 
272  srg.rowNumbers.resize(numRows);
273  srg.rowOffsets.resize(numRows+1);
274 
275  int* rowNumPtr = numRows>0 ? &(srg.rowNumbers[0]): NULL;
276  int* rowOffsPtr = &(srg.rowOffsets[0]);
277 
278  typename MAP_TYPE::iterator
279  m_iter = map_obj.begin(),
280  m_end = map_obj.end();
281 
282  int offset = 0;
283  int nnz = 0;
284 
285  for(; m_iter != m_end; ++m_iter) {
286  typename MAP_TYPE::value_type m_pair = *m_iter;
287 
288  rowNumPtr[offset] = m_pair.first;
289  rowOffsPtr[offset++] = nnz;
290  int rowLen = m_pair.second->size();
291  nnz += rowLen;
292  }
293  rowOffsPtr[offset] = nnz;
294 
295  srg.packedColumnIndices.resize(nnz);
296  int* colPtr = numRows>0 ? &(srg.packedColumnIndices[0]) : NULL;
297  offset = 0;
298  m_iter = map_obj.begin();
299  int i = 0;
300  for(; m_iter != m_end; ++m_iter, ++i) {
301  typename MAP_TYPE::value_type m_pair = *m_iter;
302 
303  int rowLen = rowOffsPtr[i+1]-rowOffsPtr[i];
304  int* colInds = &(colPtr[offset]);
305  copySetToArray(*(m_pair.second), rowLen, colInds);
306  offset += rowLen;
307  }
308  }
309 
312  template<typename MAP_TYPE, typename SET_TYPE>
315  {
317 
318  copyToSparseRowGraph<MAP_TYPE, SET_TYPE>(table, *srg);
319 
320  return( srg );
321  }
322 
324  template<typename MAP_TYPE, typename SET_TYPE>
326  {
327  int nnz = 0;
328  MAP_TYPE& map_obj = table.getMap();
329  typename MAP_TYPE::iterator
330  m_iter = map_obj.begin(),
331  m_end = map_obj.end();
332 
333  for(; m_iter != m_end; ++m_iter) {
334  nnz += (*m_iter).second->size();
335  }
336 
337  return(nnz);
338  }
339 
340 } //namespace fei
341 
342 #endif // _fei_TemplateUtils_hpp_
343 
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)