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
26 #ifdef DEBUG_COUNTING
\r
31 #include <zxing/common/Counted.h>
\r
36 template<typename T> class Array : public Counted {
\r
39 std::vector<T> values_;
\r
41 Counted(), values_(n, T()) {
\r
43 Array(T *ts, size_t n) :
\r
44 Counted(), values_(ts, ts+n) {
\r
46 Array(T v, size_t n) :
\r
47 Counted(), values_(n, v) {
\r
49 Array(std::vector<T> &v) :
\r
50 Counted(), values_(v) {
\r
52 Array(Array<T> &other) :
\r
53 Counted(), values_(other.values_) {
\r
55 Array(Array<T> *other) :
\r
56 Counted(), values_(other->values_) {
\r
60 Array<T>& operator=(const Array<T> &other) {
\r
61 #ifdef DEBUG_COUNTING
\r
62 cout << "assigning values from Array " << &other << " to this Array " << this << ", ";
\r
64 values_ = other.values_;
\r
65 #ifdef DEBUG_COUNTING
\r
66 cout << "new size = " << values_.size() << "\n";
\r
70 Array<T>& operator=(const std::vector<T> &array) {
\r
71 #ifdef DEBUG_COUNTING
\r
72 cout << "assigning values from Array " << &array << " to this Array " << this << ", ";
\r
75 #ifdef DEBUG_COUNTING
\r
76 cout << "new size = " << values_.size() << "\n";
\r
80 T operator[](size_t i) const {
\r
83 T& operator[](size_t i) {
\r
86 size_t size() const {
\r
87 return values_.size();
\r
89 std::vector<T> values() const {
\r
92 std::vector<T>& values() {
\r
97 template<typename T> class ArrayRef {
\r
103 #ifdef DEBUG_COUNTING
\r
104 cout << "instantiating empty ArrayRef " << this << "\n";
\r
107 ArrayRef(size_t n) :
\r
109 #ifdef DEBUG_COUNTING
\r
110 cout << "instantiating ArrayRef " << this << "with size " << n << "\n";
\r
112 reset(new Array<T> (n));
\r
114 ArrayRef(T *ts, size_t n) :
\r
116 #ifdef DEBUG_COUNTING
\r
117 cout << "instantiating ArrayRef " << this << "with " << n << " elements at " << (void *)ts << "\n";
\r
119 reset(new Array<T> (ts, n));
\r
121 ArrayRef(Array<T> *a) :
\r
123 #ifdef DEBUG_COUNTING
\r
124 cout << "instantiating ArrayRef " << this << " from pointer:\n";
\r
128 ArrayRef(const Array<T> &a) :
\r
130 #ifdef DEBUG_COUNTING
\r
131 cout << "instantiating ArrayRef " << this << " from reference to Array " << (void *)&a << ":\n";
\r
133 reset(const_cast<Array<T> *>(&a));
\r
135 ArrayRef(const ArrayRef &other) :
\r
137 #ifdef DEBUG_COUNTING
\r
138 cout << "instantiating ArrayRef " << this << " from ArrayRef " << &other << ":\n";
\r
140 reset(other.array_);
\r
144 ArrayRef(const ArrayRef<Y> &other) :
\r
146 #ifdef DEBUG_COUNTING
\r
147 cout << "instantiating ArrayRef " << this << " from ArrayRef " << &other << ":\n";
\r
149 reset(static_cast<const Array<T> *>(other.array_));
\r
153 #ifdef DEBUG_COUNTING
\r
154 cout << "destroying ArrayRef " << this << " with " << (array_ ? typeid(*array_).name() : "NULL") << " "
\r
163 T operator[](size_t i) const {
\r
164 return (*array_)[i];
\r
166 T& operator[](size_t i) {
\r
167 return (*array_)[i];
\r
169 size_t size() const {
\r
170 return array_->size();
\r
173 void reset(Array<T> *a) {
\r
174 #ifdef DEBUG_COUNTING
\r
175 cout << "resetting ArrayRef " << this << " from " << (array_ ? typeid(*array_).name() : "NULL") << " "
\r
176 << array_ << " to " << (a ? typeid(*a).name() : "NULL") << " " << a << "\n";
\r
186 void reset(const ArrayRef<T> &other) {
\r
187 reset(other.array_);
\r
189 ArrayRef<T>& operator=(const ArrayRef<T> &other) {
\r
193 ArrayRef<T>& operator=(Array<T> *a) {
\r
198 Array<T>& operator*() {
\r
201 Array<T>* operator->() {
\r
206 } // namespace zxing
\r
208 #endif // __ARRAY_H__
\r