ADMB Documentation  11.1x.2737
 All Classes Files Functions Variables Typedefs Friends Defines
ddlist.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: ddlist.cpp 2653 2014-11-14 20:12:46Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 
00012 #include "fvar.hpp"
00013 
00014 #ifdef __ZTC__
00015    #include <iostream.hpp>
00016 #endif
00017 
00018 #ifdef __TURBOC__
00019    #pragma hdrstop
00020    #include <iostream.h>
00021 #endif
00022 
00023 #include <stdlib.h>
00024 #if defined(__x86_64)
00025   #include <stdint.h>
00026 #endif
00027 //#define MAX_DLINKS 1000
00031 dlink* dlink::previous()
00032 {
00033   return prev;
00034 }
00035 
00037 // global list names
00038 //dlist * GRAD_LIST;
00039 //extern dlist * GRAD_LIST; //js
00041 
00042 char* ddlist_space;
00043 char* ddlist_spacea;
00044 
00048 dlist::dlist(void)
00049 {
00050   int on,nopt = 0;
00051   if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mdl",nopt))>-1)
00052   {
00053     if (nopt ==1)
00054     {
00055       int i = atoi(ad_comm::argv[on+1]);
00056       if (i > 0)
00057         gradient_structure::MAX_DLINKS = (unsigned int)i;
00058     }
00059     else
00060     {
00061       cerr << "Wrong number of options to -mdl -- must be 1"
00062         " you have " << nopt << endl;
00063       ad_exit(1);
00064     }
00065   }
00066   last = 0;
00067   last_offset = 0;
00068   nlinks = 0;
00069   dlink_addresses = new dlink*[gradient_structure::MAX_DLINKS];
00070   const size_t size = 2 * sizeof(double) * (gradient_structure::MAX_DLINKS + 1);
00071   ddlist_space = (char*)malloc(size * sizeof(char));
00072   //cout << (int) (ddlist_space) << endl;
00073   //cout << ((int) (ddlist_space))%8 << endl;
00074 #if defined(__x86_64)
00075   intptr_t adjust=(8 - ((intptr_t)(ddlist_space))%8)%8;
00076 #else
00077   int adjust=(8- ((int) (ddlist_space))%8)%8;
00078 #endif
00079 
00080   ddlist_spacea=ddlist_space+adjust;
00081 
00082   //Initialize addresses to zero
00083   memset(dlink_addresses, 0, sizeof(dlink*) * gradient_structure::MAX_DLINKS);
00084 }
00088 dlink* dlist::create()
00089 {
00090   dlink* tmp= (dlink*)(ddlist_spacea+2*sizeof(double)*nlinks);
00091   // cout << "Made a dlink with address " << _farptr_tolong(tmp) <<"\n";
00092 
00093   if (!tmp)
00094   {
00095     cerr << "Error allocating dlink in dlist::create()\n";
00096     ad_exit(21);
00097   }
00098   else
00099   {
00100     // keep track of the links so you can zero them out
00101     dlink_addresses[nlinks]=tmp;
00102     nlinks+=1;
00103 
00104     if (nlinks > gradient_structure::MAX_DLINKS)
00105     {
00106       cerr << "Need to increase the maximum number of dlinks" << endl;
00107       ad_exit(1);
00108     }
00109 
00110     //do not add to list.
00111     tmp->prev=0;
00112   }
00113 
00114   return tmp;
00115 }
00121 dlink* dlist::last_remove()
00122 {
00123   if (last)
00124   {
00125     dlink* tmp = last;
00126     last = last->prev;
00127     return tmp;
00128   }
00129   return 0;
00130 }
00134 dlist::~dlist()
00135 {
00136 /*
00137   dlink * tmp;
00138 //   cout << "used the dlist destructor\n";
00139 //   cout << "entered ~dlist   last =" << _farptr_tolong(last) << "\n";
00140 
00141   unsigned int count=0;
00142 
00143   while(last)
00144   {
00145     count+=1;
00146     tmp=last->prev;
00147 
00148 //  cout << "last =" << _farptr_tolong(last) << "\n";
00149 //  cout << "last->prev =" << _farptr_tolong(last->prev) << "\n";
00150 //  cout << "deleted dlink with address" << _farptr_tolong(last) << "\n";
00151 
00152     //delete last;
00153     last=tmp;
00154   }
00155   if (count != nlinks)
00156   {
00157     cerr << "In ~dlist() number of links destroyed not equal to number created\n";
00158     cerr << " The number created was "<< nlinks << " The number destroyed was "
00159          << count << "\n";
00160     ad_exit(1);
00161   }
00162 */
00163   ::free(ddlist_space);
00164   ddlist_space=NULL;
00165 
00166   delete [] dlink_addresses;
00167   dlink_addresses = 0;
00168 }
00172 size_t dlist::total_addresses() const
00173 {
00174   size_t total = 0;
00175   for (unsigned int i = 0; i < gradient_structure::MAX_DLINKS; ++i)
00176   {
00177     if (dlink_addresses[i] != 0)
00178     {
00179       total++;
00180     }
00181   }
00182   return total;
00183 }
00187 void dlist::check_list(void)
00188 {
00189   dlink* tmp_last=last;
00190 
00191   unsigned int count=0;
00192   while(tmp_last && count <=nlinks)
00193   {
00194     count+=1;
00195     if (count > nlinks)
00196     {
00197       cerr << "In check_list() number of links > number created\n";
00198       cerr << " The number created was "<< nlinks << endl;
00199     }
00200 
00201     dlink* tmp = tmp_last->prev;
00202 
00203 //  cout << "last =" << _farptr_tolong(last) << "\n";
00204 //  cout << "last->prev =" << _farptr_tolong(last->prev) << "\n";
00205 //  cout << "deleted dlink with address" << _farptr_tolong(last) << "\n";
00206 
00207     tmp_last = tmp;
00208   }
00209   cerr << "In check_list() number of free links is " << count << endl;
00210 }
00211 
00216 dlink* dlist::append(dlink* app)
00217 {
00218   if (!app)
00219   {
00220     cerr << "Error: NULL pointer passed to  dlist::append()\n";
00221     ad_exit(1);
00222   }
00223   else
00224   {
00225     app->prev = last;
00226     last = app;
00227   }
00228 
00229   return last;
00230 }