From f3b49e6543c4dd6dfe456d2d977a364fa0e28205 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 9 Mar 2023 22:57:01 +0900 Subject: [PATCH] internal/restorable: reduce more duplicated regions --- internal/restorable/image.go | 10 ++++++++-- internal/restorable/rect_test.go | 2 -- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/internal/restorable/image.go b/internal/restorable/image.go index 1cccb8309..557e3c9bf 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -737,13 +737,19 @@ func regionToRectangle(region graphicsdriver.Region) image.Rectangle { } // removeDuplicatedRegions removes duplicated regions and returns a shrunk slice. -// If a region covers preceding regions, the covered regions are removed. +// If a region covers other regions, the covered regions are removed. func removeDuplicatedRegions(regions []image.Rectangle) []image.Rectangle { for i, r := range regions { if r.Empty() { continue } - for j, rr := range regions[:i] { + for j, rr := range regions { + if i == j { + continue + } + if rr.Empty() { + continue + } if rr.In(r) { regions[j] = image.Rectangle{} } diff --git a/internal/restorable/rect_test.go b/internal/restorable/rect_test.go index 494f59573..143fbcc90 100644 --- a/internal/restorable/rect_test.go +++ b/internal/restorable/rect_test.go @@ -59,7 +59,6 @@ func TestRemoveDuplicatedRegions(t *testing.T) { }, Out: []image.Rectangle{ image.Rect(0, 0, 2, 2), - image.Rect(0, 0, 1, 1), }, }, { @@ -104,7 +103,6 @@ func TestRemoveDuplicatedRegions(t *testing.T) { }, Out: []image.Rectangle{ image.Rect(0, 0, 4, 4), - image.Rect(1, 1, 2, 2), }, }, {