mdds
collection.hpp
1 /*************************************************************************
2  *
3  * Copyright (c) 2016 Kohei Yoshida
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without
8  * restriction, including without limitation the rights to use,
9  * copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following
12  * conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  * OTHER DEALINGS IN THE SOFTWARE.
25  *
26  ************************************************************************/
27 
28 #ifndef INCLUDED_MDDS_COLLECTION_HPP
29 #define INCLUDED_MDDS_COLLECTION_HPP
30 
31 #include "mdds/multi_type_vector/types.hpp"
32 
33 #include <type_traits>
34 #include <vector>
35 #include <memory>
36 
37 namespace mdds { namespace mtv {
38 
39 template<typename _MtvT>
40 class collection;
41 
42 namespace detail {
43 
44 template<typename _MtvT>
46 {
47  typedef _MtvT mtv_type;
48  friend collection<mtv_type>;
49 
50  typedef typename mtv_type::size_type size_type;
51  typedef typename mtv_type::const_iterator const_iterator;
52  typedef typename mtv_type::const_position_type const_position_type;
53 
55  struct mtv_item
56  {
57  const mtv_type* vector;
58  const_iterator block_pos;
59  const_iterator block_end;
60 
61  mtv_item(const mtv_type* v, const const_iterator& bp, const const_iterator& be)
62  : vector(v), block_pos(bp), block_end(be)
63  {}
64  };
65 
67  struct node
68  {
69  friend class side_iterator;
70 
72  mdds::mtv::element_t type;
73 
75  size_type index;
76 
78  size_type position;
79 
80  template<typename _Blk>
81  typename _Blk::value_type get() const
82  {
83  return _Blk::get_value(*__position.first->data, __position.second);
84  }
85 
86  private:
88  const_position_type __position;
89  };
90 
91  enum begin_state_type
92  {
93  begin_state
94  };
95  enum end_state_type
96  {
97  end_state
98  };
99 
100  std::vector<mtv_item> m_vectors;
101  node m_cur_node;
102  size_type m_elem_pos;
103  size_type m_elem_pos_end;
104  size_type m_index_offset;
105  uintptr_t m_identity;
106 
108  std::vector<mtv_item>&& vectors, size_type elem_pos, size_type elem_size, size_type index_offset,
109  uintptr_t identity, begin_state_type);
110 
112  std::vector<mtv_item>&& vectors, size_type elem_pos, size_type elem_size, size_type index_offset,
113  uintptr_t identity, end_state_type);
114 
115 public:
116  typedef node value_type;
117 
118  side_iterator();
119 
120  template<typename _T>
121  side_iterator(const _T& begin, const _T& end);
122 
123  const value_type& operator*() const
124  {
125  return m_cur_node;
126  }
127 
128  const value_type* operator->() const
129  {
130  return &m_cur_node;
131  }
132 
133  side_iterator& operator++();
134 
135  side_iterator operator++(int);
136 
137  bool operator==(const side_iterator& other) const;
138  bool operator!=(const side_iterator& other) const;
139 };
140 
141 } // namespace detail
142 
149 template<typename _MtvT>
150 class collection
151 {
152 public:
153  typedef _MtvT mtv_type;
154  typedef typename mtv_type::size_type size_type;
155 
156 private:
157  struct range
158  {
159  size_type start;
160  size_type size;
161 
162  range() : start(0), size(0)
163  {}
164  };
165 
166  std::vector<const mtv_type*> m_vectors;
167  size_type m_mtv_size;
168  uintptr_t m_identity;
169 
170  range m_elem_range;
171  range m_col_range;
172 
173 public:
175 
176  collection();
177 
187  template<typename _T>
188  collection(const _T& begin, const _T& end);
189 
196  const_iterator begin() const;
197 
205  const_iterator end() const;
206 
214  size_type size() const;
215 
221  void swap(collection& other);
222 
237  void set_collection_range(size_type start, size_type size);
238 
255  void set_element_range(size_type start, size_type size);
256 
257 private:
258  void check_collection_range(size_type start, size_type size) const;
259  void check_element_range(size_type start, size_type size) const;
260 
261  std::vector<typename const_iterator::mtv_item> build_iterator_state() const;
262 
263  void init_insert_vector(const std::unique_ptr<mtv_type>& p);
264 
265  void init_insert_vector(const std::shared_ptr<mtv_type>& p);
266 
267  template<typename _T>
268  void init_insert_vector(const _T& t, typename std::enable_if<std::is_pointer<_T>::value>::type* = 0);
269 
270  template<typename _T>
271  void init_insert_vector(const _T& t, typename std::enable_if<!std::is_pointer<_T>::value>::type* = 0);
272 
273  void check_vector_size(const mtv_type& t);
274 };
275 
276 }} // namespace mdds::mtv
277 
278 #include "collection_def.inl"
279 
280 #endif
Definition: collection.hpp:40
const_iterator begin() const
const_iterator end() const
detail::side_iterator< mtv_type > const_iterator
collection range.
Definition: collection.hpp:174
size_type size() const
void swap(collection &other)
Definition: flat_segment_tree.hpp:46
void set_element_range(size_type start, size_type size)
void set_collection_range(size_type start, size_type size)
Definition: collection.hpp:45