Stokhos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
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
53namespace Stokhos {
54
55 template <typename index_iterator, typename value_iterator>
56 struct SparseArrayIterator;
57
58 template <typename index_iterator, typename value_iterator>
59 struct SparseArrayReverseIterator;
60
62 template <typename ordinal_type, typename val_type>
63 struct SparseArray {
64
66 Teuchos::Array<ordinal_type> indices;
67
69 Teuchos::Array<val_type> values;
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
80 typedef typename Teuchos::Array<ordinal_type>::const_iterator index_const_iterator;
81 typedef typename Teuchos::Array<val_type>::const_iterator value_const_iterator;
84
87 return const_iterator(indices.begin(), values.begin());
88 }
89
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
109 const_iterator find(ordinal_type i) const {
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>
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,
142 typename std::iterator_traits<index_iterator_type>::value_type,
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;
147 typedef typename base_type::value_type value_type;
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 &&
165 }
166
168 bool operator!=(const SparseArrayIterator& it) const {
169 return index_iterator != it.index_iterator ||
171 }
172
175 operator*() const {
176 return *index_iterator;
177 }
178
180 pointer
181 operator->() const {
182 return index_iterator.operator->();
183 }
184
189 return *this;
190 }
191
194 SparseArrayIterator tmp(*this);
195 ++(*this);
196 return tmp;
197 }
198
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 &&
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;
244 typedef typename base_type::value_type value_type;
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
Top-level namespace for Stokhos classes and functions.
Bi-directional iterator for traversing a sparse array.
index_iterator_type index_iterator
Index iterator.
value_reference value() const
Return value associated with iterator.
SparseArrayIterator()
Default constructor.
pointer operator->() const
-> operator
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
base_type::difference_type difference_type
SparseArrayIterator & operator++()
Prefix ++.
SparseArrayIterator(index_iterator_type index_it, value_iterator_type value_it)
Constructor.
bool operator<(const SparseArrayIterator &b) const
Return true of *this < b.
base_type::iterator_category iterator_category
SparseArrayIterator operator--(int)
Postfix –.
bool operator!=(const SparseArrayIterator &it) const
!= operator
bool operator==(const SparseArrayIterator &it) const
== operator
SparseArrayIterator & operator--()
Prefix –.
std::iterator_traits< value_iterator_type >::reference value_reference
SparseArrayIterator operator++(int)
Postfix ++.
value_iterator_type value_iterator
Value iterator.
Bi-directional reverse iterator for traversing a sparse array.
value_reference value() const
Return value associated with iterator.
SparseArrayReverseIterator()
Default constructor.
SparseArrayIterator< index_iterator_type, value_iterator_type > iterator_type
std::reverse_iterator< iterator_type > base_type
base_type::iterator_category iterator_category
SparseArrayReverseIterator(iterator_type it)
Constructor.
base_type::difference_type difference_type
std::iterator_traits< value_iterator_type >::reference value_reference
Container for a "sparse" array.
const_iterator begin() const
Iterator pointing to beginning of array.
Teuchos::Array< val_type > values
Nonzero values.
const_reverse_iterator rbegin() const
Reverse iterator pointing to end of array.
Teuchos::Array< ordinal_type >::const_iterator index_const_iterator
Teuchos::Array< val_type >::const_iterator value_const_iterator
Teuchos::Array< ordinal_type > indices
Indices of nonzeros.
ordinal_type size() const
Return size of array.
void resize(ordinal_type sz)
Resize array.
SparseArrayIterator< index_const_iterator, value_const_iterator > const_iterator
SparseArrayReverseIterator< index_const_iterator, value_const_iterator > const_reverse_iterator
const_iterator end() const
Iterator pointing to end of array.
const_iterator find(ordinal_type i) const
Return iterator pointing to given index i.
const_reverse_iterator rend() const
Reverse iterator pointing to begining of array.