8 * Copyright 2010 ZXing authors All rights reserved.
\r
10 * Licensed under the Apache License, Version 2.0 (the "License");
\r
11 * you may not use this file except in compliance with the License.
\r
12 * You may obtain a copy of the License at
\r
14 * http://www.apache.org/licenses/LICENSE-2.0
\r
16 * Unless required by applicable law or agreed to in writing, software
\r
17 * distributed under the License is distributed on an "AS IS" BASIS,
\r
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
19 * See the License for the specific language governing permissions and
\r
20 * limitations under the License.
\r
25 #ifdef DEBUG_COUNTING
\r
30 #include <zxing/common/Counted.h>
\r
35 template<typename T> class Array : public Counted {
\r
38 std::vector<T> values_;
\r
40 Counted(), values_(n, T()) {
\r
42 Array(T *ts, size_t n) :
\r
43 Counted(), values_(ts, ts+n) {
\r
45 Array(T v, size_t n) :
\r
46 Counted(), values_(n, v) {
\r
48 Array(std::vector<T> &v) :
\r
49 Counted(), values_(v) {
\r
51 Array(Array<T> &other) :
\r
52 Counted(), values_(other.values_) {
\r
54 Array(Array<T> *other) :
\r
55 Counted(), values_(other->values_) {
\r
59 Array<T>& operator=(const Array<T> &other) {
\r
60 #ifdef DEBUG_COUNTING
\r
61 cout << "assigning values from Array " << &other << " to this Array " << this << ", ";
\r
63 values_ = other.values_;
\r
64 #ifdef DEBUG_COUNTING
\r
65 cout << "new size = " << values_.size() << "\n";
\r
69 Array<T>& operator=(const std::vector<T> &array) {
\r
70 #ifdef DEBUG_COUNTING
\r
71 cout << "assigning values from Array " << &array << " to this Array " << this << ", ";
\r
74 #ifdef DEBUG_COUNTING
\r
75 cout << "new size = " << values_.size() << "\n";
\r
79 T operator[](size_t i) const {
\r
82 T& operator[](size_t i) {
\r
85 size_t size() const {
\r
86 return values_.size();
\r
88 std::vector<T> values() const {
\r
91 std::vector<T>& values() {
\r
96 template<typename T> class ArrayRef {
\r
102 #ifdef DEBUG_COUNTING
\r
103 cout << "instantiating empty ArrayRef " << this << "\n";
\r
106 ArrayRef(size_t n) :
\r
108 #ifdef DEBUG_COUNTING
\r
109 cout << "instantiating ArrayRef " << this << "with size " << n << "\n";
\r
111 reset(new Array<T> (n));
\r
113 ArrayRef(T *ts, size_t n) :
\r
115 #ifdef DEBUG_COUNTING
\r
116 cout << "instantiating ArrayRef " << this << "with " << n << " elements at " << (void *)ts << "\n";
\r
118 reset(new Array<T> (ts, n));
\r
120 ArrayRef(Array<T> *a) :
\r
122 #ifdef DEBUG_COUNTING
\r
123 cout << "instantiating ArrayRef " << this << " from pointer:\n";
\r
127 ArrayRef(const Array<T> &a) :
\r
129 #ifdef DEBUG_COUNTING
\r
130 cout << "instantiating ArrayRef " << this << " from reference to Array " << (void *)&a << ":\n";
\r
132 reset(const_cast<Array<T> *>(&a));
\r
134 ArrayRef(const ArrayRef &other) :
\r
136 #ifdef DEBUG_COUNTING
\r
137 cout << "instantiating ArrayRef " << this << " from ArrayRef " << &other << ":\n";
\r
139 reset(other.array_);
\r
143 ArrayRef(const ArrayRef<Y> &other) :
\r
145 #ifdef DEBUG_COUNTING
\r
146 cout << "instantiating ArrayRef " << this << " from ArrayRef " << &other << ":\n";
\r
148 reset(static_cast<const Array<T> *>(other.array_));
\r
152 #ifdef DEBUG_COUNTING
\r
153 cout << "destroying ArrayRef " << this << " with " << (array_ ? typeid(*array_).name() : "NULL") << " "
\r
162 T operator[](size_t i) const {
\r
163 return (*array_)[i];
\r
165 T& operator[](size_t i) {
\r
166 return (*array_)[i];
\r
168 size_t size() const {
\r
169 return array_->size();
\r
172 void reset(Array<T> *a) {
\r
173 #ifdef DEBUG_COUNTING
\r
174 cout << "resetting ArrayRef " << this << " from " << (array_ ? typeid(*array_).name() : "NULL") << " "
\r
175 << array_ << " to " << (a ? typeid(*a).name() : "NULL") << " " << a << "\n";
\r
185 void reset(const ArrayRef<T> &other) {
\r
186 reset(other.array_);
\r
188 ArrayRef<T>& operator=(const ArrayRef<T> &other) {
\r
192 ArrayRef<T>& operator=(Array<T> *a) {
\r
197 Array<T>& operator*() {
\r
200 Array<T>* operator->() {
\r
205 } // namespace zxing
\r
207 #endif // __ARRAY_H__
\r