00001 00007 #include "fvar.hpp" 00008 00009 #include <stdlib.h> 00010 #ifndef OPT_LIB 00011 #include <cassert> 00012 #endif 00013 00017 dlist::dlist() 00018 { 00019 int on,nopt = 0; 00020 if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mdl",nopt))>-1) 00021 { 00022 if (nopt == 1) 00023 { 00024 int i = atoi(ad_comm::argv[on+1]); 00025 if (i > 0) 00026 gradient_structure::MAX_DLINKS = (unsigned int)i; 00027 } 00028 else 00029 { 00030 cerr << "Wrong number of options to -mdl -- must be 1" 00031 " you have " << nopt << endl; 00032 ad_exit(1); 00033 } 00034 } 00035 last = 0; 00036 nlinks = 0; 00037 dlink_addresses = new dlink*[gradient_structure::MAX_DLINKS]; 00038 const size_t size = 2 * sizeof(double) * (gradient_structure::MAX_DLINKS + 1); 00039 ddlist_space = (char*)malloc(size * sizeof(char)); 00040 00041 variables_save = new double[gradient_structure::MAX_DLINKS]; 00042 00043 #ifndef OPT_LIB 00044 //fails for insufficient memory to allocate space for dvariables save buffer 00045 assert(variables_save != NULL); 00046 #endif 00047 00048 //Initialize addresses to zero 00049 memset(dlink_addresses, 0, sizeof(dlink*) * gradient_structure::MAX_DLINKS); 00050 } 00054 dlist::~dlist() 00055 { 00056 if (dlink_addresses) 00057 { 00058 delete [] dlink_addresses; 00059 dlink_addresses = NULL; 00060 } 00061 if (ddlist_space) 00062 { 00063 ::free(ddlist_space); 00064 ddlist_space = NULL; 00065 } 00066 if (variables_save) 00067 { 00068 delete [] variables_save; 00069 variables_save = NULL; 00070 } 00071 } 00075 dlink* dlist::create() 00076 { 00077 dlink* link = (dlink*)(ddlist_space+2*sizeof(double)*nlinks); 00078 #ifndef OPT_LIB 00079 assert(link); 00080 #endif 00081 00082 //do not add to list. 00083 link->prev=0; 00084 00085 #ifndef OPT_LIB 00086 //If fails, then need to increase the maximum number of dlinks. 00087 assert(nlinks <= gradient_structure::MAX_DLINKS); 00088 #endif 00089 00090 // keep track of the links so you can zero them out 00091 dlink_addresses[nlinks] = link; 00092 ++nlinks; 00093 00094 return link; 00095 } 00101 dlink* dlist::last_remove() 00102 { 00103 dlink* link = last; 00104 if (link) 00105 { 00106 last = link->prev; 00107 link->prev = NULL; 00108 } 00109 return link; 00110 } 00116 dlink* dlist::append(dlink* link) 00117 { 00118 #ifndef OPT_LIB 00119 //Should fail if link is NULL. 00120 assert(link); 00121 #endif 00122 00123 link->prev = last; 00124 last = link; 00125 00126 return last; 00127 } 00128 void dlist::initialize() 00129 { 00130 dlink** dest = dlink_addresses; 00131 for (unsigned int i = 0; i < nlinks; ++i) 00132 { 00133 (*dest)->di.x = 0; 00134 ++dest; 00135 } 00136 } 00140 void dlist::save_variables() 00141 { 00142 dlink** src = dlink_addresses; 00143 double* dest = variables_save; 00144 for (unsigned int i = 0; i < nlinks; ++i) 00145 { 00146 *dest = (*src)->di.x; 00147 ++dest; 00148 ++src; 00149 } 00150 } 00154 void dlist::restore_variables() 00155 { 00156 dlink** dest = dlink_addresses; 00157 double* src = variables_save; 00158 for (unsigned int i = 0; i < nlinks; ++i) 00159 { 00160 (*dest)->di.x = *src; 00161 ++dest; 00162 ++src; 00163 } 00164 } 00168 size_t dlist::total_addresses() const 00169 { 00170 size_t total = 0; 00171 for (unsigned int i = 0; i < gradient_structure::MAX_DLINKS; ++i) 00172 { 00173 if (dlink_addresses[i] != 0) 00174 { 00175 total++; 00176 } 00177 } 00178 return total; 00179 } 00183 void dlist::check_list(void) 00184 { 00185 dlink* tmp_last=last; 00186 00187 unsigned int count=0; 00188 while(tmp_last && count <=nlinks) 00189 { 00190 count+=1; 00191 if (count > nlinks) 00192 { 00193 cerr << "In check_list() number of links > number created\n"; 00194 cerr << " The number created was "<< nlinks << endl; 00195 } 00196 00197 dlink* tmp = tmp_last->prev; 00198 00199 // cout << "last =" << _farptr_tolong(last) << "\n"; 00200 // cout << "last->prev =" << _farptr_tolong(last->prev) << "\n"; 00201 // cout << "deleted dlink with address" << _farptr_tolong(last) << "\n"; 00202 00203 tmp_last = tmp; 00204 } 00205 cerr << "In check_list() number of free links is " << count << endl; 00206 }
Generated on Tue Mar 8 2016 19:51:31 for ADMB Documentation by 1.8.0 |