Menu

[r46]: / branches / hold_system / test / vec.hpp  Maximize  Restore  History

Download this file

202 lines (172 with data), 5.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <iostream>
#include <stdlib.h>
//=============================================================================
class vec {
public:
double *array;
int id; // for debugging
static int ID;
int n; // size of vector
bool hold; // the object is keeping its ownership of the pointer if it's true.
vec( const vec&, const bool=false );
vec( const int&, const bool=false );
~vec();
void copy(const vec&);
void destroy();
vec& operator=(const vec&);
};
int vec::ID(1);
std::ostream& operator<<(std::ostream&, const vec&);
vec operator+(const vec&, const vec&);
//=============================================================================
vec::vec( const vec & A, const bool _hold )
{
std::cout << "begin [vec(cnst vec&)]" << std::endl;
std::cout << "_hold=" << _hold << std::endl;
id = ID++;
array = NULL;
copy( A );
hold = _hold;
}
//=============================================================================
vec::vec( const int& _n, const bool _hold )
{
std::cout << "begin [vec(cnst int&)]" << std::endl;
std::cout << "_hold=" << _hold << std::endl;
id = ID++;
n = _n;
hold = _hold;
if ( n > 0 ) {
array = new double[n];
std::cout << "[vec(n) ] Memory allocation <" << array << "> for id=" << id << " hold=" << hold << std::endl;
} else {
array = NULL;
}
}
//=============================================================================
vec::~vec()
{
std::cout << "begin [~vec() ]" << std::endl;
std::cout << "id=" << id << ", hold=" << hold << std::endl;
//if(hold){ //'if(hold)' must be removed
destroy();
//}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
void vec::copy(const vec & A) //copy() changes array and n only.
{
if( A.hold ) {
if( n != A.n ) {
delete array;
array = NULL;
std::cout << "[copy(cnst vec&)] Memory deletion <" << array << "> from id=" << A.id << " to id=" << id << std::endl;
}
if( array == NULL ) {
array = new double[A.n];
n = A.n;
std::cout << "[copy(cnst vec&)] Memory allocation <" << array << "> from id=" << A.id << " to id=" << id << std::endl;
}
for( int i = 0; i < A.n; i++ ) {
array[i] = A.array[i];
}
std::cout << "[copy(cnst vec&)] Value copy <" << array << "> from id=" << A.id << " to id=" << id << std::endl;
} else {
delete array;
array = A.array;
n = A.n;
vec & _A( const_cast<vec &>( A ) );
_A.array = NULL;
_A.n = 0;
std::cout << "[copy(cnst vec&)] Memory transfer <" << array << "> from id=" << A.id << " to id=" << id << std::endl;
}
}
//=============================================================================
void vec::destroy()
{
std::cout << "destroying <" << array << ">" << std::endl;
n=0;
delete array;
array = NULL;
std::cout << "done" << std::endl;
}
//=============================================================================
vec& vec::operator=(const vec& A)// operator=() is necessary because copy constructor overwrites the value of 'hold'.
{
std::cout << "[operator=]" << std::endl;
std::cout << "arg=" << A << std::endl;
if( array != A.array ) {
copy( A );
}
return *this;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
std::ostream& operator<<( std::ostream & os, const vec & A )
{
os << " (id=" << A.id << ")";
os << " (hold=" << A.hold << ")";
os << " (array=" << A.array << ")";
os << " [";
int i;
for ( i = 0; i < A.n-1; i++ ) {
os << A.array[i] << ", ";
}
os << A.array[i] << "]";
if(!A.hold){
vec& tmp( const_cast<vec&>(A) );
tmp.destroy();
}
return os;
}
//=============================================================================
vec operator+( const vec & A, const vec & B )
{
std::cout << "[operator+]" << std::endl;
std::cout << "arg1=" << A << std::endl;
std::cout << "arg2=" << B << std::endl;
if( A.hold && B.hold ) {
vec C( A.n );
for ( int i = 0; i < A.n; i++ ) {
C.array[i] = A.array[i] + B.array[i];
}
std::cout << "before return C" << std::endl;
return C;
} else if( !B.hold ) {
vec C( B );
for ( int i = 0; i < A.n; i++ ) {
C.array[i] += A.array[i];
}
std::cout << "before return C" << std::endl;
return C;
} else {
vec C( A );
for ( int i = 0; i < B.n; i++ ) {
C.array[i] += B.array[i];
}
std::cout << "before return C" << std::endl;
return C;
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
class vecUI : public vec {
public:
vecUI( int );
vecUI( const vec & );
vecUI( const vecUI & );
};
vecUI::vecUI( const vec & A ) : vec( A, true ) {
std::cout << "end of vecUI(const vec&)" << std::endl;
}
vecUI::vecUI( const vecUI & A ) : vec( A, true ) {
std::cout << "end of vecUI(const vecUI&)" << std::endl;
}
vecUI::vecUI( int _n ) : vec( _n, true ) {
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.