ADMB Documentation  11.1.2500
 All Classes Files Functions Variables Typedefs Friends Defines
ddlist.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: ddlist.cpp 2495 2014-10-23 04:29:16Z 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       gradient_structure::MAX_DLINKS=atoi(ad_comm::argv[on+1]);
00056     }
00057     else
00058     {
00059       cerr << "Wrong number of options to -mdl -- must be 1"
00060         " you have " << nopt << endl;
00061       ad_exit(1);
00062     }
00063   }
00064   last = 0;
00065   last_offset = 0;
00066   nlinks = 0;
00067   dlink_addresses = new dlink*[gradient_structure::MAX_DLINKS];
00068   ddlist_space =
00069     (char*)malloc(2 * sizeof(double) * (gradient_structure::MAX_DLINKS + 1));
00070   //cout << (int) (ddlist_space) << endl;
00071   //cout << ((int) (ddlist_space))%8 << endl;
00072 #if defined(__x86_64)
00073   intptr_t adjust=(8 - ((intptr_t)(ddlist_space))%8)%8;
00074 #else
00075   int adjust=(8- ((int) (ddlist_space))%8)%8;
00076 #endif
00077 
00078   ddlist_spacea=ddlist_space+adjust;
00079 
00080   //Initialize addresses to zero
00081   memset(dlink_addresses, 0, sizeof(dlink*) * gradient_structure::MAX_DLINKS);
00082 }
00086 dlink* dlist::create()
00087 {
00088   dlink* tmp= (dlink*)(ddlist_spacea+2*sizeof(double)*nlinks);
00089   if (tmp == 0)
00090   {
00091     cerr << "Error allocating dlink in dlist::create()\n";
00092     ad_exit(21);
00093   }
00094   // cout << "Made a dlink with address " << _farptr_tolong(tmp) <<"\n";
00095 
00096   // keep track of the links so you can zero them out
00097   dlink_addresses[nlinks]=tmp;
00098   nlinks+=1;
00099 
00100   if (nlinks > gradient_structure::MAX_DLINKS)
00101   {
00102     cerr << "Need to increase the maximum number of dlinks" << endl;
00103     ad_exit(1);
00104   }
00105 
00106   //do not add to list.
00107   tmp->prev=0;
00108 
00109   return tmp;
00110 }
00116 dlink* dlist::last_remove()
00117 {
00118   if (last)
00119   {
00120     dlink* tmp = last;
00121     last = last->prev;
00122     return tmp;
00123   }
00124   return 0;
00125 }
00129 dlist::~dlist()
00130 {
00131 /*
00132   dlink * tmp;
00133 //   cout << "used the dlist destructor\n";
00134 //   cout << "entered ~dlist   last =" << _farptr_tolong(last) << "\n";
00135 
00136   unsigned int count=0;
00137 
00138   while(last)
00139   {
00140     count+=1;
00141     tmp=last->prev;
00142 
00143 //  cout << "last =" << _farptr_tolong(last) << "\n";
00144 //  cout << "last->prev =" << _farptr_tolong(last->prev) << "\n";
00145 //  cout << "deleted dlink with address" << _farptr_tolong(last) << "\n";
00146 
00147     //delete last;
00148     last=tmp;
00149   }
00150   if (count != nlinks)
00151   {
00152     cerr << "In ~dlist() number of links destroyed not equal to number created\n";
00153     cerr << " The number created was "<< nlinks << " The number destroyed was "
00154          << count << "\n";
00155     ad_exit(1);
00156   }
00157 */
00158   ::free(ddlist_space);
00159   ddlist_space=NULL;
00160 
00161   delete [] dlink_addresses;
00162   dlink_addresses = 0;
00163 }
00167 size_t dlist::total_addresses() const
00168 {
00169   size_t total = 0;
00170   for (unsigned int i = 0; i < gradient_structure::MAX_DLINKS; ++i)
00171   {
00172     if (dlink_addresses[i] != 0)
00173     {
00174       total++;
00175     }
00176   }
00177   return total;
00178 }
00182 void dlist::check_list(void)
00183 {
00184   dlink* tmp_last=last;
00185 
00186   unsigned int count=0;
00187   while(tmp_last && count <=nlinks)
00188   {
00189     count+=1;
00190     if (count > nlinks)
00191     {
00192       cerr << "In check_list() number of links > number created\n";
00193       cerr << " The number created was "<< nlinks << endl;
00194     }
00195 
00196     dlink* tmp = tmp_last->prev;
00197 
00198 //  cout << "last =" << _farptr_tolong(last) << "\n";
00199 //  cout << "last->prev =" << _farptr_tolong(last->prev) << "\n";
00200 //  cout << "deleted dlink with address" << _farptr_tolong(last) << "\n";
00201 
00202     tmp_last = tmp;
00203   }
00204   cerr << "In check_list() number of free links is " << count << endl;
00205 }
00210 dlink* dlist::append(dlink* app)
00211 {
00212   if (app == 0)
00213   {
00214     cerr << "NULL pointer passed to  dlist::append()\n";
00215     ad_exit(1);
00216   }
00217 
00218   dlink* tmp = last;
00219   last = app;
00220   last->prev = tmp;
00221 
00222   return last;
00223 }