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_ARRAY_ARRAY_H
 34 #define FLENS_STORAGE_ARRAY_ARRAY_H 1
 35 
 36 #include <memory>
 37 #include <flens/storage/indexoptions.h>
 38 
 39 namespace flens {
 40 
 41 template <typename T, typename I, typename A>
 42     class ConstArrayView;
 43 
 44 template <typename T, typename I, typename A>
 45     class ArrayView;
 46 
 47 template <typename T,
 48           typename I = IndexOptions<>,
 49           typename A = std::allocator<T> >
 50 class Array
 51 {
 52     public:
 53         typedef T                        ElementType;
 54         typedef typename I::IndexType    IndexType;
 55         typedef A                        Allocator;
 56 
 57         typedef ConstArrayView<T, I, A>  ConstView;
 58         typedef ArrayView<T, I, A>       View;
 59         typedef Array                    NoView;
 60 
 61         static const IndexType           defaultIndexBase = I::defaultIndexBase;
 62 
 63         Array();
 64 
 65         Array(IndexType length,
 66               IndexType firstIndex = defaultIndexBase,
 67               const ElementType &value = ElementType(),
 68               const Allocator &allocator = Allocator());
 69 
 70         Array(const Array &rhs);
 71 
 72         template <typename RHS>
 73             Array(const RHS &rhs);
 74 
 75         ~Array();
 76 
 77         //-- operators ---------------------------------------------------------
 78 
 79         const ElementType &
 80         operator()(IndexType index) const;
 81 
 82         ElementType &
 83         operator()(IndexType index);
 84 
 85         //-- methods -----------------------------------------------------------
 86 
 87         IndexType
 88         firstIndex() const;
 89 
 90         IndexType
 91         lastIndex() const;
 92 
 93         IndexType
 94         length() const;
 95 
 96         IndexType
 97         stride() const;
 98 
 99         const ElementType *
100         data() const;
101 
102         ElementType *
103         data();
104 
105         const Allocator &
106         allocator() const;
107 
108         bool
109         resize(IndexType length,
110                IndexType firstIndex = defaultIndexBase,
111                const ElementType &value = ElementType());
112 
113         template <typename ARRAY>
114             bool
115             resize(const ARRAY &rhs, const ElementType &value = ElementType());
116 
117         bool
118         fill(const ElementType &value = ElementType(0));
119 
120         void
121         changeIndexBase(IndexType firstIndex);
122 
123         const ConstView
124         view(IndexType from, IndexType to,
125              IndexType stride = IndexType(1),
126              IndexType firstViewIndex = defaultIndexBase) const;
127 
128         View
129         view(IndexType from, IndexType to,
130              IndexType stride = IndexType(1),
131              IndexType firstViewIndex = defaultIndexBase);
132 
133     private:
134         void
135         _raw_allocate();
136 
137         void
138         _allocate(const ElementType &value  = ElementType());
139 
140         void
141         _release();
142 
143         ElementType *_data;
144         Allocator   _allocator;
145         IndexType   _length, _firstIndex;
146 };
147 
148 // namespace flens
149 
150 #endif // FLENS_STORAGE_ARRAY_ARRAY_H