FEI  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
snl_fei_RaggedTable.hpp
1 #ifndef _snl_fei_RaggedTable_hpp_
2 #define _snl_fei_RaggedTable_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_macros.hpp>
13 
14 #include <snl_fei_SetTraits_specialize.hpp>
15 #include <snl_fei_MapTraits_specialize.hpp>
16 
17 #include <fei_IndexTable.hpp>
18 #include <fei_Pool_alloc.hpp>
19 
20 namespace snl_fei {
21 
28 template<typename MAP_TYPE, typename SET_TYPE>
29 class RaggedTable : public fei::IndexTable {
30  public:
32  RaggedTable(int firstKey,
33  int lastKey);
34 
36  RaggedTable(const RaggedTable<MAP_TYPE,SET_TYPE>& src);
37 
38  virtual ~RaggedTable();
39 
41  typedef MAP_TYPE map_type;
42 
44  typedef SET_TYPE row_type;
45 
47  void addDiagonals(int numIndices,
48  const int* indices);
49 
51  void addIndices(int row,
52  int numIndices,
53  const int* indices);
54 
56  void addIndices(int numRows,
57  const int* rows,
58  int numIndices,
59  const int* indices);
60 
62  MAP_TYPE& getMap();
63 
65  const MAP_TYPE& getMap() const;
66 
68  SET_TYPE* getRow(int row);
69 
72  typedef typename MAP_TYPE::iterator iterator;
73 
75  iterator begin();
76 
78  iterator end();
79 
81  bool equal(const RaggedTable<MAP_TYPE,SET_TYPE>& rhs, bool quiet=true) const;
82 
83  private:
84  MAP_TYPE map_;
85  fei_Pool_alloc<SET_TYPE> poolAllocatorSet_;
86  SET_TYPE dummy;
87 }; //class RaggedTable
88 
89 template<typename MAP_TYPE, typename SET_TYPE>
91  int lastKey)
92  : map_(),
93  poolAllocatorSet_(),
94  dummy()
95 {
96 }
97 
98 template<typename MAP_TYPE, typename SET_TYPE>
99 inline RaggedTable<MAP_TYPE,SET_TYPE>::RaggedTable(const RaggedTable<MAP_TYPE,SET_TYPE>& src)
100  : map_(src.map_),
101  poolAllocatorSet_()
102 {
103 }
104 
105 template<typename MAP_TYPE, typename SET_TYPE>
106 RaggedTable<MAP_TYPE,SET_TYPE>::~RaggedTable()
107 {
108  iterator it = begin();
109  iterator it_end = end();
110  for(; it!=it_end; ++it) {
111  poolAllocatorSet_.destroy( it->second );
112  poolAllocatorSet_.deallocate( it->second, 1 );
113  }
114 }
115 
116 template<typename MAP_TYPE, typename SET_TYPE>
117 inline void RaggedTable<MAP_TYPE,SET_TYPE>::addIndices(int row,
118  int numIndices,
119  const int* indices)
120 {
121  iterator m_end = map_.end();
122  iterator m_iter = MapTraits<MAP_TYPE>::lower_bound(map_, row);
123 
124  SET_TYPE* mapped_indices = NULL;
125 
126  bool found_row = false;
127  if (m_iter != m_end) {
128  if ((*m_iter).first == row) {
129  mapped_indices = (*m_iter).second;
130  found_row = true;
131  }
132  }
133 
134  if (!found_row) {
135  mapped_indices = poolAllocatorSet_.allocate(1);
136  poolAllocatorSet_.construct(mapped_indices, dummy);
137  typename MAP_TYPE::value_type val(row, mapped_indices);
138  MapTraits<MAP_TYPE>::insert(map_, m_iter, val);
139  }
140 
141  for(int i=0; i<numIndices; ++i) {
142  SetTraits<SET_TYPE>::insert(mapped_indices, indices[i]);
143  }
144 }
145 
146 template<typename MAP_TYPE, typename SET_TYPE>
147 inline void RaggedTable<MAP_TYPE,SET_TYPE>::addIndices(int numRows,
148  const int* rows,
149  int numIndices,
150  const int* indices)
151 {
152  iterator m_end = map_.end();
153  iterator m_iter;
154  SET_TYPE* mapped_indices = NULL;
155 
156  for(int i=0; i<numRows; ++i) {
157  int row = rows[i];
158  m_iter = MapTraits<MAP_TYPE>::lower_bound(map_, row);
159 
160  bool found_row = false;
161  if (m_iter != m_end) {
162  const typename MAP_TYPE::value_type& m_pair = *m_iter;
163  if (m_pair.first == row) {
164  mapped_indices = m_pair.second;
165  found_row = true;
166  }
167  }
168 
169  if (!found_row) {
170  mapped_indices = poolAllocatorSet_.allocate(1);
171  poolAllocatorSet_.construct(mapped_indices, dummy);
172  typename MAP_TYPE::value_type val(row, mapped_indices);
173  MapTraits<MAP_TYPE>::insert(map_, m_iter, val);
174  }
175 
176  for(int j=0; j<numIndices; ++j) {
177  SetTraits<SET_TYPE>::insert(mapped_indices, indices[j]);
178  }
179  }
180 }
181 
182 template<typename MAP_TYPE, typename SET_TYPE>
183 inline MAP_TYPE& RaggedTable<MAP_TYPE,SET_TYPE>::getMap()
184 {
185  return(map_);
186 }
187 
188 template<typename MAP_TYPE, typename SET_TYPE>
189 inline const MAP_TYPE& RaggedTable<MAP_TYPE,SET_TYPE>::getMap() const
190 {
191  return(map_);
192 }
193 
194 template<typename MAP_TYPE, typename SET_TYPE>
195 inline typename RaggedTable<MAP_TYPE,SET_TYPE>::row_type*
196 RaggedTable<MAP_TYPE,SET_TYPE>::getRow(int row)
197 {
198  iterator m_end = map_.end();
199  iterator m_iter = map_.find(row);
200  return( m_end == m_iter ? NULL : (*m_iter).second );
201 }
202 
203 template<typename MAP_TYPE, typename SET_TYPE>
204 inline typename RaggedTable<MAP_TYPE,SET_TYPE>::iterator
205 RaggedTable<MAP_TYPE,SET_TYPE>::begin()
206 {
207  return(map_.begin());
208 }
209 
210 template<typename MAP_TYPE, typename SET_TYPE>
211 inline typename RaggedTable<MAP_TYPE,SET_TYPE>::iterator
212 RaggedTable<MAP_TYPE,SET_TYPE>::end()
213 {
214  return(map_.end());
215 }
216 
217 template<typename MAP_TYPE, typename SET_TYPE>
218 inline void RaggedTable<MAP_TYPE,SET_TYPE>::addDiagonals(int numIndices,
219  const int* indices)
220 {
221  for(int i=0; i<numIndices; ++i) {
222  int ind = indices[i];
223  addIndices(ind, 1, &ind);
224  }
225 }
226 
227 template<typename MAP_TYPE, typename SET_TYPE>
228 bool RaggedTable<MAP_TYPE,SET_TYPE>::equal(const RaggedTable<MAP_TYPE,SET_TYPE>& rhs, bool quiet) const
229 {
230  if (map_.size() != rhs.getMap().size()) {
231  if (!quiet) {
232  FEI_COUT << "RaggedTable::equal sizes don't match." << FEI_ENDL;
233  }
234  return(false);
235  }
236 
237  typename map_type::const_iterator
238  m_iter = map_.begin(),
239  m_end = map_.end();
240 
241  typename map_type::const_iterator
242  rhs_iter = rhs.getMap().begin();
243 
244  for(; m_iter != m_end; ++m_iter, ++rhs_iter) {
245  if (rhs_iter->first != m_iter->first) {
246  if (!quiet) {
247  FEI_COUT << "RaggedTable::equal keys don't match." << FEI_ENDL;
248  }
249  return(false);
250  }
251 
252  if (*(rhs_iter->second) != *(m_iter->second)) {
253  if (!quiet) {
254  FEI_COUT << "RaggedTable::equal row-values don't match." << FEI_ENDL;
255  }
256  return(false);
257  }
258  }
259 
260  return(true);
261 }
262 
263 }//namespace snl_fei
264 
265 #endif
266 
void addDiagonals(int numIndices, const int *indices)
RaggedTable(int firstKey, int lastKey)
SET_TYPE * getRow(int row)
MAP_TYPE::iterator iterator
void addIndices(int row, int numIndices, const int *indices)
bool equal(const RaggedTable< MAP_TYPE, SET_TYPE > &rhs, bool quiet=true) const