ADMB Documentation  11.1x.2732
 All Classes Files Functions Variables Typedefs Friends Defines
string1.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: string1.cpp 2709 2014-11-21 21:19:47Z 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       if (tmp)
00066       {
00067         tmp->shape = shape;
00068         tmp->s = s;
00069         tmp = (adstring*)tmp->next;
00070       }
00071     }
00072   }
00073   return (* this);
00074 }
00075 
00076 void adstring::realloc(const char* t)
00077 {
00078   size_t sz = strlen(t);
00079   shape->size() = sz;
00080   ++s;
00081   delete [] s;
00082   s=new unsigned char[sz+1];
00083   strcpy((char*)(s),t);
00084   s--;
00085   adstring* tmp = (adstring*)this->next;
00086   while (tmp != this)
00087   {
00088     if (tmp)
00089     {
00090       tmp->shape = shape;
00091       tmp->s = s;
00092       tmp = (adstring *) tmp->next;
00093     }
00094   }
00095 }
00096 
00100 adstring::~adstring()
00101 {
00102   if (next==this)
00103   {
00104     deallocate();
00105   }
00106 }
00107 
00108 unsigned char & adstring::operator()(const size_t i)
00109 {
00110 /*
00111   if (i < 1 || i > shape->size())
00112   {
00113     ADMB_ARRAY_BOUNDS_ERROR("Index out of bounds",
00114     "unsigned char & adstring::operator()(int i)", 1, shape->size(), i);
00115   }
00116 */
00117   return ( ((unsigned char *)s)[i]);
00118 }
00119 
00120 const unsigned char& adstring::operator()(const size_t i) const
00121 {
00122 /*
00123   if (i < 1 || i > shape->size())
00124   {
00125     ADMB_ARRAY_BOUNDS_ERROR("Index out of bounds",
00126     "unsigned char & adstring::operator()(int i) const", 1, shape->size(), i);
00127   }
00128 */
00129   return (s[i]);
00130 }
00131 
00132 const unsigned char& adstring::operator[](const size_t i) const
00133 {
00134 /*
00135   if (i < 1 || i > shape->size())
00136   {
00137     ADMB_ARRAY_BOUNDS_ERROR("Index out of bounds",
00138     "unsigned char & adstring::operator[](int i) const ", 1, shape->size(), i);
00139   }
00140 */
00141   return (s[i]);
00142 }
00143 
00144 int adstring::operator==(const adstring& v) const
00145 {
00146   int tmp = strcmp (* this, v);
00147   return (tmp == 0);
00148 }
00149 
00150 int adstring::operator==(const adstring& v)
00151 {
00152   int tmp = strcmp (* this, v);
00153   return (tmp == 0);
00154 }
00155 /*
00156 int adstring::operator!=(const adstring& v)
00157 {
00158   int tmp = strcmp (* this, v);
00159   return (tmp != 0);
00160 }
00161 */
00162 adstring& adstring::operator+=(const adstring& v)
00163 {
00164   size_t us = size();
00165   size_t vs = v.size ();
00166   size_t bs = buff_size();
00167   if (bs > us + vs)
00168   {
00169     for (size_t i = 1; i <= vs; i++)
00170     {
00171       s[i + us] = v(i);
00172     }
00173   }
00174   else
00175   {
00176     adstring tmp(1, us + vs);
00177     for (size_t i = 1; i <= us; i++)
00178     {
00179       tmp(i) = s[i];
00180     }
00181     for (size_t i = 1; i <= vs; i++)
00182     {
00183       tmp(i + us) = v(i);
00184     }
00185     * this = tmp;
00186   }
00187   return (* this);
00188 }
00189 
00190 unsigned char& adstring::operator[](const size_t i)
00191 {
00192   if (i < 1 || i > shape->size())
00193   {
00194     cerr << "Index out of bounds in adstring::operator () (const int)\n"
00195     << "Index value was " << i << " The size of this adstring is "
00196     << shape->size() << "\n";
00197   }
00198  // return (s[i]);
00199   return ( ((unsigned char *)s)[i]);
00200 }
00201 
00202 size_t length(const adstring& t)
00203 {
00204   return t.size();
00205 }