| 229 | // must be constructable with Container(size_t). |
| 230 | template <typename Container, typename Getter> |
| 231 | void sortByComputedValue(Container& container, Getter&& valueGetter, bool stable = false) { |
| 232 | typedef typename Container::value_type ContainerValue; |
| 233 | typedef decltype(valueGetter(ContainerValue())) ComputedValue; |
| 234 | typedef std::pair<ComputedValue, size_t> ComputedPair; |
| 235 | |
| 236 | size_t containerSize = container.size(); |
| 237 | |
| 238 | if (containerSize <= 1) |
| 239 | return; |
| 240 | |
| 241 | std::vector<ComputedPair> work(containerSize); |
| 242 | for (size_t i = 0; i < containerSize; ++i) |
| 243 | work[i] = {valueGetter(container[i]), i}; |
| 244 | |
| 245 | auto compare = [](ComputedPair const& a, ComputedPair const& b) { return a.first < b.first; }; |
| 246 | |
| 247 | // Sort the comptued values and the associated indexes |
| 248 | if (stable) |
| 249 | stableSort(work, compare); |
| 250 | else |
| 251 | sort(work, compare); |
| 252 | |
| 253 | Container result(containerSize); |
| 254 | for (size_t i = 0; i < containerSize; ++i) |
| 255 | swap(result[i], container[work[i].second]); |
| 256 | |
| 257 | swap(container, result); |
| 258 | } |
| 259 | |
| 260 | template <typename Container, typename Getter> |
| 261 | void stableSortByComputedValue(Container& container, Getter&& valueGetter) { |
no test coverage detected