-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathString.cpp
190 lines (169 loc) · 3.65 KB
/
String.cpp
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
#include <memory>
#include<utility>
#include<algorithm>
#include<iostream>
#include "String.h"
std::allocator<char> String::alloc;
void String::chk_n_alloc()
{
if(size() == capacity())
reallocate();
}
void String::push_back(char c)
{
chk_n_alloc();
alloc.construct(first_free++, c);
}
std::pair<char *, char *> String::alloc_n_copy(const char * b, const char * e)
{
char * data = alloc.allocate(e - b);
return {data, std::uninitialized_copy(b, e, data)};
}
void String::free()
{
if(elements)
{
std::for_each(elements, first_free, [](char &c){ alloc.destroy(&c); });
alloc.deallocate(elements, cap - elements);
}
}
void String::reallocate()
{
size_t newcapacity = size() ? 2 * size() : 1;
reserve(newcapacity);
}
void String::reserve(size_t n)
{
if(n <= capacity())
return;
char * newdata = alloc.allocate(n);
char * dest = newdata;
char * elem = elements;
for(size_t i = 0;i != size(); ++i)
alloc.construct(dest++, std::move(*elem++));
free();
elements = newdata;
first_free = dest;
cap = elements + n;
}
void String::resize(size_t n, char c)
{
if(n == size())
return;
if(n < size())
{
while(size() != n)
alloc.destroy(--first_free);
return;
}
if(n > capacity())
reserve(n);
while(size() != n)
alloc.construct(first_free++, c);
}
String::String(const String & s)
{
std::pair<char *, char *> newdata = alloc_n_copy(s.begin(), s.end());
elements = newdata.first;
first_free = cap = newdata.second;
}
String & String::operator=(const String & rhs)
{
std::pair<char *, char *> data = alloc_n_copy(rhs.begin(), rhs.end());
free();
elements = data.first;
first_free = cap = data.second;
return *this;
}
String::String(const char *ps)
{
const char *p = ps;
while(*p != '\0')
++p;
std::pair<char *, char *> newdata = alloc_n_copy(ps, p);
elements = newdata.first;
first_free = cap = newdata.second;
}
String::String(String &&s) noexcept : elements(s.elements), first_free(s.first_free), cap(s.cap)
{
s.elements = s.first_free = s.cap = nullptr;
}
String & String::operator=(String && rhs) noexcept
{
if(this != &rhs)
{
free();
elements = rhs.elements;
first_free = rhs.first_free;
cap = rhs.cap;
rhs.elements = rhs.first_free = rhs.cap = nullptr;
}
return *this;
}
std::ostream & operator<<(std::ostream &os, const String & s)
{
for(char *p = s.begin(); p != s.end(); ++p)
os << *p;
return os;
}
bool operator==(const String &lhs, const String &rhs)
{
if(lhs.size() != rhs.size())
return false;
for(const char *p1 = lhs.begin(), *p2 = rhs.begin(); p1 != lhs.end(); ++p1, ++p2)
if(*p1 != *p2)
return false;
return true;
}
bool operator!=(const String &lhs, const String &rhs)
{
return !(lhs == rhs);
}
bool operator<(const String &lhs, const String &rhs)
{
const char *p1 = lhs.begin(), *p2 = rhs.begin();
for( ; p1 != lhs.end() && p2 != rhs.end(); ++p1, ++p2)
{
if(*p1 < *p2)
return true;
if(*p1 > *p2)
return false;
}
if(p1 != lhs.end() && p2 == rhs.end())
return false;
if(p1 == lhs.end() && p2 != rhs.end())
return true;
return false;
}
bool operator<=(const String &lhs, const String &rhs)
{
return !(lhs > rhs);
}
bool operator>(const String &lhs, const String &rhs)
{
const char *p1 = lhs.begin(), *p2 = rhs.begin();
for( ; p1 != lhs.end() && p2 != rhs.end(); ++p1, ++p2)
{
if(*p1 > *p2)
return true;
if(*p1 < *p2)
return false;
}
if(p1 != lhs.end() && p2 == rhs.end())
return true;
if(p1 == lhs.end() && p2 != rhs.end())
return false;
return false;
}
bool operator>=(const String &lhs, const String &rhs)
{
return !(lhs < rhs);
}
char & String::operator[](std::size_t n)
{
return elements[n];
}
const char & String::operator[](std::size_t n) const
{
return elements[n];
}