UCS/DATASTRUCT: Add ucs_array_for_each_index - #11853
Conversation
|
🤖 Starting review — findings will be posted here when done. |
1bee8ad to
d874a39
Compare
|
🤖 Starting review — findings will be posted here when done. |
|
🤖 Review complete — no issues found. |
d874a39 to
e1c807d
Compare
|
🤖 Starting review — findings will be posted here when done. |
|
🤖 Review complete — no issues found. |
Signed-off-by: Guy Ealey Morag <gealeymorag@nvidia.com>
e1c807d to
7a41231
Compare
|
🤖 Starting review — findings will be posted here when done. |
|
🤖 Review complete — no issues found. |
Signed-off-by: Guy Ealey Morag <gealeymorag@nvidia.com>
|
🤖 Starting review — findings will be posted here when done. |
|
🤖 Review complete — no issues found. |
| * @param _array Array to iterate over | ||
| */ | ||
| #define ucs_array_for_each_index(_elem, _idx, _array) \ | ||
| ucs_carray_for_each_index(_elem, _idx, ucs_array_begin(_array), \ |
There was a problem hiding this comment.
can we not add ucs_carray_for_each_index() macro and here use something like below which would have benefit of only relying on index to get a fresh elem pointer:
#define ucs_array_for_each_index(_elem, _idx, _array) \
for ((_idx) = 0; \
((_idx) < ucs_array_length(_array)) && \
(((_elem) = ucs_array_elem(_array, _idx)), 1); \
++(_idx))There was a problem hiding this comment.
Using ucs_array_elem would add an unnecessary assert on every iteration, the fact that it's part of the loop let us skip this check safely.
Also it's simpler to rely directly on the carray variant like it's done in ucs_array_for_each.
There was a problem hiding this comment.
assert is already there long time and if needed can be removed for that case
There was a problem hiding this comment.
This implementation is a direct extension to the existing ucs_array_for_each and ucs_carray_for_each. It uses the same patterns, same assumptions and it works in a very similar way.
The idea is that anywhere that can use the existing ucs_array_for_each would be able to use ucs_array_for_each_index to also have the index available.
|
🤖 CI Triage Agent — TL;DR: The Coverity gate failed with 1 new defect (NO_EFFECT) introduced by this PR's new gtest: Full analysisSummary: Root cause: The build and Line 1123 is new code from this PR: size_t idx, expected_idx;
ucs_carray_for_each_index(elem, idx, values, 0) { ADD_FAILURE() << ...; }
Implicated commit: 7a41231 — "UCS/DATASTRUCT: Add File: test/gtest/ucs/test_datatype.cc:1123 (macro at src/ucs/sys/compiler_def.h:256) Suggested fix: Keep the empty-array coverage but stop feeding the macro a compile-time constant 0, e.g.: UCS_TEST_F(test_array, carray_for_each_index) {
const int values[] = {3, 5, 7};
const int *elem;
size_t idx, expected_idx;
/* Non-constant length, so the loop condition is not folded to "idx < 0" */
volatile size_t empty_length = 0;
ucs_carray_for_each_index(elem, idx, values, empty_length) {
ADD_FAILURE() << "iterated over an empty array";
}
EXPECT_EQ(0u, idx);
...
}(Using Related: PR #11853 ("UCS/DATASTRUCT: Add |
Signed-off-by: Guy Ealey Morag <gealeymorag@nvidia.com>
|
🤖 Starting review — findings will be posted here when done. |
|
🤖 Review complete — no issues found. |
What?
ucs_array_for_each_indexwhich is a variation ofucs_array_for_eachthat also retains the index of the element.Why?
Code that needs both an array element and its index currently uses a manual loop with
ucs_array_elem.This helper removes that boilerplate and makes switching between indexed and non-indexed iteration straightforward.
This macro would be used in later PRs I'm currently working on too.
Before:
After: