Skip to content

Commit

Permalink
Do not assume Compare returns -1 or +1 if not zero
Browse files Browse the repository at this point in the history
  • Loading branch information
b97tsk committed Jan 22, 2024
1 parent e2bee10 commit 953f38d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
14 changes: 8 additions & 6 deletions difference.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ func difference[E Elem[E]](x, y, out Set[E]) Set[E] {
continue
}

switch lo := x[0].Low; lo.Compare(r.Low) {
case -1:
lo := x[0].Low

switch c := lo.Compare(r.Low); {
case c < 0:
if !inv {
z = append(z, Range(lo, r.Low))
}
case +1:
case c > 0:
if inv {
z = append(z, Range(r.Low, lo))
}
Expand All @@ -79,12 +81,12 @@ func difference[E Elem[E]](x, y, out Set[E]) Set[E] {
hi := x[j-1].High
x = x[j:]

switch hi.Compare(r.High) {
case -1:
switch c := hi.Compare(r.High); {
case c < 0:
if inv {
z = append(z, Range(hi, r.High))
}
case +1:
case c > 0:
r.Low, r.High = r.High, hi
x, y = y, x
inv = !inv
Expand Down
14 changes: 8 additions & 6 deletions symdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ func symmetricDifference[E Elem[E]](x, y, out Set[E]) Set[E] {
continue
}

switch lo := x[0].Low; lo.Compare(r.Low) {
case -1:
lo := x[0].Low

switch c := lo.Compare(r.Low); {
case c < 0:
z = appendInterval(z, Range(lo, r.Low))
case +1:
case c > 0:
z = appendInterval(z, Range(r.Low, lo))
}

Expand All @@ -60,10 +62,10 @@ func symmetricDifference[E Elem[E]](x, y, out Set[E]) Set[E] {
hi := x[j-1].High
x = x[j:]

switch hi.Compare(r.High) {
case -1:
switch c := hi.Compare(r.High); {
case c < 0:
z = append(z, Range(hi, r.High))
case +1:
case c > 0:
r.Low, r.High = r.High, hi
x, y = y, x

Expand Down

0 comments on commit 953f38d

Please sign in to comment.