1 // Implementation: Testprogram for 2-dimensional Segment Trees
2 // Example for the construction of a two dimensional segment tree.
3 // The data type of the first dimension is double and for the second
4 // dimension double.
5 // The elements that should be stored in the tree are pushed into
6 // a list. Then a two dimensional segment tree is created and a
7 // window query is performed.
8 
9 #include <iostream>
10 #include <utility>
11 #include <vector>
12 #include <iterator>
13 #include <list>
14 #include <CGAL/Simple_cartesian.h>
15 #include <CGAL/Segment_tree_k.h>
16 #include <CGAL/Range_segment_tree_traits.h>
17 
18 typedef CGAL::Simple_cartesian<double> K;
19 typedef CGAL::Range_segment_tree_set_traits_2<K> Traits;
20 typedef CGAL::Segment_tree_2<Traits > Segment_tree_2_type;
21 
main()22 int main()
23 {
24   typedef Traits::Interval Interval;
25   typedef Traits::Key Key;
26   // definition of the two-dimensional segment tree
27   std::list<Interval> InputList, OutputList, N;
28 
29   // insertion of the tree elements into the sequence container
30   InputList.push_back(Interval(Key(1,5), Key(2,7)));
31   InputList.push_back(Interval(Key(2,7), Key(3,8)));
32   InputList.push_back(Interval(Key(6,9), Key(9,13)));
33   InputList.push_back(Interval(Key(1,3), Key(3,9)));
34 
35   // creation of the segment tree
36   std::list<Interval>::iterator first = InputList.begin();
37   std::list<Interval>::iterator last = InputList.end();
38 
39   Segment_tree_2_type Segment_tree_2(first,last);
40 
41   // perform a window query
42   Interval a=Interval(Key(3,6), Key(7,12));
43   Segment_tree_2.window_query(a,std::back_inserter(OutputList));
44 
45   // output of the querey elements on stdout
46   std::list<Interval>::iterator j = OutputList.begin();
47   std::cerr << "\n window_query (3,6), (7,12)\n";
48   while(j!=OutputList.end())
49   {
50     std::cerr << (*j).first.x() << "-" << (*j).second.x() << " "
51          << (*j).first.y() << "-" << (*j).second.y() << std::endl;
52     j++;
53   }
54   std::cerr << "\n enclosing_query (6,10),(7,11) \n";
55   Interval b=Interval(Key(6,10),Key(7,11));
56   Segment_tree_2.enclosing_query(b,std::back_inserter(N));
57   j = N.begin();
58   std::cerr << "\n enclosing_query (6,10),(7,11) \n";
59   while(j!=N.end())
60   {
61     std::cerr << (*j).first.x() << "-" << (*j).second.x() << " "
62          << (*j).first.y() << "-" << (*j).second.y() << std::endl;
63     j++;
64   }
65   if(Segment_tree_2.segment_tree_2->is_valid())
66     std::cerr << "Tree is valid\n";
67   else
68     std::cerr << "Tree is not valid\n";
69   return 0;
70 }
71