ADMB Documentation  11.1.1913
 All Classes Files Functions Variables Typedefs Friends Defines
string1.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: string1.cpp 1707 2014-02-28 20:46:24Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00007 #include <fvar.hpp>
00008 #include <string.h>
00009 #include <stdlib.h>
00010 #include "admb_messages.h"
00011 
00012 adstring adstring::operator()(int i, int j)
00013 {
00014   if (i < 1 || i > (int) shape->size())
00015   {
00016     ADMB_ARRAY_BOUNDS_ERROR("First index out of bounds",
00017     "adstring adstring::operator()(int i, int j)", 1, shape->size(), i);
00018   }
00019   if (i > j)
00020   {
00021     ADMB_ARRAY_BOUNDS_ERROR(
00022     "First index must be less than or equal to second index",
00023     "adstring adstring::operator()(int i, int j)", 1, shape->size(), j);
00024   }
00025   if (j > (int) shape->size())
00026   {
00027     ADMB_ARRAY_BOUNDS_ERROR("Second index out of bounds",
00028     "adstring adstring::operator()(int i, int j)", 1, shape->size(), j);
00029   }
00030   adstring tmp(1, size_t(j - i + 1));
00031   for (int ii = i; ii <= j; ii++)
00032   {
00033     tmp(ii - i + 1) = (* this) (ii);
00034   }
00035   return (tmp);
00036 }
00037 
00038 adstring adstring::operator()(int i, int j) const
00039 {
00040   if (i < 1 || i > (int) shape->size())
00041   {
00042     ADMB_ARRAY_BOUNDS_ERROR("First index out of bounds",
00043     "adstring adstring::operator()(int i, int j)", 1, shape->size(), i);
00044   }
00045   if (i > j)
00046   {
00047     ADMB_ARRAY_BOUNDS_ERROR(
00048     "First index must be less than or equal to second index",
00049     "adstring adstring::operator()(int i, int j)", 1, shape->size(), j);
00050   }
00051   if (j > (int) shape->size())
00052   {
00053     ADMB_ARRAY_BOUNDS_ERROR("Second index out of bounds",
00054     "adstring adstring::operator()(int i, int j)", 1, shape->size(), j);
00055   }
00056   adstring tmp(1, size_t(j - i + 1));
00057   for (int ii = i; ii <= j; ii++)
00058   {
00059     tmp(ii - i + 1) = (* this) (ii);
00060   }
00061   return (tmp);
00062 }
00063 
00064 adstring& adstring::operator=(const adstring & t)
00065 {
00066   if (s != t.s)
00067   {
00068     int sz = t.size ();
00069     shape->size() = t.size();
00070     delete [] ++s;
00071     s=new unsigned char[sz+1];
00072     s--;
00073     int size = t.size();
00074     for (int i = 1; i <= size; i++)
00075     {
00076       s[i] = t[i];
00077     }
00078     s[sz + 1] = '\0';
00079     adstring * tmp = (adstring *) this->next;
00080     while (tmp != this)
00081     {
00082       tmp->shape = shape;
00083       tmp->s = s;
00084       tmp = (adstring *) tmp->next;
00085     }
00086   }
00087   return (* this);
00088 }
00089 
00090 void adstring::realloc(const char * t)
00091 {
00092   int sz = strlen(t);
00093   shape->size() = strlen(t);
00094   delete [] ++s;
00095   s=new unsigned char[sz+1];
00096   strcpy((char*)(s),t);
00097   s--;
00098   adstring * tmp = (adstring *) this->next;
00099   while (tmp != this)
00100   {
00101     tmp->shape = shape;
00102     tmp->s = s;
00103     tmp = (adstring *) tmp->next;
00104   }
00105 }
00106 
00110 adstring::~adstring()
00111 {
00112   if (next==this)
00113   {
00114     deallocate();
00115   }
00116 }
00117 
00118 unsigned char & adstring::operator()(const int i)
00119 {
00120   if (i < 1 || i > (int) shape->size())
00121   {
00122     ADMB_ARRAY_BOUNDS_ERROR("Index out of bounds",
00123     "unsigned char & adstring::operator()(int i)", 1, shape->size(), i);
00124   }
00125   return ( ((unsigned char *)s)[i]);
00126 }
00127 
00128 const unsigned char& adstring::operator()(const int i) const
00129 {
00130   if (i < 1 || i > (int) shape->size())
00131   {
00132     ADMB_ARRAY_BOUNDS_ERROR("Index out of bounds",
00133     "unsigned char & adstring::operator()(int i) const", 1, shape->size(), i);
00134   }
00135   return (s[i]);
00136 }
00137 
00138 const unsigned char& adstring::operator[](const int i) const
00139 {
00140   if (i < 1 || i > (int) shape->size())
00141   {
00142     ADMB_ARRAY_BOUNDS_ERROR("Index out of bounds",
00143     "unsigned char & adstring::operator[](int i) const ", 1, shape->size(), i);
00144   }
00145   return (s[i]);
00146 }
00147 
00148 int adstring::operator==(const adstring& v) const
00149 {
00150   int tmp = strcmp (* this, v);
00151   return (tmp == 0);
00152 }
00153 
00154 int adstring::operator==(const adstring& v)
00155 {
00156   int tmp = strcmp (* this, v);
00157   return (tmp == 0);
00158 }
00159 /*
00160 int adstring::operator!=(const adstring& v)
00161 {
00162   int tmp = strcmp (* this, v);
00163   return (tmp != 0);
00164 }
00165 */
00166 adstring& adstring::operator+=(const adstring& v)
00167 {
00168   int us = size ();
00169   int vs = v.size ();
00170   int bs = buff_size();
00171   if (bs > us + vs)
00172   {
00173     for (int i = 1; i <= vs; i++)
00174     {
00175       s[i + us] = v(i);
00176     }
00177   }
00178   else
00179   {
00180     adstring tmp(1, us + vs);
00181     int i;
00182     for (i = 1; i <= us; i++)
00183     {
00184       tmp(i) = s[i];
00185     }
00186     for (i = 1; i <= vs; i++)
00187     {
00188       tmp(i + us) = v(i);
00189     }
00190     * this = tmp;
00191   }
00192   return (* this);
00193 }
00194 
00195 unsigned char& adstring::operator[](const int i)
00196 {
00197   if (i < 1 || i > (int) shape->size())
00198   {
00199     cerr << "Index out of bounds in adstring::operator () (const int)\n"
00200     << "Index value was " << i << " The size of this adstring is "
00201     << shape->size() << "\n";
00202   }
00203  // return (s[i]);
00204   return ( ((unsigned char *)s)[i]);
00205 }
00206 
00207 int length(const adstring& t)
00208 {
00209   return (int(t.size()));
00210 }