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 
66  struct node
67  {
68  friend class side_iterator;
69 
71  mdds::mtv::element_t type;
72 
74  size_type index;
75 
77  size_type position;
78 
79  template<typename _Blk>
80  typename _Blk::value_type get() const
81  {
82  return _Blk::get_value(*__position.first->data, __position.second);
83  }
84 
85  private:
87  const_position_type __position;
88  };
89 
90  enum begin_state_type { begin_state };
91  enum end_state_type { end_state };
92 
93  std::vector<mtv_item> m_vectors;
94  node m_cur_node;
95  size_type m_elem_pos;
96  size_type m_elem_pos_end;
97  size_type m_index_offset;
98  uintptr_t m_identity;
99 
101  std::vector<mtv_item>&& vectors, size_type elem_pos, size_type elem_size,
102  size_type index_offset, uintptr_t identity, begin_state_type);
103 
105  std::vector<mtv_item>&& vectors, size_type elem_pos, size_type elem_size,
106  size_type index_offset, uintptr_t identity, end_state_type);
107 
108 public:
109  typedef node value_type;
110 
111  side_iterator();
112 
113  template<typename _T>
114  side_iterator(const _T& begin, const _T& end);
115 
116  const value_type& operator*() const
117  {
118  return m_cur_node;
119  }
120 
121  const value_type* operator->() const
122  {
123  return &m_cur_node;
124  }
125 
126  side_iterator& operator++();
127 
128  side_iterator operator++(int);
129 
130  bool operator== (const side_iterator& other) const;
131  bool operator!= (const side_iterator& other) const;
132 };
133 
134 }
135 
142 template<typename _MtvT>
143 class collection
144 {
145 public:
146  typedef _MtvT mtv_type;
147  typedef typename mtv_type::size_type size_type;
148 
149 private:
150 
151  struct range
152  {
153  size_type start;
154  size_type size;
155 
156  range() : start(0), size(0) {}
157  };
158 
159  std::vector<const mtv_type*> m_vectors;
160  size_type m_mtv_size;
161  uintptr_t m_identity;
162 
163  range m_elem_range;
164  range m_col_range;
165 
166 public:
167 
169 
170  collection();
171 
181  template<typename _T>
182  collection(const _T& begin, const _T& end);
183 
190  const_iterator begin() const;
191 
199  const_iterator end() const;
200 
208  size_type size() const;
209 
215  void swap(collection& other);
216 
231  void set_collection_range(size_type start, size_type size);
232 
249  void set_element_range(size_type start, size_type size);
250 
251 private:
252 
253  void check_collection_range(size_type start, size_type size) const;
254  void check_element_range(size_type start, size_type size) const;
255 
256  std::vector<typename const_iterator::mtv_item> build_iterator_state() const;
257 
258  void init_insert_vector(const std::unique_ptr<mtv_type>& p);
259 
260  void init_insert_vector(const std::shared_ptr<mtv_type>& p);
261 
262  template<typename _T>
263  void init_insert_vector(const _T& t, typename std::enable_if<std::is_pointer<_T>::value>::type* = 0);
264 
265  template<typename _T>
266  void init_insert_vector(const _T& t, typename std::enable_if<!std::is_pointer<_T>::value>::type* = 0);
267 
268  void check_vector_size(const mtv_type& t);
269 };
270 
271 }}
272 
273 #include "collection_def.inl"
274 
275 #endif
Definition: collection.hpp:40
detail::side_iterator< mtv_type > const_iterator
collection range.
Definition: collection.hpp:168
Definition: flat_segment_tree.hpp:46
Definition: collection.hpp:45