MyraMath
PatternRange.h
Go to the documentation of this file.
1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
6 #ifndef MYRAMATH_SPARSE_PATTERNRANGE_H
7 #define MYRAMATH_SPARSE_PATTERNRANGE_H
8 
14 #include <myramath/utility/detail/LIBPUBLIC.h>
15 
16 #include <utility>
17 #include <iosfwd>
18 #include <vector>
19 
20 namespace myra {
21 
22 // Forward declarations.
23 class intCRange;
24 class PatternRange;
25 class PatternIterator;
26 template<class T> class Array1;
27 template<class T> class Array2;
28 class OutputStream;
29 
31 class LIBPUBLIC PatternRange
32  {
33  public:
34 
36  // Data pointers for underlying Pattern
38  const int* Ao; // Offsets for each j into Ai[] data.
39  const int* Ai; // Row indices (i) in compressed-sparse-column format.
40 
41  // Extents of this Range within underlying Pattern.
42  int I0,I1; // I-extents of Range
43  int J0,J1; // J-extents of Range
44  bool sliced; // If true, this PatternRange has been sliced.
46 
47  // Useful typedefs.
48  typedef std::pair<PatternRange,PatternRange> Pair;
49 
51  PatternRange();
52 
54  PatternRange(const int* in_Ao, const int* in_Ai, int I, int J);
55 
57  void write(OutputStream& out) const;
58 
59  // ------------------------------------ Size and pattern queries.
60 
62  std::pair<int,int> size() const;
63 
65  int n_nonzeros() const;
66 
68  int n_nonzeros(int j) const;
69 
71  bool test(int i, int j) const;
72 
74  std::vector<int> pattern(int j) const;
75 
76  // ------------------------------------ Iterating.
77  typedef PatternIterator Iterator;
78 
80  PatternIterator begin() const;
82  PatternIterator end() const;
84 
86  PatternIterator begin(int j) const;
88  PatternIterator end(int j) const;
90 
91  // ------------------------------------ General slicing.
92 
94  PatternRange window(int i0, int i1, int j0, int j1) const ;
95 
97  Array2<PatternRange> windows(const intCRange& i, const intCRange& j) const;
98 
99  // ------------------------------------ Row slicing.
100 
102  PatternRange row(int i) const;
103 
105  PatternRange rows(int i0, int i1) const;
106 
108  Array1<PatternRange> rows(const intCRange& i) const;
109 
111  PatternRange top(int i) const;
112 
114  PatternRange cut_top(int i) const;
115 
117  Pair split_top(int i) const;
118 
120  PatternRange bottom(int i) const;
121 
123  PatternRange cut_bottom(int i) const;
124 
126  Pair split_bottom(int i) const;
127 
128  // ------------------------------------ Column slicing.
129 
131  PatternRange column(int j) const;
132 
134  PatternRange columns(int j0, int j1) const;
135 
137  Array1<PatternRange> columns(const intCRange& j) const;
138 
140  PatternRange left(int j) const;
141 
143  PatternRange cut_left(int j) const;
144 
146  Pair split_left(int j) const;
147 
149  PatternRange right(int j) const;
150 
152  PatternRange cut_right(int j) const;
153 
155  Pair split_right(int j) const;
156 
157  private:
158 
159  // Implementation details, helps make subranges.
160  int I() const;
161  int J() const;
162 
163  // Implementation detail for iterator compare, verifies both iterators are over same range.
164  bool same (const PatternRange& that) const;
165 
166  // Implementation detail for iterator compare, verifies both iterators are over same range.
167  bool notsame (const PatternRange& that) const;
168 
169  // A special memory location so that a default-constructed 0x0 PatternRange can point to valid Ai[] data
170  static int Ai_zero;
171 
172  friend class PatternIterator;
173  friend class Pattern;
174 
175  };
176 
178 class LIBPUBLIC PatternIterator
179  {
180  public:
181 
183  void operator ++ ();
185  bool operator == (const PatternIterator& that) const;
186  bool operator != (const PatternIterator& that) const;
188 
190  int i() const;
192  int j() const;
194 
195  private:
196 
197  // Implementation detail of increment()
198  void seek();
199 
200  // Should call this when jay is modified, sets up traversal A(I0:I1,jay)
201  void j_initialize(int jj);
202 
203  // Implementation detail of initialize(), returns offset to A(ii,jay)
204  int i_initialize(int ii);
205 
206  // Copy of underlying range data.
207  PatternRange r;
208 
209  // State members of iterator.
210  int jay; // Column iterator jay
211  int b; // Offset to A(I0,jay), the begin of column jay
212  int e; // Offset to A(I1,jay), the end of column jay
213  int o; // Offset to current nonzero.
214 
215  friend class PatternRange;
216 
217  };
218 
220 LIBPUBLIC bool operator == (const PatternRange& A, const PatternRange& B);
222 LIBPUBLIC bool operator != (const PatternRange& A, const PatternRange& B);
224 
226 LIBPUBLIC std::ostream& operator << (std::ostream& out, const PatternRange& A);
227 
228 } // namespace
229 
230 #endif
Container of values, allows random (i,j) access.
Definition: Array2.h:30
const int * Ao
---------— Data members, all public ----------------—
Definition: PatternRange.h:38
bool sliced
---------— Data members, all public ----------------—
Definition: PatternRange.h:44
An iterator over a Pattern.
Definition: PatternRange.h:178
const int * Ai
---------— Data members, all public ----------------—
Definition: PatternRange.h:39
Definition: syntax.dox:1
Represents an immutable view of a Pattern.
Definition: PatternRange.h:31
Abstraction layer, serializable objects write themselves to these.
Definition: Streams.h:39
int I1
---------— Data members, all public ----------------—
Definition: PatternRange.h:42
Holds the nonzero pattern of a sparse matrix.
Definition: Pattern.h:55
Container of values, allows random (i) access.
Definition: Array1.h:32
int J1
---------— Data members, all public ----------------—
Definition: PatternRange.h:43
Represents a const intRange.
Definition: intRange.h:142