Introduce 'Count' type to avoid abusing 'Offset'

This commit is contained in:
Bryan Boreham authored and Matthias Radestock committed 2016-03-19 11:47:34 +00:00
1 parent 7b6364696c
commit f11314ed3e
9 files changed
+45 -38

No files matched your search

+7 -1
View File
@@ -10,6 +10,7 @@ import (
// Using 32-bit integer to represent IPv4 address
type Address uint32
type Offset uint32
type Count uint32
type Range struct {
Start, End Address // [Start, End); Start <= End
@@ -18,7 +19,7 @@ type Range struct {
func NewRange(start Address, size Offset) Range {
return Range{Start: start, End: Add(start, size)}
}
func (r Range) Size() Offset { return Subtract(r.End, r.Start) }
func (r Range) Size() Count { return Length(r.End, r.Start) }
func (r Range) String() string { return fmt.Sprintf("%s-%s", r.Start, r.End-1) }
func (r Range) Overlaps(or Range) bool { return !(r.Start >= or.End || r.End <= or.Start) }
func (r Range) Contains(addr Address) bool { return addr >= r.Start && addr < r.End }
@@ -116,6 +117,11 @@ func Subtract(a, b Address) Offset {
return Offset(a - b)
}
func Length(a, b Address) Count {
common.Assert(a >= b)
return Count(a - b)
}
func Min(a, b Offset) Offset {
if a > b {
return b