BLI: IndexMask: Comparison operators #117814

Merged
Jacques Lucke merged 26 commits from mod_moder/blender:index_mask_cmp into main 2024-02-10 15:32:44 +01:00

Support == and != operators for IndexMask.
This implementation is single thread and avoid any allocation.

Support `==` and `!=` operators for `IndexMask`. This implementation is single thread and avoid any allocation.
Iliya Katushenock added 1 commit 2024-02-04 19:36:45 +01:00
Iliya Katushenock added 3 commits 2024-02-04 22:11:03 +01:00
Iliya Katushenock added this to the Core Libraries project 2024-02-04 22:11:26 +01:00
Iliya Katushenock requested review from Jacques Lucke 2024-02-04 22:11:30 +01:00
Iliya Katushenock added 1 commit 2024-02-04 22:55:26 +01:00
Iliya Katushenock added 1 commit 2024-02-04 22:58:10 +01:00
Jacques Lucke requested changes 2024-02-05 16:58:34 +01:00
@ -760,2 +760,4 @@
template void IndexMask::to_indices(MutableSpan<int64_t>) const;
template<int Count, typename Func>
static void foreach_sequence(std::array<IndexMask, Count> masks, Func &&func)
Member

Not sure I fully understand this function yet. Maybe you can add a comment and maybe also add a test for this function specifically?

Not sure I fully understand this function yet. Maybe you can add a comment and maybe also add a test for this function specifically?
Author
Member

I added comment to describe this function few more. Not sure this function can be covered by test: this function is template right now, not sure i want to move this in header or make it more generic.
I'd like to implement more general version of this function (to handle any number of any mask (offset and size) and default values) in order to make more optimized merge/diff/... functions for #117805 (as like Segment::== calls in this case). But i think this is enough for now.

I added comment to describe this function few more. Not sure this function can be covered by test: this function is template right now, not sure i want to move this in header or make it more generic. I'd like to implement more general version of this function (to handle any number of any mask (offset and size) and default values) in order to make more optimized merge/diff/... functions for #117805 (as like `Segment::==` calls in this case). But i think this is enough for now.
Iliya Katushenock added 2 commits 2024-02-05 18:47:26 +01:00
Iliya Katushenock added 1 commit 2024-02-05 18:56:40 +01:00
Iliya Katushenock requested review from Jacques Lucke 2024-02-05 18:57:02 +01:00
Jacques Lucke requested changes 2024-02-06 18:01:52 +01:00
@ -868,4 +868,104 @@ template IndexMask IndexMask::from_indices(Span<int64_t>, IndexMaskMemory &);
template void IndexMask::to_indices(MutableSpan<int32_t>) const;
template void IndexMask::to_indices(MutableSpan<int64_t>) const;
/* Any mask is built from segments. Even if masks is the same, segments fragmentation might be
Member

I think an simple example as part of the description would make this more clear.

I think an simple example as part of the description would make this more clear.
Author
Member

Added.

Added.
@ -871,0 +871,4 @@
/* Any mask is built from segments. Even if masks is the same, segments fragmentation might be
* different. Produce re-fragmentation of each mask on the fly to have segment of each mask in some
* certain range and call function for them. */
template<int Count, typename Func>
Member

IndexMaskNum

