Commit Graph

8 Commits

Author SHA1 Message Date
Stephen Kelly 10e53e2308 cmAlgorithms: Add missing const to functors. 2015-02-17 20:18:57 +01:00
Stephen Kelly 7490632258 cmAlgorithms: Remove sort of already-sorted container.
The indices is populated by an increasing number.
2015-02-17 20:18:57 +01:00
Stephen Kelly cebeed2486 cmAlgorithms: Add cmRemoveDuplicates algorithm.
Start by creating a vector to hold a unique values of the input range.  We
expect that in most cases, there will be relatively few duplicates, so
reserving enough memory for a complete copy is worthwhile.  Unlike a solution
involving a std::set, this algorithm allocates all the memory it needs
in one go and in one place, so it is more cache friendly.

Populate the unique copy with a lower_bound insert algorithm and record the
indices of duplicates.  This is the same complexity as the std::set insert
algorithm, but without the need to allocate memory on the heap and other
disadvantages of std::set.

Remove the duplicates with the cmRemoveIndices algorithm.
2015-02-15 20:46:25 +01:00
Stephen Kelly 050958a328 cmAlgorithms: Add cmRemoveMatching algorithm.
Implement it in terms of std::remove_if with a binary search through
a matching range.
2015-02-15 19:56:08 +01:00
Stephen Kelly 0b5cf0dabd cmAlgorithms: Implement algorithm for removing indexes.
Implement ContainerAlgorithms::RemoveN to remove N elements to the
end of a container by rotating.  The rotate is implemented in terms
of the efficient swap algorithm, optimized even more in the standard
library implementation when the compiler supports the rvalue-references
feature to move elements.  Implement cmRemoveN with a Range API
for completeness.

std::rotate in C++11 is specified to return an iterator, but
c++98 specifies it to return void.  libstdc++ 5.0 will be the first
version to have the correct return type.  Implement
ContainerAlgorithms::Rotate in terms of std::rotate and return the
correct iterator from it.  While std::rotate requires forward iterators,
 this workaround means cmRotate requires bidirectional iterators.  As
most of CMake uses random access iterators anyway, this should not
be a problem.

Implement cmRemoveIndices in terms of the RemoveN algorithm, such
that each element which is not removed is rotated only once.  This
can not use the cmRemoveN range-API algorithm because that would
require creating a new range, but the range must be taken by reference
and so it can't be a temporary.

These remove algorithms are not part of the STL and I couldn't find them
anywhere else either.
2015-02-15 19:04:31 +01:00
Stephen Kelly 0c12f1ea0d cmAlgorithms: Add a range adaptor and API for adjusting a range. 2015-02-11 22:57:55 +01:00
Stephen Kelly 4e78ebbdf9 cmAlgorithms: Add a Range container and adaptor method.
This can make a pair of iterators API compatible with the
cmJoin algorithm and other range-based algorithms.

Accept different iterator types in the cmRange adaptor so that
a const and non-const iterator are accepted.
2015-02-11 22:57:55 +01:00
Stephen Kelly ac26d4b343 Split cmAlgorithms into a separate header file. 2015-02-10 22:14:18 +01:00