BDE 4.14.0 Production release
|
Typedefs | |
typedef bslalg::RangeCompare | bslalg_RangeCompare |
This alias is defined for backward compatibility. | |
Provide algorithms to compare iterator-ranges of elements.
This component provides a utility struct
, bslalg::RangeCompare
, that defines two overloaded class methods, equal
and lexicographical
, for comparing two ranges, each specified by a pair of input iterators that are compliant with the C++11 standard [24.2.3]. The equal
method determines whether two specified ranges compare equal. The lexicographical
method determines whether the first range compares lexicographically less than, equal to, or greater than the second range. Under certain circumstances, bslalg::RangeCompare::equal
and bslalg::RangeCompare::lexicographical
may perform optimized comparisons, as described below.
bslalg::RangeCompare::equal
may perform a bit-wise comparison of the two ranges when the following two criteria are met:
bslmf::IsBitwiseEqualityComparable
is declared for the type of the objects in the ranges being compared.bslalg::RangeCompare::lexicographical
may perform a bit-wise comparison of the two ranges when the following criterion is met:
Note that a class having the bslmf::IsBitwiseEqualityComparable
trait can be described as bit-wise comparable and should meet the following criteria:
Note that this component is for use primarily by the bslstl
package. Other clients should use the STL algorithms (in headers <bsl_algorithm.h>
and <bsl_memory.h>
).
This section illustrates intended use of this component.
In this example we will use the bslalg::RangeCompare::equal
class method to implement the equality-comparison operators for an iterable container type residing in the bslstl
package, and highlight the circumstances under which the optimization provided by the class method may be applied.
Suppose that we have a new iterable container type that will be included in the bslstl
package, and we wish to define comparison operators for the container. If the container has an iterator that provides access to the container's elements in a consistent order, and the elements themselves are equality-comparable, we can implement the container's equality-comparison operators by pair-wise comparing each of the elements over the entire range of elements in both containers. In such cases the container can use the bslalg::RangeCompare::equal
class method to equal-compare the container's elements, taking advantage of the optimizations the class method provides for bit-wise equality-comparable objects.
First, we create an elided definition of a container class, MyContainer
, which provides read-only iterators of the type MyContainer::ConstIterator
:
Notice that ConstIterator
is defined as a pointer type, which is one of the criteria required to enable the optimizations provided by the bslalg::RangeCompare::equal
class method.
Then, we declare the equality-comparison operators for MyContainer
:
Next, we implement the equality-comparison operators using bslalg::RangeCompare::equal
:
Then, we create the elided definition of a value-semantic class, MyString
, together with its definition of operator==
:
Notice that MyString
is not bit-wise comparable because the address values of the d_start_p
pointer data members in two MyString
objects will be different, even if the string values of the two objects are the same.
Next, we create two MyContainer<MyString>
objects, and compare them using operator==
:
Here, the call to the bslalg::RangeCompare::equal
class method in operator==
will perform an unoptimized pair-wise comparison of the elements in c1
and c2
.
Then, we create the elided definition of another value-semantic class, MyPoint
, together with its definition of operator==
:
Notice that the value of a MyPoint
object derives from the values of all of its data members, and that no padding is required for alignment. Furthermore, MyPoint
has no virtual methods. Therefore, MyPoint
objects are bit-wise comparable, and we can correctly declare the bslmf::IsBitwiseEqualityComparable
trait for the class, as shown above under the public TRAITS
section.
Now, we create two MyContainer<MyPoint>
objects and compare them using operator==
:
Here, the call to bslalg::RangeCompare::equal
in operator==
may take advantage of the fact that MyPoint
is bit-wise comparable and perform the comparison by directly bit-wise comparing the entire range of elements contained in the MyContainer<MyPoint>
objects. This comparison can provide a significant performance boost over the comparison between two MyContainer<MyPoint>
objects in which the nested bslmf::IsBitwiseEqualityComparable
trait is not associated with the MyPoint
class.
Finally, note that we can instantiate MyContainer
with int
or any other primitive type as the VALUE_TYPE
and still benefit from the optimized comparison operators, because primitive (i.e.: fundamental, enumerated, and pointer) types are inherently bit-wise comparable: