pushable_array.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Boost.Geometry Index
  2. //
  3. // Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland.
  4. //
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_PUSHABLE_ARRAY_HPP
  9. #define BOOST_GEOMETRY_INDEX_DETAIL_PUSHABLE_ARRAY_HPP
  10. #include <boost/array.hpp>
  11. #include <boost/geometry/index/detail/assert.hpp>
  12. namespace boost { namespace geometry { namespace index { namespace detail {
  13. template <typename Element, size_t Capacity>
  14. class pushable_array
  15. {
  16. typedef typename boost::array<Element, Capacity> array_type;
  17. public:
  18. typedef typename array_type::value_type value_type;
  19. typedef typename array_type::size_type size_type;
  20. typedef typename array_type::iterator iterator;
  21. typedef typename array_type::const_iterator const_iterator;
  22. typedef typename array_type::reverse_iterator reverse_iterator;
  23. typedef typename array_type::const_reverse_iterator const_reverse_iterator;
  24. typedef typename array_type::reference reference;
  25. typedef typename array_type::const_reference const_reference;
  26. inline pushable_array()
  27. : m_size(0)
  28. {}
  29. inline explicit pushable_array(size_type s)
  30. : m_size(s)
  31. {
  32. BOOST_GEOMETRY_INDEX_ASSERT(s <= Capacity, "size too big");
  33. }
  34. inline void resize(size_type s)
  35. {
  36. BOOST_GEOMETRY_INDEX_ASSERT(s <= Capacity, "size too big");
  37. m_size = s;
  38. }
  39. inline void reserve(size_type /*s*/)
  40. {
  41. //BOOST_GEOMETRY_INDEX_ASSERT(s <= Capacity, "size too big");
  42. // do nothing
  43. }
  44. inline Element & operator[](size_type i)
  45. {
  46. BOOST_GEOMETRY_INDEX_ASSERT(i < m_size, "index of the element outside the container");
  47. return m_array[i];
  48. }
  49. inline Element const& operator[](size_type i) const
  50. {
  51. BOOST_GEOMETRY_INDEX_ASSERT(i < m_size, "index of the element outside the container");
  52. return m_array[i];
  53. }
  54. inline Element const& front() const
  55. {
  56. BOOST_GEOMETRY_INDEX_ASSERT(0 < m_size, "there are no elements in the container");
  57. return m_array.front();
  58. }
  59. inline Element & front()
  60. {
  61. BOOST_GEOMETRY_INDEX_ASSERT(0 < m_size, "there are no elements in the container");
  62. return m_array.front();
  63. }
  64. inline Element const& back() const
  65. {
  66. BOOST_GEOMETRY_INDEX_ASSERT(0 < m_size, "there are no elements in the container");
  67. return *(begin() + (m_size - 1));
  68. }
  69. inline Element & back()
  70. {
  71. BOOST_GEOMETRY_INDEX_ASSERT(0 < m_size, "there are no elements in the container");
  72. return *(begin() + (m_size - 1));
  73. }
  74. inline iterator begin()
  75. {
  76. return m_array.begin();
  77. }
  78. inline iterator end()
  79. {
  80. return m_array.begin() + m_size;
  81. }
  82. inline const_iterator begin() const
  83. {
  84. return m_array.begin();
  85. }
  86. inline const_iterator end() const
  87. {
  88. return m_array.begin() + m_size;
  89. }
  90. inline reverse_iterator rbegin()
  91. {
  92. return reverse_iterator(end());
  93. }
  94. inline reverse_iterator rend()
  95. {
  96. return reverse_iterator(begin());
  97. }
  98. inline const_reverse_iterator rbegin() const
  99. {
  100. return const_reverse_iterator(end());
  101. }
  102. inline const_reverse_iterator rend() const
  103. {
  104. return const_reverse_iterator(begin());
  105. }
  106. inline void clear()
  107. {
  108. m_size = 0;
  109. }
  110. inline void push_back(Element const& v)
  111. {
  112. BOOST_GEOMETRY_INDEX_ASSERT(m_size < Capacity, "can't further increase the size of the container");
  113. m_array[m_size] = v;
  114. ++m_size;
  115. }
  116. inline void pop_back()
  117. {
  118. BOOST_GEOMETRY_INDEX_ASSERT(0 < m_size, "there are no elements in the container");
  119. --m_size;
  120. }
  121. inline bool empty() const
  122. {
  123. return m_size == 0;
  124. }
  125. inline size_t size() const
  126. {
  127. return m_size;
  128. }
  129. inline size_t capacity() const
  130. {
  131. return Capacity;
  132. }
  133. private:
  134. boost::array<Element, Capacity> m_array;
  135. size_type m_size;
  136. };
  137. }}}} // namespace boost::geometry::index::detail
  138. #endif // BOOST_GEOMETRY_INDEX_DETAIL_PUSHABLE_ARRAY_HPP