/* The following code declares class array, * an STL container (as wrapper) for arrays of constant size. * * See * http://www.josuttis.com/cppcode * for details and the latest version. * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. * * Jul 31, 2000 */ #ifndef BOOST_ARRAY_HPP #define BOOST_ARRAY_HPP #include #include #include #include #include // for std::size_t and std::ptrdiff_t workarounds namespace boost { template class array { public: T elems[N]; // fixed-size array of elements of type T public: // type definitions typedef T value_type; typedef T* iterator; typedef const T* const_iterator; typedef T& reference; typedef const T& const_reference; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; // iterator support iterator begin() { return elems; } const_iterator begin() const { return elems; } iterator end() { return elems+N; } const_iterator end() const { return elems+N; } // reverse iterator support typedef std::reverse_iterator reverse_iterator; typedef std::reverse_iterator const_reverse_iterator; reverse_iterator rbegin() { return reverse_iterator(end()); } const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } reverse_iterator rend() { return reverse_iterator(begin()); } const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } // operator[] reference operator[](size_type i) { return elems[i]; } const_reference operator[](size_type i) const { return elems[i]; } // at() with range check // note: rangecheck() is public because we have implemented array // as aggregate, which forbids non-public members void rangecheck (size_type i) const { if (i >= size()) { throw std::range_error("array"); } } reference at(size_type i) { rangecheck(i); return elems[i]; } const_reference at(size_type i) const { rangecheck(i); return elems[i]; } // front() and back() reference front() { return elems[0]; } const_reference front() const { return elems[0]; } reference back() { return elems[N-1]; } const_reference back() const { return elems[N-1]; } // size is constant static size_type size() { return N; } static bool empty() { return false; } static size_type max_size() { return N; } enum { static_size = N }; // swap (note: linear complexity) void swap (array& y) { std::swap_ranges(begin(),end(),y.begin()); } // direct access to data const T* data() const { return elems; } // assignment with type conversion //template //T& operator= (const array& rhs) { // std::copy (begin(),end(),rhs.begin()); //} }; // comparisons template bool operator== (const array& x, const array& y) { return std::equal(x.begin(), x.end(), y.begin()); } template bool operator< (const array& x, const array& y) { return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end()); } template bool operator!= (const array& x, const array& y) { return !(x==y); } template bool operator> (const array& x, const array& y) { return y bool operator<= (const array& x, const array& y) { return !(y bool operator>= (const array& x, const array& y) { return !(x inline void swap (const array& x, const array& y) { x.swap(y); } } /* namespace boost */ #endif /*BOOST_ARRAY_HPP*/