3
* Template Numerical Toolkit (TNT)
5
* Mathematical and Computational Sciences Division
6
* National Institute of Technology,
10
* This software was developed at the National Institute of Standards and
11
* Technology (NIST) by employees of the Federal Government in the course
12
* of their official duties. Pursuant to title 17 Section 105 of the
13
* United States Code, this software is not subject to copyright protection
14
* and is in the public domain. NIST assumes no responsibility whatsoever for
15
* its use by other parties, and makes no guarantees, expressed or implied,
16
* about its quality, reliability, or any other characteristic.
22
#ifndef TNT_I_REFVEC_H
23
#define TNT_I_REFVEC_H
28
#ifdef TNT_BOUNDS_CHECK
39
Internal representation of ref-counted array. The TNT
40
arrays all use this building block.
43
If an array block is created by TNT, then every time
44
an assignment is made, the left-hand-side reference
45
is decreased by one, and the right-hand-side refernce
46
count is increased by one. If the array block was
47
external to TNT, the refernce count is a NULL pointer
48
regardless of how many references are made, since the
49
memory is not freed by TNT.
67
explicit i_refvec(int n);
68
inline i_refvec(T* data);
69
inline i_refvec(const i_refvec &v);
71
inline const T* begin() const;
72
inline T& operator[](int i);
73
inline const T& operator[](int i) const;
74
inline i_refvec<T> & operator=(const i_refvec<T> &V);
75
void copy_(T* p, const T* q, const T* e);
76
void set_(T* p, const T* b, const T* e);
77
inline int ref_count() const;
78
inline int is_null() const;
79
inline void destroy();
85
void i_refvec<T>::copy_(T* p, const T* q, const T* e)
87
for (T* t=p; q<e; t++, q++)
92
i_refvec<T>::i_refvec() : data_(NULL), ref_count_(NULL) {}
95
In case n is 0 or negative, it does NOT call new.
98
i_refvec<T>::i_refvec(int n) : data_(NULL), ref_count_(NULL)
103
std::cout << "new data storage.\n";
106
ref_count_ = new int;
112
inline i_refvec<T>::i_refvec(const i_refvec<T> &V): data_(V.data_),
113
ref_count_(V.ref_count_)
115
if (V.ref_count_ != NULL)
121
i_refvec<T>::i_refvec(T* data) : data_(data), ref_count_(NULL) {}
124
inline T* i_refvec<T>::begin()
130
inline const T& i_refvec<T>::operator[](int i) const
136
inline T& i_refvec<T>::operator[](int i)
143
inline const T* i_refvec<T>::begin() const
151
i_refvec<T> & i_refvec<T>::operator=(const i_refvec<T> &V)
157
if (ref_count_ != NULL)
160
if ((*ref_count_) == 0)
165
ref_count_ = V.ref_count_;
167
if (V.ref_count_ != NULL)
174
void i_refvec<T>::destroy()
176
if (ref_count_ != NULL)
179
std::cout << "destorying data... \n";
184
std::cout << "deleted ref_count_ ...\n";
189
std::cout << "deleted data_[] ...\n";
196
* return 1 is vector is empty, 0 otherwise
198
* if is_null() is false and ref_count() is 0, then
202
int i_refvec<T>::is_null() const
204
return (data_ == NULL ? 1 : 0);
208
* returns -1 if data is external,
209
* returns 0 if a is NULL array,
210
* otherwise returns the positive number of vectors sharing
214
int i_refvec<T>::ref_count() const
219
return (ref_count_ != NULL ? *ref_count_ : -1) ;
223
i_refvec<T>::~i_refvec()
225
if (ref_count_ != NULL)
229
if (*ref_count_ == 0)
235
} /* namespace TNT */