func (rs Ranges) Compare(right Ranges) int {
	x, y, result := rs.equiv(right)
	if result {
		return 0
	}
	for _, a := range x {
		matched := false
		for _, b := range y {
			if a.Begin >= b.Begin && a.End <= b.End {
				matched = true
				break
			}
		}
		if !matched {
			return 1
		}
	}
	return -1
}
Compare assumes that both Ranges are already in sort-order.
Returns 0 if rs and right are equivalent, -1 if rs is a subset of right, or else 1
package mesos
import (
	"sort"
)
type Ranges []Value_Range
func NewRanges(ns ...uint64) Ranges {
	xs := append(uint64s{}, ns...)
	sort.Sort(xs)
	rs := make(Ranges, len(xs))
	for i := range xs {
		rs[i].Begin, rs[i].End = xs[i], xs[i]
	}
	return rs.Squash()
}
func NewPortRanges(o *Offer) Ranges {
	if o == nil {
		return Ranges{}
	}
	var (
		r     Resource
		found bool
	)
	for i := range o.Resources {
		if o.Resources[i].GetName() == "ports" {
			r = o.Resources[i]
			found = true
			break
		}
	}
	if !found {
		return Ranges{}
	}
	offered := r.GetRanges().GetRange()
	rs := make(Ranges, len(offered))
	for i, r := range offered {
		if lo, hi := r.GetBegin(), r.GetEnd(); lo <= hi {
			rs[i].Begin, rs[i].End = lo, hi
		} else {
			rs[i].Begin, rs[i].End = hi, lo
		}
	}
	return rs.Sort().Squash()
}
func (rs Ranges) Len() int      { return len(rs) }
func (rs Ranges) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] }
func (rs Ranges) Less(i, j int) bool {
	return rs[i].Begin < rs[j].Begin || (rs[i].Begin == rs[j].Begin && rs[i].End < rs[j].End)
}
func (rs Ranges) Size() uint64 {
	var sz uint64
	for i := range rs {
		sz += 1 + (rs[i].End - rs[i].Begin)
	}
	return sz
}
func (rs Ranges) Sort() Ranges {
	sort.Sort(rs)
	return rs
}
func (rs Ranges) Squash() Ranges {
	if len(rs) < 2 {
		return rs
	}
	squashed := Ranges{rs[0]}
	for i := 1; i < len(rs); i++ {
		switch max := squashed[len(squashed)-1].End; {
		case 1+max < rs[i].Begin:  
			squashed = append(squashed, rs[i])
		case max <= rs[i].End:  
			squashed[len(squashed)-1].End = rs[i].End
		}
	}
	return squashed
}
func (rs Ranges) Search(n uint64) int {
	for lo, hi := 0, len(rs)-1; lo <= hi; {
		switch m := lo + (hi-lo)/2; {
		case n < rs[m].Begin:
			hi = m - 1
		case n > rs[m].End:
			lo = m + 1
		default:
			return m
		}
	}
	return -1
}
func (rs Ranges) Partition(n uint64) (Ranges, bool) {
	i := rs.Search(n)
	if i < 0 {
		return rs, false
	}
	pn := make(Ranges, 0, len(rs)+1)
	switch pn = append(pn, rs[:i]...); {
	case rs[i].Begin == rs[i].End:  
	case rs[i].Begin == n:  
		pn = append(pn, Value_Range{rs[i].Begin + 1, rs[i].End})
	case rs[i].End == n:  
		pn = append(pn, Value_Range{rs[i].Begin, rs[i].End - 1})
	default:  
		pn = append(pn, Value_Range{rs[i].Begin, n - 1}, Value_Range{n + 1, rs[i].End})
	}
	return append(pn, rs[i+1:]...), true
}
func (rs Ranges) Remove(removal Value_Range) Ranges {
	ranges := make([]Value_Range, 0, len(rs))
	for _, r := range rs {
		if r.Begin >= removal.Begin && r.End <= removal.End {
			continue
		}
		if r.Begin < removal.Begin && r.End > removal.End {
			ranges = append(ranges,
				Value_Range{r.Begin, removal.Begin - 1},
				Value_Range{removal.End + 1, r.End},
			)
			continue
		}
		if r.End < removal.Begin || r.Begin > removal.End {
			ranges = append(ranges, r)
			continue
		}
		if r.End > removal.End {
			ranges = append(ranges, Value_Range{removal.End + 1, r.End})
		} else {
			if r.Begin >= removal.Begin {
				panic("r.Begin >= removal.Begin")
			}
			ranges = append(ranges, Value_Range{r.Begin, removal.Begin - 1})
		}
	}
	return Ranges(ranges).Squash()
}