`IndexMaskNum`
mod_moder marked this conversation as resolved
@ -871,0 +875,4 @@
static void foreach_sequence(std::array<IndexMask, Count> masks, Func &&func)
{
BLI_assert(std::all_of(masks.begin() + 1, masks.end(), [&](const IndexMask &maks) {
return masks[0].size() == maks.size();
Member

typo: maks.

typo: `maks`.
mod_moder marked this conversation as resolved
@ -871,0 +886,4 @@
std::array<IndexMaskSegment, Count> segments;
while (segment_iter[0] != masks[0].segments_num()) {
Member

Where is masks[1].segments_num() taken into consideration? Looks like we could go out of range somewhere.

Where is `masks[1].segments_num()` taken into consideration? Looks like we could go out of range somewhere.
Author
Member

Added comment about this.

Added comment about this.
@ -871,0 +893,4 @@
}
}
int16_t min_sequence_size = std::numeric_limits<int16_t>::max();
Member

Maybe use max_segment_size instead?
Maybe can also call this next_common_sequence_size.

Maybe use `max_segment_size` instead? Maybe can also call this `next_common_sequence_size`.
mod_moder marked this conversation as resolved
@ -871,0 +920,4 @@
}
}
static bool operator==(const IndexMaskSegment &a, const IndexMaskSegment &b)
Member

Need to be a bit careful here, because IndexMaskSegment is currently actually an alias for OffsetSpan<int64_t, int16_t>, but this comparison function does not work for OffsetSpan<int64_t, int16_t> in general.

Solution could be to make it so that IndexMaskSegment derives from OffsetSpan instead.

Need to be a bit careful here, because `IndexMaskSegment` is currently actually an `alias` for `OffsetSpan<int64_t, int16_t>`, but this comparison function does not work for `OffsetSpan<int64_t, int16_t>` in general. Solution could be to make it so that `IndexMaskSegment` derives from `OffsetSpan` instead.
Author
Member

Solution could be to make it so that IndexMaskSegment derives from OffsetSpan instead.

I really want to make this inheritance at some point to gather all methods to process mask segments in segment class.

This is static operator, more specific overload instead of template operator for this type (if such will be added). I see that this might be not so clear and be error-prone, for now, this operator could be done as just function.

> Solution could be to make it so that IndexMaskSegment derives from OffsetSpan instead. I really want to make this inheritance at some point to gather all methods to process mask segments in segment class. This is static operator, more specific overload instead of template operator for this type (if such will be added). I see that this might be not so clear and be error-prone, for now, this operator could be done as just function.
mod_moder marked this conversation as resolved
Iliya Katushenock added 2 commits 2024-02-06 18:45:12 +01:00
Iliya Katushenock requested review from Jacques Lucke 2024-02-06 18:46:31 +01:00
Iliya Katushenock added 1 commit 2024-02-06 18:48:59 +01:00
Jacques Lucke requested changes 2024-02-08 16:55:15 +01:00
@ -871,0 +882,4 @@
* in positions space. Indices can be any.
*/
template<int IndexMaskNum, typename Func>
static void foreach_sequence(std::array<IndexMask, IndexMaskNum> masks, Func &&func)
Member

The function generally looks ok, but I think it would still be nice if we could have tests specifically for it. I recommend exposing the function as a static method on IndexMask like so:

  /**
   * Iterate over the indices in multiple masks which have the same size. The given function is
   * called for groups of segments where each segment has the same size and comes from a different
   * input mask.
   * For example, if the input masks are (both have size 18):
   *   A: [0, 15), {20, 24, 25}
   *   B: [0, 5), [10, 15], {20, 30, 40, 50, 60, 70, 80, 90}
   * Then the function will be called multiple times, each time with two segments:
   *   1. [0, 5), [0, 5)
   *   2. [5, 10), [10, 15)
   *   3. [10, 15), {20, 30, 40, 50, 60}
   *   4. {20, 24, 25}, {70, 80, 90}
   */
  static void foreach_segment_zipped(Span<IndexMask> masks,
                                     FunctionRef<bool(Span<IndexMaskSegment> segments)> fn);

It doesn't seem necessary that the function is templated for now. Maybe that can be an optimization later if it ever becomes necessary.

The function generally looks ok, but I think it would still be nice if we could have tests specifically for it. I recommend exposing the function as a static method on `IndexMask` like so: ```cpp /** * Iterate over the indices in multiple masks which have the same size. The given function is * called for groups of segments where each segment has the same size and comes from a different * input mask. * For example, if the input masks are (both have size 18): * A: [0, 15), {20, 24, 25} * B: [0, 5), [10, 15], {20, 30, 40, 50, 60, 70, 80, 90} * Then the function will be called multiple times, each time with two segments: * 1. [0, 5), [0, 5) * 2. [5, 10), [10, 15) * 3. [10, 15), {20, 30, 40, 50, 60} * 4. {20, 24, 25}, {70, 80, 90} */ static void foreach_segment_zipped(Span<IndexMask> masks, FunctionRef<bool(Span<IndexMaskSegment> segments)> fn); ``` It doesn't seem necessary that the function is templated for now. Maybe that can be an optimization later if it ever becomes necessary.
Author
Member

Much better example case of fragmentation.

Much better example case of fragmentation.
mod_moder marked this conversation as resolved
Iliya Katushenock added 2 commits 2024-02-08 21:26:03 +01:00
Iliya Katushenock added 5 commits 2024-02-09 12:21:37 +01:00
Iliya Katushenock requested review from Jacques Lucke 2024-02-09 12:21:53 +01:00
Iliya Katushenock added 1 commit 2024-02-09 12:27:02 +01:00
Jacques Lucke added 6 commits 2024-02-10 14:33:48 +01:00
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-windows-amd64 Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-arm64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
0a4a9cbe6f
use EXPECT_FALSE
Member

@blender-bot build

@blender-bot build
Jacques Lucke approved these changes 2024-02-10 14:34:15 +01:00
Jacques Lucke merged commit bb5b4b1f2f into main 2024-02-10 15:32:44 +01:00
Iliya Katushenock deleted branch index_mask_cmp 2024-02-10 15:44:47 +01:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#117814
No description provided.