1 /*
  2  *   Copyright (c) 2007, Michael Lehn
  3  *
  4  *   All rights reserved.
  5  *
  6  *   Redistribution and use in source and binary forms, with or without
  7  *   modification, are permitted provided that the following conditions
  8  *   are met:
  9  *
 10  *   1) Redistributions of source code must retain the above copyright
 11  *      notice, this list of conditions and the following disclaimer.
 12  *   2) Redistributions in binary form must reproduce the above copyright
 13  *      notice, this list of conditions and the following disclaimer in
 14  *      the documentation and/or other materials provided with the
 15  *      distribution.
 16  *   3) Neither the name of the FLENS development group nor the names of
 17  *      its contributors may be used to endorse or promote products derived
 18  *      from this software without specific prior written permission.
 19  *
 20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31  */
 32 
 33 #ifndef FLENS_STORAGE_FULLSTORAGE_FULLSTORAGE_H
 34 #define FLENS_STORAGE_FULLSTORAGE_FULLSTORAGE_H 1
 35 
 36 #include <cxxblas/typedefs.h>
 37 #include <flens/storage/indexoptions.h>
 38 #include <flens/typedefs.h>
 39 
 40 namespace flens {
 41 
 42 template <typename T, typename I, typename A>
 43     class Array;
 44 
 45 template <typename T, typename I, typename A>
 46     class ArrayView;
 47 
 48 template <typename T, typename I, typename A>
 49     class ConstArrayView;
 50 
 51 template <typename T, StorageOrder Order, typename I, typename A>
 52     class FullStorageView;
 53 
 54 template <typename T, StorageOrder Order, typename I, typename A>
 55     class ConstFullStorageView;
 56 
 57 template <typename T,
 58           StorageOrder Order = ColMajor,
 59           typename I = IndexOptions<>,
 60           typename A = std::allocator<T> >
 61 class FullStorage
 62 {
 63     public:
 64         typedef T                                     ElementType;
 65         typedef typename I::IndexType                 IndexType;
 66         typedef A                                     Allocator;
 67 
 68         static const StorageOrder                     order = Order;
 69         static const IndexType                        defaultIndexBase
 70                                                           = I::defaultIndexBase;
 71 
 72         typedef ConstFullStorageView<T, Order, I, A>  ConstView;
 73         typedef FullStorageView<T, Order, I, A>       View;
 74         typedef FullStorage                           NoView;
 75 
 76         typedef flens::ConstArrayView<T, I, A>        ConstArrayView;
 77         typedef flens::ArrayView<T, I, A>             ArrayView;
 78         typedef flens::Array<T, I, A>                 Array;
 79 
 80         FullStorage();
 81 
 82         FullStorage(IndexType numRows, IndexType numCols,
 83                     IndexType firstRow = I::defaultIndexBase,
 84                     IndexType firstCol = I::defaultIndexBase,
 85                     const ElementType &value = ElementType(),
 86                     const Allocator &allocator = Allocator());
 87 
 88         FullStorage(const FullStorage &rhs);
 89 
 90         template <typename RHS>
 91             FullStorage(const RHS &rhs);
 92 
 93         ~FullStorage();
 94 
 95         //-- operators ---------------------------------------------------------
 96 
 97         const ElementType &
 98         operator()(IndexType row, IndexType col) const;
 99 
100         ElementType &
101         operator()(IndexType row, IndexType col);
102 
103         //-- methods -----------------------------------------------------------
104 
105         IndexType
106         firstRow() const;
107 
108         IndexType
109         lastRow() const;
110 
111         IndexType
112         firstCol() const;
113 
114         IndexType
115         lastCol() const;
116 
117         IndexType
118         numRows() const;
119 
120         IndexType
121         numCols() const;
122 
123         IndexType
124         leadingDimension() const;
125 
126         IndexType
127         strideRow() const;
128 
129         IndexType
130         strideCol() const;
131 
132         const ElementType *
133         data() const;
134 
135         ElementType *
136         data();
137 
138         const Allocator &
139         allocator() const;
140 
141         bool
142         resize(IndexType numRows, IndexType numCols,
143                IndexType firstRow = I::defaultIndexBase,
144                IndexType firstCol = I::defaultIndexBase,
145                const ElementType &value = ElementType());
146 
147         bool
148         resize(const Range<IndexType> &rows,
149                const Range<IndexType> &cols,
150                const ElementType &value = ElementType());
151 
152         template <typename FS>
153             bool
154             resize(const FS &rhs, const ElementType &value = ElementType());
155 
156         bool
157         fill(const ElementType &value = ElementType(0));
158 
159         bool
160         fill(StorageUpLo upLo, const ElementType &value = ElementType(0));
161 
162         void
163         changeIndexBase(IndexType firstRow, IndexType firstCol);
164 
165         // view of fullstorage scheme as an array
166         const ConstArrayView
167         arrayView(IndexType firstViewIndex = I::defaultIndexBase) const;
168 
169         ArrayView
170         arrayView(IndexType firstViewIndex = I::defaultIndexBase);
171 
172         // view of rectangular part
173         const ConstView
174         view(IndexType fromRow, IndexType fromCol,
175              IndexType toRow, IndexType toCol,
176              IndexType firstViewRow = I::defaultIndexBase,
177              IndexType firstViewCol = I::defaultIndexBase) const;
178 
179         View
180         view(IndexType fromRow, IndexType fromCol,
181              IndexType toRow, IndexType toCol,
182              IndexType firstViewRow = I::defaultIndexBase,
183              IndexType firstViewCol = I::defaultIndexBase);
184 
185         // view of single row
186         const ConstArrayView
187         viewRow(IndexType row,
188                 IndexType firstViewIndex = I::defaultIndexBase) const;
189 
190         ArrayView
191         viewRow(IndexType row,
192                 IndexType firstViewIndex = I::defaultIndexBase);
193 
194         const ConstArrayView
195         viewRow(IndexType row,
196                 IndexType firstCol, IndexType lastCol,
197                 IndexType firstViewIndex = I::defaultIndexBase) const;
198 
199         ArrayView
200         viewRow(IndexType row,
201                 IndexType firstCol, IndexType lastCol,
202                 IndexType firstViewIndex = I::defaultIndexBase);
203 
204         // view of single column
205         const ConstArrayView
206         viewCol(IndexType col,
207                 IndexType firstViewIndex = I::defaultIndexBase) const;
208 
209         ArrayView
210         viewCol(IndexType col,
211                 IndexType firstViewIndex = I::defaultIndexBase);
212 
213         const ConstArrayView
214         viewCol(IndexType firstRow, IndexType lastRow,
215                 IndexType col,
216                 IndexType firstViewIndex = I::defaultIndexBase) const;
217 
218         ArrayView
219         viewCol(IndexType firstRow, IndexType lastRow,
220                 IndexType col,
221                 IndexType firstViewIndex = I::defaultIndexBase);
222 
223         // view of d-th diagonal
224         const ConstArrayView
225         viewDiag(IndexType d,
226                  IndexType firstViewIndex = I::defaultIndexBase) const;
227 
228         ArrayView
229         viewDiag(IndexType d,
230                  IndexType firstViewIndex = I::defaultIndexBase);
231 
232     private:
233 
234         void
235         _setIndexBase(IndexType firstRow, IndexType firstCol);
236 
237         void
238         _raw_allocate();
239 
240         void
241         _allocate(const ElementType &value = ElementType());
242 
243         void
244         _release();
245 
246         ElementType  *_data;
247         Allocator    _allocator;
248         IndexType    _numRows, _numCols;
249         IndexType    _firstRow, _firstCol;
250 };
251 
252 // namespace flens
253 
254 #endif // FLENS_STORAGE_FULLSTORAGE_FULLSTORAGE_H