From 39b0cb9afeb53e9a1720bad64347e72d318c554a Mon Sep 17 00:00:00 2001 From: manuel Date: Wed, 3 Jun 2009 17:26:57 +0200 Subject: replacing 1xtab with 2xspace implementing array --- ue5/array.hpp | 136 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 104 insertions(+), 32 deletions(-) (limited to 'ue5/array.hpp') diff --git a/ue5/array.hpp b/ue5/array.hpp index 19cdc02..37a33b4 100644 --- a/ue5/array.hpp +++ b/ue5/array.hpp @@ -2,44 +2,116 @@ #define ARRAY_H #undef SOLVED_2 -//#define SOLVED_2 +#define SOLVED_2 -/* TODO Includes */ +/* TODO: + * Angabe: Im Gegensatz zu den Beispielen bis jetzt ist die Verwendung + * der STL und Boostuntersagt! + */ +#include // for std::move +#include // for std::out_of_range +#include // for std::fill_n + std::swap_ranges namespace Ti { + template + struct array + { + typedef T& reference; + typedef const T& const_reference; + typedef T* iterator; + typedef const T* const_iterator; + typedef size_t size_type; -/* TODO */ -struct array -{ - /* TODO typedef */ - - /* TODO static_assert */ - - /* TODO: - void fill (const T& u); - void swap (array & other); - iterator begin(); - const_iterator begin() const; - iterator end(); - const_iterator end() const; - size_type size() const; - size_type max_size() const; - bool empty() const; - reference operator[] (size_type n); - const_reference operator[] (size_type n) const; - reference at(size_type n); - const_reference at (size_type n) const; - T* data(); - */ - -private: - /* TODO data */ -}; - -/* TODO */ -array/* TODO */&& make_array(); + static_assert(N != 0, "array not allowed"); + + void fill (const T& u) + { + std::fill_n(begin(), size(), u); + } + + void swap (array & other) + { + std::swap_ranges(begin(), end(), other.begin()); + } + + iterator begin() + { + return m_data; + } + + const_iterator begin() const + { + return m_data; + } + + iterator end() + { + return m_data + size(); + } + + const_iterator end() const + { + return m_data + size(); + } + + size_type size() const + { + return N; + } + + size_type max_size() const + { + return N; + } + + bool empty() const + { + return false; /* size() == 0 */ + } + + reference operator[] (size_type n) + { + return m_data[n]; + } + + const_reference operator[] (size_type n) const + { + return m_data[n]; + } + + reference at(size_type n) + { + if (n >= size()) + throw std::out_of_range("array::at"); + return m_data[n]; + } + + const_reference at (size_type n) const + { + if (n >= size()) + throw std::out_of_range("array::at"); + return m_data[n]; + } + + T* data() + { + return m_data; + } + + private: + T m_data[N]; + }; + + /* TODO: was macht std::move() genau?! :) */ + template + array&& make_array() + { + return std::move(array()); + } } // namespace #endif + +/* vim: set et sw=2 ts=2: */ -- cgit v1.2.3