ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ptr_vec.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 
33 struct void_ptr
34 {
35  void * t;
36 };
37 
40 {
41 #ifdef DIAG
42  cout << "deleting an ivector with address " << _farptr_tolong(v)
43  <<" and ncopies = " << shape->ncopies <<"\n";
44 #endif
45  if (shape->ncopies)
46  {
47  (shape->ncopies)--;
48  }
49  else
50  {
51  if (v == NULL)
52  {
53  cerr << " Trying to delete NULL pointer in ~ivector\n";
54  ad_exit(21);
55  }
56  v += indexmin();
57  delete [] v;
58  v=NULL;
59 
60 #ifdef DIAG
61  cout << "Really deleting an ivector with address "
62  << _farptr_tolong(v) <<"\n";
63 #endif
64 
65  delete shape;
66  shape = NULL;
67  }
68 }
73 {
74 #ifdef DIAG
75  cout << "Copy constructor called for ivector with address "
76  << _farptr_tolong(t.v) <<"\n";
77 #endif
78  shape=t.shape;
79  (shape->ncopies)++;
80  v = t.v;
81 }
88 {
90  // disconnect ivector pointer from old array
91  if (v != t.v)
92  {
93  if (indexmin() != t.indexmin() || indexmax() != t.indexmax())
94  {
95  cerr << " Array sizes do not match in "
96  "ptr_vector operator =(const ivector&)\n";
97  }
98  for ( int i=indexmin(); i<=indexmax(); i++)
99  {
100  elem(i) = t.elem(i);
101  }
102  }
103  return *this;
104 }
111 ptr_vector::ptr_vector(int ncl,int nch)
112 {
113  allocate(ncl,nch);
114 }
119 {
120  shape=NULL;
121  v=NULL;
122 }
131 void ptr_vector::allocate(int ncl,int nch)
132 {
133  if ((shape = new vector_shape(ncl,nch)) == 0)
134  {
135  cerr << " Error trying to allocate memory for ivector\n";
136  }
137  v = (void**) new void_ptr [(size_t) (nch-ncl+1)];
138 #ifdef DIAG
139  cout << "Created a ivector with address " << _farptr_tolong(v) <<"\n";
140 #endif
141  if (v == 0)
142  {
143  cerr << " Error trying to allocate memory for ptr_vector\n";
144  ad_exit(21);
145  }
146  v -= indexmin();
147 #ifndef OPT_LIB
148  this->initialize();
149 #endif
150 }
155 {
156  for (int i = indexmin(); i <= indexmax(); i++)
157  {
158  v[i] = NULL;
159  }
160 }
166 {
167  if (i > indexmax())
168  {
169  cerr << "array bound exceeded -- index too high in ivector::operator[]";
170  ad_exit(1);
171  }
172  if (i < indexmin())
173  {
174  cerr << "array bound exceeded -- index too low in ivector::operator[]";
175  ad_exit(1);
176  }
177  return (*(v+i));
178 }
179 
185 {
186  if (i>indexmax())
187  {
188  cerr << "array bound exceeded -- index too high in ivector::operator[]";
189  ad_exit(1);
190  }
191  if (i<indexmin())
192  {
193  cerr << "array bound exceeded -- index too low in ivector::operator[]";
194  ad_exit(1);
195  }
196  return(*(v+i));
197 }
202 ostream& operator<<(const ostream& _s, const ptr_vector& _v)
203 {
205  ostream & s = (ostream&) _s;
206  for (int i=v.indexmin();i<=v.indexmax();i++)
207  {
208  s << v.elem(i) << " ";
209  }
210  s << endl;
211  return s;
212 }
void initialize()
Initialize elements in pointer array to null.
Definition: ptr_vec.cpp:154
int indexmin() const
Definition: fvar.hpp:580
void *& operator[](int i)
Description not yet available.
Definition: ptr_vec.cpp:165
unsigned int ncopies
Definition: fvar.hpp:522
Description not yet available.
Definition: fvar.hpp:509
void * t
Definition: ptr_vec.cpp:35
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
Description not yet available.
Definition: fvar.hpp:568
exitptr ad_exit
Definition: gradstrc.cpp:53
int indexmax() const
Definition: fvar.hpp:585
void *& elem(int i)
Definition: fvar.hpp:601
prnstream & endl(prnstream &)
int operator()(void) const
Definition: fvar.hpp:609
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
vector_shape * shape
Definition: fvar.hpp:571
void allocate(int, int)
Allocate array [ncl,...,nch].
Definition: ptr_vec.cpp:131
ptr_vector & operator=(const ptr_vector &t)
Copy values from _t to this.
Definition: ptr_vec.cpp:87
Description not yet available.
Definition: ptr_vec.cpp:33
void ** v
Definition: fvar.hpp:570
ostream & operator<<(const ostream &_s, preshowpoint p)
Description not yet available.
Definition: admanip.cpp:48
ptr_vector()
Default constructor.
Definition: ptr_vec.cpp:118
~ptr_vector()
Destructor.
Definition: ptr_vec.cpp:39