diff options
Diffstat (limited to 'ue5/array.hpp')
| -rw-r--r-- | ue5/array.hpp | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/ue5/array.hpp b/ue5/array.hpp index 37a33b4..279ad7d 100644 --- a/ue5/array.hpp +++ b/ue5/array.hpp | |||
| @@ -4,35 +4,39 @@ | |||
| 4 | #undef SOLVED_2 | 4 | #undef SOLVED_2 |
| 5 | #define SOLVED_2 | 5 | #define SOLVED_2 |
| 6 | 6 | ||
| 7 | /* TODO: | ||
| 8 | * Angabe: Im Gegensatz zu den Beispielen bis jetzt ist die Verwendung | ||
| 9 | * der STL und Boostuntersagt! | ||
| 10 | */ | ||
| 11 | #include <utility> // for std::move | 7 | #include <utility> // for std::move |
| 12 | #include <stdexcept> // for std::out_of_range | 8 | #include <stdexcept> // for std::out_of_range |
| 13 | #include <algorithm> // for std::fill_n + std::swap_ranges | ||
| 14 | 9 | ||
| 15 | namespace Ti | 10 | namespace Ti |
| 16 | { | 11 | { |
| 17 | template<typename T, size_t N> | 12 | template<typename T, std::size_t N> |
| 18 | struct array | 13 | struct array |
| 19 | { | 14 | { |
| 20 | typedef T& reference; | 15 | typedef T& reference; |
| 21 | typedef const T& const_reference; | 16 | typedef const T& const_reference; |
| 22 | typedef T* iterator; | 17 | typedef T* iterator; |
| 23 | typedef const T* const_iterator; | 18 | typedef const T* const_iterator; |
| 24 | typedef size_t size_type; | 19 | typedef std::size_t size_type; |
| 25 | 20 | ||
| 26 | static_assert(N != 0, "array<T, 0> not allowed"); | 21 | static_assert(N != 0, "array<T, 0> not allowed"); |
| 27 | 22 | ||
| 28 | void fill (const T& u) | 23 | void fill(const T& u) |
| 29 | { | 24 | { |
| 30 | std::fill_n(begin(), size(), u); | 25 | for(size_type i = 0; i < size(); ++i) |
| 26 | m_data[i] = u; | ||
| 27 | /* std::fill_n(begin(), size(), u); */ | ||
| 31 | } | 28 | } |
| 32 | 29 | ||
| 33 | void swap (array<T,N> & other) | 30 | /* range check not necessary. N must be the same in other */ |
| 31 | void swap(array<T,N> & other) | ||
| 34 | { | 32 | { |
| 35 | std::swap_ranges(begin(), end(), other.begin()); | 33 | for(size_type i = 0; i < size(); ++i) |
| 34 | { | ||
| 35 | T x(m_data[i]); | ||
| 36 | m_data[i] = other[i]; | ||
| 37 | other[i] = x; | ||
| 38 | } | ||
| 39 | /* std::swap_ranges(begin(), end(), other.begin()); */ | ||
| 36 | } | 40 | } |
| 37 | 41 | ||
| 38 | iterator begin() | 42 | iterator begin() |
| @@ -70,12 +74,12 @@ namespace Ti | |||
| 70 | return false; /* size() == 0 */ | 74 | return false; /* size() == 0 */ |
| 71 | } | 75 | } |
| 72 | 76 | ||
| 73 | reference operator[] (size_type n) | 77 | reference operator[](size_type n) |
| 74 | { | 78 | { |
| 75 | return m_data[n]; | 79 | return m_data[n]; |
| 76 | } | 80 | } |
| 77 | 81 | ||
| 78 | const_reference operator[] (size_type n) const | 82 | const_reference operator[](size_type n) const |
| 79 | { | 83 | { |
| 80 | return m_data[n]; | 84 | return m_data[n]; |
| 81 | } | 85 | } |
| @@ -87,7 +91,7 @@ namespace Ti | |||
| 87 | return m_data[n]; | 91 | return m_data[n]; |
| 88 | } | 92 | } |
| 89 | 93 | ||
| 90 | const_reference at (size_type n) const | 94 | const_reference at(size_type n) const |
| 91 | { | 95 | { |
| 92 | if (n >= size()) | 96 | if (n >= size()) |
| 93 | throw std::out_of_range("array::at"); | 97 | throw std::out_of_range("array::at"); |
| @@ -103,8 +107,8 @@ namespace Ti | |||
| 103 | T m_data[N]; | 107 | T m_data[N]; |
| 104 | }; | 108 | }; |
| 105 | 109 | ||
| 106 | /* TODO: was macht std::move() genau?! :) */ | 110 | /* std::move returns lvalue as rvalue */ |
| 107 | template<typename T, size_t N> | 111 | template<typename T, std::size_t N> |
| 108 | array<T, N>&& make_array() | 112 | array<T, N>&& make_array() |
| 109 | { | 113 | { |
| 110 | return std::move(array<T, N>()); | 114 | return std::move(array<T, N>()); |
