ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ivector.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 #include "fvar.hpp"
12 
13 #ifdef __TURBOC__
14  #pragma hdrstop
15  #include <iostream.h>
16 #endif
17 
18 #ifdef __ZTC__
19  #include <iostream.hpp>
20 #endif
21 
22 #include <stdlib.h>
23 
24 #ifdef DIAG
25 long int _farptr_tolong(void* px);
26 long int farptr_tolong(void*);
27 #endif
28 
29 #ifndef OPT_LIB
30  #include <cassert>
31  #include <climits>
32 #endif
33 
38 {
39  allocate();
40 }
45 {
46 #ifdef DIAG
47  cout << "Copy constructor called for ivector with address "
48  << _farptr_tolong(t.v) <<"\n";
49 #endif
50 
51  index_min = t.index_min;
52  index_max = t.index_max;
53 
54  shape=t.shape;
55  if (shape)
56  {
57  (shape->ncopies)++;
58  v = t.v;
59  }
60  else
61  {
62  v = NULL;
63  }
64 }
71 {
72  deallocate();
73 }
78  void ivector::safe_allocate(int ncl,int nch)
79  {
80  if (allocated())
81  {
82  cerr << "trying to allocate an already allocated dvector " << endl;
83  ad_exit(1);
84  }
85  else
86  {
87  allocate(ncl,nch);
88  }
89  }
92 {
93  //Called by destructor to deallocate memory for a ivector object.
94  //Produces an error if the int* member is NULL.
95  if (shape)
96  {
97  if (shape->ncopies > 0)
98  {
99  --(shape->ncopies);
100  }
101  else
102  {
103  v = static_cast<int*>(shape->trueptr);
104  delete [] v;
105  delete shape;
106  }
107  allocate();
108  }
109 #if defined(DIAG)
110  else
111  {
112  cerr << "Warning -- Unable to deallocate an unallocated i3_array.\n";
113  }
114 #endif
115 }
120 {
121  if (shape)
122  {
123  if (shape->ncopies)
124  {
125  cerr << "trying to deallocate a ivector with copies" << endl;
126  ad_exit(1);
127  }
128  deallocate();
129  }
130 }
136 void ivector::shallow_copy(const ivector& other)
137 {
138 #ifdef DIAG
139  cout << "Copy constructor called for ivector with address "
140  << _farptr_tolong(other.v) <<"\n";
141 #endif
142  if (other.shape)
143  {
144  shape = other.shape;
145  ++(shape->ncopies);
146  v = other.v;
147 
148  index_min = other.index_min;
149  index_max = other.index_max;
150  }
151  else
152  {
153 #ifdef DIAG
154  cerr << "Warning -- Unable to shallow copy an unallocated i3_array.\n";
155 #endif
156  allocate();
157  }
158 }
159 
165  {
166  // disconnect ivector pointer from old array
167  if (::allocated(*this))
168  {
169  if (v != t.v)
170  {
171  if (indexmin() != t.indexmin() || indexmax() != t.indexmax())
172  {
173  cerr << " Array sizes do not match in ivector operator"
174  " =(const ivector&)" << endl;
175  ad_exit(1);
176  }
177 
178  for ( int i=indexmin(); i<=indexmax(); i++)
179  {
180  elem(i) = t.elem(i);
181  }
182  }
183  }
184  else
185  {
186  shallow_copy(t);
187  }
188  return (*this);
189  }
196 {
197  for (int i = indexmin(); i <= indexmax(); ++i)
198  {
199  elem(i) = value;
200  }
201  return *this;
202 }
206 ivector::ivector(unsigned int sz, long int* x)
207 {
208 #ifndef OPT_LIB
209  assert(sz > 0 && sz <= INT_MAX);
210  assert(x);
211 #endif
212  allocate(0, (int)(sz - 1));
213 
214  if (v)
215  {
216  for (unsigned int i = 0; i < sz; i++)
217  {
218 #ifdef OPT_LIB
219  v[i] = (int)x[i];
220 #else
221  long int xi = x[i];
222  assert(xi <= INT_MAX);
223  v[i] = static_cast<int>(xi);
224 #endif
225  }
226  }
227 }
232 {
233  allocate(u);
234  for (int i=indexmin();i<=indexmax();i++)
235  {
236 #ifdef OPT_LIB
237  elem(i) = static_cast<int>(u.elem(i));
238 #else
239  double ui = u.elem(i);
240  assert(ui <= INT_MAX);
241  elem(i) = static_cast<int>(ui);
242 #endif
243  }
244 }
252 ivector::ivector(int ncl, int nch)
253 {
254  allocate(ncl, nch);
255 }
263 void ivector::allocate(int ncl,int nch)
264 {
265  if (ncl > nch)
266  {
267  allocate();
268  }
269  else
270  {
271  if ((v = new int[static_cast<unsigned int>(nch - ncl + 1)]) == 0 )
272  {
273  cerr << " Error: ivector unable to allocate memory in "
274  << __FILE__ << ':' << __LINE__ << '\n';
275  ad_exit(1);
276  }
277  if ((shape = new vector_shapex(ncl, nch, v)) == NULL)
278  {
279  cerr << " Error: ivector unable to allocate memory in "
280  << __FILE__ << ':' << __LINE__ << '\n';
281  ad_exit(1);
282  }
283 
284  index_min = ncl;
285  index_max = nch;
286 
287  v -= indexmin();
288 
289 #ifdef SAFE_INITIALIZE
290  for (int i = ncl; i <= nch; ++i)
291  {
292  v[i] = 0;
293  }
294 #endif
295  }
296 }
297 
302 void ivector::allocate(const dvector& dv)
303 {
304  allocate(dv.indexmin(),dv.indexmax());
305 }
306 
311 void ivector::allocate(const ivector& dv)
312 {
313  allocate(dv.indexmin(),dv.indexmax());
314 }
317 {
318  index_min = 1;
319  index_max = 0;
320  v = nullptr;
321  shape = nullptr;
322 }
328 {
329 #if defined(DIAG)
330  cout << "starting out in ivector constructor\n";
331 #endif
332 
333  shape=pdv.p->shape;
334  if (shape)
335  {
336  (shape->ncopies)++;
337  }
338 #if defined(DEBUG)
339  else
340  {
341  cerr << "Taking a subvector of an unallocated ivector" << endl;
342  }
343 #endif
344  v = pdv.p->v;
345  index_min = pdv.lb;
346  index_max = pdv.ub;
347 }
348 
354 int norm2(const ivector& ivec)
355 {
356  int sum = 0;
357  for (int i = ivec.indexmin(); i <= ivec.indexmax(); ++i)
358  {
359  sum += ivec(i) * ivec(i);
360  }
361  return sum;
362 }
368 int sumsq(const ivector& ivec)
369 {
370  return norm2(ivec);
371 }
377 void clean(ivector& v, int level)
378 {
379  int max = v.indexmax();
380  for (int i = level + 1; i <= max; ++i)
381  {
382  v(i) = 0;
383  }
384 }
int & elem(int i)
Definition: ivector.h:90
void safe_deallocate()
Safely deallocates memory by reporting if shallow copies are still in scope.
Definition: ivector.cpp:119
void * trueptr
Address of first element in the vector.
Definition: vector_shapex.h:82
int allocated() const
Definition: ivector.h:63
ivector()
Default constructor.
Definition: ivector.cpp:37
double & elem(int i)
Definition: dvector.h:152
double sumsq(const d3_array &a)
Definition: d3arr2a.cpp:181
int lb
Definition: fvar.hpp:625
#define x
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
~ivector()
Default destructor.
Definition: ivector.cpp:70
void clean(ivector &v, int level)
Set elements of ivec to zero starting from level + 1;.
Definition: ivector.cpp:377
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
exitptr ad_exit
Definition: gradstrc.cpp:53
ivector & operator=(const ivector &t)
Description not yet available.
Definition: ivector.cpp:164
prnstream & endl(prnstream &)
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
vector_shapex * shape
Definition: ivector.h:56
int index_max
Definition: ivector.h:54
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Description not yet available.
Definition: fvar.hpp:622
int indexmin() const
Definition: ivector.h:99
int indexmax() const
Definition: ivector.h:104
unsigned int ncopies
Copy counter to enable shallow copies.
Definition: vector_shapex.h:79
double norm2(const d3_array &a)
Return sum of squared elements in a.
Definition: d3arr2a.cpp:167
void allocate()
Does not allocate, but initializes class members.
Definition: ivector.cpp:316
Holds &quot;shape&quot; information for vector objects.
Definition: vector_shapex.h:46
void deallocate()
Deallocate i3_array memory.
Definition: ivector.cpp:91
int * v
Definition: ivector.h:55
void safe_allocate(int ncl, int ncu)
Description not yet available.
Definition: ivector.cpp:78
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
#define max(a, b)
Definition: cbivnorm.cpp:189
ivector * p
Definition: fvar.hpp:624
void shallow_copy(const ivector &t)
Shallow copy other data structure pointers.
Definition: ivector.cpp:136
int index_min
Definition: ivector.h:53
int ub
Definition: fvar.hpp:626