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