diff options
author | Paul Gilbert | 2016-07-26 19:48:14 -0400 |
---|---|---|
committer | Paul Gilbert | 2016-07-26 19:48:14 -0400 |
commit | 504cf6ecb688a3f1c65a857bffd527d8b0e6ba63 (patch) | |
tree | 0c0d96d4061c11850c851f0fc981c75a58c20515 /common/algorithm.h | |
parent | d8c28d15ae553d047b7e571f98727fa79ee143f3 (diff) | |
parent | e19922d181e775791f9105b8be7ff410770ede51 (diff) | |
download | scummvm-rg350-504cf6ecb688a3f1c65a857bffd527d8b0e6ba63.tar.gz scummvm-rg350-504cf6ecb688a3f1c65a857bffd527d8b0e6ba63.tar.bz2 scummvm-rg350-504cf6ecb688a3f1c65a857bffd527d8b0e6ba63.zip |
Merge branch 'master' into xeen
Diffstat (limited to 'common/algorithm.h')
-rw-r--r-- | common/algorithm.h | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/common/algorithm.h b/common/algorithm.h index 6453073ae5..13cdd9f991 100644 --- a/common/algorithm.h +++ b/common/algorithm.h @@ -177,7 +177,8 @@ T sortChoosePivot(T first, T last) { template<typename T, class StrictWeakOrdering> T sortPartition(T first, T last, T pivot, StrictWeakOrdering &comp) { --last; - SWAP(*pivot, *last); + if (pivot != last) + SWAP(*pivot, *last); T sorted; for (sorted = first; first != last; ++first) { @@ -188,7 +189,8 @@ T sortPartition(T first, T last, T pivot, StrictWeakOrdering &comp) { } } - SWAP(*last, *sorted); + if (last != sorted) + SWAP(*last, *sorted); return sorted; } @@ -268,5 +270,26 @@ T gcd(T a, T b) { #pragma warning(pop) #endif +/** + * Replacement algorithm for iterables. + * + * Replaces all occurrences of "original" in [begin, end) with occurrences of "replaced". + * + * @param[in, out] begin: First element to be examined. + * @param[in] end: Last element in the seubsection. Not examined. + * @param[in] original: Elements to be replaced. + * @param[in] replaced: Element to replace occurrences of "original". + * + * @note Usage examples and unit tests may be found in "test/common/algorithm.h" + */ +template<class It, class Dat> +void replace(It begin, It end, const Dat &original, const Dat &replaced) { + for (; begin != end; ++begin) { + if (*begin == original) { + *begin = replaced; + } + } +} + } // End of namespace Common #endif |