3 // Copyright (C) 2005 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32 // Permission to use, copy, modify, sell, and distribute this software
33 // is hereby granted without fee, provided that the above copyright
34 // notice appears in all copies, and that both that copyright notice and
35 // this permission notice appear in supporting documentation. None of
36 // the above authors, nor IBM Haifa Research Laboratories, make any
37 // representation about the suitability of this software for any
38 // purpose. It is provided "as is" without express or implied warranty.
41 * @file tree_set_intervals_example.cpp
42 * An example showing how to augment a trees to support operations involving
47 #include <ext/pb_assoc/assoc_cntnr.hpp>
54 * Following are definitions of line intervals and functors operating on them.
55 * As the purpose of this example is node invariants, and not
56 * computational-geometry algorithms per-se, some simplifications are made
57 *(e.g., intervals are defined by unsigned integers, and not by*a parameterized type, data members are public, etc.).
61 * An interval of unsigned integers.
67 * @param start [i] - Start point.
68 * @param end [i] - End point.
70 interval(unsigned int start
, unsigned int end
) : m_start(start
),
77 * Comparison predicate.
78 * @param r_rhs [i] - Right-hand object with which to compare.
81 operator<(const interval
& r_rhs
) const
83 if (m_start
!= r_rhs
.m_start
)
84 return (m_start
< r_rhs
.m_start
);
86 return (m_end
< r_rhs
.m_end
);
100 struct intervals_node_updator
;
102 template<class Cntnr
>
104 overlaps(const Cntnr
& r_c
, const interval
& r_interval
);
107 * The entry of the set. It includes an interval and the
108 * maximal endpoint of the intervals in its subtree.
112 // Constructor. The maximal endpoint is set to the endpoint
113 explicit entry(unsigned int start
, unsigned int end
) : m_interval(start
, end
),
117 // Compares two entries by their intervals.
119 operator<(const entry
& r_rhs
) const
121 return (m_interval
< r_rhs
.m_interval
);
128 // The maximal endpoint of the intervals in its subtree.
129 mutable unsigned int m_max_endpoint
;
131 friend struct intervals_node_updator
;
133 template<class Cntnr
>
135 overlaps(const Cntnr
& r_c
, const interval
& r_interval
);
140 * Functor updating maximal endpoints of entries.
141 * Algorithm taken from "Introduction to Algorithms" by Cormen, Leiserson,
144 struct intervals_node_updator
147 operator()(const entry
* p_entry
, const entry
* p_l_child_entry
, const entry
* p_r_child_entry
)
149 /* The left maximal endpoint is 0 if there is no left child.
151 const unsigned int l_max_endpoint
=(p_l_child_entry
== NULL
)? 0 : p_l_child_entry
->m_max_endpoint
;
153 /* The right maximal endpoint is 0 if there is no right child.
155 const unsigned int r_max_endpoint
=(p_r_child_entry
== NULL
)? 0 : p_r_child_entry
->m_max_endpoint
;
157 p_entry
->m_max_endpoint
= std::max(p_entry
->m_interval
.m_end
,
158 std::max
<unsigned int>(l_max_endpoint
, r_max_endpoint
));
163 * Checks whether a set of intervals contains at least one interval
164 * overlapping some interval.
165 * Algorithm taken from "Introduction to Algorithms" by Cormen, Leiserson,
168 template<class Cntnr
>
170 overlaps(const Cntnr
& r_c
, const interval
& r_interval
)
172 typedef typename
Cntnr::const_iterator intr_set_const_it
;
174 typedef typename
Cntnr::const_node_iterator intr_set_const_node_it
;
176 intr_set_const_node_it node_it
= r_c
.node_begin();
178 while (node_it
!= r_c
.node_end())
180 // Check whether r_interval overlaps the current interval.
182 intr_set_const_it it
=* node_it
;
184 if (r_interval
.m_end
>= it
->m_interval
.m_start
&&
185 r_interval
.m_start
<= it
->m_interval
.m_end
)
188 intr_set_const_node_it l_node_it
= node_it
.l_child();
190 const unsigned int l_max_endpoint
=(l_node_it
== r_c
.node_end())?
191 0 : (*l_node_it
)->m_max_endpoint
;
193 if (l_max_endpoint
>= r_interval
.m_start
)
196 node_it
= node_it
.r_child();
202 template<class Cntnr
>
204 some_op_sequence(Cntnr c
)
206 // Insert some entries.
208 c
.insert(entry(0, 100));
209 c
.insert(entry(150, 160));
210 c
.insert(entry(300, 1000));
211 c
.insert(entry(10000, 100000));
212 c
.insert(entry(200, 100200));
216 // Overlaps 150 - 160
217 assert(overlaps(c
, interval(145, 165)) == true);
218 // Overlaps 150 - 160
219 assert(overlaps(c
, interval(145, 155)) == true);
220 assert(overlaps(c
, interval(165, 175)) == false);
221 assert(overlaps(c
, interval(100201, 100203)) == false);
229 // Test overlaps again.
231 assert(overlaps(c
, interval(145, 165)) == false);
232 assert(overlaps(c
, interval(165, 175)) == false);
233 assert(overlaps(c
, interval(0, 300000)) == true);
239 some_op_sequence(pb_assoc::tree_assoc_cntnr
<
241 pb_assoc::null_data_type
,
243 pb_assoc::ov_tree_ds_tag
,
244 intervals_node_updator
>());
246 some_op_sequence(pb_assoc::tree_assoc_cntnr
<
248 pb_assoc::null_data_type
,
250 pb_assoc::rb_tree_ds_tag
,
251 intervals_node_updator
>());
253 some_op_sequence(pb_assoc::tree_assoc_cntnr
<
255 pb_assoc::null_data_type
,
257 pb_assoc::splay_tree_ds_tag
,
258 intervals_node_updator
>());