MyraMath
PatternBuilder.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_PATTERNBUILDER_H
7 #define MYRAMATH_SPARSE_PATTERNBUILDER_H
8 
15 #include <myramath/MYRAMATH_EXPORT.h>
16 
17 #include <vector>
18 #include <set>
19 #include <unordered_set>
20 #include <iosfwd>
21 
22 namespace myra {
23 
24 // Forward declarations.
25 class InputStream;
26 class OutputStream;
27 class intCRange;
28 class Pattern;
29 class PatternRange;
30 
32 class MYRAMATH_EXPORT PatternCouplet
33  {
34  public:
35 
36  // Data members, all public.
37  int i;
38  int j;
39 
40  // Constructs from (i,j) arguments.
41  PatternCouplet(int ii, int jj)
42  : i(ii), j(jj) { }
43 
44  // Default constructs, i = j = 0.
46  : i(0), j(0) { }
47 
48  };
49 
51 class MYRAMATH_EXPORT PatternBuilder
52  {
53 
54  public:
55 
56  // Useful typedefs.
57  typedef PatternCouplet Couplet;
58  typedef std::vector<Couplet> Couplets;
59  typedef const Couplet* Iterator;
60 
61  // ------------------------------------ Construction, serialization, value semantics.
62 
63  // Default constructor, makes empty PatternBuilder with size = 0x0
65 
67  explicit PatternBuilder (std::pair<int,int> IJ);
68  explicit PatternBuilder (int in_I, int in_J);
69 
71  PatternBuilder(const PatternBuilder& that);
72 
74  void swap(PatternBuilder& that);
75 
76 #ifdef MYRAMATH_ENABLE_CPP11
79 #endif
80 
82  PatternBuilder& operator = (PatternBuilder that);
83 
85  PatternBuilder& operator = (const PatternRange& that);
86 
88  explicit PatternBuilder(const PatternRange& that);
89 
91  explicit PatternBuilder(InputStream& in);
92 
94  void write(OutputStream& out) const;
95 
96  // ------------------------------------ Manipulating symbolic contents.
97 
99  void insert(int i, int j);
100 
102  // Prefer insert_column() over this, due to internal column-based layout.
103  void insert_row(int i, const intCRange& j_pattern);
104 
106  // Prefer this over insert_row(), due to internal column-based layout.
107  void insert_column(const intCRange& i_pattern, int j);
108 
110  void insert_tensor(const intCRange& i_pattern, const intCRange& j_pattern);
111 
113  void insert_tensor(const intCRange& ij_pattern);
114 
116  void insert(const PatternBuilder& that, int i_offset = 0, int j_offset = 0);
117  void insert(const PatternRange& that, int i_offset = 0, int j_offset = 0);
118 
120  void erase(int i, int j);
121 
122  // ------------------------------------ Iterators.
123 
125  Iterator begin() const;
126  Iterator end() const;
127 
129  std::pair<int,int> size() const;
130 
131  // ------------------------------------ Static generators.
132 
134  static PatternBuilder identity(int IJ);
135 
137  static PatternBuilder random(std::pair<int,int> IJ, int N);
138  static PatternBuilder random(int I, int J, int N);
139 
141  Pattern make_Pattern() const;
142 
143  private:
144 
145  // Implementation detail of constructors.
146  void constructor_detail(int in_I, int in_J);
147 
148  // Holds size.
149  int I;
150  int J;
151 
152  // Internal contents.
153  Couplets couplets;
154 
155  // Compresses couplets into CSC-compatible order.
156  void compress();
157 
158  };
159 
161 MYRAMATH_EXPORT std::ostream& operator << (std::ostream& out, const PatternBuilder& p);
162 
163 } // namespace
164 
165 #endif
Number random()
Generate random real/complex Numbers, uniformly distributed over [-1,1].
Definition: syntax.dox:1
Value type of PatternBuilder, stores (i,j) indices.
Definition: PatternBuilder.h:32
Represents an immutable view of a Pattern.
Definition: PatternRange.h:31
Abstraction layer, serializable objects write themselves to these.
Definition: Streams.h:39
Abstraction layer, deserializable objects read themselves from these.
Definition: Streams.h:47
Holds the nonzero pattern of a sparse matrix.
Definition: Pattern.h:55
Like Pattern, but easier to populate via insert()/erase() methods.
Definition: PatternBuilder.h:51
Represents a const intRange.
Definition: intRange.h:142