CGAL is designed in the spirit of the generic programming paradigm to work together with the Standard Template Library (STL) [C++96, MS96]. This chapter documents non-geometric STL-like components that are not provided in the STL standard but in CGAL: a doubly-connected list managing items in place (where inserted items are not copied), generic functions, function objects for projection and creation, classes for composing function objects and adaptor classes around iterators and circulators. See also circulators in Chapter .
The class In_place_list<T,&T::next,&T::prev> manages a sequence of items in place in a doubly-connected list. Its goals are the flexible handling of memory management and performance optimization. The item type is supposed to provide the two necessary pointers &T::next_link and &T::prev_link. One possibility to obtain these pointers is to inherit them from the base class In_place_list_base<T>.
The class In_place_list<T> is a container and quite similar to STL containers, with the advantage that it is able to handle the stored elements by reference instead of copying them. It is possible to delete an element only knowing its address and no iterator to it. This simplifies mutually pointered data structures like a halfedge data structure for planar maps or polyhedral surfaces. The usual iterators are also available. Another container with this property of working with pointers to objects is the STL vector (at least in the current STL implementations).
The node base classes provides pointers to build linked lists. The class In_place_sl_list_base<T> provides a pointer next_link for a single linked list. The class In_place_list_base<T> provides an additional pointer prev_link for doubly linked lists. These names conform to the default parameters used in the template argument lists of the container classes. The pointers are public members.
#include <CGAL/In_place_list.h>
copy_n() copies items from an input iterator to an output iterator which is useful for possibly infinite sequences of random geometric objects1.
#include <CGAL/algorithm.h>
min_max_element() computes the minimal and the maximal element of a range. It is modeled after the STL functions min_element and max_element. The advantage of min_max_element compared to calling both STL functions is that one only iterates once over the sequence. This is more efficient especially for large and/or complex sequences.
#include <CGAL/algorithm.h>
| ||||
| ||||
| ||||
return a pair of iterators where the first component refers to the minimal and the second component refers to the maximal element in the range [first, last). The ordering is defined by the operator< on the value type of ForwardIterator. | ||||
| ||||
| ||||
| ||||
return a pair of iterators where the first component refers to the minimal and the second component refers to the maximal element in the range [first, last). CompareMin and CompareMax are adaptable binary function objects: VT VT bool where VT is the value type of ForwardIterator. |
#include <CGAL/algorithm.h> #include <vector> #include <iostream> using std::vector; using std::pair; using std::cout; using std::endl; using CGAL::min_max_element; int main() { vector< int > v; v.push_back(3); v.push_back(6); v.push_back(5); typedef std::vector< int >::iterator iterator; pair< iterator, iterator > p = min_max_element(v.begin(), v.end()); cout << "min = " << *p.first << ", max = " << *p.second << endl; return 0; }
min_element_if() and min_element_if() compute the minimum respectively maximum among the elements of a range which satisfy a certain predicate. They are modeled after the STL functions min_element and max_element.
#include <CGAL/algorithm.h>
| ||||||
|
| |||||
return an iterator referring to the minimal element among those satifying the predicate pred in the range [first, last). The ordering is defined by the operator< on VT where VT is the value type of ForwardIterator. pred is an unary function object: VT bool. | ||||||
| ||||||
|
| |||||
return an iterator referring to the maximal element among those satifying the predicate pred in the range [first, last). The ordering is defined by the operator< on VT where VT is the value type of ForwardIterator. pred is an unary function object: VT bool. | ||||||
| ||||||
|
| |||||
return an iterator referring to the minimal element among those satifying the predicate pred in the range [first, last). The ordering is defined by the binary function object comp: VT VT bool where VT is the value type of ForwardIterator. pred is an unary function object: VT bool. | ||||||
| ||||||
|
| |||||
return an iterator referring to the maximal element among those satifying the predicate pred in the range [first, last). The ordering is defined by the binary function object comp: VT VT bool where VT is the value type of ForwardIterator. pred is an unary function object: VT bool. |
#include <CGAL/algorithm.h> #include <vector> #include <iostream> #include <functional> using std::vector; using std::cout; using std::endl; using std::modulus; using std::greater; using std::compose1; using std::bind2nd; using CGAL::min_element_if; int main() { vector< int > v; v.push_back(3); v.push_back(5); v.push_back(2); cout << "min_odd = " << *min_element_if(v.begin(), v.end(), compose1(bind2nd(greater< int >(), 0), bind2nd(modulus< int >(), 2))) << endl; return 0; }
Two kinds of function objects are provided: Projections and Creators.
A projection function object of type has an unary parentheses operator expecting an argument of type O::argument_type as a reference or constant reference, and returns a reference or constant reference of type O::result_type respectively.
#include <CGAL/function_objects.h>
|
| |
| ||
|
The following projection function objects are available:
| |
the identity function for projection objects.
argument_type and result_type are equal to Value.
|
| |
composes two projections: .
argument_type is equal to Fct2::argument_type,
result_type to Fct1::result_type.
|
| |
dereferences a pointer.
argument_type is equal to Value* and
result_type is equal to Value.
|
| |
Get the address for a reference.
argument_type is equal to Value and
result_type is equal to Value*.
|
| |
applies a C-style type cast to its argument.
argument_type is equal to Arg and
result_type is equal to Result.
|
The following function objects are provided with respect to the polyhedral surfaces in the basic library.
| |
calls the member function vertex() on an instance of type
Node. argument_type is equal to Node and
result_type is equal to Node::Vertex.
|
| |
calls the member function facet() on an instance of type
Node. argument_type is equal to Node and
result_type is equal to Node::Facet.
|
| |
calls the member function point() on an instance of type
Node. argument_type is equal to Node and
result_type is equal to Node::Point.
|
| |
calls the member function normal() on an instance of type
Node. argument_type is equal to Node and
result_type is equal to Node::Normal.
|
| |
calls the member function plane() on an instance of type
Node. argument_type is equal to Node and
result_type is equal to Node::Plane.
|
| |
calls the member function next() on an instance of type
Node. argument_type and result_type are equal
to Node*.
|
| |
calls the member function prev() on an instance of type
Node. argument_type and result_type are equal
to Node*.
|
| |
calls the member functions next()->opposite() on an
instance of type Node. argument_type and
result_type are equal to Node*.
|
| |
calls the member functions opposite()->prev() on an
instance of type Node. argument_type and
result_type are equal to Node*.
|
A creator function object of type has a parentheses operator which acts like a constructor. The operator might expect arguments and returns an instance of type O::result_type.
#include <CGAL/function_objects.h>
|
|
The following creator function objects are available for one to five arguments:
| |
applies the constructor Result(Arg).
result_type is equal to Result.
|
...
| |
applies the constructor Result(Arg1, Arg2, Arg3, Arg4, Arg5).
result_type is equal to Result.
|
The following creator function objects are available for two to nine arguments (one argument is already captured with Creator_1):
| |
applies the constructor Result(Arg,Arg).
result_type is equal to Result.
|
...
| |
applies the constructor for Result with nine arguments of
type Arg. result_type is equal to Result.
|
Although not mentioned in the ANSI draft(CD2), most STL implementations provide two global functions for composing function objects, compose1 and compose2, defined in function.h. Since for both the resulting function is unary, one can only construct unary function objects in this way. This seems to be quite a limitation, since many (also STL) algorithms can be parameterized with binary (e.g. comparison) functions.
In analogy to STL compose1/2 we define two functions, compose1_2 and compose2_2, that can be used to construct a binary function object via composition.
#include <CGAL/function_objects.h>
| ||
|
|
| ||||
|
#include <CGAL/function_objects.h>
| ||||
|
|
Operation1 must be an adaptable binary function, Operation2 and Operation3 must be adaptable unary functions, Operation2::result_type must be convertible to Operation1::first_argument_type and Operation3::result_type must be convertible to Operation1::second_argument_type. Then compose2_2 returns an adaptable binary function object c (of some complicated type Composition) such that
| ||||
|
A join of input iterators and a creator function object is again an input iterator which reads an object from each stream and applies the creator function object on them whenever it advances.
#include <CGAL/Join_input_iterator.h>
The following join input iterator objects are available for one to five iterators:
| |
the join of a single iterator . Applies Creator to
each item read from .
value_type is equal to Creator::result_type.
|
...
| |||
the join of five iterators i1 to i5. Applies
Creator with five arguments *i1 up to *i5.
value_type is equal to Creator::result_type.
|
Creator_1 ... and Creator_uniform_2 ....
1 | The STL release June 13, 1997, from SGI has a new function copy_n which is equivalent with copy_n. |