/Src/Dependencies/Boost/boost/multi_index/detail/rnd_index_ptr_array.hpp

http://hadesmem.googlecode.com/ · C++ Header · 143 lines · 110 code · 25 blank · 8 comment · 2 complexity · 50d16cd4c90a295fa0b8c198f42ce727 MD5 · raw file

  1. /* Copyright 2003-2008 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_DETAIL_RND_INDEX_PTR_ARRAY_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_RND_INDEX_PTR_ARRAY_HPP
  10. #if defined(_MSC_VER)&&(_MSC_VER>=1200)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <algorithm>
  15. #include <boost/detail/allocator_utilities.hpp>
  16. #include <boost/multi_index/detail/auto_space.hpp>
  17. #include <boost/multi_index/detail/prevent_eti.hpp>
  18. #include <boost/multi_index/detail/rnd_index_node.hpp>
  19. #include <boost/noncopyable.hpp>
  20. #include <cstddef>
  21. namespace boost{
  22. namespace multi_index{
  23. namespace detail{
  24. /* pointer structure for use by random access indices */
  25. template<typename Allocator>
  26. class random_access_index_ptr_array:private noncopyable
  27. {
  28. typedef typename prevent_eti<
  29. Allocator,
  30. random_access_index_node_impl<
  31. typename boost::detail::allocator::rebind_to<
  32. Allocator,
  33. char
  34. >::type
  35. >
  36. >::type node_impl_type;
  37. public:
  38. typedef typename node_impl_type::pointer value_type;
  39. typedef typename prevent_eti<
  40. Allocator,
  41. typename boost::detail::allocator::rebind_to<
  42. Allocator,value_type
  43. >::type
  44. >::type::pointer pointer;
  45. random_access_index_ptr_array(
  46. const Allocator& al,value_type end_,std::size_t size):
  47. size_(size),
  48. capacity_(size),
  49. spc(al,capacity_+1)
  50. {
  51. *end()=end_;
  52. end_->up()=end();
  53. }
  54. std::size_t size()const{return size_;}
  55. std::size_t capacity()const{return capacity_;}
  56. void room_for_one()
  57. {
  58. if(size_==capacity_){
  59. reserve(capacity_<=10?15:capacity_+capacity_/2);
  60. }
  61. }
  62. void reserve(std::size_t c)
  63. {
  64. if(c>capacity_){
  65. auto_space<value_type,Allocator> spc1(spc.get_allocator(),c+1);
  66. node_impl_type::transfer(begin(),end()+1,spc1.data());
  67. spc.swap(spc1);
  68. capacity_=c;
  69. }
  70. }
  71. pointer begin()const{return ptrs();}
  72. pointer end()const{return ptrs()+size_;}
  73. pointer at(std::size_t n)const{return ptrs()+n;}
  74. void push_back(value_type x)
  75. {
  76. *(end()+1)=*end();
  77. (*(end()+1))->up()=end()+1;
  78. *end()=x;
  79. (*end())->up()=end();
  80. ++size_;
  81. }
  82. void erase(value_type x)
  83. {
  84. node_impl_type::extract(x->up(),end()+1);
  85. --size_;
  86. }
  87. void clear()
  88. {
  89. *begin()=*end();
  90. (*begin())->up()=begin();
  91. size_=0;
  92. }
  93. void swap(random_access_index_ptr_array& x)
  94. {
  95. std::swap(size_,x.size_);
  96. std::swap(capacity_,x.capacity_);
  97. spc.swap(x.spc);
  98. }
  99. private:
  100. std::size_t size_;
  101. std::size_t capacity_;
  102. auto_space<value_type,Allocator> spc;
  103. pointer ptrs()const
  104. {
  105. return spc.data();
  106. }
  107. };
  108. template<typename Allocator>
  109. void swap(
  110. random_access_index_ptr_array<Allocator>& x,
  111. random_access_index_ptr_array<Allocator>& y)
  112. {
  113. x.swap(y);
  114. }
  115. } /* namespace multi_index::detail */
  116. } /* namespace multi_index */
  117. } /* namespace boost */
  118. #endif