重构
This commit is contained in:
@@ -8,11 +8,11 @@ namespace TinySTL{
|
|||||||
string::string(size_t n, char c){
|
string::string(size_t n, char c){
|
||||||
allocateAndFillN(n, c);
|
allocateAndFillN(n, c);
|
||||||
}
|
}
|
||||||
template<class InputIterator>
|
//template<class InputIterator>
|
||||||
string::string(InputIterator first, InputIterator last){
|
//string::string(InputIterator first, InputIterator last){
|
||||||
//<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ּ<EFBFBD><D6BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD>
|
// //<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ּ<EFBFBD><D6BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD>
|
||||||
string_aux(first, last, typename std::is_integral<InputIterator>::type());
|
// string_aux(first, last, typename std::is_integral<InputIterator>::type());
|
||||||
}
|
//}
|
||||||
string::string(const char* s){
|
string::string(const char* s){
|
||||||
allocateAndCopy(s, s + strlen(s));
|
allocateAndCopy(s, s + strlen(s));
|
||||||
}
|
}
|
||||||
@@ -29,6 +29,9 @@ namespace TinySTL{
|
|||||||
len = changeVarWhenEuqalNPOS(len, str.size(), pos);
|
len = changeVarWhenEuqalNPOS(len, str.size(), pos);
|
||||||
allocateAndCopy(str.start_ + pos, str.start_ + pos + len);
|
allocateAndCopy(str.start_ + pos, str.start_ + pos + len);
|
||||||
}
|
}
|
||||||
|
string::~string(){
|
||||||
|
destroyAndDeallocate();
|
||||||
|
}
|
||||||
string& string::operator= (const string& str){
|
string& string::operator= (const string& str){
|
||||||
if (this != &str){
|
if (this != &str){
|
||||||
destroyAndDeallocate();
|
destroyAndDeallocate();
|
||||||
@@ -86,7 +89,7 @@ namespace TinySTL{
|
|||||||
finish_ = newFinish;
|
finish_ = newFinish;
|
||||||
endOfStorage_ = start_ + n;
|
endOfStorage_ = start_ + n;
|
||||||
}
|
}
|
||||||
template <class InputIterator>
|
/*template <class InputIterator>
|
||||||
string::iterator string::insert_aux_copy(iterator p, InputIterator first, InputIterator last){
|
string::iterator string::insert_aux_copy(iterator p, InputIterator first, InputIterator last){
|
||||||
size_t lengthOfInsert = last - first;
|
size_t lengthOfInsert = last - first;
|
||||||
auto newCapacity = getNewCapacity(lengthOfInsert);
|
auto newCapacity = getNewCapacity(lengthOfInsert);
|
||||||
@@ -101,8 +104,8 @@ namespace TinySTL{
|
|||||||
finish_ = newFinish;
|
finish_ = newFinish;
|
||||||
endOfStorage_ = start_ + newCapacity;
|
endOfStorage_ = start_ + newCapacity;
|
||||||
return res;
|
return res;
|
||||||
}
|
}*/
|
||||||
template <class InputIterator>
|
/*template <class InputIterator>
|
||||||
string::iterator string::insert(iterator p, InputIterator first, InputIterator last){
|
string::iterator string::insert(iterator p, InputIterator first, InputIterator last){
|
||||||
auto lengthOfLeft = capacity() - size();
|
auto lengthOfLeft = capacity() - size();
|
||||||
size_t lengthOfInsert = last - first;
|
size_t lengthOfInsert = last - first;
|
||||||
@@ -117,7 +120,7 @@ namespace TinySTL{
|
|||||||
else{
|
else{
|
||||||
return insert_aux_copy(p, first, last);
|
return insert_aux_copy(p, first, last);
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
string& string::insert(size_t pos, const string& str){
|
string& string::insert(size_t pos, const string& str){
|
||||||
insert(start_ + pos, str.begin(), str.end());
|
insert(start_ + pos, str.begin(), str.end());
|
||||||
return *this;
|
return *this;
|
||||||
@@ -203,11 +206,11 @@ namespace TinySTL{
|
|||||||
insert(end(), n, c);
|
insert(end(), n, c);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
template <class InputIterator>
|
/*template <class InputIterator>
|
||||||
string& string::append(InputIterator first, InputIterator last){
|
string& string::append(InputIterator first, InputIterator last){
|
||||||
insert(end(), first, last);
|
insert(end(), first, last);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}*/
|
||||||
string::iterator string::erase(iterator first, iterator last){
|
string::iterator string::erase(iterator first, iterator last){
|
||||||
size_t lengthOfMove = finish_ - last;
|
size_t lengthOfMove = finish_ - last;
|
||||||
for (auto i = 0; i != lengthOfMove; ++i){
|
for (auto i = 0; i != lengthOfMove; ++i){
|
||||||
@@ -229,13 +232,13 @@ namespace TinySTL{
|
|||||||
//2014.12.24
|
//2014.12.24
|
||||||
return erase(p, p + 1);
|
return erase(p, p + 1);
|
||||||
}
|
}
|
||||||
template <class InputIterator>
|
/*template <class InputIterator>
|
||||||
string& string::replace(iterator i1, iterator i2,
|
string& string::replace(iterator i1, iterator i2,
|
||||||
InputIterator first, InputIterator last){
|
InputIterator first, InputIterator last){
|
||||||
auto ptr = erase(i1, i2);
|
auto ptr = erase(i1, i2);
|
||||||
insert(ptr, first, last);
|
insert(ptr, first, last);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}*/
|
||||||
string& string::replace(size_t pos, size_t len, const string& str){
|
string& string::replace(size_t pos, size_t len, const string& str){
|
||||||
return replace(begin() + pos, begin() + pos + len, str.begin(), str.end());
|
return replace(begin() + pos, begin() + pos + len, str.begin(), str.end());
|
||||||
}
|
}
|
||||||
@@ -657,4 +660,50 @@ namespace TinySTL{
|
|||||||
void swap(string& x, string& y){
|
void swap(string& x, string& y){
|
||||||
x.swap(y);
|
x.swap(y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void string::moveData(string& str){
|
||||||
|
start_ = str.start_;
|
||||||
|
finish_ = str.finish_;
|
||||||
|
endOfStorage_ = str.endOfStorage_;
|
||||||
|
str.start_ = str.finish_ = str.endOfStorage_ = 0;
|
||||||
|
}
|
||||||
|
string::size_type string::getNewCapacity(size_type len)const{
|
||||||
|
size_type oldCapacity = endOfStorage_ - start_;
|
||||||
|
auto res = TinySTL::max(oldCapacity, len);
|
||||||
|
//size_type newCapacity = (oldCapacity != 0 ? (oldCapacity + res) : 1);
|
||||||
|
auto newCapacity = oldCapacity + res;
|
||||||
|
return newCapacity;
|
||||||
|
}
|
||||||
|
void string::allocateAndFillN(size_t n, char c){
|
||||||
|
start_ = dataAllocator::allocate(n);
|
||||||
|
finish_ = TinySTL::uninitialized_fill_n(start_, n, c);
|
||||||
|
endOfStorage_ = finish_;
|
||||||
|
}
|
||||||
|
/*template<class InputIterator>
|
||||||
|
void string::allocateAndCopy(InputIterator first, InputIterator last){
|
||||||
|
start_ = dataAllocator::allocate(last - first);
|
||||||
|
finish_ = TinySTL::uninitialized_copy(first, last, start_);
|
||||||
|
endOfStorage_ = finish_;
|
||||||
|
}*/
|
||||||
|
void string::string_aux(size_t n, char c, std::true_type){
|
||||||
|
allocateAndFillN(n, c);
|
||||||
|
}
|
||||||
|
/*template<class InputIterator>
|
||||||
|
void string::string_aux(InputIterator first, InputIterator last, std::false_type){
|
||||||
|
allocateAndCopy(first, last);
|
||||||
|
}*/
|
||||||
|
void string::destroyAndDeallocate(){
|
||||||
|
dataAllocator::destroy(start_, finish_);
|
||||||
|
dataAllocator::deallocate(start_, endOfStorage_ - start_);
|
||||||
|
}
|
||||||
|
bool string::isContained(char ch, const_iterator first, const_iterator last)const{
|
||||||
|
for (auto cit = first; cit != last; ++cit){
|
||||||
|
if (*cit == ch)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
size_t string::changeVarWhenEuqalNPOS(size_t var, size_t minuend, size_t minue)const{
|
||||||
|
return (var == npos ? minuend - minue : var);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
114
TinySTL/String.h
114
TinySTL/String.h
@@ -47,7 +47,7 @@ namespace TinySTL{
|
|||||||
string& operator= (const char* s);
|
string& operator= (const char* s);
|
||||||
string& operator= (char c);
|
string& operator= (char c);
|
||||||
|
|
||||||
~string(){ destroyAndDeallocate(); }
|
~string();
|
||||||
|
|
||||||
iterator begin(){ return start_; }
|
iterator begin(){ return start_; }
|
||||||
const_iterator begin() const{ return start_; }
|
const_iterator begin() const{ return start_; }
|
||||||
@@ -170,59 +170,25 @@ namespace TinySTL{
|
|||||||
int compare(size_t pos, size_t len, const char* s) const;
|
int compare(size_t pos, size_t len, const char* s) const;
|
||||||
int compare(size_t pos, size_t len, const char* s, size_t n) const;
|
int compare(size_t pos, size_t len, const char* s, size_t n) const;
|
||||||
private:
|
private:
|
||||||
void moveData(string& str){
|
void moveData(string& str);
|
||||||
start_ = str.start_;
|
|
||||||
finish_ = str.finish_;
|
|
||||||
endOfStorage_ = str.endOfStorage_;
|
|
||||||
str.start_ = str.finish_ = str.endOfStorage_ = 0;
|
|
||||||
}
|
|
||||||
//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD>ռ䲻<D5BC><E4B2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD>ռ䲻<D5BC><E4B2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
template<class InputIterator>
|
template<class InputIterator>
|
||||||
iterator insert_aux_copy(iterator p, InputIterator first, InputIterator last);
|
iterator insert_aux_copy(iterator p, InputIterator first, InputIterator last);
|
||||||
//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD>ռ䲻<D5BC><E4B2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD>ռ䲻<D5BC><E4B2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
iterator insert_aux_filln(iterator p, size_t n, value_type c);
|
iterator insert_aux_filln(iterator p, size_t n, value_type c);
|
||||||
size_type getNewCapacity(size_type len)const{
|
size_type getNewCapacity(size_type len)const;
|
||||||
size_type oldCapacity = endOfStorage_ - start_;
|
void allocateAndFillN(size_t n, char c);
|
||||||
auto res = TinySTL::max(oldCapacity, len);
|
|
||||||
//size_type newCapacity = (oldCapacity != 0 ? (oldCapacity + res) : 1);
|
|
||||||
auto newCapacity = oldCapacity + res;
|
|
||||||
return newCapacity;
|
|
||||||
}
|
|
||||||
void allocateAndFillN(size_t n, char c){
|
|
||||||
start_ = dataAllocator::allocate(n);
|
|
||||||
finish_ = TinySTL::uninitialized_fill_n(start_, n, c);
|
|
||||||
endOfStorage_ = finish_;
|
|
||||||
}
|
|
||||||
template<class InputIterator>
|
template<class InputIterator>
|
||||||
void allocateAndCopy(InputIterator first, InputIterator last){
|
void allocateAndCopy(InputIterator first, InputIterator last);
|
||||||
start_ = dataAllocator::allocate(last - first);
|
void string_aux(size_t n, char c, std::true_type);
|
||||||
finish_ = TinySTL::uninitialized_copy(first, last, start_);
|
|
||||||
endOfStorage_ = finish_;
|
|
||||||
}
|
|
||||||
void string_aux(size_t n, char c, std::true_type){
|
|
||||||
allocateAndFillN(n, c);
|
|
||||||
}
|
|
||||||
template<class InputIterator>
|
template<class InputIterator>
|
||||||
void string_aux(InputIterator first, InputIterator last, std::false_type){
|
void string_aux(InputIterator first, InputIterator last, std::false_type);
|
||||||
allocateAndCopy(first, last);
|
void destroyAndDeallocate();
|
||||||
}
|
|
||||||
void destroyAndDeallocate(){
|
|
||||||
dataAllocator::destroy(start_, finish_);
|
|
||||||
dataAllocator::deallocate(start_, endOfStorage_ - start_);
|
|
||||||
}
|
|
||||||
size_t rfind_aux(const_iterator cit, size_t pos, size_t lengthOfS, int cond)const;
|
size_t rfind_aux(const_iterator cit, size_t pos, size_t lengthOfS, int cond)const;
|
||||||
size_t find_aux(const_iterator cit, size_t pos, size_t lengthOfS, size_t cond)const;
|
size_t find_aux(const_iterator cit, size_t pos, size_t lengthOfS, size_t cond)const;
|
||||||
int compare_aux(size_t pos, size_t len, const_iterator cit, size_t subpos, size_t sublen)const;
|
int compare_aux(size_t pos, size_t len, const_iterator cit, size_t subpos, size_t sublen)const;
|
||||||
bool isContained(char ch, const_iterator first, const_iterator last)const{
|
bool isContained(char ch, const_iterator first, const_iterator last)const;
|
||||||
for (auto cit = first; cit != last; ++cit){
|
size_t changeVarWhenEuqalNPOS(size_t var, size_t minuend, size_t minue)const;
|
||||||
if (*cit == ch)
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
size_t changeVarWhenEuqalNPOS(size_t var, size_t minuend, size_t minue)const{
|
|
||||||
return (var == npos ? minuend - minue : var);
|
|
||||||
}
|
|
||||||
public:
|
public:
|
||||||
friend std::ostream& operator <<(std::ostream& os, const string&str);
|
friend std::ostream& operator <<(std::ostream& os, const string&str);
|
||||||
friend std::istream& operator >> (std::istream& is, string& str);
|
friend std::istream& operator >> (std::istream& is, string& str);
|
||||||
@@ -253,5 +219,65 @@ namespace TinySTL{
|
|||||||
friend std::istream& getline(std::istream& is, string& str, char delim);
|
friend std::istream& getline(std::istream& is, string& str, char delim);
|
||||||
friend std::istream& getline(std::istream& is, string& str);
|
friend std::istream& getline(std::istream& is, string& str);
|
||||||
};// end of string
|
};// end of string
|
||||||
|
|
||||||
|
template<class InputIterator>
|
||||||
|
string::string(InputIterator first, InputIterator last){
|
||||||
|
//<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ּ<EFBFBD><D6BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD>
|
||||||
|
string_aux(first, last, typename std::is_integral<InputIterator>::type());
|
||||||
|
}
|
||||||
|
template <class InputIterator>
|
||||||
|
string::iterator string::insert_aux_copy(iterator p, InputIterator first, InputIterator last){
|
||||||
|
size_t lengthOfInsert = last - first;
|
||||||
|
auto newCapacity = getNewCapacity(lengthOfInsert);
|
||||||
|
iterator newStart = dataAllocator::allocate(newCapacity);
|
||||||
|
iterator newFinish = TinySTL::uninitialized_copy(start_, p, newStart);
|
||||||
|
newFinish = TinySTL::uninitialized_copy(first, last, newFinish);
|
||||||
|
auto res = newFinish;
|
||||||
|
newFinish = TinySTL::uninitialized_copy(p, finish_, newFinish);
|
||||||
|
|
||||||
|
destroyAndDeallocate();
|
||||||
|
start_ = newStart;
|
||||||
|
finish_ = newFinish;
|
||||||
|
endOfStorage_ = start_ + newCapacity;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
template <class InputIterator>
|
||||||
|
string::iterator string::insert(iterator p, InputIterator first, InputIterator last){
|
||||||
|
auto lengthOfLeft = capacity() - size();
|
||||||
|
size_t lengthOfInsert = last - first;
|
||||||
|
if (lengthOfInsert <= lengthOfLeft){
|
||||||
|
for (iterator it = finish_ - 1; it >= p; --it){
|
||||||
|
*(it + lengthOfInsert) = *(it);
|
||||||
|
}
|
||||||
|
TinySTL::uninitialized_copy(first, last, p);
|
||||||
|
finish_ += lengthOfInsert;
|
||||||
|
return (p + lengthOfInsert);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return insert_aux_copy(p, first, last);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
template <class InputIterator>
|
||||||
|
string& string::append(InputIterator first, InputIterator last){
|
||||||
|
insert(end(), first, last);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
template <class InputIterator>
|
||||||
|
string& string::replace(iterator i1, iterator i2,
|
||||||
|
InputIterator first, InputIterator last){
|
||||||
|
auto ptr = erase(i1, i2);
|
||||||
|
insert(ptr, first, last);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
template<class InputIterator>
|
||||||
|
void string::allocateAndCopy(InputIterator first, InputIterator last){
|
||||||
|
start_ = dataAllocator::allocate(last - first);
|
||||||
|
finish_ = TinySTL::uninitialized_copy(first, last, start_);
|
||||||
|
endOfStorage_ = finish_;
|
||||||
|
}
|
||||||
|
template<class InputIterator>
|
||||||
|
void string::string_aux(InputIterator first, InputIterator last, std::false_type){
|
||||||
|
allocateAndCopy(first, last);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user