Merge -r 127928:132243 from trunk
[official-gcc.git] / libstdc++-v3 / testsuite / ext / pb_ds / example / tree_intervals.cc
blobe4411aaab43912ee04e651dc745d8511bb6f9547
1 // -*- C++ -*-
3 // Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4 //
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 terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
9 // version.
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
29 // Public License.
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
40 // warranty.
42 /**
43 * @file tree_intervals_example.cpp
44 * An example showing how to augment a trees to support operations involving
45 * line intervals.
48 /**
49 * In some cases tree structure can be used for various purposes other
50 * than storing entries by key order. This example shows how a
51 * tree-based container can be used for geometric-line intersection
52 * determination. That is, the key of the container is a pair of
53 * numbers representing a line interval. The container object can be
54 * used to query whether a line interval intersects any line interval
55 * it currently stores.
57 * This type of problem arises not only in geometric applications, but
58 * also sometimes in distributed filesystems. Assume that "leases" are
59 * taken for parts of files or LUNs. When a new lease is requested, it
60 * is necessary to check that it does not conflict with a lease
61 * already taken. In this case a file or LUN can be envisioned as a
62 * line segment; leases requested and granted for contiguous parts of
63 * the file or LUN can be represented as line intervals as well.
66 #include <cassert>
67 #include <cstdlib>
68 #include <ext/pb_ds/assoc_container.hpp>
70 using namespace std;
71 using namespace __gnu_pbds;
73 // Following are definitions of line intervals and functors operating
74 // on them. As the purpose of this example is node invariants, and not
75 // computational-geometry algorithms per-se, some simplifications are
76 // made (e.g., intervals are defined by unsigned integers, and not by
77 // a parameterized type, data members are public, etc.).
79 // An interval of unsigned integers.
80 typedef pair< unsigned int, unsigned int> interval;
82 // Functor updating maximal endpoints of entries. Algorithm taken from
83 // "Introduction to Algorithms" by Cormen, Leiserson, and Rivest.
84 template<class Const_Node_Iterator,
85 class Node_Iterator,
86 class Cmp_Fn,
87 class Allocator>
88 struct intervals_node_update
90 public:
91 // The metadata that each node stores is the largest endpoint of an
92 // interval in its subtree. In this case, this is an unsigned int.
93 typedef unsigned int metadata_type;
95 // Checks whether a set of intervals contains at least one interval
96 // overlapping some interval. Algorithm taken from "Introduction to
97 // Algorithms" by Cormen, Leiserson, and Rivest.
98 bool
99 overlaps(const interval& r_interval)
101 Const_Node_Iterator nd_it = node_begin();
102 Const_Node_Iterator end_it = node_end();
104 while (nd_it != end_it)
106 // Check whether r_interval overlaps the current interval.
107 if (r_interval.second >= (*nd_it)->first&&
108 r_interval.first <= (*nd_it)->second)
109 return true;
111 // Get the const node iterator of the node's left child.
112 Const_Node_Iterator l_nd_it = nd_it.get_l_child();
114 // Calculate the maximal endpoint of the left child. If the
115 // node has no left child, then this is the node's maximal
116 // endpoint.
117 const unsigned int l_max_endpoint =(l_nd_it == end_it)?
118 0 : l_nd_it.get_metadata();
120 // Now use the endpoint to determine which child to choose.
121 if (l_max_endpoint >= r_interval.first)
122 nd_it = l_nd_it;
123 else
124 nd_it = nd_it.get_r_child();
127 return false;
130 protected:
131 // Update predicate: nd_it is a node iterator to the node currently
132 // updated; end_nd_it is a const node iterator to a just-after leaf
133 // node.
134 inline void
135 operator()(Node_Iterator nd_it, Const_Node_Iterator end_nd_it)
137 // The left maximal endpoint is 0 if there is no left child.
138 const unsigned int l_max_endpoint =(nd_it.get_l_child() == end_nd_it)?
139 0 : nd_it.get_l_child().get_metadata();
141 // The right maximal endpoint is 0 if there is no right child.
142 const unsigned int r_max_endpoint =(nd_it.get_r_child() == end_nd_it)?
143 0 : nd_it.get_r_child().get_metadata();
145 // The maximal endpoint is the endpoint of the node's interval,
146 // and the maximal endpoints of its children.
147 const_cast<unsigned int&>(nd_it.get_metadata()) =
148 max((*nd_it)->second, max<unsigned int>(l_max_endpoint, r_max_endpoint));
151 virtual Const_Node_Iterator
152 node_begin() const = 0;
154 virtual Const_Node_Iterator
155 node_end() const = 0;
157 virtual
158 ~intervals_node_update()
162 // The following function performs some operation sequence on a
163 // generic associative container supporting order statistics. It
164 // inserts some intervals, and checks for overlap.
165 template<class Cntnr>
166 void
167 some_op_sequence(Cntnr r_c)
169 // Insert some entries.
170 r_c.insert(make_pair(0, 100));
171 r_c.insert(make_pair(150, 160));
172 r_c.insert(make_pair(300, 1000));
173 r_c.insert(make_pair(10000, 100000));
174 r_c.insert(make_pair(200, 100200));
176 // Test overlaps.
178 // Overlaps 150 - 160
179 assert(r_c.overlaps(make_pair(145, 165)) == true);
180 // Overlaps 150 - 160
181 assert(r_c.overlaps(make_pair(145, 155)) == true);
182 assert(r_c.overlaps(make_pair(165, 175)) == false);
183 assert(r_c.overlaps(make_pair(100201, 100203)) == false);
185 // Erase an interval
186 r_c.erase(make_pair(150, 160));
188 // Test overlaps again.
189 assert(r_c.overlaps(make_pair(145, 165)) == false);
190 assert(r_c.overlaps(make_pair(165, 175)) == false);
191 assert(r_c.overlaps(make_pair(0, 300000)) == true);
194 int main()
196 // Test a red-black tree.
197 some_op_sequence(tree<
198 interval,
199 null_mapped_type,
200 less<interval>,
201 rb_tree_tag,
202 intervals_node_update>());
204 // Test an ordered-vector tree.
205 some_op_sequence(tree<
206 interval,
207 null_mapped_type,
208 less<interval>,
209 ov_tree_tag,
210 intervals_node_update>());
212 // Test a splay tree.
213 some_op_sequence(tree<
214 interval,
215 null_mapped_type,
216 less<interval>,
217 splay_tree_tag,
218 intervals_node_update>());
220 return 0;