-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Daxin Wang <daxinwang@harmonycloud.cn>
- Loading branch information
Daxin Wang
committed
Jun 9, 2022
1 parent
18547da
commit aca2955
Showing
5 changed files
with
226 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package compare | ||
|
||
type Int32Slice struct { | ||
oldElements []int32 | ||
oldFlags []bool | ||
newElements []int32 | ||
newFlags []bool | ||
} | ||
|
||
func NewInt32Slice(oldElements []int32, newElements []int32) Int32Slice { | ||
return Int32Slice{ | ||
oldElements: oldElements, | ||
oldFlags: make([]bool, len(oldElements)), | ||
newElements: newElements, | ||
newFlags: make([]bool, len(newElements)), | ||
} | ||
} | ||
|
||
func (c *Int32Slice) Compare() { | ||
// The elements could be repeated, so we must iterate all the elements. | ||
for i, newElement := range c.newElements { | ||
for j, oldElement := range c.oldElements { | ||
if oldElement == newElement { | ||
c.newFlags[i] = true | ||
c.oldFlags[j] = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (c *Int32Slice) GetRemovedElements() []int32 { | ||
ret := make([]int32, 0) | ||
for i, flag := range c.oldFlags { | ||
if !flag { | ||
ret = append(ret, c.oldElements[i]) | ||
} | ||
} | ||
return ret | ||
} | ||
|
||
func (c *Int32Slice) GetAddedElements() []int32 { | ||
ret := make([]int32, 0) | ||
for i, flag := range c.newFlags { | ||
if !flag { | ||
ret = append(ret, c.newElements[i]) | ||
} | ||
} | ||
return ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package compare | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestInt32Slice(t *testing.T) { | ||
oldElements := []int32{0, 0, 1, 2, 3} | ||
newElements := []int32{1, 3, 4, 6} | ||
compareSlice := NewInt32Slice(oldElements, newElements) | ||
compareSlice.Compare() | ||
removedElements := compareSlice.GetRemovedElements() | ||
assert.ElementsMatch(t, removedElements, []int32{0, 0, 2}) | ||
addedElements := compareSlice.GetAddedElements() | ||
assert.ElementsMatch(t, addedElements, []int32{4, 6}) | ||
} | ||
|
||
func TestStringSlice(t *testing.T) { | ||
oldElements := []string{"a", "b", "c"} | ||
newElements := []string{"d", "e", "f"} | ||
compareSlice := NewStringSlice(oldElements, newElements) | ||
compareSlice.Compare() | ||
removedElements := compareSlice.GetRemovedElements() | ||
assert.ElementsMatch(t, removedElements, []string{"a", "b", "c"}) | ||
addedElements := compareSlice.GetAddedElements() | ||
assert.ElementsMatch(t, addedElements, []string{"d", "e", "f"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package compare | ||
|
||
type StringSlice struct { | ||
oldElements []string | ||
oldFlags []bool | ||
newElements []string | ||
newFlags []bool | ||
} | ||
|
||
func NewStringSlice(oldElements []string, newElements []string) StringSlice { | ||
return StringSlice{ | ||
oldElements: oldElements, | ||
oldFlags: make([]bool, len(oldElements)), | ||
newElements: newElements, | ||
newFlags: make([]bool, len(newElements)), | ||
} | ||
} | ||
|
||
func (c *StringSlice) Compare() { | ||
// The elements could be repeated, so we must iterate all the elements. | ||
for i, newElement := range c.newElements { | ||
for j, oldElement := range c.oldElements { | ||
if oldElement == newElement { | ||
c.newFlags[i] = true | ||
c.oldFlags[j] = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (c *StringSlice) GetRemovedElements() []string { | ||
ret := make([]string, 0) | ||
for i, flag := range c.oldFlags { | ||
if !flag { | ||
ret = append(ret, c.oldElements[i]) | ||
} | ||
} | ||
return ret | ||
} | ||
|
||
func (c *StringSlice) GetAddedElements() []string { | ||
ret := make([]string, 0) | ||
for i, flag := range c.newFlags { | ||
if !flag { | ||
ret = append(ret, c.newElements[i]) | ||
} | ||
} | ||
return ret | ||
} |