Skip to content

Commit

Permalink
AddressSet.subtract: pay off technical debt
Browse files Browse the repository at this point in the history
No longer exponential time!
  • Loading branch information
terpstra committed May 12, 2020
1 parent 983e08c commit 88cf2b1
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/main/scala/diplomacy/Parameters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ case class AddressSet(base: BigInt, mask: BigInt) extends Ordered[AddressSet]
}

def subtract(x: AddressSet): Seq[AddressSet] = {
if (!overlaps(x)) {
Seq(this)
} else {
val new_inflex = ~x.mask & mask
// !!! this fractures too much; find a better algorithm
val fracture = AddressSet.enumerateMask(new_inflex).flatMap(m => intersect(AddressSet(m, ~new_inflex)))
fracture.filter(!_.overlaps(x))
intersect(x) match {
case None => Seq(this)
case Some(remove) => AddressSet.enumerateBits(mask & ~remove.mask).map { bit =>
val nmask = (mask & (bit-1)) | remove.mask
val nbase = (remove.base ^ bit) & ~nmask
AddressSet(nbase, nmask)
}
}
}

Expand Down

0 comments on commit 88cf2b1

Please sign in to comment.