Skip to content

Commit f37f4ce

Browse files
committed
chore: minor emplace() & ranges refactor
1 parent b315e77 commit f37f4ce

7 files changed

Lines changed: 44 additions & 36 deletions

File tree

src/BackgroundImagesLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void BackgroundImagesLoader::enqueue(const fs::path& path, string_view channelSe
4646
vector<fs::directory_entry> entries;
4747
forEachFileInDir(mRecursiveDirectories, canonicalPath, [&](const auto& entry) {
4848
if (!entry.is_directory()) {
49-
mFilesFoundInDirectories.emplace(PathAndChannelSelector{entry, string{channelSelector}});
49+
mFilesFoundInDirectories.emplace(entry, string{channelSelector});
5050
entries.emplace_back(entry);
5151
}
5252
});

src/Common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ bool matchesFuzzy(string_view text, string_view filter, size_t* matchedPartId) {
297297

298298
// Perform matching via smart casing: if the filter is all lowercase, we want to match case-insensitively. If the filter contains any
299299
// uppercase characters, we want to match case-sensitively.
300-
const bool caseInsensitive = all_of(begin(filter), end(filter), [](char c) { return islower(c); });
300+
const bool caseInsensitive = ranges::all_of(filter, [](char c) { return islower(c); });
301301

302302
string casedText, casedFilter;
303303
if (caseInsensitive) {

src/Image.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -834,29 +834,33 @@ Texture* Image::texture(span<const string> channelNames, EInterpolationMode minF
834834
default: throw runtime_error{"Unsupported number of channels for texture."};
835835
}
836836

837-
Texture::ComponentFormat componentFormat = Texture::ComponentFormat::Float16;
838-
for (const auto& chanName : channelNames) {
839-
const Channel* chan = channel(chanName);
840-
if (chan && chan->desiredPixelFormat() == EPixelFormat::F32) {
841-
componentFormat = Texture::ComponentFormat::Float32;
842-
break; // No need to check further, we already have a channel that requires F32.
843-
}
844-
}
837+
const Texture::ComponentFormat componentFormat = ranges::any_of(
838+
channelNames | views::transform([this](const auto& c) { return channel(c); }),
839+
[](const auto& c) { return c && c->desiredPixelFormat() == EPixelFormat::F32; }
840+
) ?
841+
Texture::ComponentFormat::Float32 :
842+
Texture::ComponentFormat::Float16;
845843

846844
mTextures.emplace(
847-
lookup,
848-
ImageTexture{
845+
piecewise_construct,
846+
tuple{
847+
lookup
848+
},
849+
tuple{
849850
new Texture{
850-
pixelFormat, componentFormat,
851-
{size().x(), size().y()},
852-
toNanogui(minFilter),
853-
toNanogui(magFilter),
854-
Texture::WrapMode::ClampToEdge,
855-
1, Texture::TextureFlags::ShaderRead,
856-
true, },
857-
{channelNames.begin(), channelNames.end()},
851+
pixelFormat,
852+
componentFormat,
853+
{size().x(), size().y()},
854+
toNanogui(minFilter),
855+
toNanogui(magFilter),
856+
Texture::WrapMode::ClampToEdge,
857+
1,
858+
Texture::TextureFlags::ShaderRead,
859+
true,
860+
},
861+
channelNames | toVector,
858862
false,
859-
}
863+
}
860864
);
861865

862866
auto& texture = mTextures.at(lookup).nanoguiTexture;
@@ -1182,7 +1186,7 @@ Task<HeapArray<float>> Image::getRgbaHdrImageData(
11821186
const Channel* alphaChannel = nullptr;
11831187

11841188
// Only treat the alpha channel specially if it is not the only channel of the image.
1185-
if (!all_of(begin(channels), end(channels), [](const Channel& c) { return c.isAlpha(); })) {
1189+
if (ranges::any_of(channels, [](const Channel& c) { return !c.isAlpha(); })) {
11861190
if (channels.back().isAlpha()) {
11871191
alphaChannel = &channels.back();
11881192
}

src/ImageCanvas.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ Task<shared_ptr<CanvasStatistics>> ImageCanvas::computeCanvasStatistics(
831831
const ChannelView<float>* alphaChannel = nullptr;
832832

833833
// Only treat the alpha channel specially if it is not the only channel of the image.
834-
if (!all_of(begin(flattened), end(flattened), [](const Channel& c) { return c.isAlpha(); })) {
834+
if (ranges::any_of(flattened, [](const Channel& c) { return !c.isAlpha(); })) {
835835
if (flattened.back().isAlpha()) {
836836
alphaChannel = &views.back();
837837
}

src/ImageViewer.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,17 +1206,21 @@ bool ImageViewer::keyboard_event(int key, int scancode, int action, int modifier
12061206
}
12071207

12081208
return true;
1209-
} else if (key == GLFW_KEY_UP || key == GLFW_KEY_W || key == GLFW_KEY_PAGE_UP ||
1210-
(key == GLFW_KEY_TAB && (modifiers & GLFW_MOD_CONTROL) && (modifiers & GLFW_MOD_SHIFT))) {
1209+
} else if (
1210+
key == GLFW_KEY_UP || key == GLFW_KEY_W || key == GLFW_KEY_PAGE_UP ||
1211+
(key == GLFW_KEY_TAB && (modifiers & GLFW_MOD_CONTROL) && (modifiers & GLFW_MOD_SHIFT))
1212+
) {
12111213
if (key != GLFW_KEY_TAB && (modifiers & GLFW_MOD_SHIFT)) {
12121214
selectReference(nextImage(mCurrentReference, Backward));
12131215
} else {
12141216
selectImage(nextImage(mCurrentImage, Backward));
12151217
}
12161218

12171219
return true;
1218-
} else if (key == GLFW_KEY_DOWN || key == GLFW_KEY_S || key == GLFW_KEY_PAGE_DOWN ||
1219-
(key == GLFW_KEY_TAB && (modifiers & GLFW_MOD_CONTROL) && !(modifiers & GLFW_MOD_SHIFT))) {
1220+
} else if (
1221+
key == GLFW_KEY_DOWN || key == GLFW_KEY_S || key == GLFW_KEY_PAGE_DOWN ||
1222+
(key == GLFW_KEY_TAB && (modifiers & GLFW_MOD_CONTROL) && !(modifiers & GLFW_MOD_SHIFT))
1223+
) {
12201224
if (key != GLFW_KEY_TAB && (modifiers & GLFW_MOD_SHIFT)) {
12211225
selectReference(nextImage(mCurrentReference, Forward));
12221226
} else {
@@ -1387,7 +1391,7 @@ void ImageViewer::draw_contents() {
13871391
}
13881392

13891393
const bool anyImageVisible = mCurrentImage || mCurrentReference ||
1390-
any_of(begin(mImageButtonContainer->children()), end(mImageButtonContainer->children()), [](const auto& c) { return c->visible(); });
1394+
ranges::any_of(mImageButtonContainer->children(), [](const auto& c) { return c->visible(); });
13911395

13921396
for (auto button : mAnyImageButtons) {
13931397
button->set_enabled(anyImageVisible);
@@ -2403,7 +2407,7 @@ void ImageViewer::openImageDialog() {
24032407
allImages.push_back(filter.first);
24042408
}
24052409

2406-
filters.emplace(filters.begin(), pair<string, string>{join(allImages, ","), "All images"});
2410+
filters.emplace(filters.begin(), pair{join(allImages, ","), "All images"});
24072411
const auto paths = file_dialog(this, FileDialogType::OpenMultiple, filters);
24082412

24092413
for (size_t i = 0; i < paths.size(); ++i) {
@@ -2735,7 +2739,7 @@ void ImageViewer::updateFilter() {
27352739
do {
27362740
int len = codePointLength(first[beginOffset]);
27372741

2738-
allStartWithSameChar = all_of(begin(activeImageNames), end(activeImageNames), [&first, beginOffset, len](string_view name) {
2742+
allStartWithSameChar = ranges::all_of(activeImageNames, [&first, beginOffset, len](string_view name) {
27392743
if (beginOffset + len > (int)name.size()) {
27402744
return false;
27412745
}
@@ -2755,7 +2759,7 @@ void ImageViewer::updateFilter() {
27552759
bool allEndWithSameChar;
27562760
do {
27572761
char lastChar = first[firstSize - endOffset - 1];
2758-
allEndWithSameChar = all_of(begin(activeImageNames), end(activeImageNames), [lastChar, endOffset](string_view name) {
2762+
allEndWithSameChar = ranges::all_of(activeImageNames, [lastChar, endOffset](string_view name) {
27592763
int index = (int)name.size() - endOffset - 1;
27602764
return index >= 0 && name[index] == lastChar;
27612765
});

src/imageio/Jpeg2000ImageLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ Jp2Metadata extractJp2Metadata(span<const uint8_t> data) {
205205

206206
if (memcmp(box->data.data(), xmpUuid, 16) == 0) {
207207
meta.xmpXml = box->data.subspan(16);
208-
} else if (any_of(begin(exifUuids), end(exifUuids), [&box](const uint8_t (&knownUuid)[16]) {
208+
} else if (ranges::any_of(exifUuids, [&box](const uint8_t (&knownUuid)[16]) {
209209
return memcmp(box->data.data(), knownUuid, 16) == 0;
210210
})) {
211211
meta.exifData = box->data.subspan(16);

src/imageio/TiffImageLoader.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ Task<void> tiffDataToFloat32(
240240
} else if (kind == ETiffKind::U32) {
241241
co_await toFloat32(imageData.span<const uint32_t>().subspan(offset), numSppIn, rgbaView, hasAlpha, priority, scale);
242242
} else if (kind == ETiffKind::Palette) {
243-
if (any_of(palette.begin(), palette.end(), [](const auto& c) { return c.empty(); })) {
243+
if (ranges::any_of(palette, [](const auto& c) { return c.empty(); })) {
244244
throw runtime_error{"Palette data is empty."};
245245
}
246246

@@ -723,7 +723,7 @@ Task<void> linearizeAndNormalizeRawDng(
723723
channelScale[c] = 1.0f / (whiteLevel[c] - maxBlackLevel[c]);
724724
}
725725

726-
if (any_of(channelScale.begin(), channelScale.end(), [](float s) { return s != 1.0f; })) {
726+
if (ranges::any_of(channelScale, [](float s) { return s != 1.0f; })) {
727727
tlog::debug("Non-1.0 channel scale [{}]", join(channelScale, ","));
728728

729729
co_await ThreadPool::global().parallelFor(
@@ -1806,7 +1806,7 @@ Task<ImageData> readTiffImage(
18061806
}
18071807
}
18081808

1809-
if (all_of(begin(SUPPORTED_PHOTOMETRICS), end(SUPPORTED_PHOTOMETRICS), [&](uint16_t p) { return p != photometric; })) {
1809+
if (ranges::all_of(SUPPORTED_PHOTOMETRICS, [&](uint16_t p) { return p != photometric; })) {
18101810
throw ImageLoadError{fmt::format("Unsupported photometric interpretation: {}", photometric)};
18111811
}
18121812

@@ -1913,7 +1913,7 @@ Task<ImageData> readTiffImage(
19131913
throw ImageLoadError{"Palette images must have unsigned integer sample format."};
19141914
}
19151915

1916-
if (any_of(begin(palette), end(palette), [](const auto& c) { return c.empty(); })) {
1916+
if (ranges::any_of(palette, [](const auto& c) { return c.empty(); })) {
19171917
throw ImageLoadError{"Failed to read color palette."};
19181918
}
19191919

0 commit comments

Comments
 (0)