1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
#pragma once
#include <string.h>
namespace ivy
{
#ifdef WIN32
#ifdef IVY_EXPORTS
class _declspec(dllexport) string;
template <class T> class _declspec(dllexport) list;
template <class T> class _declspec(dllexport) vector;
#else
#pragma comment(lib,"Ivy.LIB" )
class _declspec(dllimport) string;
template <class T> class _declspec(dllimport) list;
template <class T> class _declspec(dllimport) vector;
#endif
#endif
class string {
public:
static const size_t npos = size_t(-1);
string(size_t len = 0)
{ ptr = allocBuffer( len ); };
string ( const string& s )
{ ptr = 0; copy( s.ptr, s.size ); }
string( const char * s, size_t len = npos )
{
ptr = 0;
if ( len == npos )
len = strlen(s) ;
copy( s, len );
}
~string()
{ delete [] ptr; }
size_t length()
{ return size; }
bool empty() const
{ return size==0; }
void copy( const char * s, size_t len )
{
if ( ptr ) delete [] ptr;
ptr = allocBuffer( len );
strncpy_s( ptr, len + 1, s, len );
}
void erase(size_t start=0, size_t len = npos)
{
char *newptr;
size_t real_len;
size_t buf_size;
real_len = len == npos ? size - start : len;
if ( real_len > size ) real_len = size;
buf_size = size - real_len;
newptr = allocBuffer( buf_size );
strncpy_s( newptr, buf_size +1, ptr , start );
strncpy_s( &newptr[start], buf_size +1 - start, &ptr[start+real_len], size - start );
delete ptr;
ptr = newptr;
}
void append( const char *s , size_t len = npos )
{
insert ( size, s , len );
}
void insert(size_t index, const char * s, size_t len = npos)
{
char *newptr;
if ( len == npos ) len = strlen(s) ;
size_t buf_len = size + len;
newptr = allocBuffer( buf_len );
strncpy_s( newptr , buf_len +1, ptr, index );
strncpy_s( newptr+index, buf_len + 1 - index, s, len ) ;
strncpy_s( newptr+index+len, buf_len + 1 - index -len, &ptr[index], size - (index+len) ) ;
delete ptr;
ptr = newptr;
}
string substr( size_t start, size_t len = npos )
{ return string ( &ptr[start], len ); }
size_t find_first_of( const char* strCharSet )
{ char *fnd = strpbrk( ptr, strCharSet ); if ( fnd ) return (size_t)(fnd - ptr); else return npos; }
size_t rfind( char c )
{ char *fnd = strrchr( ptr, c); if ( fnd ) return (size_t)(fnd - ptr); else return npos; }
// friend string operator + (const string& left, const char* right);
// friend string operator + (const char* left, const string& right);
friend string operator + (const string& left, const string& right)
{
string str( left.size + right.size );
str.append( left.ptr, left.size);
str.append( right.ptr, right.size);
return str;
}
void operator +=(string s)
{
append( s.ptr, s.size );
}
void operator +=(const char* s)
{
append( s, strlen(s) );
}
void operator +=(char c)
{ append( &c, 1);}
string operator +(string s) const
{ string str ( ptr, size ); str.append( s.ptr, s.size ); return str; }
string operator +(const char c) const
{ string str ( ptr, size ); str.append( &c, 1); return str; }
string& operator=(const string& s)
{ copy(s.ptr,s.size); return *this; }
string& operator=(const char* s)
{ copy(s,strlen(s)); return *this; }
// Nondestructive typecast operator
operator const char*() const
{ return ptr; }
const char *c_str() const // remove this ugly thing
{ return ptr; }
private:
char *ptr;
size_t size;
// always allocate one extra character for '\0' termination
char *allocBuffer(size_t len)
{
char *newptr;
size = len;
newptr = new char[len+1] ;
newptr[len] = '\0';
return newptr;
}
};
template <class T> class list {
protected:
struct _Node;
friend struct _Node;
typedef struct _Node Node;
typedef Node* NodePtr;
struct _Node {
NodePtr next, prev;
T value;
};
public:
//friend class iterator;
class iterator {
public:
iterator()
{ptr = 0;}
iterator(NodePtr p)
{ptr = p;}
T& operator*() const
{return ptr->value; }
// T* operator->() const
// {return &(ptr->value); }
// T* operator->() const
// {return (&**this); }
iterator& operator++()
{ptr = ptr->next;
return (*this); }
iterator operator++(int)
{iterator tmp = *this;
++*this;
return (tmp); }
iterator& operator--()
{ptr = ptr.prev;
return (*this); }
iterator operator--(int)
{iterator tmp = *this;
--*this;
return (tmp); }
bool operator==(const iterator& p) const
{return (ptr == p.ptr); }
bool operator!=(const iterator& p) const
{return (ptr != p.ptr); }
NodePtr next() const
{return ptr->next; };
NodePtr prev() const
{return ptr->prev ; };
NodePtr node() const
{return (ptr); }
protected:
NodePtr ptr;
};
list()
{
head = new Node;
head->next = head->prev = head;
}
~list()
{
clear();
delete head;
}
bool empty()
{ return head->next == head ; }
T& front()
{return (*begin()); }
const T& front() const
{return (*begin()); }
T& back()
{return (*(--end())); }
const T& back() const
{return (*(--end())); }
iterator erase(iterator p)
{
NodePtr s = (p++).node();
s->prev->next = s->next;
s->next->prev = s->prev;
delete s;
return (p);
}
iterator insert(iterator p, const T& v =T())
{
NodePtr s = p.node();
NodePtr newnode = new Node;
newnode->value = v;
newnode->prev = s->prev;
newnode->next = s;
s->prev->next = newnode;
s->prev = newnode;
return (iterator(newnode));
}
void push_front(const T& v)
{insert(begin(), v); }
void pop_front()
{erase(begin()); }
void push_back(const T& v)
{insert(end(), v); }
void pop_back()
{erase(--end()); }
iterator erase(iterator first, iterator last)
{
while (first != last)
erase(first++);
return (first);
}
void clear()
{erase(begin(), end()); }
void remove( const T& data )
{
iterator last = end();
for (iterator iter = begin(); iter != last; )
if (*iter == data)
erase(iter++);
else
++iter;
}
iterator begin()
{return (iterator(head->next)); }
iterator end()
{return (iterator(head));}
protected:
NodePtr head;
};
template <class T> class vector {
public:
vector ( )
{ data = 0; len = 0; }
size_t size()
{ return len; }
void reserve( size_t index )
{
if ( index >= len )
resize( index );
}
void resize( size_t index )
{
size_t i;
T* newdata = new T[index];
for ( i = 0; i < len ; i ++ )
newdata[i] = data[i];
for ( i = len; i < index ; i ++ )
newdata[i] = 0;
if ( data ) delete [] data;
data = newdata;
len = index;
}
// Accesses indexed component of vector
T & operator [](size_t index )
{
// reserve( index ); // TODO or NOT check range
return data[index] ;
}
T & operator [](size_t index ) const
{
return data[index] ;
}
void push_back( const T& value )
{
T* newdata = new T[len+1];
for ( size_t i = 0; i < len ; i ++ )
newdata[i] = data[i];
newdata[len++] = value;
if ( data ) delete [] data;
data = newdata;
}
void erase( int i )
{
data[i] = 0;
}
void clear()
{
delete [] data;
data = 0;
len = 0;
}
private:
T* data;
size_t len;
};
}
|