
Thorsten Ottosen skrev:
Hi Ion,
When using e.g. a flat_set as a priority queue, it is annoying not to have back() and pop_back() available.
As for operator[](), then of couse cont.begin()[n] works, but this is somewhat less pretty compared to just cont[n].
Therefore I would really like to see all the above functions added to the interface.
Hi Ion, I have attached the patches neccessary for this change. Please give it a look at let me know what you think. Thanks -Thorsten Index: flat_set.hpp =================================================================== --- flat_set.hpp (revision 61938) +++ flat_set.hpp (working copy) @@ -308,6 +308,76 @@ size_type max_size() const { return m_flat_tree.max_size(); } + //! <b>Effects</b>: Returns the first element of the container. + //! + //! <b>Requires</b>: A non-empty container. + //! + //! <b>Throws</b>: Nothing. + //! + //! <b>Complexity</b>: Constant. + reference front() + { return m_flat_tree.front(); } + + //! <b>Effects</b>: Returns the first element of the container. + //! + //! <b>Requires</b>: A non-empty container. + //! + //! <b>Throws</b>: Nothing. + //! + //! <b>Complexity</b>: Constant. + const_reference front() const + { return m_flat_tree.front(); } + + //! <b>Effects</b>: Returns the last element of the container. + //! + //! <b>Requires</b>: A non-empty container. + //! + //! <b>Throws</b>: Nothing. + //! + //! <b>Complexity</b>: Constant. + reference back() + { return m_flat_tree.back(); } + + //! <b>Effects</b>: Returns the last element of the container. + //! + //! <b>Requires</b>: A non-empty container. + //! + //! <b>Throws</b>: Nothing. + //! + //! <b>Complexity</b>: Constant. + const_reference back() const + { return m_flat_tree.back(); } + + //! <b>Effects</b>: Returns the 'n'th element of the container. + //! + //! <b>Requires</b>: 'size() > n' + //! + //! <b>Throws</b>: Nothing. + //! + //! <b>Complexity</b>: Constant. + reference operator[](size_type n) + { return m_flat_tree[n]; } + + //! <b>Effects</b>: Returns the 'n'th element of the container. + //! + //! <b>Requires</b>: 'size() > n' + //! + //! <b>Throws</b>: Nothing. + //! + //! <b>Complexity</b>: Constant. + const_reference operator[](size_type n) const + { return m_flat_tree[n]; } + + //! <b>Effects</b>: Erases the last element of the container. + //! + //! <b>Requires</b>: A non-empty container. + //! + //! <b>Throws</b>: Nothing. + //! + //! <b>Complexity</b>: Constant. + void pop_back() + { return m_flat_tree.pop_back(); } + //! <b>Effects</b>: Swaps the contents of *this and x. //! If this->allocator_type() != x.allocator_type() allocators are also swapped. //! Index: flat_map.hpp =================================================================== --- flat_map.hpp (revision 61938) +++ flat_map.hpp (working copy) @@ -843,9 +843,18 @@ template<class D, class S> static D force_copy(S s) { - value_type *vp = reinterpret_cast<value_type *>(&*s); + value_type *vp = static_cast<value_type *>( + static_cast<void*>(&*s)); return D(vp); } + + template< class Pair, class InternalPair > + static Pair force_pair_copy( const InternalPair& p ) + { + typedef typename Pair::first_type iter; + return Pair( force_copy<iter>( p.first ), force_copy<iter>( p.second ) ); + } + /// @endcond public: @@ -1049,6 +1058,76 @@ size_type max_size() const { return m_flat_tree.max_size(); } + //! <b>Effects</b>: Returns the first element of the container. + //! + //! <b>Requires</b>: A non-empty container. + //! + //! <b>Throws</b>: Nothing. + //! + //! <b>Complexity</b>: Constant. + reference front() + { return m_flat_tree.front(); } + + //! <b>Effects</b>: Returns the first element of the container. + //! + //! <b>Requires</b>: A non-empty container. + //! + //! <b>Throws</b>: Nothing. + //! + //! <b>Complexity</b>: Constant. + const_reference front() const + { return m_flat_tree.front(); } + + //! <b>Effects</b>: Returns the last element of the container. + //! + //! <b>Requires</b>: A non-empty container. + //! + //! <b>Throws</b>: Nothing. + //! + //! <b>Complexity</b>: Constant. + reference back() + { return m_flat_tree.back(); } + + //! <b>Effects</b>: Returns the last element of the container. + //! + //! <b>Requires</b>: A non-empty container. + //! + //! <b>Throws</b>: Nothing. + //! + //! <b>Complexity</b>: Constant. + const_reference back() const + { return m_flat_tree.back(); } + + //! <b>Effects</b>: Returns the 'n'th element of the container. + //! + //! <b>Requires</b>: 'size() > n' + //! + //! <b>Throws</b>: Nothing. + //! + //! <b>Complexity</b>: Constant. + reference operator[](size_type n) + { return m_flat_tree[n]; } + + //! <b>Effects</b>: Returns the 'n'th element of the container. + //! + //! <b>Requires</b>: 'size() > n' + //! + //! <b>Throws</b>: Nothing. + //! + //! <b>Complexity</b>: Constant. + const_reference operator[](size_type n) const + { return m_flat_tree[n]; } + + //! <b>Effects</b>: Erases the last element of the container. + //! + //! <b>Requires</b>: A non-empty container. + //! + //! <b>Throws</b>: Nothing. + //! + //! <b>Complexity</b>: Constant. + void pop_back() + { return m_flat_tree.pop_back(); } + //! <b>Effects</b>: Swaps the contents of *this and x. //! If this->allocator_type() != x.allocator_type() allocators are also swapped. //! @@ -1313,14 +1392,14 @@ //! //! <b>Complexity</b>: Logarithmic std::pair<iterator,iterator> equal_range(const key_type& x) - { return force_copy<std::pair<iterator,iterator> >(m_flat_tree.equal_range(x)); } + { return force_pair_copy<std::pair<iterator,iterator> >(m_flat_tree.equal_range(x)); } //! <b>Effects</b>: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)). //! //! <b>Complexity</b>: Logarithmic std::pair<const_iterator,const_iterator> equal_range(const key_type& x) const - { return force_copy<std::pair<const_iterator,const_iterator> >(m_flat_tree.equal_range(x)); } + { return force_pair_copy<std::pair<const_iterator,const_iterator> >(m_flat_tree.equal_range(x)); } //! <b>Effects</b>: Number of elements for which memory has been allocated. //! capacity() is always greater than or equal to size(). Index: flat_tree.hpp =================================================================== --- flat_tree.hpp (revision 61938) +++ flat_tree.hpp (working copy) @@ -239,6 +239,27 @@ size_type max_size() const { return this->m_data.m_vect.max_size(); } + reference front() + { return this->m_data.m_vect.front(); } + + const_reference front() const + { return this->m_data.m_vect.front(); } + + reference back() + { return this->m_data.m_vect.back(); } + + const_reference back() const + { return this->m_data.m_vect.back(); } + + reference operator[]( size_type at ) + { return this->m_data.m_vect[at]; } + + const_reference operator[]( size_type at ) const + { return this->m_data.m_vect[at]; } + + void pop_back() + { this->m_data.m_vect.pop_back(); } + void swap(flat_tree& other) { value_compare& mycomp = this->m_data;