ADMB Documentation  11.1.2274
 All Classes Files Functions Variables Typedefs Friends Defines
ddlist.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: ddlist.cpp 1711 2014-02-28 22:46:44Z 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 //#define MAX_DLINKS 1000
00028 dlink* dlink::previous()
00029 {
00030   return prev;
00031 }
00032 
00034 // global list names
00035 //dlist * GRAD_LIST;
00036 //extern dlist * GRAD_LIST; //js
00038 
00039 char* ddlist_space;
00040 char* ddlist_spacea;
00041 
00045 dlist::dlist(void)
00046 {
00047   int on,nopt = 0;
00048   if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mdl",nopt))>-1)
00049   {
00050     if (nopt ==1)
00051     {
00052       gradient_structure::MAX_DLINKS=atoi(ad_comm::argv[on+1]);
00053     }
00054     else
00055     {
00056       cerr << "Wrong number of options to -mdl -- must be 1"
00057         " you have " << nopt << endl;
00058       ad_exit(1);
00059     }
00060   }
00061   last = 0;
00062   last_offset = 0;
00063   nlinks = 0;
00064   dlink_addresses = new dlink*[gradient_structure::MAX_DLINKS];
00065   ddlist_space =
00066     (char*)malloc(2 * sizeof(double) * (gradient_structure::MAX_DLINKS + 1));
00067   //cout << (int) (ddlist_space) << endl;
00068   //cout << ((int) (ddlist_space))%8 << endl;
00069 #if defined(__x86_64)
00070   intptr_t adjust=(8 - ((intptr_t)(ddlist_space))%8)%8;
00071 #else
00072   int adjust=(8- ((int) (ddlist_space))%8)%8;
00073 #endif
00074 
00075   ddlist_spacea=ddlist_space+adjust;
00076 
00077   //Initialize addresses to zero
00078   memset(dlink_addresses, 0, sizeof(dlink*) * gradient_structure::MAX_DLINKS);
00079 }
00083 dlink* dlist::create()
00084 {
00085   dlink* tmp= (dlink*)(ddlist_spacea+2*sizeof(double)*nlinks);
00086   if (tmp == 0)
00087   {
00088     cerr << "Error allocating dlink in dlist::create()\n";
00089     ad_exit(21);
00090   }
00091   // cout << "Made a dlink with address " << _farptr_tolong(tmp) <<"\n";
00092 
00093   // keep track of the links so you can zero them out
00094   dlink_addresses[nlinks]=tmp;
00095   nlinks+=1;
00096 
00097   if (nlinks > gradient_structure::MAX_DLINKS)
00098   {
00099     cerr << "Need to increase the maximum number of dlinks" << endl;
00100     ad_exit(1);
00101   }
00102 
00103   //do not add to list.
00104   tmp->prev=0;
00105 
00106   return tmp;
00107 }
00113 dlink* dlist::last_remove()
00114 {
00115   if (last)
00116   {
00117     dlink* tmp = last;
00118     last = last->prev;
00119     return tmp;
00120   }
00121   return 0;
00122 }
00126 dlist::~dlist()
00127 {
00128 /*
00129   dlink * tmp;
00130 //   cout << "used the dlist destructor\n";
00131 //   cout << "entered ~dlist   last =" << _farptr_tolong(last) << "\n";
00132 
00133   unsigned int count=0;
00134 
00135   while(last)
00136   {
00137     count+=1;
00138     tmp=last->prev;
00139 
00140 //  cout << "last =" << _farptr_tolong(last) << "\n";
00141 //  cout << "last->prev =" << _farptr_tolong(last->prev) << "\n";
00142 //  cout << "deleted dlink with address" << _farptr_tolong(last) << "\n";
00143 
00144     //delete last;
00145     last=tmp;
00146   }
00147   if (count != nlinks)
00148   {
00149     cerr << "In ~dlist() number of links destroyed not equal to number created\n";
00150     cerr << " The number created was "<< nlinks << " The number destroyed was "
00151          << count << "\n";
00152     ad_exit(1);
00153   }
00154 */
00155   ::free(ddlist_space);
00156   ddlist_space=NULL;
00157 
00158   delete [] dlink_addresses;
00159   dlink_addresses = 0;
00160 }
00164 size_t dlist::total_addresses() const
00165 {
00166   size_t total = 0;
00167   for (unsigned int i = 0; i < gradient_structure::MAX_DLINKS; ++i)
00168   {
00169     if (dlink_addresses[i] != 0)
00170     {
00171       total++;
00172     }
00173   }
00174   return total;
00175 }
00179 void dlist::check_list(void)
00180 {
00181   dlink* tmp_last=last;
00182 
00183   unsigned int count=0;
00184   while(tmp_last && count <=nlinks)
00185   {
00186     count+=1;
00187     if (count > nlinks)
00188     {
00189       cerr << "In check_list() number of links > number created\n";
00190       cerr << " The number created was "<< nlinks << endl;
00191     }
00192 
00193     dlink* tmp = tmp_last->prev;
00194 
00195 //  cout << "last =" << _farptr_tolong(last) << "\n";
00196 //  cout << "last->prev =" << _farptr_tolong(last->prev) << "\n";
00197 //  cout << "deleted dlink with address" << _farptr_tolong(last) << "\n";
00198 
00199     tmp_last = tmp;
00200   }
00201   cerr << "In check_list() number of free links is " << count << endl;
00202 }
00207 dlink* dlist::append(dlink* app)
00208 {
00209   if (app == 0)
00210   {
00211     cerr << "NULL pointer passed to  dlist::append()\n";
00212     ad_exit(1);
00213   }
00214 
00215   dlink* tmp = last;
00216   last = app;
00217   last->prev = tmp;
00218 
00219   return last;
00220 }