https://bugs.documentfoundation.org/show_bug.cgi?id=163738

            Bug ID: 163738
           Summary: Use insert() to add multiple values in containers
                    instead of a loop
           Product: LibreOffice
           Version: unspecified
          Hardware: All
                OS: All
            Status: UNCONFIRMED
          Severity: normal
          Priority: medium
         Component: LibreOffice
          Assignee: [email protected]
          Reporter: [email protected]

Description:
In several places in the code, you may see the use of for loops to insert
values from a container to another.

It is more efficient and better readable if insert function is used instead of
a loop.

For example, consider vector. One may use insert function. The insert function
is improved in C++20 which is now the baseline for LibreOffice code:

std::vector<T,Allocator>::insert
https://en.cppreference.com/w/cpp/container/vector/insert

As an example, consider this code:

std::vector<int> v {1, 2, 3}, to_insert {4, 5};

for ( size_t i = 0; i < to_insert.size(); ++i ) {
    v.push_back(to_insert[i]);
}

The loop can be written as simple as:

v.insert(v.end(), to_insert.begin(), to_insert.end());

Finding Candidates:
You can try finding "for" loops which consider the size or length of a
container with: 

$ git grep -E "for ?\(" | grep -iF "size()"
$ git grep -E "for ?\(" | grep -iF "length()"

Note all of the found instances are suitable for the current issue. Some of
them can be improved with a range-based for loop:

tdf#145538 Use range based for loops
https://bugs.documentfoundation.org/show_bug.cgi?id=145538

One trick to find some of the more relevant instances is searching for
push_back in the next line:

$ git grep -A 1 -E "for ?\(" *.cxx|grep push_back

-- 
You are receiving this mail because:
You are the assignee for the bug.

Reply via email to