ADMB Documentation  11.1.2542
 All Classes Files Functions Variables Typedefs Friends Defines
string1.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: string1.cpp 2421 2014-09-28 03:59:00Z 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 
00011 #ifndef OPT_LIB
00012   #include <cassert>
00013 #endif
00014 
00015 adstring adstring::operator()(const size_t i, const size_t j)
00016 {
00017 #ifndef OPT_LIB
00018   assert(1 <= i && i <= shape->size());
00019   assert(i <= j);
00020   assert(j <= shape->size());
00021 #endif
00022 
00023   adstring tmp(1, j - i + 1);
00024   for (size_t ii = i; ii <= j; ii++)
00025   {
00026     tmp(ii - i + 1) = (* this) (ii);
00027   }
00028   return tmp;
00029 }
00030 
00031 adstring adstring::operator()(const size_t i, const size_t j) const
00032 {
00033 #ifndef OPT_LIB
00034   assert(1 <= i && i <= shape->size());
00035   assert(i <= j);
00036   assert(j <= shape->size());
00037 #endif
00038 
00039   adstring tmp(1, j - i + 1);
00040   for (size_t ii = i; ii <= j; ii++)
00041   {
00042     tmp(ii - i + 1) = (* this) (ii);
00043   }
00044   return tmp;
00045 }
00046 
00047 adstring& adstring::operator=(const adstring & t)
00048 {
00049   if (s != t.s)
00050   {
00051     size_t sz = t.size();
00052     shape->size() = sz;
00053     delete [] ++s;
00054     s=new unsigned char[sz+1];
00055     s--;
00056     size_t size = t.size();
00057     for (size_t i = 1; i <= size; i++)
00058     {
00059       s[i] = t[i];
00060     }
00061     s[sz + 1] = '\0';
00062     adstring * tmp = (adstring *) this->next;
00063     while (tmp != this)
00064     {
00065       tmp->shape = shape;
00066       tmp->s = s;
00067       tmp = (adstring *) tmp->next;
00068     }
00069   }
00070   return (* this);
00071 }
00072 
00073 void adstring::realloc(const char* t)
00074 {
00075   size_t sz = strlen(t);
00076   shape->size() = sz;
00077   delete [] ++s;
00078   s=new unsigned char[sz+1];
00079   strcpy((char*)(s),t);
00080   s--;
00081   adstring * tmp = (adstring *) this->next;
00082   while (tmp != this)
00083   {
00084     tmp->shape = shape;
00085     tmp->s = s;
00086     tmp = (adstring *) tmp->next;
00087   }
00088 }
00089 
00093 adstring::~adstring()
00094 {
00095   if (next==this)
00096   {
00097     deallocate();
00098   }
00099 }
00100 
00101 unsigned char & adstring::operator()(const size_t i)
00102 {
00103 /*
00104   if (i < 1 || i > shape->size())
00105   {
00106     ADMB_ARRAY_BOUNDS_ERROR("Index out of bounds",
00107     "unsigned char & adstring::operator()(int i)", 1, shape->size(), i);
00108   }
00109 */
00110   return ( ((unsigned char *)s)[i]);
00111 }
00112 
00113 const unsigned char& adstring::operator()(const size_t i) const
00114 {
00115 /*
00116   if (i < 1 || i > shape->size())
00117   {
00118     ADMB_ARRAY_BOUNDS_ERROR("Index out of bounds",
00119     "unsigned char & adstring::operator()(int i) const", 1, shape->size(), i);
00120   }
00121 */
00122   return (s[i]);
00123 }
00124 
00125 const unsigned char& adstring::operator[](const size_t i) const
00126 {
00127 /*
00128   if (i < 1 || i > shape->size())
00129   {
00130     ADMB_ARRAY_BOUNDS_ERROR("Index out of bounds",
00131     "unsigned char & adstring::operator[](int i) const ", 1, shape->size(), i);
00132   }
00133 */
00134   return (s[i]);
00135 }
00136 
00137 int adstring::operator==(const adstring& v) const
00138 {
00139   int tmp = strcmp (* this, v);
00140   return (tmp == 0);
00141 }
00142 
00143 int adstring::operator==(const adstring& v)
00144 {
00145   int tmp = strcmp (* this, v);
00146   return (tmp == 0);
00147 }
00148 /*
00149 int adstring::operator!=(const adstring& v)
00150 {
00151   int tmp = strcmp (* this, v);
00152   return (tmp != 0);
00153 }
00154 */
00155 adstring& adstring::operator+=(const adstring& v)
00156 {
00157   size_t us = size();
00158   size_t vs = v.size ();
00159   size_t bs = buff_size();
00160   if (bs > us + vs)
00161   {
00162     for (size_t i = 1; i <= vs; i++)
00163     {
00164       s[i + us] = v(i);
00165     }
00166   }
00167   else
00168   {
00169     adstring tmp(1, us + vs);
00170     for (size_t i = 1; i <= us; i++)
00171     {
00172       tmp(i) = s[i];
00173     }
00174     for (size_t i = 1; i <= vs; i++)
00175     {
00176       tmp(i + us) = v(i);
00177     }
00178     * this = tmp;
00179   }
00180   return (* this);
00181 }
00182 
00183 unsigned char& adstring::operator[](const size_t i)
00184 {
00185   if (i < 1 || i > shape->size())
00186   {
00187     cerr << "Index out of bounds in adstring::operator () (const int)\n"
00188     << "Index value was " << i << " The size of this adstring is "
00189     << shape->size() << "\n";
00190   }
00191  // return (s[i]);
00192   return ( ((unsigned char *)s)[i]);
00193 }
00194 
00195 size_t length(const adstring& t)
00196 {
00197   return t.size();
00198 }