MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / sortByComputedValue

Function sortByComputedValue

source/core/StarAlgorithm.hpp:231–258  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

229// must be constructable with Container(size_t).
230template <typename Container, typename Getter>
231void 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
260template <typename Container, typename Getter>
261void stableSortByComputedValue(Container& container, Getter&& valueGetter) {

Callers 8

entitiesAtMethod · 0.85
parseABCMethod · 0.85
flushMethod · 0.85
entityQueryImplFunction · 0.85
determineRecipesMethod · 0.85
updateMethod · 0.85
binnedChoiceFromJsonFunction · 0.85

Calls 3

stableSortFunction · 0.85
sortFunction · 0.70
sizeMethod · 0.45

Tested by

no test coverage detected