ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lvector.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 #ifdef __TURBOC__
12  #pragma hdrstop
13  #include <iostream.h>
14 #endif
15 
16 #ifdef __ZTC__
17  #include <iostream.hpp>
18 #endif
19 
20 #include <stdlib.h>
21 #include "fvar.hpp"
22 #ifndef OPT_LIB
23  #include <cassert>
24  #include <climits>
25 #endif
26 
31 {
32  allocate();
33 }
38 {
39  shape=t.shape;
40  (shape->ncopies)++;
41  v = t.v;
42 }
47 {
48  if (shape)
49  {
50  if (shape->ncopies)
51  {
52  (shape->ncopies)--;
53  }
54  else
55  {
56  if (v)
57  {
58  v += indexmin();
59  delete [] v;
60  v = NULL;
61  }
62  delete shape;
63  shape = NULL;
64  }
65  }
66 }
72  {
73  if ( (shape=new vector_shape(u.indexmin(),u.indexmax()))==0 )
74  {
75  cerr << " Error trying to allocate memory for ivector\n";
76  }
77  v = new AD_LONG_INT [(size_t) u.size() ];
78  if (v ==0)
79  {
80  cerr << " Error trying to allocate memory for ivector\n";
81  ad_exit(21);
82  }
83 
84  v -= indexmin();
85  for (int i=indexmin(); i<=indexmax(); i++)
86  {
87  #if !defined(USE_DDOUBLE)
88  v[i]= (AD_LONG_INT) u.elem(i);
89  #else
90  v[i]= int(u.elem(i));
91  #endif
92  }
93  }
94 
100  {
101  if (v != t.v)
102  {
103  if (indexmin() != t.indexmin() || indexmax() != t.indexmax())
104  {
105  cerr <<
106  " Array sizes do not match in lvector operator =(const lvector&)\n";
107  }
108 
109  for ( int i=indexmin(); i<=indexmax(); i++)
110  {
111  elem(i) = t.elem(i);
112  }
113  }
114  return (*this);
115  }
116 
121 lvector::lvector( unsigned int sz, AD_LONG_INT * x )
122 {
123 #ifndef OPT_LIB
124  assert(sz > 0 && sz - 1 <= INT_MAX);
125 #endif
126  if ((shape=new vector_shape(0, (int)(sz - 1))) == 0)
127  {
128  cerr << " Error trying to allocate memory for lvector\n";
129  }
130  if ( (v = new AD_LONG_INT [sz]) == NULL)
131  {
132  cerr << "Error trying to allocate memory for lvector\n";
133  ad_exit(1);
134  }
135 
136  for (unsigned int i = 0; i < sz; i++)
137  {
138  //cout << "Doing the assignment in constructor\n";
139  v[i] = x[i];
140  }
141 }
146 void lvector::allocate(const lvector& lv)
147 {
148  allocate(lv.indexmin(),lv.indexmax());
149 }
156 void lvector::allocate(int ncl, int nch)
157 {
158  if ((shape = new vector_shape(ncl,nch)) == 0)
159  {
160  cerr << " Error trying to allocate memory for lvector\n";
161  }
162  v = new AD_LONG_INT[(size_t)(nch-ncl+1)];
163  if (v == 0)
164  {
165  cerr << " Error trying to allocate memory for lvector\n";
166  ad_exit(21);
167  }
168  v -= indexmin();
169 #ifndef OPT_LIB
170  initialize();
171 #endif
172 }
177 {
178  shape = NULL;
179  v = NULL;
180 }
187 lvector::lvector(int ncl, int nch)
188 {
189  allocate(ncl, nch);
190 }
196  {
197  if ((shape=new vector_shape(u.indexmin(),u.indexmax()))==0 )
198  {
199  cerr << " Error trying to allocate memory for lvector\n";
200  }
201  v = new AD_LONG_INT [(size_t) (size()) ];
202  if (v ==0)
203  {
204  cerr << " Error trying to allocate memory for lvector\n";
205  ad_exit(21);
206  }
207  v -= indexmin();
208  for ( int i=indexmin(); i<=indexmax(); i++)
209  {
210  elem(i)=u.elem(i);
211  }
212  }
217 {
218  //for (int i = indexmin(); i <= indexmax(); i++) { elem(i) = 0; }
219 #ifndef OPT_LIB
220  assert(size() > 0);
221 #endif
222  memset((void*)(v + indexmin()), 0,
223  sizeof(AD_LONG_INT) * (unsigned int)size());
224 }
int & elem(int i)
Definition: ivector.h:90
lvector(void)
Default constructor.
Definition: lvector.cpp:30
double & elem(int i)
Definition: dvector.h:152
AD_LONG_INT * v
Definition: fvar.hpp:658
unsigned int ncopies
Definition: fvar.hpp:522
Description not yet available.
Definition: fvar.hpp:509
~lvector()
Destructor.
Definition: lvector.cpp:46
int size() const
Definition: fvar.hpp:686
#define x
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
void initialize(void)
Intialize vector values to zero.
Definition: lvector.cpp:216
int indexmin() const
Definition: fvar.hpp:676
Description not yet available.
Definition: fvar.hpp:656
exitptr ad_exit
Definition: gradstrc.cpp:53
int indexmax() const
Definition: fvar.hpp:681
vector_shape * shape
Definition: fvar.hpp:659
AD_LONG_INT & elem(int i)
Definition: fvar.hpp:667
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
int indexmin() const
Definition: ivector.h:99
int indexmax() const
Definition: ivector.h:104
unsigned int size() const
Get number of elements in array.
Definition: dvector.h:209
#define AD_LONG_INT
Definition: fvar.hpp:81
lvector & operator=(const lvector &t)
Description not yet available.
Definition: lvector.cpp:99
void allocate(void)
Does not really allocate, but empties the array.
Definition: lvector.cpp:176