8 * Created by Christian Brunschen on 07/05/2008.
9 * Copyright 2008 Google UK. All rights reserved.
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
37 template<typename T> class Array : public Counted {
40 std::valarray<T> values_;
42 Counted(), values_(T(), n) {
44 Array(T *ts, size_t n) :
45 Counted(), values_(ts, n) {
47 Array(T v, size_t n) :
48 Counted(), values_(v, n) {
50 Array(std::valarray<T> &v) :
51 Counted(), values_(v) {
53 Array(Array<T> &other) :
54 Counted(), values_(other.values_) {
56 Array(Array<T> *other) :
57 Counted(), values_(other->values_) {
61 Array<T>& operator=(const Array<T> &other) {
63 cout << "assigning values from Array " << &other << " to this Array " << this << ", ";
65 values_ = other.values_;
67 cout << "new size = " << values_.size() << "\n";
71 Array<T>& operator=(const std::valarray<T> &array) {
73 cout << "assigning values from Array " << &array << " to this Array " << this << ", ";
77 cout << "new size = " << values_.size() << "\n";
81 T operator[](size_t i) const {
84 T& operator[](size_t i) {
88 return values_.size();
90 std::valarray<T> values() const {
93 std::valarray<T>& values() {
98 template<typename T> class ArrayRef {
104 #ifdef DEBUG_COUNTING
105 cout << "instantiating empty ArrayRef " << this << "\n";
110 #ifdef DEBUG_COUNTING
111 cout << "instantiating ArrayRef " << this << "with size " << n << "\n";
113 reset(new Array<T> (n));
115 ArrayRef(T *ts, size_t n) :
117 #ifdef DEBUG_COUNTING
118 cout << "instantiating ArrayRef " << this << "with " << n << " elements at " << (void *)ts << "\n";
120 reset(new Array<T> (ts, n));
122 ArrayRef(Array<T> *a) :
124 #ifdef DEBUG_COUNTING
125 cout << "instantiating ArrayRef " << this << " from pointer:\n";
129 ArrayRef(const Array<T> &a) :
131 #ifdef DEBUG_COUNTING
132 cout << "instantiating ArrayRef " << this << " from reference to Array " << (void *)&a << ":\n";
134 reset(const_cast<Array<T> *>(&a));
136 ArrayRef(const ArrayRef &other) :
138 #ifdef DEBUG_COUNTING
139 cout << "instantiating ArrayRef " << this << " from ArrayRef " << &other << ":\n";
145 ArrayRef(const ArrayRef<Y> &other) :
147 #ifdef DEBUG_COUNTING
148 cout << "instantiating ArrayRef " << this << " from ArrayRef " << &other << ":\n";
150 reset(static_cast<const Array<T> *>(other.array_));
154 #ifdef DEBUG_COUNTING
155 cout << "destroying ArrayRef " << this << " with " << (array_ ? typeid(*array_).name() : "NULL") << " "
164 T operator[](size_t i) const {
167 T& operator[](size_t i) {
170 size_t size() const {
171 return array_->size();
174 void reset(Array<T> *a) {
175 #ifdef DEBUG_COUNTING
176 cout << "resetting ArrayRef " << this << " from " << (array_ ? typeid(*array_).name() : "NULL") << " "
177 << array_ << " to " << (a ? typeid(*a).name() : "NULL") << " " << a << "\n";
187 void reset(const ArrayRef<T> &other) {
190 ArrayRef<T>& operator=(const ArrayRef<T> &other) {
194 ArrayRef<T>& operator=(Array<T> *a) {
199 Array<T>& operator*() {
202 Array<T>* operator->() {
209 #endif // __ARRAY_H__