1 /*
  2  *   Copyright (c) 2007, Michael Lehn
  3  *   Copyright (c) 2011, Michael Lehn
  4  *
  5  *   All rights reserved.
  6  *
  7  *   Redistribution and use in source and binary forms, with or without
  8  *   modification, are permitted provided that the following conditions
  9  *   are met:
 10  *
 11  *   1) Redistributions of source code must retain the above copyright
 12  *      notice, this list of conditions and the following disclaimer.
 13  *   2) Redistributions in binary form must reproduce the above copyright
 14  *      notice, this list of conditions and the following disclaimer in
 15  *      the documentation and/or other materials provided with the
 16  *      distribution.
 17  *   3) Neither the name of the FLENS development group nor the names of
 18  *      its contributors may be used to endorse or promote products derived
 19  *      from this software without specific prior written permission.
 20  *
 21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32  */
 33 
 34 #ifndef FLENS_VECTORTYPES_IMPL_DENSEVECTOR_H
 35 #define FLENS_VECTORTYPES_IMPL_DENSEVECTOR_H 1
 36 
 37 #include <flens/aux/range.h>
 38 #include <flens/aux/underscore.h>
 39 #include <flens/scalartypes/scalar.h>
 40 #include <flens/vectortypes/vector.h>
 41 #include <flens/vectortypes/impl/dv/constelementclosure.h>
 42 #include <flens/vectortypes/impl/dv/elementclosure.h>
 43 #include <flens/vectortypes/impl/dv/initializer.h>
 44 
 45 namespace flens {
 46 
 47 // forward declaration
 48 template <typename IndexType>
 49     class IndexVariable;
 50 
 51 template <typename A>
 52 class DenseVector
 53     : public Vector<DenseVector<A> >
 54 {
 55     public:
 56         typedef A                                           Engine;
 57         typedef typename Engine::ElementType                ElementType;
 58         typedef typename Engine::IndexType                  IndexType;
 59 
 60         // view types from Engine
 61         typedef typename Engine::ConstView                  EngineConstView;
 62         typedef typename Engine::View                       EngineView;
 63         typedef typename Engine::NoView                     EngineNoView;
 64 
 65         // view types
 66         typedef DenseVector<EngineConstView>                ConstView;
 67         typedef DenseVector<EngineView>                     View;
 68         typedef DenseVector<EngineNoView>                   NoView;
 69 
 70     private:
 71         typedef DenseVector                                 DV;
 72 
 73     public:
 74         typedef flens::IndexVariable<IndexType>             IndexVariable;
 75         typedef densevector::ConstElementClosure<DV>        ConstElementClosure;
 76         typedef densevector::ElementClosure<DV>             ElementClosure;
 77         typedef densevector::Initializer<DV>                Initializer;
 78 
 79         DenseVector();
 80 
 81         explicit
 82         DenseVector(IndexType length);
 83 
 84         DenseVector(IndexType length, IndexType firstIndex);
 85 
 86         DenseVector(const Range<IndexType> &range);
 87 
 88         DenseVector(const Engine &engine, bool reverse=false);
 89 
 90         DenseVector(const DenseVector &rhs);
 91 
 92         template <typename RHS>
 93             DenseVector(const DenseVector<RHS> &rhs);
 94 
 95         template <typename RHS>
 96             DenseVector(DenseVector<RHS> &rhs);
 97 
 98         template <typename RHS>
 99             DenseVector(const Vector<RHS> &rhs);
100 
101         // -- operators --------------------------------------------------------
102 
103         Initializer
104         operator=(const ElementType &value);
105 
106         DenseVector &
107         operator=(const DenseVector &rhs);
108 
109         template <typename RHS>
110             DenseVector &
111             operator=(const Vector<RHS> &rhs);
112 
113         template <typename RHS>
114             DenseVector &
115             operator+=(const Vector<RHS> &rhs);
116 
117         template <typename RHS>
118             DenseVector &
119             operator-=(const Vector<RHS> &rhs);
120 
121         DenseVector &
122         operator+=(const ElementType &rhs);
123 
124         DenseVector &
125         operator-=(const ElementType &rhs);
126 
127         DenseVector &
128         operator*=(const ElementType &alpha);
129 
130         DenseVector &
131         operator/=(const ElementType &alpha);
132 
133         const ElementType &
134         operator()(IndexType index) const;
135 
136         ElementType &
137         operator()(IndexType index);
138 
139         template <typename S>
140             const densevector::ConstElementClosure<DenseVector,
141                                                    typename Scalar<S>::Impl>
142             operator()(const Scalar<S> &index) const;
143 
144         const ConstElementClosure
145         operator()(const IndexVariable &index) const;
146 
147         ElementClosure
148         operator()(IndexVariable &index);
149 
150         //-- views -------------------------------------------------------------
151 
152         const ConstView
153         operator()(const Range<IndexType> &range) const;
154 
155         View
156         operator()(const Range<IndexType> &range);
157 
158         const ConstView
159         operator()(const Range<IndexType> &range,
160                    IndexType firstViewIndex) const;
161 
162         View
163         operator()(const Range<IndexType> &range, 
164                    IndexType firstViewIndex);
165 
166         const ConstView
167         operator()(const Underscore<IndexType> &all,
168                    IndexType firstViewIndex) const;
169 
170         View
171         operator()(const Underscore<IndexType> &all, 
172                    IndexType firstViewIndex);
173 
174         const ConstView
175         reverse() const;
176 
177         View
178         reverse();
179 
180         // -- methods ----------------------------------------------------------
181         Range<IndexType>
182         range() const;
183 
184         IndexType
185         firstIndex() const;
186 
187         IndexType
188         lastIndex() const;
189 
190         IndexType
191         length() const;
192 
193         IndexType
194         inc() const;
195 
196         IndexType
197         endIndex() const;
198 
199         const ElementType *
200         data() const;
201 
202         ElementType *
203         data();
204 
205         IndexType
206         stride() const;
207 
208         IndexType
209         indexBase() const;
210 
211         template <typename RHS>
212             bool
213             resize(const DenseVector<RHS> &rhs,
214                    const ElementType &value = ElementType());
215 
216         bool
217         resize(IndexType length,
218                IndexType firstIndex = Engine::defaultIndexBase,
219                const ElementType &value = ElementType());
220 
221         bool
222         fill(const ElementType &value = ElementType(0));
223 
224         void
225         changeIndexBase(IndexType firstIndex);
226 
227         // -- implementation ---------------------------------------------------
228         const A &
229         engine() const;
230 
231         A &
232         engine();
233 
234         bool
235         reversed() const;
236 
237     private:
238         A    _array;
239         bool _reverse;
240 };
241 
242 // namespace flens
243 
244 #endif // FLENS_VECTORTYPES_IMPL_DENSEVECTOR_H