ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
string1.cpp
Go to the documentation of this file.
1 
8 #include <fvar.hpp>
9 #include <string.h>
10 #include <stdlib.h>
11 
12 #ifndef OPT_LIB
13  #include <cassert>
14 #endif
15 
16 adstring adstring::operator()(const size_t i, const size_t j)
17 {
18 #ifndef OPT_LIB
19  assert(1 <= i && i <= shape->size());
20  assert(i <= j);
21  assert(j <= shape->size());
22 #endif
23 
24  adstring tmp(1, j - i + 1);
25  for (size_t ii = i; ii <= j; ii++)
26  {
27  tmp(ii - i + 1) = (* this) (ii);
28  }
29  return tmp;
30 }
31 
32 adstring adstring::operator()(const size_t i, const size_t j) const
33 {
34 #ifndef OPT_LIB
35  assert(1 <= i && i <= shape->size());
36  assert(i <= j);
37  assert(j <= shape->size());
38 #endif
39 
40  adstring tmp(1, j - i + 1);
41  for (size_t ii = i; ii <= j; ii++)
42  {
43  tmp(ii - i + 1) = (* this) (ii);
44  }
45  return tmp;
46 }
47 
49 {
50  if (s != t.s)
51  {
52  size_t sz = t.size();
53  shape->size() = sz;
54  delete [] ++s;
55  s=new unsigned char[sz+1];
56  s--;
57  size_t size = t.size();
58  for (size_t i = 1; i <= size; i++)
59  {
60  s[i] = t[i];
61  }
62  s[sz + 1] = '\0';
63  adstring* tmp = (adstring*)this->next;
64  while (tmp != this)
65  {
66  if (tmp)
67  {
68  tmp->shape = shape;
69  tmp->s = s;
70  tmp = (adstring*)tmp->next;
71  }
72  }
73  }
74  return (* this);
75 }
76 
77 void adstring::realloc(const char* t)
78 {
79  size_t sz = strlen(t);
80  shape->size() = sz;
81  ++s;
82  delete [] s;
83  s=new unsigned char[sz+1];
84  strcpy((char*)(s),t);
85  s--;
86  adstring* tmp = (adstring*)this->next;
87  while (tmp != this)
88  {
89  if (tmp)
90  {
91  tmp->shape = shape;
92  tmp->s = s;
93  tmp = (adstring *) tmp->next;
94  }
95  }
96 }
99 {
100  if (next == this)
101  {
102  deallocate();
103  }
104 }
110 unsigned char& adstring::operator()(const size_t i)
111 {
112  if (i < 1 || (shape && i > shape->size()))
113  {
114  cerr << "Index out of bounds in adstring::operator()(const size_t)\n"
115  << "Index value was " << i << '\n'
116  << "The size of this adstring is " << shape->size() << '\n';
117  ad_exit(1);
118  }
119  return s[i];
120 }
126 const unsigned char& adstring::operator()(const size_t i) const
127 {
128  if (i < 1 || (shape && i > shape->size()))
129  {
130  cerr << "Index out of bounds in adstring::operator()const size_t) const\n"
131  << "Index value was " << i << '\n'
132  << "The size of this adstring is " << shape->size() << '\n';
133  ad_exit(1);
134  }
135  return s[i];
136 }
142 unsigned char& adstring::operator[](const size_t i) const
143 {
144  if (i < 1 || (shape && i > shape->size()))
145  {
146  cerr << "Index out of bounds in adstring::operator[](const size_t) const\n"
147  << "Index value was " << i << '\n'
148  << "The size of this adstring is " << shape->size() << '\n';
149  ad_exit(1);
150  }
151  return s[i];
152 }
158 bool adstring::operator==(const adstring& other) const
159 {
160  return !strcmp(*this, other);
161 }
167 /*
168 bool adstring::operator==(const adstring& other)
169 {
170  return !strcmp(*this, other);
171 }
172 */
173 /*
174 int adstring::operator!=(const adstring& v)
175 {
176  int tmp = strcmp (*this, v);
177  return (tmp != 0);
178 }
179 */
181 {
182  size_t us = size();
183  size_t vs = v.size ();
184  size_t bs = buff_size();
185  if (bs > us + vs)
186  {
187  for (size_t i = 1; i <= vs; i++)
188  {
189  s[i + us] = v(i);
190  }
191  }
192  else
193  {
194  adstring tmp(1, us + vs);
195  for (size_t i = 1; i <= us; i++)
196  {
197  tmp(i) = s[i];
198  }
199  for (size_t i = 1; i <= vs; i++)
200  {
201  tmp(i + us) = v(i);
202  }
203  * this = tmp;
204  }
205  return (* this);
206 }
212 unsigned char& adstring::operator[](const size_t i)
213 {
214  if (i < 1 || (shape && i > shape->size()))
215  {
216  cerr << "Index out of bounds in adstring::operator[](const size_t)\n"
217  << "Index value was " << i << '\n'
218  << "The size of this adstring is " << shape->size() << '\n';
219  ad_exit(1);
220  }
221  return s[i];
222 }
228 size_t length(const adstring& adstr)
229 {
230  return adstr.size();
231 }
size_t size() const
Definition: string.cpp:58
adstring_shape * shape
Definition: adstring.hpp:72
adstring & operator+=(const adstring &u)
Return true if adstring is equal to other, otherwise false.
Definition: string1.cpp:180
bool operator==(const adstring &other) const
Return true if adstring is equal to other, otherwise false.
Definition: string1.cpp:158
void realloc(const char *t)
Definition: string1.cpp:77
exitptr ad_exit
Definition: gradstrc.cpp:53
unsigned char & operator()(const size_t i)
Returns reference to element at specified index i.
Definition: string1.cpp:110
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
~adstring()
Destructor.
Definition: string1.cpp:98
unsigned char & operator[](const size_t i)
Returns reference to element at specified index i.
Definition: string1.cpp:212
unsigned char * s
Definition: adstring.hpp:76
clist * next
Definition: clist.h:48
size_t buff_size() const
Definition: string.cpp:63
size_t & size()
Definition: adstring.hpp:60
adstring & operator=(const adstring &t)
Definition: string1.cpp:48
size_t length(const adstring &t)
Returns the size of adstr.
Definition: string1.cpp:228
void deallocate(void)
Definition: string.cpp:23