From 1311fe5c49146b14a476f47c10529931a467ff98 Mon Sep 17 00:00:00 2001 From: Bertrand Augereau Date: Tue, 10 Nov 2015 09:46:58 +0100 Subject: COMMON: Avoid useless (and dangerous when cctor/operator= don't support it) SWAP(x, x) in sorting --- common/algorithm.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'common/algorithm.h') diff --git a/common/algorithm.h b/common/algorithm.h index 6453073ae5..cbd6eae708 100644 --- a/common/algorithm.h +++ b/common/algorithm.h @@ -177,7 +177,8 @@ T sortChoosePivot(T first, T last) { template 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; } -- cgit v1.2.3 From a19b50ddf26ac4e36b3e86f781ef7c2ae5fc69fd Mon Sep 17 00:00:00 2001 From: Borja Lorente Escobar Date: Wed, 2 Mar 2016 17:07:50 +0100 Subject: COMMON: Add replace functions to Common and String. COMMON: Add replacement to common/algorithm.h COMMON: Intermediate commit to show doubts. COMMON: Basic String::replace() methods implemented. COMMON: Fix typo in the algorithm.h documentation. COMMON: Fix documentation of String::replace() COMMON: Fix formatting issues in method signatures. COMMON: Add assert and reformat loops in str and algorithm. COMMON: Fix typo in comment. COMMON: Fix style in string test cases. COMMON: Add Doxygen documentation to algorithm and String. COMMON: Add Doxygen documentation to algorithm and String. COMMON: Add Doxygen documentation to algorithm. COMMON: Fix style in algorithm comments. COMMON: Add Doxygen comments to String. COMMON: Add Doxygen comments to algorithm test function. COMMON: Add String support for substring replace. COMMON: Fix string replace to comply with STL COMMON: Fix documentation on string replace COMMON: Fix style in string replace COMMON: Fix unwanted reference problem in String::replace(). COMMON: Fix indentation in comments for replace COMMON: Fix indentation in replace COMMON: Fix comments in String::replace to match implementation. COMMON: Remove assert to allow for not-null-terminated character arrays COMMON: Add new test for String::replace COMMON: Fix broken comments on String::replace COMMON: Fix sharing bug on ensureCapacity COMMON: Remove superfluous call to makeUnique() --- common/algorithm.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'common/algorithm.h') diff --git a/common/algorithm.h b/common/algorithm.h index cbd6eae708..13cdd9f991 100644 --- a/common/algorithm.h +++ b/common/algorithm.h @@ -270,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 +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 -- cgit v1.2.3