1 // SPDX-License-Identifier: Apache-2.0
2 //
3 // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
4 // Copyright 2008-2016 National ICT Australia (NICTA)
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 // ------------------------------------------------------------------------
17 
18 
19 //! \addtogroup MapMat
20 //! @{
21 
22 
23 
24 // this class is for internal use only; subject to change and/or removal without notice
25 template<typename eT>
26 class MapMat
27   {
28   public:
29 
30   typedef eT                                elem_type;  //!< the type of elements stored in the matrix
31   typedef typename get_pod_type<eT>::result  pod_type;  //!< if eT is std::complex<T>, pod_type is T; otherwise pod_type is eT
32 
33   static constexpr bool is_row  = false;
34   static constexpr bool is_col  = false;
35   static constexpr bool is_xvec = false;
36 
37   const uword n_rows;    //!< number of rows     (read-only)
38   const uword n_cols;    //!< number of columns  (read-only)
39   const uword n_elem;    //!< number of elements (read-only)
40 
41 
42   private:
43 
44   typedef typename std::map<uword, eT> map_type;
45 
46   arma_aligned map_type* map_ptr;
47 
48 
49   public:
50 
51   inline ~MapMat();
52   inline  MapMat();
53 
54   inline explicit MapMat(const uword in_n_rows, const uword in_n_cols);
55   inline explicit MapMat(const SizeMat& s);
56 
57   inline          MapMat(const MapMat<eT>& x);
58   inline void  operator=(const MapMat<eT>& x);
59 
60   inline explicit MapMat(const SpMat<eT>& x);
61   inline void  operator=(const SpMat<eT>& x);
62 
63   inline          MapMat(MapMat<eT>&& x);
64   inline void  operator=(MapMat<eT>&& x);
65 
66   inline void reset();
67   inline void set_size(const uword in_n_rows);
68   inline void set_size(const uword in_n_rows, const uword in_n_cols);
69   inline void set_size(const SizeMat& s);
70 
71   inline void zeros();
72   inline void zeros(const uword in_n_rows);
73   inline void zeros(const uword in_n_rows, const uword in_n_cols);
74   inline void zeros(const SizeMat& s);
75 
76   inline void eye();
77   inline void eye(const uword in_n_rows, const uword in_n_cols);
78   inline void eye(const SizeMat& s);
79 
80   inline void speye();
81   inline void speye(const uword in_n_rows, const uword in_n_cols);
82   inline void speye(const SizeMat& s);
83 
84   arma_inline arma_warn_unused MapMat_val<eT> operator[](const uword index);
85        inline arma_warn_unused            eT  operator[](const uword index) const;
86 
87   arma_inline arma_warn_unused MapMat_val<eT> operator()(const uword index);
88        inline arma_warn_unused            eT  operator()(const uword index) const;
89 
90   arma_inline arma_warn_unused MapMat_val<eT>         at(const uword in_row, const uword in_col);
91        inline arma_warn_unused            eT          at(const uword in_row, const uword in_col) const;
92 
93   arma_inline arma_warn_unused MapMat_val<eT> operator()(const uword in_row, const uword in_col);
94        inline arma_warn_unused            eT  operator()(const uword in_row, const uword in_col) const;
95 
96   inline arma_warn_unused bool is_empty()  const;
97   inline arma_warn_unused bool is_vec()    const;
98   inline arma_warn_unused bool is_rowvec() const;
99   inline arma_warn_unused bool is_colvec() const;
100   inline arma_warn_unused bool is_square() const;
101 
102 
103   inline void sprandu(const uword in_n_rows, const uword in_n_cols, const double density);
104 
105   inline void print(const std::string& extra_text) const;
106 
107   inline uword get_n_nonzero() const;
108   inline void  get_locval_format(umat& locs, Col<eT>& vals) const;
109 
110 
111   private:
112 
113   inline void init_cold();
114   inline void init_warm(const uword in_n_rows, const uword in_n_cols);
115 
116   arma_inline void   set_val(const uword index, const eT& in_val);
117        inline void erase_val(const uword index);
118 
119 
120   friend class                SpMat<eT>;
121   friend class           MapMat_val<eT>;
122   friend class     SpMat_MapMat_val<eT>;
123   friend class SpSubview_MapMat_val<eT>;
124   };
125 
126 
127 
128 template<typename eT>
129 class MapMat_val
130   {
131   private:
132 
133   arma_aligned MapMat<eT>& parent;
134 
135   arma_aligned const uword index;
136 
137   inline MapMat_val(MapMat<eT>& in_parent, const uword in_index);
138 
139   friend class MapMat<eT>;
140 
141 
142   public:
143 
144   arma_inline operator eT() const;
145 
146   arma_inline typename get_pod_type<eT>::result real() const;
147   arma_inline typename get_pod_type<eT>::result imag() const;
148 
149   arma_inline void operator= (const MapMat_val<eT>& x);
150   arma_inline void operator= (const eT in_val);
151   arma_inline void operator+=(const eT in_val);
152   arma_inline void operator-=(const eT in_val);
153   arma_inline void operator*=(const eT in_val);
154   arma_inline void operator/=(const eT in_val);
155 
156   arma_inline void operator++();
157   arma_inline void operator++(int);
158 
159   arma_inline void operator--();
160   arma_inline void operator--(int);
161   };
162 
163 
164 
165 template<typename eT>
166 class SpMat_MapMat_val
167   {
168   private:
169 
170   arma_aligned  SpMat<eT>& s_parent;
171   arma_aligned MapMat<eT>& m_parent;
172 
173   arma_aligned const uword row;
174   arma_aligned const uword col;
175 
176   inline SpMat_MapMat_val(SpMat<eT>& in_s_parent, MapMat<eT>& in_m_parent, const uword in_row, const uword in_col);
177 
178   friend class                SpMat<eT>;
179   friend class               MapMat<eT>;
180   friend class SpSubview_MapMat_val<eT>;
181 
182 
183   public:
184 
185   inline operator eT() const;
186 
187   inline typename get_pod_type<eT>::result real() const;
188   inline typename get_pod_type<eT>::result imag() const;
189 
190   inline SpMat_MapMat_val<eT>& operator= (const SpMat_MapMat_val<eT>& x);
191 
192   inline SpMat_MapMat_val<eT>& operator= (const eT in_val);
193   inline SpMat_MapMat_val<eT>& operator+=(const eT in_val);
194   inline SpMat_MapMat_val<eT>& operator-=(const eT in_val);
195   inline SpMat_MapMat_val<eT>& operator*=(const eT in_val);
196   inline SpMat_MapMat_val<eT>& operator/=(const eT in_val);
197 
198   inline                  SpMat_MapMat_val<eT>& operator++();
199   inline arma_warn_unused eT                    operator++(int);
200 
201   inline                  SpMat_MapMat_val<eT>& operator--();
202   inline arma_warn_unused eT                    operator--(int);
203 
204   inline void set(const eT in_val);
205   inline void add(const eT in_val);
206   inline void sub(const eT in_val);
207   inline void mul(const eT in_val);
208   inline void div(const eT in_val);
209   };
210 
211 
212 
213 template<typename eT>
214 class SpSubview_MapMat_val : public SpMat_MapMat_val<eT>
215   {
216   private:
217 
218   arma_inline SpSubview_MapMat_val(SpSubview<eT>& in_sv_parent, MapMat<eT>& in_m_parent, const uword in_row, const uword in_col);
219 
220   arma_aligned SpSubview<eT>& sv_parent;
221 
222   friend class            SpMat<eT>;
223   friend class           MapMat<eT>;
224   friend class        SpSubview<eT>;
225   friend class SpMat_MapMat_val<eT>;
226 
227 
228   public:
229 
230   inline SpSubview_MapMat_val<eT>& operator= (const SpSubview_MapMat_val<eT>& x);
231 
232   inline SpSubview_MapMat_val<eT>& operator= (const eT in_val);
233   inline SpSubview_MapMat_val<eT>& operator+=(const eT in_val);
234   inline SpSubview_MapMat_val<eT>& operator-=(const eT in_val);
235   inline SpSubview_MapMat_val<eT>& operator*=(const eT in_val);
236   inline SpSubview_MapMat_val<eT>& operator/=(const eT in_val);
237 
238   inline                  SpSubview_MapMat_val<eT>& operator++();
239   inline arma_warn_unused eT                        operator++(int);
240 
241   inline                  SpSubview_MapMat_val<eT>& operator--();
242   inline arma_warn_unused eT                        operator--(int);
243   };
244 
245 
246 
247 //! @}
248