ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ddlist.cpp
Go to the documentation of this file.
1 
7 #include "fvar.hpp"
8 
9 #include <stdlib.h>
10 #ifndef OPT_LIB
11  #include <cassert>
12 #endif
13 
18 {
19  int on,nopt = 0;
20  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mdl",nopt))>-1)
21  {
22  if (nopt == 1)
23  {
24  int i = atoi(ad_comm::argv[on+1]);
25  if (i > 0)
26  gradient_structure::MAX_DLINKS = (unsigned int)i;
27  }
28  else
29  {
30  cerr << "Wrong number of options to -mdl -- must be 1"
31  " you have " << nopt << endl;
32  ad_exit(1);
33  }
34  }
35 
36  last = NULL;
37  nlinks = 0;
39  ddlist_space =
40  (char*)malloc(gradient_structure::MAX_DLINKS * sizeof(dlink));
42 
43 #ifndef OPT_LIB
44  //fails for insufficient memory to allocate space for dvariables save buffer
45  assert(variables_save != NULL);
46 #endif
47 
48  //Initialize addresses to zero
50 }
55 {
56  if (dlink_addresses)
57  {
58  delete [] dlink_addresses;
59  dlink_addresses = NULL;
60  }
61  if (ddlist_space)
62  {
63  ::free(ddlist_space);
64  ddlist_space = NULL;
65  }
66  if (variables_save)
67  {
68  delete [] variables_save;
69  variables_save = NULL;
70  }
71 }
76 {
77 #ifndef OPT_LIB
78  //If fails, then need to increase the maximum number of dlinks.
80 #endif
81 
82  dlink* link = (dlink*)(&ddlist_space[sizeof(dlink) * nlinks]);
83 
84 #ifndef OPT_LIB
85  assert(link);
86 #endif
87 
88  //Do not add to list.
89  link->prev = NULL;
90 
91  //Keep track of the links so you can zero them out (ie gradcalc).
92  dlink_addresses[nlinks] = link;
93  ++nlinks;
94 
95  return link;
96 }
103 {
104  dlink* link = last;
105  if (link)
106  {
107  last = link->prev;
108  link->prev = NULL;
109  }
110  return link;
111 }
118 {
119 #ifndef OPT_LIB
120  //Should fail if link is NULL.
121  assert(link);
122 #endif
123 
124  link->prev = last;
125  last = link;
126 
127  return last;
128 }
130 {
131  dlink** dest = dlink_addresses;
132  for (unsigned int i = 0; i < nlinks; ++i)
133  {
134  (*dest)->di.x = 0;
135  ++dest;
136  }
137 }
142 {
143  dlink** src = dlink_addresses;
144  double* dest = variables_save;
145  for (unsigned int i = 0; i < nlinks; ++i)
146  {
147  *dest = (*src)->di.x;
148  ++dest;
149  ++src;
150  }
151 }
156 {
157  dlink** dest = dlink_addresses;
158  double* src = variables_save;
159  for (unsigned int i = 0; i < nlinks; ++i)
160  {
161  (*dest)->di.x = *src;
162  ++dest;
163  ++src;
164  }
165 }
170 {
171  size_t total = 0;
172  for (unsigned int i = 0; i < gradient_structure::MAX_DLINKS; ++i)
173  {
174  if (dlink_addresses[i] != 0)
175  {
176  total++;
177  }
178  }
179  return total;
180 }
185 {
186  dlink* tmp_last=last;
187 
188  unsigned int count=0;
189  while(tmp_last && count <=nlinks)
190  {
191  count+=1;
192  if (count > nlinks)
193  {
194  cerr << "In check_list() number of links > number created\n";
195  cerr << " The number created was "<< nlinks << endl;
196  }
197 
198  dlink* tmp = tmp_last->prev;
199 
200 // cout << "last =" << _farptr_tolong(last) << "\n";
201 // cout << "last->prev =" << _farptr_tolong(last->prev) << "\n";
202 // cout << "deleted dlink with address" << _farptr_tolong(last) << "\n";
203 
204  tmp_last = tmp;
205  }
206  cerr << "In check_list() number of free links is " << count << endl;
207 }
void check_list(void)
Check link list integrity.
Definition: ddlist.cpp:184
double * variables_save
Definition: fvar.hpp:840
size_t total_addresses() const
Get total addresses stored.
Definition: ddlist.cpp:169
static char ** argv
Definition: fvar.hpp:8866
void save_variables()
Save variables to a buffer.
Definition: ddlist.cpp:141
~dlist()
Destructor.
Definition: ddlist.cpp:54
dlink * append(dlink *link)
Append link to list.
Definition: ddlist.cpp:117
exitptr ad_exit
Definition: gradstrc.cpp:53
dlink * last_remove()
If list is not empty, pop and return last node.
Definition: ddlist.cpp:102
void initialize()
Definition: ddlist.cpp:129
dlink * last
Definition: fvar.hpp:836
dlist()
Default constructor.
Definition: ddlist.cpp:17
int atoi(adstring &s)
Returns a integer converted from input s.
Definition: atoi.cpp:20
prnstream & endl(prnstream &)
static int argc
Definition: fvar.hpp:8863
unsigned int nlinks
Definition: fvar.hpp:837
dlink ** dlink_addresses
Definition: fvar.hpp:838
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
char * ddlist_space
Definition: fvar.hpp:839
void restore_variables()
Restore variables from buffer.
Definition: ddlist.cpp:155
int option_match(int argc, char *argv[], const char *string)
Checks if the program has been invoked with a particular command line argument (&quot;string&quot;).
Definition: optmatch.cpp:25
dlink * create()
Return new unlinked node.
Definition: ddlist.cpp:75
static unsigned int MAX_DLINKS
double x
&lt; value of the variable
Definition: fvar.hpp:195