summaryrefslogtreecommitdiff
path: root/Libraries/LibGfx/DisjointRectSet.cpp
diff options
context:
space:
mode:
authorTom <tomut@yahoo.com>2020-08-18 16:25:58 -0600
committerAndreas Kling <kling@serenityos.org>2020-08-19 11:20:27 +0200
commit5cd2a370795b2447e1a894befbf3c4ee155484be (patch)
tree58986876dba5842763c6e445f14fe59b8ca96a3e /Libraries/LibGfx/DisjointRectSet.cpp
parent983f4f935c4a26d024966896c6ea1ec853d44280 (diff)
downloadserenity-5cd2a370795b2447e1a894befbf3c4ee155484be.zip
LibGfx: Small improvement for DisjointRectSet::shatter
This avoids a call to clone() which would be discarded immediately. Also, avoid essentially cloning for each hammer rectangle unless there is actually a need for it.
Diffstat (limited to 'Libraries/LibGfx/DisjointRectSet.cpp')
-rw-r--r--Libraries/LibGfx/DisjointRectSet.cpp11
1 files changed, 7 insertions, 4 deletions
diff --git a/Libraries/LibGfx/DisjointRectSet.cpp b/Libraries/LibGfx/DisjointRectSet.cpp
index b68f4cab05..1ec3d95d90 100644
--- a/Libraries/LibGfx/DisjointRectSet.cpp
+++ b/Libraries/LibGfx/DisjointRectSet.cpp
@@ -173,10 +173,13 @@ DisjointRectSet DisjointRectSet::shatter(const DisjointRectSet& hammer) const
return clone();
// TODO: This could use some optimization
- auto shards = clone();
- for (auto& hammer_rect : hammer.m_rects) {
- auto shattered = shards.shatter(hammer_rect);
- shards = move(shattered);
+ DisjointRectSet shards = shatter(hammer.m_rects[0]);
+ auto rects_count = hammer.m_rects.size();
+ for (size_t i = 1; i < rects_count && !shards.is_empty(); i++) {
+ if (hammer.m_rects[i].intersects(shards.m_rects)) {
+ auto shattered = shards.shatter(hammer.m_rects[i]);
+ shards = move(shattered);
+ }
}
// Since there should be no overlaps, we don't need to call shatter()
return shards;