ADMB Documentation  11.1.2494
 All Classes Files Functions Variables Typedefs Friends Defines
dmat10.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: dmat10.cpp 2278 2014-09-03 20:25:08Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include "fvar.hpp"
00012 #include <string.h>
00013 #include <ctype.h>
00014 #include <cassert>
00015 #include <climits>
00016 
00021  struct dvec_ptr_ptr
00022  {
00023    void ** m;
00024  };
00025 
00026 const int MAXROWS = 5050;
00027 
00037 void dmatrix::fill(const char* s)
00038 {
00039   #ifdef DIAG
00040     myheapcheck("Entering dmatrix(const char * s)" );
00041   #endif
00042 
00043   size_t len = strlen(s);
00044   assert(len <= INT_MAX);
00045   int n = (int)len;
00046   int braces = 0;
00047   int nrow = 0;
00048   int ncol = 0;
00049   int mincolumn=colmin();
00050   int minrow=rowmin();
00051   rowshift(1);
00052   colshift(1);
00053 
00054   ivector columns(1, MAXROWS);
00055   ivector k1(1, MAXROWS);
00056   ivector k2(1, MAXROWS);
00057 
00058   for (int k = 0; k < n; k++)
00059   {
00060     if (s[k] == '{')
00061     {
00062       braces ++;
00063       if (braces != 1)
00064       {
00065         cerr << "Unbalanced braces in dmatrix::fill(const char * s)\n";
00066         cerr << "at character " << k << "\n";
00067         ad_exit(1);
00068       }
00069       ncol = 1;
00070       k1[nrow+1] = k;
00071     }
00072     else if (s[k] == '}')
00073     {
00074       braces --;
00075       if (braces != 0)
00076       {
00077         cerr << "Unbalanced braces in dmatrix::dmatrix(const char * s)\n";
00078         cerr << "at character " << k << "\n";
00079         ad_exit(1);
00080       }
00081       k2[nrow+1] = k;
00082       nrow ++;
00083       if (nrow > MAXROWS)
00084       {
00085         cerr << "Too many rows in dmatrix::dmatrix(const char * s)\n";
00086         ad_exit(1);
00087       }
00088       columns[nrow] = ncol;
00089     }
00090     else if (s[k] == ',')
00091     {
00092       if (braces != 0)
00093       {
00094         ncol++;
00095       }
00096     }
00097   }
00098 
00099   if (braces != 0)
00100   {
00101     cerr << "Unbalanced braces in dmatrix::dmatrix(const char * s)\n";
00102     cerr << s << "\n";
00103     ad_exit(1);
00104   }
00105 
00106   if (nrow != rowsize())
00107   {
00108     if (nrow < rowsize())
00109     {
00110       cerr << " Not enough rows in the data for dmatrix::fill(const char *) \n";
00111       ad_exit(1);
00112     }
00113     else
00114     {
00115       cerr << " Too many rows in the data for dmatrix::fill(const char *) \n";
00116       ad_exit(1);
00117     }
00118   }
00119 
00120   for (int i=1; i<=nrow; i++)
00121   {
00122     cout << "row  " << i << " matrix  "
00123        << ((*this)[rowmin()+i-1]).size()
00124        << "  colvector " << columns[i] << "\n";
00125 
00126     if ( ((*this)[rowmin()+i-1]).size() != columns[i])
00127     {
00128       if ( ((*this)[rowmin()+i-1]).size() > columns[i])
00129       {
00130         cerr << " Not enough columns in the data in row "
00131              << i << " for dmatrix::fill(const char *) \n";
00132         ad_exit(1);
00133       }
00134       else
00135       {
00136         cerr << " Too many columns in the data in row "
00137              << i << " for dmatrix::fill(const char *) \n";
00138         ad_exit(1);
00139       }
00140     }
00141   }
00142 
00143   for (int i=rowmin(); i<=rowmax(); i++)
00144   {
00145     char *t = new char[strlen(s)+1];
00146     //t = (char*) new[strlen(s)+1];
00147 
00148     for (int k = k1[i]; k <= k2[i]; k++)
00149     {
00150       t[k-k1[i]] = s[k];
00151     }
00152     t[k2[i]-k1[i]+1] = '\0';
00153 
00154     dvector tt(t);
00155     tt.shift(( (*this)(i)).indexmin() );
00156 
00157    //    (*this)(i)=dvector(tt);
00158     (*this)(i)=tt;
00159 
00160     delete[] t;
00161     t = 0;
00162   }
00163   rowshift(minrow);
00164   colshift(mincolumn);
00165 }