yat  0.21pre
SegmentTree.h
1 #ifndef theplu_yat_utility_segment_tree
2 #define theplu_yat_utility_segment_tree
3 
4 // $Id: SegmentTree.h 4032 2021-01-21 07:44:38Z peter $
5 
6 /*
7  Copyright (C) 2010, 2011, 2012, 2013, 2021 Peter Johansson
8 
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10 
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15 
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 #include "Segment.h"
26 
27 #include "yat_assert.h"
28 
29 #include <algorithm>
30 #include <functional>
31 #include <utility>
32 
33 namespace theplu {
34 namespace yat {
35 namespace utility {
36 
45  template<class Container, class Compare, class Value2Key>
47  {
48  public:
54  typedef typename Container::key_type key_type;
55 
62  typedef typename Container::value_type value_type;
63 
69  typedef typename key_type::value_type element_type;
70 
72  typedef typename Container::key_compare key_compare;
74  typedef typename Container::value_compare value_compare;
76  typedef Compare element_compare;
77 
79  typedef typename Container::pointer pointer;
81  typedef typename Container::reference reference;
83  typedef typename Container::const_reference const_reference;
85  typedef typename Container::size_type size_type;
87  typedef typename Container::difference_type difference_type;
89  typedef typename Container::iterator iterator;
91  typedef typename Container::const_iterator const_iterator;
92 
96  SegmentTree(void) {}
97 
101  virtual ~SegmentTree(void) {}
102 
106  const_iterator begin(void) const { return container_.begin(); }
107 
111  iterator begin(void) { return container_.begin(); }
112 
116  void clear(void) { container_.clear(); }
117 
121  size_type count(const element_type& element) const;
122 
126  bool empty(void) const { return container_.empty(); }
127 
131  const_iterator end(void) const { return container_.end(); }
132 
136  iterator end(void) { return container_.end(); }
137 
143  void erase(iterator first, iterator last) { container_.erase(first, last);}
144 
150  void erase(iterator pos) { container_.erase(pos); }
151 
157  iterator find(const element_type& element);
158 
167  const_iterator find(const element_type& element) const;
168 
180  std::pair<iterator, bool> insert(const value_type& value);
181 
187  std::pair<iterator, bool> insert(value_type&& value);
188 
192  key_compare key_comp(void) const
193  {
194  return container_.key_comp();
195  }
196 
203  iterator lower_bound(const element_type& element);
204 
211  const_iterator lower_bound(const element_type& element) const;
212 
216  size_type size(void) const { return container_.size(); }
217 
222  iterator upper_bound(const element_type& element);
223 
230  const_iterator upper_bound(const element_type& element) const;
231 
236  value_compare value_comp(void) const { return container_.value_comp(); }
237 
238  protected:
240  Container container_;
241 
247  std::pair<iterator, iterator> overlap_range(const key_type& segment)
248  {
249  iterator first = container_.lower_bound(segment);
250  if (first!=begin()) {
251  --first;
252  if (compare(*first, segment))
253  ++first;
254  }
255  iterator last = first;
256  while (last!=end() && !compare(segment, *last))
257  ++last;
258  YAT_ASSERT(last==end() || compare(segment, *last));
259  return std::make_pair(first, last);
260  }
261 
262  // using compiler generated copying
263  //SegmentTree(const SegmentTree&);
264  //SegmentTree& operator=(const SegmentTree&);
265  };
266 
267  template<class Container, class Compare, class Value2Key>
270  {
271  if (find(element)==end())
272  return 0;
273  return 1;
274  }
275 
276 
277  template<class Container, class Compare, class Value2Key>
280  {
281  const_iterator iter = lower_bound(vt);
282  element_compare comp;
283  // if (iter==end() || comp(vt, iter->begin()))
284  if (iter==end() || comp(vt, Value2Key()(*iter).begin()))
285  return end();
286  return iter;
287  }
288 
289 
290  template<class Container, class Compare, class Value2Key>
293  {
294  iterator iter = lower_bound(vt);
295  element_compare comp;
296  if (iter==end() || comp(vt, Value2Key()(*iter).begin()))
297  return end();
298  return iter;
299  }
300 
301 
302  template<typename T, class Compare, class Value2Key>
303  std::pair<typename SegmentTree<T, Compare, Value2Key>::iterator, bool>
305  {
306  return container_.insert(segment);
307  }
308 
309 
310  template<typename T, class Compare, class Value2Key>
311  std::pair<typename SegmentTree<T, Compare, Value2Key>::iterator, bool>
313  {
314  return container_.insert(std::move(segment));
315  }
316 
317 
318  template<typename T, class Compare, class Value2Key>
321  {
322  key_type segment(element, element);
323  iterator result = container_.lower_bound(segment);
324  // result is larger or overlapping with segment (i.e.! result<segment)
325  YAT_ASSERT(result==end()
326  || !compare(Value2Key()(*result),segment));
327  return result;
328  }
329 
330 
331  template<typename T, class Compare, class Value2Key>
334  {
335  key_type segment(element, element);
336  const_iterator result = container_.lower_bound(segment);
337  // result is larger or overlapping with segment (i.e.! result<segment)
338  YAT_ASSERT(result==end()
339  || !compare(Value2Key()(*result),segment));
340  return result;
341  }
342 
343 
344  template<typename T, class Compare, class Value2Key>
347  {
348  key_type segment(element, element);
349  iterator result = container_.upper_bound(segment);
350  Compare comp;
351  Value2Key value2key;
352  if (result==end() || comp(element, value2key(*result).begin()))
353  return result;
354  ++result;
355  // result is larger than segment
356  YAT_ASSERT(result==end() || compare(segment, value2key(*result)));
357  return result;
358  }
359 
360 
361  template<typename T, class Compare, class Value2Key>
364  {
365  Segment<element_type, Compare> segment(element, element);
366  const_iterator result = container_.upper_bound(segment);
367  Compare comp;
368  Value2Key value2key;
369  if (result==end() || comp(element, value2key(*result).begin()))
370  return result;
371  ++result;
372  // result is larger than segment
373  YAT_ASSERT(result==end() || compare(segment, value2key(*result)));
374  return result;
375  }
376 
377 }}}
378 #endif
iterator upper_bound(const element_type &element)
Definition: SegmentTree.h:346
iterator begin(void)
Definition: SegmentTree.h:111
Container::iterator iterator
iterator
Definition: SegmentTree.h:89
const_iterator end(void) const
Definition: SegmentTree.h:131
iterator find(const element_type &element)
Definition: SegmentTree.h:292
void erase(iterator first, iterator last)
erase values in range [first, last)
Definition: SegmentTree.h:143
Container::value_type value_type
value type is same as Container &#39;s value_type.
Definition: SegmentTree.h:62
Container::key_compare key_compare
key compare
Definition: SegmentTree.h:72
key_type::value_type element_type
element type is same as key_type &#39;s value_type.
Definition: SegmentTree.h:69
iterator end(void)
Definition: SegmentTree.h:136
The Department of Theoretical Physics namespace as we define it.
Container::pointer pointer
pointer
Definition: SegmentTree.h:79
std::pair< iterator, bool > insert(const value_type &value)
insert value
Definition: SegmentTree.h:304
void erase(iterator pos)
Definition: SegmentTree.h:150
Container::const_reference const_reference
const reference
Definition: SegmentTree.h:83
Container::difference_type difference_type
difference_type
Definition: SegmentTree.h:87
void clear(void)
erases all values
Definition: SegmentTree.h:116
iterator lower_bound(const element_type &element)
similar to lower_bound in std::set and std::map
Definition: SegmentTree.h:320
Container::const_iterator const_iterator
const_iterator
Definition: SegmentTree.h:91
Container::reference reference
reference
Definition: SegmentTree.h:81
bool empty(void) const
Definition: SegmentTree.h:126
SegmentTree(void)
creates a SegmentTree with no segments
Definition: SegmentTree.h:96
key_compare key_comp(void) const
Definition: SegmentTree.h:192
a class for a Segment or Interval
Definition: Segment.h:49
virtual ~SegmentTree(void)
Destructor.
Definition: SegmentTree.h:101
Container::key_type key_type
key type is same as Container &#39;s key_type.
Definition: SegmentTree.h:54
Container::value_compare value_compare
value compare
Definition: SegmentTree.h:74
Base Class for SegmentSet and SegmentMap.
Definition: SegmentTree.h:46
Container::size_type size_type
size_type
Definition: SegmentTree.h:85
std::pair< iterator, iterator > overlap_range(const key_type &segment)
Definition: SegmentTree.h:247
Compare element_compare
element compare
Definition: SegmentTree.h:76
size_type count(const element_type &element) const
Definition: SegmentTree.h:269
value_compare value_comp(void) const
Definition: SegmentTree.h:236
size_type size(void) const
Definition: SegmentTree.h:216
const_iterator begin(void) const
Definition: SegmentTree.h:106
Container container_
underlying container
Definition: SegmentTree.h:240

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14