diff options
author | Nao Pross <naopross@thearcway.org> | 2018-12-10 13:57:15 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2018-12-10 13:57:15 +0100 |
commit | b58a953f3fc9ec28ea5487e9c3372489ef62e4c5 (patch) | |
tree | dd69f6fa32287398a6cd37b34fcb0b82fa87081d /vector/vector.c | |
parent | Add C++ templated vector example (diff) | |
download | cplusplus-b58a953f3fc9ec28ea5487e9c3372489ef62e4c5.tar.gz cplusplus-b58a953f3fc9ec28ea5487e9c3372489ef62e4c5.zip |
Simplified vector by inheriting std::array instead of having m_data
Diffstat (limited to '')
-rw-r--r-- | vector/vector.cpp | 60 |
1 files changed, 8 insertions, 52 deletions
diff --git a/vector/vector.cpp b/vector/vector.cpp index 972ade2..f422db2 100644 --- a/vector/vector.cpp +++ b/vector/vector.cpp @@ -6,12 +6,8 @@ #include <algorithm> template<typename T, std::size_t d> -class basic_vec { +class basic_vec : public std::array<T, d> { public: - typedef T value_type; - typedef typename std::array<T, d>::iterator iterator; - typedef typename std::array<T, d>::const_iterator const_iterator; - static constexpr std::size_t dimensions = d; basic_vec(); @@ -20,44 +16,34 @@ public: basic_vec(const basic_vec<T, n>& other); T length() const; - T& operator[](std::size_t idx); - const T& operator[](std::size_t idx) const; - - iterator begin(); - const_iterator cbegin() const; - iterator end(); - const_iterator cend() const; - -private: - std::array<T, d> m_data; }; template<typename T, std::size_t d> -basic_vec<T, d>::basic_vec() { - m_data.fill(static_cast<T>(0)); +basic_vec<T, d>::basic_vec() : std::array<T, d>() { + this->fill(static_cast<T>(0)); } template<typename T, std::size_t d> basic_vec<T, d>::basic_vec(std::initializer_list<T> l) { assert(l.size() <= d); - m_data.fill(static_cast<T>(0)); - std::copy(l.begin(), l.end(), m_data.begin()); + this->fill(static_cast<T>(0)); + std::copy(l.begin(), l.end(), this->begin()); } template<typename T, std::size_t d> template<std::size_t n> basic_vec<T, d>::basic_vec(const basic_vec<T, n>& other) { static_assert(d >= n); - m_data.fill(static_cast<T>(0)); + this->fill(static_cast<T>(0)); for (std::size_t i = 0; i < n; i++) { - m_data[i] = other[i]; + this[i] = other[i]; } } template<typename T, std::size_t d> T basic_vec<T, d>::length() const { T res = static_cast<T>(0); - for (const T& val : m_data) { + for (const T& val : *this) { res += val * val; } @@ -65,36 +51,6 @@ T basic_vec<T, d>::length() const { } template<typename T, std::size_t d> -T& basic_vec<T, d>::operator[](std::size_t idx) { - return m_data[idx]; -} - -template<typename T, std::size_t d> -const T& basic_vec<T, d>::operator[](std::size_t idx) const { - return m_data[idx]; -} - -template<typename T, std::size_t d> -typename basic_vec<T, d>::iterator basic_vec<T, d>::begin() { - return m_data.begin(); -} - -template<typename T, std::size_t d> -typename basic_vec<T, d>::const_iterator basic_vec<T, d>::cbegin() const { - return m_data.cbegin(); -} - -template<typename T, std::size_t d> -typename basic_vec<T, d>::iterator basic_vec<T, d>::end() { - return m_data.end(); -} - -template<typename T, std::size_t d> -typename basic_vec<T, d>::const_iterator basic_vec<T, d>::cend() const { - return m_data.cend(); -} - -template<typename T, std::size_t d> basic_vec<T, d> operator+(const basic_vec<T, d>& rhs, const basic_vec<T, d>& lhs) { basic_vec<T, d> out; auto rit = rhs.cbegin(), lit = lhs.cbegin(); |