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