From 96a5fcd10bb13902a85ae12a17e8159dd5ad13c8 Mon Sep 17 00:00:00 2001 From: jxd134 Date: Mon, 14 Mar 2016 21:20:47 +0800 Subject: [PATCH 1/7] modify erase function in Vector.impl.h --- TinySTL/Detail/Vector.impl.h | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/TinySTL/Detail/Vector.impl.h b/TinySTL/Detail/Vector.impl.h index 55b3097..2d82400 100644 --- a/TinySTL/Detail/Vector.impl.h +++ b/TinySTL/Detail/Vector.impl.h @@ -92,16 +92,10 @@ namespace TinySTL{ } template typename vector::iterator vector::erase(iterator first, iterator last){ - //尾部残留对象数 - difference_type lenOfTail = end() - last; - //删去的对象数目 - difference_type lenOfRemoved = last - first; - finish_ = finish_ - lenOfRemoved; - for (; lenOfTail != 0; --lenOfTail){ - auto temp = (last - lenOfRemoved); - *temp = *(last++); - } - return (first); + iterator pos = std::copy(last, finish_, first); + dataAllocator::destroy(pos, finish_); + finish_ = finish_ - (last - first); + return first; } template template From f7f069b30d8a0f51a600212aff581a419471e147 Mon Sep 17 00:00:00 2001 From: jxd134 Date: Mon, 14 Mar 2016 22:47:59 +0800 Subject: [PATCH 2/7] modify resize function in Vector.impl.h --- TinySTL/Detail/Vector.impl.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TinySTL/Detail/Vector.impl.h b/TinySTL/Detail/Vector.impl.h index 2d82400..f92f792 100644 --- a/TinySTL/Detail/Vector.impl.h +++ b/TinySTL/Detail/Vector.impl.h @@ -63,7 +63,8 @@ namespace TinySTL{ } else if (n > capacity()){ auto lengthOfInsert = n - size(); - T *newStart = dataAllocator::allocate(getNewCapacity(lengthOfInsert)); + //T *newStart = dataAllocator::allocate(getNewCapacity(lengthOfInsert)); + T *newStart = dataAllocator::allocate(getNewCapacity(n)); T *newFinish = TinySTL::uninitialized_copy(begin(), end(), newStart); newFinish = TinySTL::uninitialized_fill_n(newFinish, lengthOfInsert, val); From 4e82d989f9c5497a968a091d0061d76c06163240 Mon Sep 17 00:00:00 2001 From: jxd134 Date: Tue, 15 Mar 2016 13:33:19 +0800 Subject: [PATCH 3/7] modify erase(iterator position) function in vector --- TinySTL/Detail/Vector.impl.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/TinySTL/Detail/Vector.impl.h b/TinySTL/Detail/Vector.impl.h index f92f792..e1a6c23 100644 --- a/TinySTL/Detail/Vector.impl.h +++ b/TinySTL/Detail/Vector.impl.h @@ -89,7 +89,12 @@ namespace TinySTL{ //***************修改容器的相关操作************************** template typename vector::iterator vector::erase(iterator position){ - return erase(position, position + 1); + if (position + 1 != end()) + std::copy(position + 1, finish_, position); + auto tmp = finish_; + --finish_; + dataAllocator::destroy(tmp); + return position; } template typename vector::iterator vector::erase(iterator first, iterator last){ From 2f185b1b5ea68e81879b8c60aa8e32bd820a56fe Mon Sep 17 00:00:00 2001 From: jxd134 Date: Tue, 15 Mar 2016 21:35:26 +0800 Subject: [PATCH 4/7] modify operator== function in List --- TinySTL/Detail/List.impl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TinySTL/Detail/List.impl.h b/TinySTL/Detail/List.impl.h index aeb4e84..4735ede 100644 --- a/TinySTL/Detail/List.impl.h +++ b/TinySTL/Detail/List.impl.h @@ -395,7 +395,7 @@ namespace TinySTL{ auto node1 = lhs.head.p, node2 = rhs.head.p; for (; node1 != lhs.tail.p && node2 != rhs.tail.p; node1 = node1->next, node2 = node2->next){ if (node1->data != node2->data) - break; + return false; } if (node1 == lhs.tail.p && node2 == rhs.tail.p) return true; From 13f06c7e467d57fb2398e61936e280418b16aeba Mon Sep 17 00:00:00 2001 From: jxd134 Date: Mon, 21 Mar 2016 19:37:27 +0800 Subject: [PATCH 5/7] modify find(const string& str, size_t pos) in String.cpp --- TinySTL/Detail/String.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/TinySTL/Detail/String.cpp b/TinySTL/Detail/String.cpp index 2f38c69..4e9fb13 100644 --- a/TinySTL/Detail/String.cpp +++ b/TinySTL/Detail/String.cpp @@ -243,7 +243,9 @@ namespace TinySTL{ } size_t string::find(const string& str, size_t pos) const{ size_t lengthOfS = str.size(); - if (size() - pos < lengthOfS) + //if (size() - pos < lengthOfS) + // return npos; + if (size() < lengthOfS + pos) return npos; return find_aux(str.cbegin(), pos, lengthOfS, size()); } From 7d3c15191008774f4131f7c371cd0d658bb46cc5 Mon Sep 17 00:00:00 2001 From: jxd134 Date: Mon, 21 Mar 2016 20:02:51 +0800 Subject: [PATCH 6/7] modify functions which used changeVarWhenEuqalNPOS function --- TinySTL/Detail/String.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/TinySTL/Detail/String.cpp b/TinySTL/Detail/String.cpp index 4e9fb13..9c56c7c 100644 --- a/TinySTL/Detail/String.cpp +++ b/TinySTL/Detail/String.cpp @@ -264,6 +264,7 @@ namespace TinySTL{ } size_t string::rfind(char c, size_t pos) const{ pos = changeVarWhenEuqalNPOS(pos, size(), 1); + if (npos == pos) return npos; for (auto cit = cbegin() + pos; cit >= cbegin(); --cit){ if (*cit == c) return cit - cbegin(); @@ -290,10 +291,12 @@ namespace TinySTL{ //if (pos - 0 < lengthOfS) // return npos; pos = changeVarWhenEuqalNPOS(pos, size(), 1); + if (npos == pos) return npos; return rfind_aux(str.begin(), pos, lengthOfS, 0); } size_t string::rfind(const char* s, size_t pos) const{ pos = changeVarWhenEuqalNPOS(pos, size(), 1); + if (npos == pos) return npos; return rfind(s, pos, strlen(s)); } size_t string::rfind(const char* s, size_t pos, size_t n) const{ @@ -388,6 +391,7 @@ namespace TinySTL{ size_t string::find_last_of(const string& str, size_t pos) const{ pos = changeVarWhenEuqalNPOS(pos, size(), 1); //return find_last_of(str.begin(), pos, pos + 1); + if (npos == pos) return npos; return find_last_of(str.begin(), pos, str.size()); } size_t string::find_last_of(const char* s, size_t pos) const{ @@ -414,11 +418,13 @@ namespace TinySTL{ size_t string::find_last_not_of(const string& str, size_t pos) const{ pos = changeVarWhenEuqalNPOS(pos, size(), 1); //return find_last_not_of(str.begin(), pos, size()); + if (npos == pos) return npos; return find_last_not_of(str.begin(), pos, str.size()); } size_t string::find_last_not_of(const char* s, size_t pos) const{ pos = changeVarWhenEuqalNPOS(pos, size(), 1); //return find_last_not_of(s, pos, pos + 1); + if (npos == pos) return npos; return find_last_not_of(s, pos, strlen(s)); } size_t string::find_last_not_of(const char* s, size_t pos, size_t n) const{ From c95053e13bae8951c39e9df21e6e67054bf440d4 Mon Sep 17 00:00:00 2001 From: jxd134 Date: Wed, 23 Mar 2016 22:12:24 +0800 Subject: [PATCH 7/7] minor modify --- TinySTL/Detail/String.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TinySTL/Detail/String.cpp b/TinySTL/Detail/String.cpp index 9c56c7c..868cadd 100644 --- a/TinySTL/Detail/String.cpp +++ b/TinySTL/Detail/String.cpp @@ -382,7 +382,8 @@ namespace TinySTL{ return npos; } size_t string::find_first_not_of(char c, size_t pos) const{ - for (size_t i = pos; i != size(); ++i){ + //bug fix (if pos>size()) + for (size_t i = pos; i < size(); ++i){ if ((*this)[i] != c) return i; }