yat  0.14.5pre
SegmentSet.h
1 #ifndef theplu_yat_utility_segment_set
2 #define theplu_yat_utility_segment_set
3 
4 // $Id: SegmentSet.h 3550 2017-01-03 05:41:02Z peter $
5 
6 /*
7  Copyright (C) 2010 Peter Johansson
8  Copyright (C) 2012 Jari Häkkinen
9  Copyright (C) 2014, 2016 Peter Johansson
10 
11  This file is part of the yat library, http://dev.thep.lu.se/yat
12 
13  The yat library is free software; you can redistribute it and/or
14  modify it under the terms of the GNU General Public License as
15  published by the Free Software Foundation; either version 3 of the
16  License, or (at your option) any later version.
17 
18  The yat library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  General Public License for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with yat. If not, see <http://www.gnu.org/licenses/>.
25 */
26 
27 #include "Segment.h"
28 #include "SegmentTree.h"
29 #include "stl_utility.h"
30 #include "yat_assert.h"
31 
32 #include <set>
33 #include <utility>
34 
35 namespace theplu {
36 namespace yat {
37 namespace utility {
38 
46  template<typename T, class Compare = std::less<T> >
47  class SegmentSet
48  : public SegmentTree<std::set<Segment<T, Compare>,
49  SegmentCompare<T, Compare> >,
50  Compare,
51  Identity<const Segment<T, Compare>&> >
52  {
53  typedef SegmentSet<T, Compare> me;
54  public:
58  SegmentSet(void) {}
59 
64  typename me::const_iterator
65  insert_merge(const typename me::value_type& segment)
66  {
67  std::pair<typename me::iterator, typename me::iterator> p =
68  this->overlap_range(segment);
69  return insert_merge(p, segment);
70  }
71 
82  typename me::iterator insert_merge(typename me::iterator hint,
83  const typename me::value_type& segment)
84  {
85  std::pair<typename me::iterator, typename me::iterator> p(hint, hint);
86  if (this->container_.empty())
87  return insert_merge(p, segment);
88 
89  // If hint points to an element that is less than segment, hint
90  // is no good and we ignore the hint.
91  if (hint!=this->end() && compare(*hint, segment))
92  return insert_merge(segment);
93 
94  if (p.first!=this->begin()) {
95  --p.first;
96  // If --hint point to an element greater than segment, hint is
97  // no good.
98  if (compare(segment, *p.first))
99  return insert_merge(segment);
100  // find first element that is smaller than segment
101  while (p.first!=this->begin() && !compare(*p.first, segment))
102  --p.first;
103  YAT_ASSERT(p.first==this->begin() || compare(*p.first, segment));
104  YAT_ASSERT(p.first!=this->end());
105  if (compare(*p.first, segment))
106  ++p.first;
107  }
108 
109  // find first element greater than segment
110  while (p.second!=this->end() && !compare(segment, *p.second))
111  ++p.second;
112 
113  return insert_merge(p, segment);
114  }
115 
124  template<typename Iterator>
125  void insert_merge(Iterator first, Iterator last)
126  {
127  typename me::iterator it = this->end();
128  for ( ; first!=last; ++first) {
129  it = insert_merge(it, *first);
130  ++it;
131  }
132  }
133  private:
134  // used by public functions insert_merge.
135  //
136  // p.first points to the first segment that overlaps with \a segment or
137  // is greater than segment.
138  // p.second points to the first segment that is greater than \a segment
139  typename me::const_iterator
140  insert_merge(const std::pair<typename me::iterator,
141  typename me::iterator>& p,
142  const typename me::value_type& segment)
143  {
144  YAT_ASSERT(p.first==this->end() || !compare(*p.first, segment));
145  YAT_ASSERT(p.second==this->end() || compare(segment, *p.second));
146  if (p.first==p.second) { // no overlap between set and segment
147  return this->container_.insert(p.first, segment);
148  }
149  /*
150  p.first last p.second
151  ---> ---> ---> --->
152 
153  ----------------------->
154  segment
155  */
156  Compare comp;
157  typename me::iterator last=p.second;
158  YAT_ASSERT(last==this->end() || compare(segment, *last));
159  YAT_ASSERT(last!=this->begin()); // last!=p.first
160  --last;
161  YAT_ASSERT(compare_3way(segment, *last)==0);
162 
163  Segment<T, Compare>
164  segment2(std::min(p.first->begin(), segment.begin(), comp),
165  std::max(last->end(), segment.end(), comp));
166 
167  this->container_.erase(p.first, p.second);
168  return this->container_.insert(p.second, segment2);
169  }
170 
171  // using compiler generated copying
172  //SegmentSet(const SegmentSet&);
173  //SegmentSet& operator=(const SegmentSet&);
174  };
175 
176 }}}
177 #endif
SegmentSet(void)
creates a set with no segments
Definition: SegmentSet.h:58
std::set< Segment< T, Compare >, SegmentCompare< T, Compare > >::iterator iterator
iterator
Definition: SegmentTree.h:88
std::set< Segment< T, Compare >, SegmentCompare< T, Compare > >::value_type value_type
value type is same as Container &#39;s value_type.
Definition: SegmentTree.h:61
me::iterator insert_merge(typename me::iterator hint, const typename me::value_type &segment)
insert with a hint
Definition: SegmentSet.h:82
me::const_iterator insert_merge(const typename me::value_type &segment)
Definition: SegmentSet.h:65
T max(const T &a, const T &b, const T &c)
Definition: stl_utility.h:697
std::set< Segment< T, Compare >, SegmentCompare< T, Compare > >::const_iterator const_iterator
const_iterator
Definition: SegmentTree.h:90
a set of Segments
Definition: SegmentSet.h:47
void insert_merge(Iterator first, Iterator last)
Definition: SegmentSet.h:125
Base Class for SegmentSet and SegmentMap.
Definition: SegmentTree.h:45
std::set< Segment< T, Compare >, SegmentCompare< T, Compare > > container_
underlying container
Definition: SegmentTree.h:232

Generated on Tue Sep 26 2017 02:33:29 for yat by  doxygen 1.8.5