]>
Commit | Line | Data |
---|---|---|
11fdf7f2 | 1 | /* Copyright 2003-2018 Joaquin M Lopez Munoz. |
7c673cae FG |
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 | ||
9 | #ifndef BOOST_MULTI_INDEX_DETAIL_RND_INDEX_PTR_ARRAY_HPP | |
10 | #define BOOST_MULTI_INDEX_DETAIL_RND_INDEX_PTR_ARRAY_HPP | |
11 | ||
12 | #if defined(_MSC_VER) | |
13 | #pragma once | |
14 | #endif | |
15 | ||
16 | #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */ | |
17 | #include <algorithm> | |
18 | #include <boost/detail/allocator_utilities.hpp> | |
19 | #include <boost/multi_index/detail/auto_space.hpp> | |
20 | #include <boost/multi_index/detail/rnd_index_node.hpp> | |
21 | #include <boost/noncopyable.hpp> | |
22 | #include <cstddef> | |
11fdf7f2 | 23 | #include <memory> |
7c673cae FG |
24 | |
25 | namespace boost{ | |
26 | ||
27 | namespace multi_index{ | |
28 | ||
29 | namespace detail{ | |
30 | ||
31 | /* pointer structure for use by random access indices */ | |
32 | ||
33 | template<typename Allocator> | |
34 | class random_access_index_ptr_array:private noncopyable | |
35 | { | |
36 | typedef random_access_index_node_impl< | |
37 | typename boost::detail::allocator::rebind_to< | |
38 | Allocator, | |
39 | char | |
40 | >::type | |
41 | > node_impl_type; | |
42 | ||
43 | public: | |
44 | typedef typename node_impl_type::pointer value_type; | |
45 | typedef typename boost::detail::allocator::rebind_to< | |
46 | Allocator,value_type | |
11fdf7f2 TL |
47 | >::type value_allocator; |
48 | #ifdef BOOST_NO_CXX11_ALLOCATOR | |
49 | typedef typename value_allocator::pointer pointer; | |
50 | #else | |
51 | typedef typename std::allocator_traits< | |
52 | value_allocator | |
53 | >::pointer pointer; | |
54 | #endif | |
7c673cae FG |
55 | |
56 | random_access_index_ptr_array( | |
57 | const Allocator& al,value_type end_,std::size_t sz): | |
58 | size_(sz), | |
59 | capacity_(sz), | |
60 | spc(al,capacity_+1) | |
61 | { | |
62 | *end()=end_; | |
63 | end_->up()=end(); | |
64 | } | |
65 | ||
66 | std::size_t size()const{return size_;} | |
67 | std::size_t capacity()const{return capacity_;} | |
68 | ||
69 | void room_for_one() | |
70 | { | |
71 | if(size_==capacity_){ | |
72 | reserve(capacity_<=10?15:capacity_+capacity_/2); | |
73 | } | |
74 | } | |
75 | ||
76 | void reserve(std::size_t c) | |
77 | { | |
78 | if(c>capacity_)set_capacity(c); | |
79 | } | |
80 | ||
81 | void shrink_to_fit() | |
82 | { | |
83 | if(capacity_>size_)set_capacity(size_); | |
84 | } | |
85 | ||
86 | pointer begin()const{return ptrs();} | |
87 | pointer end()const{return ptrs()+size_;} | |
88 | pointer at(std::size_t n)const{return ptrs()+n;} | |
89 | ||
90 | void push_back(value_type x) | |
91 | { | |
92 | *(end()+1)=*end(); | |
93 | (*(end()+1))->up()=end()+1; | |
94 | *end()=x; | |
95 | (*end())->up()=end(); | |
96 | ++size_; | |
97 | } | |
98 | ||
99 | void erase(value_type x) | |
100 | { | |
101 | node_impl_type::extract(x->up(),end()+1); | |
102 | --size_; | |
103 | } | |
104 | ||
105 | void clear() | |
106 | { | |
107 | *begin()=*end(); | |
108 | (*begin())->up()=begin(); | |
109 | size_=0; | |
110 | } | |
111 | ||
112 | void swap(random_access_index_ptr_array& x) | |
113 | { | |
114 | std::swap(size_,x.size_); | |
115 | std::swap(capacity_,x.capacity_); | |
116 | spc.swap(x.spc); | |
117 | } | |
118 | ||
119 | private: | |
120 | std::size_t size_; | |
121 | std::size_t capacity_; | |
122 | auto_space<value_type,Allocator> spc; | |
123 | ||
124 | pointer ptrs()const | |
125 | { | |
126 | return spc.data(); | |
127 | } | |
128 | ||
129 | void set_capacity(std::size_t c) | |
130 | { | |
131 | auto_space<value_type,Allocator> spc1(spc.get_allocator(),c+1); | |
132 | node_impl_type::transfer(begin(),end()+1,spc1.data()); | |
133 | spc.swap(spc1); | |
134 | capacity_=c; | |
135 | } | |
136 | }; | |
137 | ||
138 | template<typename Allocator> | |
139 | void swap( | |
140 | random_access_index_ptr_array<Allocator>& x, | |
141 | random_access_index_ptr_array<Allocator>& y) | |
142 | { | |
143 | x.swap(y); | |
144 | } | |
145 | ||
146 | } /* namespace multi_index::detail */ | |
147 | ||
148 | } /* namespace multi_index */ | |
149 | ||
150 | } /* namespace boost */ | |
151 | ||
152 | #endif |