mdds
rectangle_set.hpp
1 /*************************************************************************
2  *
3  * Copyright (c) 2010-2015 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_RECTANGLE_SET_HPP
29 #define INCLUDED_MDDS_RECTANGLE_SET_HPP
30 
31 #include "segment_tree.hpp"
32 #include "global.hpp"
33 
34 namespace mdds {
35 
36 template<typename _Key, typename _Value>
38 {
39 public:
40  typedef _Key key_type;
41  typedef _Value value_type;
42 
43 #ifdef MDDS_UNIT_TEST
44 public:
45 #else
46 private:
47 #endif
48  struct rectangle
49  {
50  key_type x1;
51  key_type y1;
52  key_type x2;
53  key_type y2;
54 
55  rectangle(key_type _x1, key_type _y1, key_type _x2, key_type _y2) :
56  x1(_x1), y1(_y1), x2(_x2), y2(_y2) {}
57 
58  rectangle(const rectangle& r) :
59  x1(r.x1), y1(r.y1), x2(r.x2), y2(r.y2) {}
60 
61  rectangle& operator= (const rectangle& r)
62  {
63  x1 = r.x1;
64  y1 = r.y1;
65  x2 = r.x2;
66  y2 = r.y2;
67  return *this;
68  }
69 
70  bool operator== (const rectangle& r) const
71  {
72  return x1 == r.x1 && y1 == r.y1 && x2 == r.x2 && y2 == r.y2;
73  }
74 
75  bool operator!= (const rectangle& r) const
76  {
77  return !operator==(r);
78  }
79  };
80  typedef std::unordered_map<value_type, rectangle> dataset_type;
81 private:
84 
85  typedef std::pair<key_type, key_type> interval_type;
86  typedef std::map<interval_type, std::unique_ptr<inner_type>> inner_segment_map_type;
87 
88 public:
89  typedef typename inner_type::search_result_type search_result_type;
90 
96  class search_result : public inner_type::search_result_base
97  {
98  public:
99  typedef typename inner_type::search_result_base::res_chains_type res_chains_type;
100  typedef typename inner_type::search_result_base::res_chains_ptr res_chains_ptr;
101  typedef typename inner_type::data_chain_type data_chain_type;
102 
103  public:
104 
105  class iterator : public inner_type::iterator_base
106  {
107  friend class rectangle_set<_Key,_Value>::search_result;
108  private:
109  iterator(const res_chains_ptr& p) : inner_type::iterator_base(p) {}
110  public:
111  iterator() : inner_type::iterator_base() {}
112  };
113 
114  search_result() : inner_type::search_result_base() {}
115  search_result(const search_result& r) : inner_type::search_result_base(r) {}
116 
117  typename search_result::iterator begin()
118  {
119  typename search_result::iterator itr(
120  inner_type::search_result_base::get_res_chains());
121  itr.move_to_front();
122  return itr;
123  }
124 
125  typename search_result::iterator end()
126  {
127  typename search_result::iterator itr(
128  inner_type::search_result_base::get_res_chains());
129  itr.move_to_end();
130  return itr;
131  }
132  };
133 
134  rectangle_set();
135  rectangle_set(const rectangle_set& r);
136  ~rectangle_set();
137 
138  rectangle_set& operator= (const rectangle_set& r);
139 
144  bool operator== (const rectangle_set& r) const;
145 
146  bool operator!= (const rectangle_set& r) const { return !operator==(r); }
147 
166  bool insert(key_type x1, key_type y1, key_type x2, key_type y2, value_type data);
167 
177  bool search(key_type x, key_type y, search_result_type& result);
178 
188  search_result search(key_type x, key_type y);
189 
196  void remove(value_type data);
197 
201  void clear();
202 
208  size_t size() const;
209 
215  bool empty() const;
216 
217 private:
218  void build_inner_map(const inner_segment_map_type& r);
219  void build_outer_segment_tree();
220 
221 #ifdef MDDS_UNIT_TEST
222 public:
223  void dump_rectangles() const;
224  bool verify_rectangles(const dataset_type& expected) const;
225 #endif
226 
227 private:
228 
233  outer_type m_outer_segments;
234 
238  inner_segment_map_type m_inner_map;
239 
245  dataset_type m_dataset;
246 };
247 
248 }
249 
250 #include "rectangle_set_def.inl"
251 
252 #endif
Definition: segment_tree.hpp:50
Definition: rectangle_set.hpp:105
bool empty() const
Definition: rectangle_set.hpp:96
bool search(key_type x, key_type y, search_result_type &result)
bool operator==(const rectangle_set &r) const
bool insert(key_type x1, key_type y1, key_type x2, key_type y2, value_type data)
Definition: rectangle_set.hpp:37
Definition: flat_segment_tree.hpp:46
size_t size() const