MCPcopy Create free account
hub / github.com/Snapchat/Valdi / select

Method select

valdi/src/valdi/runtime/Resources/AssetDensityResolver.cpp:20–40  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18}
19
20std::optional<size_t> AssetDensityResolver::select(double desiredDensity) const {
21 double bestDensity = 0;
22 double bestDensityDistance = std::numeric_limits<double>::max();
23 std::optional<size_t> bestIndex;
24
25 for (size_t index = 0; index < _densities.size(); index++) {
26 auto density = _densities[index];
27 auto densityDistance = std::abs(desiredDensity - density);
28
29 // We take the density that matches the most closely our device screen density.
30 // If two densities are equally distant to the optimal distance, we take the smallest one.
31 if (!bestIndex || densityDistance < bestDensityDistance ||
32 (densityDistance == bestDensityDistance && density < bestDensity)) {
33 bestDensity = density;
34 bestDensityDistance = densityDistance;
35 bestIndex = {index};
36 }
37 }
38
39 return bestIndex;
40}
41
42} // namespace Valdi

Callers 2

findBestAssetMethod · 0.45

Calls 2

maxFunction · 0.85
sizeMethod · 0.45

Tested by

no test coverage detected