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