Last active
September 21, 2018 14:45
-
-
Save zeux/bc4ec25e1527a6c69021925b13b11586 to your computer and use it in GitHub Desktop.
A quick exercise in implementing enough parts of std::vector/any_of to reproduce MS JustMyCode example (https://blogs.msdn.microsoft.com/vcblog/2018/06/29/announcing-jmc-stepping-in-visual-studio/); tstd takes 8 F11 presses to get into the predicate body, compared to (allegedly) 140 presses for std:: implementation in VS 2017.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace tstd | |
{ | |
template <typename T> | |
class vector | |
{ | |
public: | |
typedef T* iterator; | |
typedef const T* const_iterator; | |
iterator begin() { return begin_; } | |
const_iterator begin() const { return begin_; } | |
iterator end() { return end_; } | |
const_iterator end() const { return end_; } | |
size_t size() const { return end_ - begin_; } | |
size_t capacity() const { return eos_ - begin_; } | |
vector(): begin_(0), end_(0), eos_(0) | |
{ | |
} | |
vector(size_t size_): begin_(0), end_(0), eos_(0) | |
{ | |
begin_ = (T*)::operator new(size_ * sizeof(T)); | |
end_ = begin_ + size_; | |
eos_ = begin_ + size_; | |
T* begin_r = begin_; | |
for (size_t i = 0; i < size_; ++i) | |
new (begin_r + i) T(); | |
} | |
template <typename U> | |
vector(std::initializer_list<U> list): begin_(0), end_(0), eos_(0) | |
{ | |
size_t size_ = list.size(); | |
begin_ = (T*)::operator new(size_ * sizeof(T)); | |
end_ = begin_ + size_; | |
eos_ = begin_ + size_; | |
T* begin_r = begin_; | |
const U* list_begin_r = list.begin(); | |
for (size_t i = 0; i < size_; ++i) | |
new (begin_r + i) T(list_begin_r[i]); | |
} | |
~vector() | |
{ | |
if (begin_) | |
{ | |
T* begin_r = begin_; | |
T* end_r = end_; | |
for (T* it = begin_r; it != end_r; ++it) | |
it->~T(); | |
::operator delete(begin_r); | |
} | |
} | |
vector(const vector& other): begin_(0), end_(0), eos_(0) | |
{ | |
size_t size_ = other.end_ - other.begin_; | |
begin_ = (T*)::operator new(size_ * sizeof(T)); | |
end_ = begin_ + size_; | |
eos_ = begin_ + size_; | |
T* begin_r = begin_; | |
T* other_begin_r = other.begin_; | |
for (size_t i = 0; i < size_; ++i) | |
new (begin_r + i) T(other_begin_r[i]); | |
} | |
vector(vector&& other): begin_(0), end_(0), eos_(0) | |
{ | |
begin_ = other.begin_; | |
end_ = other.end_; | |
eos_ = other.eos_; | |
other.begin_ = 0; | |
other.end_ = 0; | |
other.eos_ = 0; | |
} | |
vector& operator=(const vector& other) | |
{ | |
vector temp(other); | |
*this = std::move(temp); | |
} | |
vector& operator=(vector&& other) | |
{ | |
T* temp; | |
temp = begin_, begin_ = other.begin_, other.begin_ = temp; | |
temp = end_, end_ = other.end_, other.end_ = temp; | |
temp = eos_, eos_ = other.eos_, other.eos_ = temp; | |
return *this; | |
} | |
private: | |
T* begin_; | |
T* end_; | |
T* eos_; | |
}; | |
template <typename It, typename Pred> bool any_of(It begin, It end, Pred pred) | |
{ | |
for (It it = begin; it != end; ++it) | |
if (pred(*it)) | |
return true; | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment