Skip to content

Commit 02b221b

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

9 files changed

Lines changed: 88 additions & 85 deletions

File tree

include/tev/Common.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,13 @@ template <template <typename...> class Vector> struct to_vector_fn {
278278
Vector<value_type> v;
279279
v.reserve(std::ranges::size(r));
280280
for (auto&& e : r) {
281-
v.emplace_back(static_cast<decltype(e)&&>(e));
281+
if constexpr (std::same_as<Vector<value_type>, std::basic_string<value_type>>) {
282+
v.push_back(static_cast<value_type>(e));
283+
} else {
284+
v.emplace_back(static_cast<decltype(e)&&>(e));
285+
}
282286
}
287+
283288
return v;
284289
} else {
285290
return Vector<value_type>(std::ranges::begin(r), std::ranges::end(r));
@@ -297,6 +302,7 @@ template <size_t N> struct fixed_chunks_fn {
297302
} // namespace detail
298303

299304
inline constexpr detail::to_vector_fn<std::vector> toVector{};
305+
inline constexpr detail::to_vector_fn<std::basic_string> toBasicString{};
300306
template <std::size_t N> inline constexpr detail::fixed_chunks_fn<N> fixed_chunks{};
301307

302308
// Helper for std::visit on multiple lambdas
@@ -540,12 +546,12 @@ template <typename Int> Int nextMultiple(Int value, Int multiple) { return divRo
540546

541547
template <typename T> std::string join(const T& components, std::string_view delim) {
542548
std::ostringstream s;
543-
for (const auto& component : components) {
544-
if (&components[0] != &component) {
549+
for (size_t i = 0; i < components.size(); ++i) {
550+
if (i != 0) {
545551
s << delim;
546552
}
547553

548-
s << component;
554+
s << components[i];
549555
}
550556

551557
return std::move(s).str();

include/tev/Image.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ struct ImageData {
120120

121121
size_t numPixels() const { return channels.front().numPixels(); }
122122

123-
std::vector<std::string> channelsInLayer(std::string_view layerName) const;
123+
std::vector<std::string_view> channelsInLayer(std::string_view layerName) const;
124124

125125
Task<void> applyColorConversion(const nanogui::Matrix3f& mat, int priority);
126126

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: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,11 @@ vector<string_view> split(string_view text, string_view delim, bool inclusive) {
163163
vector<string_view> splitWhitespace(string_view s, bool inclusive) { return split(s, ws, inclusive); }
164164

165165
string toLower(string_view str) {
166-
string result{str};
167-
transform(begin(result), end(result), begin(result), [](unsigned char c) { return (char)tolower(c); });
168-
return result;
166+
return str | views::transform([](auto c) { return (char)tolower(c); }) | toBasicString;
169167
}
170168

171169
string toUpper(string_view str) {
172-
string result{str};
173-
transform(begin(result), end(result), begin(result), [](unsigned char c) { return (char)toupper(c); });
174-
return result;
170+
return str | views::transform([](auto c) { return (char)toupper(c); }) | toBasicString;
175171
}
176172

177173
string_view trimLeft(string_view s) {
@@ -297,7 +293,7 @@ bool matchesFuzzy(string_view text, string_view filter, size_t* matchedPartId) {
297293

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

302298
string casedText, casedFilter;
303299
if (caseInsensitive) {

src/Image.cpp

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ void ImageData::readMetadataFromCicp(const ColorProfile::CICP& cicp) {
133133
hdrMetadata.bestGuessWhiteLevel = ituth273::bestGuessReferenceWhiteLevel(cicp.transfer);
134134
}
135135

136-
vector<string> ImageData::channelsInLayer(string_view layerName) const {
137-
vector<string> result;
136+
vector<string_view> ImageData::channelsInLayer(string_view layerName) const {
137+
vector<string_view> result;
138138

139139
for (const auto& c : channels) {
140140
// If the layer name starts at the beginning, and if no other dot is found after the end of the layer name, then we have found a
@@ -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;
@@ -973,7 +977,7 @@ void Image::ungroup(string_view groupName) {
973977
}
974978

975979
vector<ChannelGroup> Image::getGroupedChannels(string_view layerName) const {
976-
vector<vector<string>> groups = {
980+
static const vector<vector<string>> groups = {
977981
{"R", "G", "B"},
978982
{"r", "g", "b"},
979983
{"X", "Y", "Z"},
@@ -984,40 +988,37 @@ vector<ChannelGroup> Image::getGroupedChannels(string_view layerName) const {
984988
{"z"},
985989
};
986990

987-
static constexpr auto createChannelGroup = [](string_view layer, vector<string> channels) {
991+
static constexpr auto createChannelGroup = [](string_view layer, span<const string_view> channels) {
988992
TEV_ASSERT(!channels.empty(), "Can't create a channel group without channels.");
989993

990-
vector<string_view> channelTails = {channels.begin(), channels.end()};
994+
vector<string_view> channelTails = channels | views::transform(Channel::tail) | toVector;
991995
channelTails.erase(unique(begin(channelTails), end(channelTails)), end(channelTails));
992-
transform(begin(channelTails), end(channelTails), begin(channelTails), Channel::tail);
993996

994997
const string channelsString = join(channelTails, ",");
995998
const string name = layer.empty() ?
996999
channelsString :
9971000
(channelTails.size() == 1 ? fmt::format("{}{}", layer, channelsString) : fmt::format("{}({})", layer, channelsString));
9981001

999-
return ChannelGroup{name, std::move(channels)};
1002+
return ChannelGroup{name, channels | views::transform([](const auto& c) { return string{c}; }) | toVector};
10001003
};
10011004

1002-
string alphaChannelName = fmt::format("{}A", layerName);
1005+
const string alphaChannelName = fmt::format("{}A", layerName);
10031006

1004-
vector<string> allChannels = mData.channelsInLayer(layerName);
1007+
vector<string_view> allChannels = mData.channelsInLayer(layerName);
10051008

1006-
auto alphaIt = find(begin(allChannels), end(allChannels), alphaChannelName);
1007-
bool hasAlpha = alphaIt != end(allChannels);
1009+
const auto alphaIt = ranges::find(allChannels, alphaChannelName);
1010+
const bool hasAlpha = alphaIt != end(allChannels);
10081011
if (hasAlpha) {
10091012
allChannels.erase(alphaIt);
10101013
}
10111014

10121015
vector<ChannelGroup> result;
10131016

10141017
for (const auto& group : groups) {
1015-
vector<string> groupChannels;
1018+
vector<string_view> groupChannels;
10161019
for (string_view channel : group) {
1017-
string name = fmt::format("{}{}", layerName, channel);
1018-
auto it = find(begin(allChannels), end(allChannels), name);
1019-
if (it != end(allChannels)) {
1020-
groupChannels.emplace_back(name);
1020+
if (const auto it = ranges::find(allChannels, fmt::format("{}{}", layerName, channel)); it != end(allChannels)) {
1021+
groupChannels.emplace_back(*it);
10211022
allChannels.erase(it);
10221023
}
10231024
}
@@ -1027,20 +1028,20 @@ vector<ChannelGroup> Image::getGroupedChannels(string_view layerName) const {
10271028
groupChannels.emplace_back(alphaChannelName);
10281029
}
10291030

1030-
result.emplace_back(createChannelGroup(layerName, std::move(groupChannels)));
1031+
result.emplace_back(createChannelGroup(layerName, groupChannels));
10311032
}
10321033
}
10331034

1034-
for (const auto& name : allChannels) {
1035+
for (auto& name : allChannels) {
10351036
if (hasAlpha) {
1036-
result.emplace_back(createChannelGroup(layerName, vector<string>{name, alphaChannelName}));
1037+
result.emplace_back(createChannelGroup(layerName, array<string_view, 2>{name, alphaChannelName}));
10371038
} else {
1038-
result.emplace_back(createChannelGroup(layerName, vector<string>{name}));
1039+
result.emplace_back(createChannelGroup(layerName, span{&name, 1}));
10391040
}
10401041
}
10411042

10421043
if (hasAlpha && result.empty()) {
1043-
result.emplace_back(createChannelGroup(layerName, vector<string>{alphaChannelName}));
1044+
result.emplace_back(createChannelGroup(layerName, array<string_view, 1>{alphaChannelName}));
10441045
}
10451046

10461047
TEV_ASSERT(!result.empty(), "Images with no channels should never exist.");
@@ -1182,7 +1183,7 @@ Task<HeapArray<float>> Image::getRgbaHdrImageData(
11821183
const Channel* alphaChannel = nullptr;
11831184

11841185
// 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(); })) {
1186+
if (ranges::any_of(channels, [](const Channel& c) { return !c.isAlpha(); })) {
11861187
if (channels.back().isAlpha()) {
11871188
alphaChannel = &channels.back();
11881189
}
@@ -1376,21 +1377,21 @@ string Image::toString() const {
13761377

13771378
sstream << "\nChannels:\n";
13781379

1379-
auto localLayers = mData.layers;
1380-
transform(begin(localLayers), end(localLayers), begin(localLayers), [this](string layer) {
1381-
auto channels = mData.channelsInLayer(layer);
1382-
transform(begin(channels), end(channels), begin(channels), [](string channel) { return Channel::tail(channel); });
1380+
sstream << join(
1381+
mData.layers | views::transform([this](string_view layer) {
1382+
const auto channels = mData.channelsInLayer(layer) | views::transform([](string_view c) { return Channel::tail(c); });
13831383

1384-
if (layer.empty()) {
1385-
return join(channels, ",");
1386-
} else if (channels.size() == 1) {
1387-
return layer + channels.front();
1388-
} else {
1389-
return layer + "("s + join(channels, ",") + ")"s;
1390-
}
1391-
});
1384+
if (layer.empty()) {
1385+
return join(channels, ",");
1386+
} else if (channels.size() == 1) {
1387+
return fmt::format("{}{}", layer, channels.front());
1388+
} else {
1389+
return fmt::format("{}({})", layer, join(channels, ","));
1390+
}
1391+
}),
1392+
"\n"
1393+
);
13921394

1393-
sstream << join(localLayers, "\n");
13941395
return std::move(sstream).str();
13951396
}
13961397

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 & 12 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
});
@@ -2851,15 +2855,11 @@ void ImageViewer::updateTitle() {
28512855
// Only treat alpha specially if it is not the only channel.
28522856
const bool hasAlpha = channels.size() > 1 && Channel::isAlpha(channels.back());
28532857

2854-
auto channelTails = channels;
2855-
transform(begin(channelTails), end(channelTails), begin(channelTails), Channel::tail);
2856-
28572858
caption << fmt::format("{} – {} – {}%", mCurrentImage->shortName(), mCurrentGroup, (int)std::round(mImageCanvas->scale() * 100));
28582859

28592860
const auto rel = mouse_pos() - mImageCanvas->position();
28602861
const vector<float> values = mImageCanvas->getValuesAtNanoPos({rel.x(), rel.y()}, channels);
28612862
const Vector2i imageCoords = mImageCanvas->getImageCoords(mCurrentImage.get(), {rel.x(), rel.y()});
2862-
TEV_ASSERT(values.size() >= channelTails.size(), "Should obtain a value for every existing channel.");
28632863

28642864
caption << fmt::format(
28652865
" – @{},{} ({:.3f},{:.3f}) / {}x{}: ",

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);

0 commit comments

Comments
 (0)