Bug 153109 - Use all_of, any_of or none_of from STD instead of looping to check a condition over a range
Summary: Use all_of, any_of or none_of from STD instead of looping to check a conditio...
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: LibreOffice (show other bugs)
Version:
(earliest affected)
4.4 all versions
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard: target:24.2.0
Keywords: difficultyBeginner, easyHack, skillCpp, topicCleanup
Depends on:
Blocks: Dev-related
  Show dependency treegraph
 
Reported: 2023-01-19 14:57 UTC by Hossein
Modified: 2023-06-12 11:11 UTC (History)
3 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Hossein 2023-01-19 14:57:24 UTC
Since C++11, STL provides means to process containers easier than before. Some of the available functions inside <algorithm> are describe below:

std::all_of
  Test the condition on all of the elements
  https://cplusplus.com/reference/algorithm/all_of/
std::any_of
  Test if any element satisfies the condition
  https://cplusplus.com/reference/algorithm/any_of/
std::none_of
  Test if no elements satisfies the condition
  https://cplusplus.com/reference/algorithm/none_of/

In the above links, you can find description for each of the above functions. Beyond that, you can find good tutorials for these functions. For example:

The big STL Algorithms tutorial: all_of, any_of, none_of
https://www.sandordargo.com/blog/2019/02/20/stl-algorithm-tutorial-part-1-any-all-none

For examples of such changes inside LibreOffice code, see this commits:

writerfilter: use std::any_of() e542b622aeb70230365971cab4838dbe0b6e0a93%5E%21

desktop: simplify code by std::none_of b4848def4684ad277af23a10d73b4b5c9cb201fb

xmloff: simplify code by using std::all_of/std::any_of/std::none_of c675a2d669c191f3b18ae006c78ef83efba14c30

You have to make sure that you keep the code behavior unchanged when using the above STD functions.
Comment 1 Stéphane Guillou (stragu) 2023-01-20 16:00:32 UTC
Thanks Hossein. Sounds good to me.

Marking 4.4 as earliest affected as that's when C++11 started being used AFAIK. (e.g. https://meeksfamily.uk/~michael/blog/2015-01-29-under-the-hood-4-4.html and https://archive.fosdem.org/2015/schedule/event/editors_cpp11_libreoffice/attachments/slides/633/export/events/attachments/editors_cpp11_libreoffice/slides/633/FOSDEM2015_LibreOffice_C_11.pdf)
Comment 2 Baole Fang 2023-03-12 21:12:33 UTC
Hi, I'd like to take a look at this.
Comment 3 Mike Kaganski 2023-03-15 03:18:51 UTC
Note that a common sense should apply: this change should not be applied when it doesn't improve things: sometimes the loop is optimal both in terms of performance and readability.
Comment 4 Commit Notification 2023-06-12 11:11:18 UTC
Paris Oplopoios committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/81e815edbf5b4694789aa1d7b9e3ecde08b08b21

tdf#153109 Use any_of instead of loop to check a range

It will be available in 24.2.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.