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.
36 template<typename T> class Array : public Counted {
40 Array(size_t n) : Counted(), values_(T(), n) { }
41 Array(T *ts, size_t n) : Counted(), values_(ts, n) { }
42 Array(T v, size_t n) : Counted(), values_(v, n) { }
43 Array(valarray<T> &v) : Counted(), values_(v) { }
44 Array(Array<T> &other) : Counted(), values_(other.values_) { }
45 Array(Array<T> *other) : Counted(), values_(other->values_) { }
47 Array<T>& operator=(const Array<T> &other) {
49 cout << "assigning values from Array " <<
50 &other << " to this Array " << this << ", ";
52 values_ = other.values_;
54 cout << "new size = " << values_.size() << "\n";
58 Array<T>& operator=(const valarray<T> &array) {
60 cout << "assigning values from Array " <<
61 &array << " to this Array " << this << ", ";
65 cout << "new size = " << values_.size() << "\n";
69 T operator[](size_t i) const { return values_[i]; }
70 T& operator[](size_t i) { return values_[i]; }
71 size_t size() const { return values_.size(); }
72 valarray<T> values() const { return values_; }
73 valarray<T>& values() { return values_; }
76 template<typename T> class ArrayRef {
80 ArrayRef() : array_(0) {
82 cout << "instantiating empty ArrayRef " << this << "\n";
85 ArrayRef(size_t n) : array_(0) {
87 cout << "instantiating ArrayRef " << this <<
88 "with size " << n << "\n";
90 reset(new Array<T>(n));
92 ArrayRef(T *ts, size_t n) : array_(0) {
94 cout << "instantiating ArrayRef " << this <<
95 "with " << n << " elements at " << (void *)ts << "\n";
97 reset(new Array<T>(ts, n));
99 ArrayRef(Array<T> *a) : array_(0) {
100 #ifdef DEBUG_COUNTING
101 cout << "instantiating ArrayRef " << this << " from pointer:\n";
105 ArrayRef(const Array<T> &a) : array_(0) {
106 #ifdef DEBUG_COUNTING
107 cout << "instantiating ArrayRef " << this <<
108 " from reference to Array " << (void *)&a << ":\n";
110 reset(const_cast<Array<T> *>(&a));
112 ArrayRef(const ArrayRef &other) : array_(0) {
113 #ifdef DEBUG_COUNTING
114 cout << "instantiating ArrayRef " << this << " from ArrayRef " <<
121 ArrayRef(const ArrayRef<Y> &other) : array_(0) {
122 #ifdef DEBUG_COUNTING
123 cout << "instantiating ArrayRef " << this << " from ArrayRef " <<
126 reset(static_cast<const Array<T> *>(other.array_));
130 #ifdef DEBUG_COUNTING
131 cout << "destroying ArrayRef " << this << " with " <<
132 (array_ ? typeid(*array_).name() : "NULL") << " " << array_ << "\n";
140 T operator[](size_t i) const { return (*array_)[i]; }
141 T& operator[](size_t i) { return (*array_)[i]; }
142 size_t size() const { return array_->size(); }
144 void reset(Array<T> *a) {
145 #ifdef DEBUG_COUNTING
146 cout << "resetting ArrayRef " << this << " from " <<
147 (array_ ? typeid(*array_).name() : "NULL") << " " << array_ <<
148 " to " << (a ? typeid(*a).name() : "NULL") << " " << a << "\n";
150 if (a) { a->retain(); }
151 if (array_) { array_->release(); }
154 void reset(const ArrayRef<T> &other) {
157 ArrayRef<T>& operator=(const ArrayRef<T> &other) {
161 ArrayRef<T>& operator=(Array<T> *a) {
167 Array<T>& operator*() { return *array_; }
168 Array<T>* operator->() { return array_; }
172 #endif // __ARRAY_H__