-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: kaizhe <derek0405@gmail.com>
- Loading branch information
Showing
9 changed files
with
219 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
) | ||
|
||
type PortRangeList []*PortRange | ||
|
||
type PortRange struct { | ||
Min int32 | ||
Max int32 | ||
} | ||
|
||
func NewPortRange(min, max int32) *PortRange { | ||
return &PortRange{ | ||
Min: min, | ||
Max: max, | ||
} | ||
} | ||
|
||
func (pl PortRangeList) Consolidate() PortRangeList { | ||
newPortRangeList := PortRangeList{} | ||
|
||
max := pl.GetMax() | ||
|
||
min := pl.GetMin() | ||
|
||
if min == -1 { | ||
return newPortRangeList | ||
} | ||
|
||
tmpPl := make(PortRangeList, max+1) | ||
|
||
for _, pr := range pl { | ||
tmpPl[pr.Min] = pr | ||
} | ||
|
||
pr := tmpPl[min] | ||
i := min | ||
|
||
for ; i <= max; i++ { | ||
if tmpPl[i] != nil { | ||
pr.Max = tmpPl[i].Max | ||
} else { | ||
// there is a break | ||
newPortRangeList = append(newPortRangeList, pr) | ||
|
||
// look for next port range | ||
for { | ||
i++ | ||
if i > max { | ||
break | ||
} | ||
|
||
if tmpPl[i] != nil { | ||
pr = tmpPl[i] | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
newPortRangeList = append(newPortRangeList, pr) | ||
|
||
sort.Sort(newPortRangeList) | ||
|
||
return newPortRangeList | ||
} | ||
|
||
func (pl PortRangeList) GetMin() int32 { | ||
min := int32(-1) | ||
|
||
for _, pr := range pl { | ||
if pr != nil { | ||
if min == int32(-1) { | ||
min = pr.Min | ||
} | ||
|
||
if pr.Min < min { | ||
min = pr.Min | ||
} | ||
} | ||
} | ||
|
||
return min | ||
} | ||
|
||
func (pl PortRangeList) GetMax() int32 { | ||
max := int32(-1) | ||
|
||
for _, pr := range pl { | ||
if pr != nil { | ||
if pr.Max > max { | ||
max = pr.Max | ||
} | ||
} | ||
} | ||
|
||
return max | ||
} | ||
|
||
func (pl PortRangeList) String() string { | ||
ret := "[" | ||
|
||
for idx, pr := range pl { | ||
ret += fmt.Sprintf("{%d %d}", pr.Min, pr.Max) | ||
|
||
if idx < len(pl)-1 { | ||
ret += ", " | ||
} | ||
} | ||
|
||
ret += "]" | ||
|
||
return ret | ||
} | ||
|
||
func (pl PortRangeList) Less(i, j int) bool { return pl[i].Min < pl[j].Min } | ||
|
||
func (pl PortRangeList) Len() int { return len(pl) } | ||
|
||
func (pl PortRangeList) Swap(i, j int) { pl[j], pl[i] = pl[i], pl[j] } |
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,39 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
var ( | ||
prList = PortRangeList{ | ||
&PortRange{Min: 1, Max: 1}, | ||
&PortRange{Min: 2, Max: 2}, | ||
&PortRange{Min: 3, Max: 3}, | ||
&PortRange{Min: 5, Max: 5}, | ||
&PortRange{Min: 6, Max: 6}, | ||
&PortRange{Min: 7, Max: 7}, | ||
&PortRange{Min: 50, Max: 50}, | ||
&PortRange{Min: 99, Max: 99}, | ||
} | ||
|
||
expectedPrList = PortRangeList{ | ||
&PortRange{Min: 1, Max: 3}, | ||
&PortRange{Min: 5, Max: 7}, | ||
&PortRange{Min: 50, Max: 50}, | ||
&PortRange{Min: 99, Max: 99}, | ||
} | ||
) | ||
|
||
func TestPortRange(t *testing.T) { | ||
newPrList := prList.Consolidate() | ||
|
||
if len(newPrList) != 4 { | ||
t.Errorf("length is not 3: %+v", newPrList) | ||
} | ||
|
||
for i := range expectedPrList { | ||
if newPrList[i].Min != expectedPrList[i].Min || newPrList[i].Max != expectedPrList[i].Max { | ||
t.Errorf("expected port range: %v; actual port range: %v", *expectedPrList[i], *newPrList[i]) | ||
} | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -1 +1 @@ | ||
1.7.0 | ||
1.8.0 |