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 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef STOKHOS_SPARSEARRAY_HPP
11 #define STOKHOS_SPARSEARRAY_HPP
12 
13 // disable clang warnings
14 #if defined (__clang__) && !defined (__INTEL_COMPILER)
15 #pragma clang system_header
16 #endif
17 
18 #include <algorithm>
19 #include "Teuchos_Array.hpp"
20 
21 namespace Stokhos {
22 
23  template <typename index_iterator, typename value_iterator>
25 
26  template <typename index_iterator, typename value_iterator>
28 
30  template <typename ordinal_type, typename val_type>
31  struct SparseArray {
32 
35 
38 
40  ordinal_type size() const { return indices.size(); }
41 
43  void resize(ordinal_type sz) {
44  indices.resize(sz);
45  values.resize(sz);
46  }
47 
52 
54  const_iterator begin() const {
55  return const_iterator(indices.begin(), values.begin());
56  }
57 
59  const_iterator end() const {
60  return const_iterator(indices.end(), values.end());
61  }
62 
65  return const_reverse_iterator(end());
66  }
67 
70  return const_reverse_iterator(begin());
71  }
72 
74 
78  const_iterator it = std::lower_bound(begin(), end(), i);
79  bool found = it != end() && *it == i;
80  if (found)
81  return it;
82  else
83  return end();
84  }
85 
86  };
87 
89 
98  template <typename index_iterator_type, typename value_iterator_type>
99  class SparseArrayIterator :
100  public std::iterator<
101  std::bidirectional_iterator_tag,
102  typename std::iterator_traits<index_iterator_type>::value_type,
103  typename std::iterator_traits<index_iterator_type>::difference_type,
104  typename std::iterator_traits<index_iterator_type>::pointer,
105  typename std::iterator_traits<index_iterator_type>::reference > {
106  public:
107 
108  typedef std::iterator<
109  std::bidirectional_iterator_tag,
111  typename std::iterator_traits<index_iterator_type>::difference_type,
112  typename std::iterator_traits<index_iterator_type>::pointer,
113  typename std::iterator_traits<index_iterator_type>::reference > base_type;
114  typedef typename base_type::iterator_category iterator_category;
116  typedef typename base_type::difference_type difference_type;
117  typedef typename base_type::reference reference;
118  typedef typename base_type::pointer pointer;
119  typedef typename std::iterator_traits<value_iterator_type>::reference value_reference;
120 
123 
125  SparseArrayIterator(index_iterator_type index_it,
126  value_iterator_type value_it) :
127  index_iterator(index_it), value_iterator(value_it) {}
128 
130  bool operator==(const SparseArrayIterator& it) const {
131  return index_iterator == it.index_iterator &&
132  value_iterator == it.value_iterator;
133  }
134 
136  bool operator!=(const SparseArrayIterator& it) const {
137  return index_iterator != it.index_iterator ||
138  value_iterator != it.value_iterator;
139  }
140 
142  reference
143  operator*() const {
144  return *index_iterator;
145  }
146 
148  pointer
149  operator->() const {
150  return index_iterator.operator->();
151  }
152 
155  index_iterator++;
156  value_iterator++;
157  return *this;
158  }
159 
162  SparseArrayIterator tmp(*this);
163  ++(*this);
164  return tmp;
165  }
166 
169  index_iterator--;
170  value_iterator--;
171  return *this;
172  }
173 
176  SparseArrayIterator tmp(*this);
177  --(*this);
178  return tmp;
179  }
180 
182  bool operator<(const SparseArrayIterator& b) const {
183  return index_iterator < b.index_iterator &&
184  value_iterator < b.value_iterator;
185  }
186 
189  return *value_iterator;
190  }
191 
192  protected:
193 
195  index_iterator_type index_iterator;
196 
198  value_iterator_type value_iterator;
199 
200  };
201 
203  template <typename index_iterator_type, typename value_iterator_type>
205  public std::reverse_iterator< SparseArrayIterator<index_iterator_type,
206  value_iterator_type> > {
207  public:
208 
210  typedef std::reverse_iterator<iterator_type> base_type;
211  typedef typename base_type::iterator_category iterator_category;
213  typedef typename base_type::difference_type difference_type;
214  typedef typename base_type::reference reference;
215  typedef typename base_type::pointer pointer;
216  typedef typename std::iterator_traits<value_iterator_type>::reference value_reference;
217 
220 
223 
226  iterator_type tmp = this->base();
227  --tmp;
228  return tmp.value();
229  }
230 
231  };
232 
233 } // namespace Stokhos
234 
235 #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.