Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_SparseArray.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef STOKHOS_SPARSEARRAY_HPP
43 #define STOKHOS_SPARSEARRAY_HPP
44 
45 // disable clang warnings
46 #if defined (__clang__) && !defined (__INTEL_COMPILER)
47 #pragma clang system_header
48 #endif
49 
50 #include <algorithm>
51 #include "Teuchos_Array.hpp"
52 
53 namespace Stokhos {
54 
55  template <typename index_iterator, typename value_iterator>
57 
58  template <typename index_iterator, typename value_iterator>
60 
62  template <typename ordinal_type, typename val_type>
63  struct SparseArray {
64 
67 
70 
72  ordinal_type size() const { return indices.size(); }
73 
75  void resize(ordinal_type sz) {
76  indices.resize(sz);
77  values.resize(sz);
78  }
79 
84 
86  const_iterator begin() const {
87  return const_iterator(indices.begin(), values.begin());
88  }
89 
91  const_iterator end() const {
92  return const_iterator(indices.end(), values.end());
93  }
94 
97  return const_reverse_iterator(end());
98  }
99 
102  return const_reverse_iterator(begin());
103  }
104 
106 
110  const_iterator it = std::lower_bound(begin(), end(), i);
111  bool found = it != end() && *it == i;
112  if (found)
113  return it;
114  else
115  return end();
116  }
117 
118  };
119 
121 
130  template <typename index_iterator_type, typename value_iterator_type>
131  class SparseArrayIterator :
132  public std::iterator<
133  std::bidirectional_iterator_tag,
134  typename std::iterator_traits<index_iterator_type>::value_type,
135  typename std::iterator_traits<index_iterator_type>::difference_type,
136  typename std::iterator_traits<index_iterator_type>::pointer,
137  typename std::iterator_traits<index_iterator_type>::reference > {
138  public:
139 
140  typedef std::iterator<
141  std::bidirectional_iterator_tag,
143  typename std::iterator_traits<index_iterator_type>::difference_type,
144  typename std::iterator_traits<index_iterator_type>::pointer,
145  typename std::iterator_traits<index_iterator_type>::reference > base_type;
146  typedef typename base_type::iterator_category iterator_category;
148  typedef typename base_type::difference_type difference_type;
149  typedef typename base_type::reference reference;
150  typedef typename base_type::pointer pointer;
151  typedef typename std::iterator_traits<value_iterator_type>::reference value_reference;
152 
155 
157  SparseArrayIterator(index_iterator_type index_it,
158  value_iterator_type value_it) :
159  index_iterator(index_it), value_iterator(value_it) {}
160 
162  bool operator==(const SparseArrayIterator& it) const {
163  return index_iterator == it.index_iterator &&
164  value_iterator == it.value_iterator;
165  }
166 
168  bool operator!=(const SparseArrayIterator& it) const {
169  return index_iterator != it.index_iterator ||
170  value_iterator != it.value_iterator;
171  }
172 
174  reference
175  operator*() const {
176  return *index_iterator;
177  }
178 
180  pointer
181  operator->() const {
182  return index_iterator.operator->();
183  }
184 
187  index_iterator++;
188  value_iterator++;
189  return *this;
190  }
191 
194  SparseArrayIterator tmp(*this);
195  ++(*this);
196  return tmp;
197  }
198 
201  index_iterator--;
202  value_iterator--;
203  return *this;
204  }
205 
208  SparseArrayIterator tmp(*this);
209  --(*this);
210  return tmp;
211  }
212 
214  bool operator<(const SparseArrayIterator& b) const {
215  return index_iterator < b.index_iterator &&
216  value_iterator < b.value_iterator;
217  }
218 
221  return *value_iterator;
222  }
223 
224  protected:
225 
227  index_iterator_type index_iterator;
228 
230  value_iterator_type value_iterator;
231 
232  };
233 
235  template <typename index_iterator_type, typename value_iterator_type>
237  public std::reverse_iterator< SparseArrayIterator<index_iterator_type,
238  value_iterator_type> > {
239  public:
240 
242  typedef std::reverse_iterator<iterator_type> base_type;
243  typedef typename base_type::iterator_category iterator_category;
245  typedef typename base_type::difference_type difference_type;
246  typedef typename base_type::reference reference;
247  typedef typename base_type::pointer pointer;
248  typedef typename std::iterator_traits<value_iterator_type>::reference value_reference;
249 
252 
255 
258  iterator_type tmp = this->base();
259  --tmp;
260  return tmp.value();
261  }
262 
263  };
264 
265 } // namespace Stokhos
266 
267 #endif // STOKHOS_SPARSEARRAY_HPP
std::reverse_iterator< iterator_type > base_type
bool operator==(const SparseArrayIterator &it) const
== operator
SparseArrayIterator operator--(int)
Postfix –.
std::iterator_traits< value_iterator_type >::reference value_reference
std::iterator_traits< value_iterator_type >::reference value_reference
const_reverse_iterator rbegin() const
Reverse iterator pointing to end of array.
SparseArrayReverseIterator(iterator_type it)
Constructor.
Container for a &quot;sparse&quot; array.
base_type::iterator_category iterator_category
SparseArrayReverseIterator()
Default constructor.
Bi-directional iterator for traversing a sparse array.
Bi-directional reverse iterator for traversing a sparse array.
Teuchos::Array< val_type >::const_iterator value_const_iterator
bool operator<(const SparseArrayIterator &b) const
Return true of *this &lt; b.
SparseArrayIterator(index_iterator_type index_it, value_iterator_type value_it)
Constructor.
value_iterator_type value_iterator
Value iterator.
SparseArrayIterator & operator++()
Prefix ++.
value_reference value() const
Return value associated with iterator.
SparseArrayReverseIterator< index_const_iterator, value_const_iterator > const_reverse_iterator
const_iterator find(ordinal_type i) const
Return iterator pointing to given index i.
SparseArrayIterator & operator--()
Prefix –.
void resize(size_type new_size, const value_type &x=value_type())
iterator end()
Teuchos::Array< ordinal_type > indices
Indices of nonzeros.
std::vector< T >::const_iterator const_iterator
Teuchos::Array< ordinal_type >::const_iterator index_const_iterator
base_type::difference_type difference_type
bool operator!=(const SparseArrayIterator &it) const
!= operator
const_reverse_iterator rend() const
Reverse iterator pointing to begining of array.
SparseArrayIterator< index_iterator_type, value_iterator_type > iterator_type
base_type::difference_type difference_type
SparseArrayIterator operator++(int)
Postfix ++.
const_iterator begin() const
Iterator pointing to beginning of array.
size_type size() const
index_iterator_type index_iterator
Index iterator.
Teuchos::Array< val_type > values
Nonzero values.
SparseArrayIterator< index_const_iterator, value_const_iterator > const_iterator
base_type::iterator_category iterator_category
const_iterator end() const
Iterator pointing to end of array.
pointer operator->() const
-&gt; operator
SparseArrayIterator()
Default constructor.
std::iterator< std::bidirectional_iterator_tag, typename std::iterator_traits< index_iterator_type >::value_type, typename std::iterator_traits< index_iterator_type >::difference_type, typename std::iterator_traits< index_iterator_type >::pointer, typename std::iterator_traits< index_iterator_type >::reference > base_type
ordinal_type size() const
Return size of array.
value_reference value() const
Return value associated with iterator.
iterator begin()
void resize(ordinal_type sz)
Resize array.