Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:45:58

0001 /* Copyright 2024 Joaquin M Lopez Munoz.
0002  * Distributed under the Boost Software License, Version 1.0.
0003  * (See accompanying file LICENSE_1_0.txt or copy at
0004  * http://www.boost.org/LICENSE_1_0.txt)
0005  *
0006  * See http://www.boost.org/libs/poly_collection for library home page.
0007  */
0008 
0009 #ifndef BOOST_POLY_COLLECTION_DETAIL_SIZE_T_MAP_HPP
0010 #define BOOST_POLY_COLLECTION_DETAIL_SIZE_T_MAP_HPP
0011 
0012 #if defined(_MSC_VER)
0013 #pragma once
0014 #endif
0015 
0016 #include <boost/assert.hpp>
0017 #include <cstddef>
0018 #include <memory>
0019 #include <utility>
0020 #include <vector>
0021 
0022 namespace boost{
0023 
0024 namespace poly_collection{
0025 
0026 namespace detail{
0027 
0028 /* Map-like wrapper over a vector of std::pair<std::size_t,T>. It is required
0029  * that the first member of the i-th element be i, which implies that
0030  * insert(i,x) must executed in increasing order of i.
0031  */
0032 
0033 template<typename T,typename Allocator>
0034 class size_t_map
0035 {
0036   using vector_type=std::vector<
0037     std::pair<std::size_t,T>,
0038     typename std::allocator_traits<Allocator>::template
0039       rebind_alloc<std::pair<std::size_t,T>>
0040   >;
0041 
0042 public:
0043   using key_type=std::size_t;
0044   using mapped_type=T;
0045   using value_type=typename vector_type::value_type;
0046   using allocator_type=typename vector_type::allocator_type;
0047   using iterator=typename vector_type::iterator;
0048   using const_iterator=typename vector_type::const_iterator;
0049 
0050   size_t_map()=default;
0051   size_t_map(const size_t_map& x)=default;
0052   size_t_map(size_t_map&& x)=default;
0053   size_t_map(const allocator_type& al):v{al}{}
0054   size_t_map(const size_t_map& x,const allocator_type& al):v{x.v,al}{}
0055   size_t_map(size_t_map&& x,const allocator_type& al):v{std::move(x.v),al}{}
0056   size_t_map& operator=(const size_t_map& x)=default;
0057   size_t_map& operator=(size_t_map&& x)=default;
0058 
0059   allocator_type get_allocator()const noexcept{return v.get_allocator();}
0060 
0061   iterator       begin()noexcept{return v.begin();}
0062   iterator       end()noexcept{return v.end();}
0063   const_iterator begin()const noexcept{return v.begin();}
0064   const_iterator end()const noexcept{return v.end();}
0065   const_iterator cbegin()const noexcept{return v.cbegin();}
0066   const_iterator cend()const noexcept{return v.cend();}
0067 
0068   const_iterator find(const key_type& key)const
0069   {
0070     if(key<v.size())return v.begin()+key;
0071     else            return v.end();
0072   }
0073 
0074   template<typename P>
0075   std::pair<iterator,bool> insert(const key_type& key,P&& x)
0076   {
0077     BOOST_ASSERT(key==v.size());
0078     v.emplace_back(key,std::forward<P>(x));
0079     return {v.end()-1,true};
0080   }
0081 
0082   void swap(size_t_map& x){v.swap(x.v);}
0083 
0084 private:
0085   vector_type v;
0086 };
0087 
0088 template<typename T,typename Allocator>
0089 void swap(size_t_map<T,Allocator>& x,size_t_map<T,Allocator>& y)
0090 {
0091   x.swap(y);
0092 }
0093 
0094 } /* namespace poly_collection::detail */
0095 
0096 } /* namespace poly_collection */
0097 
0098 } /* namespace boost */
0099 
0100 #endif