/** * @module array * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) * @brief Class template array * @date 13.06.2009 */ #ifndef ARRAY_H #define ARRAY_H #include #include #include #undef SOLVED_2 #define SOLVED_2 namespace Ti { /** * @class array * * Array defines a class template for storing fixed-size sequences of objects. */ template struct array { /** lvalue of T */ typedef T& reference; /** constant lvalue of T */ typedef const T& const_reference; /** iterator type whose value type is T */ typedef T* iterator; /** constant iterator type whose value type is T */ typedef const T* const_iterator; /** unsigned integral type */ typedef std::size_t size_type; /* we don't suppport zero-sized arrays */ static_assert(N != 0, "array not allowed"); /** * @method fill * @brief Fills container with elements of u * @param u object to fill array with * @return return N * @globalvars none * @exception none * @pre requires CopyAssignable void fill(const T& u) * @post none */ void fill(const T& u) { std::fill_n(begin(), N, u); } /** * @method swap * @brief Exchanges the contents of *this and other. * @param other object to swap with * @return - * @globalvars none * @exception none * @pre none * @post none */ void swap(array& other) { std::swap_ranges(begin(), end(), other.begin()); } /** * @method begin * @brief Get an iterator referring to the first element in the container * @param - * @return iterator referring to the first element in the container. * @globalvars none * @exception none * @pre none * @post none */ iterator begin() { return m_data; } /** * @method begin * @brief Get a constant iterator referring to the first element in the * container * @param - * @return constant iterator referring to the first element in the container. * @globalvars none * @exception none * @pre none * @post none */ const_iterator begin() const { return m_data; } /** * @method end * @brief Get an iterator which is the past-the-end value for the container. * @param - * @return iterator which is the past-the-end value for the container * @globalvars none * @exception none * @pre none * @post none */ iterator end() { return m_data + N; } /** * @method end * @brief Get a constant iterator which is the past-the-end value for the * container. * @param - * @return constant iterator which is the past-the-end value for the container * @globalvars none * @exception none * @pre none * @post none */ const_iterator end() const { return m_data + N; } /** * @method size * @brief Get number of elements in the container * @param - * @return return N * @globalvars none * @exception none * @pre none * @post none */ size_type size() const { return N; } /** * @method max_size * @brief Get maximal number of elements which fit into the container * @param - * @return return N * @globalvars none * @exception none * @pre none * @post none */ size_type max_size() const { return N; } /** * @method empty * @brief Check if container is empty * @param - * @return always false * @globalvars none * @exception none * @pre none * @post none */ bool empty() const { return false; /* size() == 0 */ } /** * @method operator[] * @brief operator [] * @param n position of an element in the array * @return reference to the element at position n in the container * @globalvars none * @exception none * @pre none * @post none */ reference operator[](size_type n) { return m_data[n]; } /** * @method operator[] * @brief operator [] * @param n position of an element in the array * @return constant reference to the element at position n in the container * @globalvars none * @exception none * @pre none * @post none */ const_reference operator[](size_type n) const { return m_data[n]; } /** * @method at * @brief Get reference to the element at position n * @param n position of an element in the array * @return reference to the element at position n in the container. * The difference between this member function and member operator * function operator[] is that at signals if the requested * position is out of range by throwing an out_of_range exception. * @globalvars none * @exception out_of_range * @pre none * @post none */ reference at(size_type n) { if (n >= N) throw std::out_of_range("array::at"); return m_data[n]; } /** * @method at * @brief Get constant reference to the element at position n * @param n position of an element in the array * @return constant reference to the element at position n in the container. * The difference between this member function and member operator * function operator[] is that at signals if the requested * position is out of range by throwing an out_of_range exception. * @globalvars none * @exception out_of_range * @pre none * @post none */ const_reference at(size_type n) const { if (n >= N) throw std::out_of_range("array::at"); return m_data[n]; } /** * @method data * @brief Check if container is empty * @param - * @return returns enclosed array * @globalvars none * @exception none * @pre none * @post none */ T* data() { return m_data; } private: /** enclosed array */ T m_data[N]; }; /** * @method make_array * @brief Get an rvalue of an empty array * @param none * @return rvalue of an empty array * @globalvars none * @exception none * @pre none * @post none */ template array&& make_array() { return std::move(array()); } } // namespace #endif /* vim: set et sw=2 ts=2: */