Skip to content

Commit

Permalink
feat(spdx-utils): Deduplicate equal SpdxCompoundExpression operands
Browse files Browse the repository at this point in the history
Calculating whether expressions are equal might be a bit computation
intensive for huge expressions, but the idea with this change is that
expression should not grow huge to begin with.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Oct 31, 2024
1 parent 42429f3 commit 041eb86
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions utils/spdx/src/main/kotlin/SpdxExpression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ sealed class SpdxExpression {
* Concatenate [this][SpdxExpression] and [other] using [SpdxOperator.AND].
*/
infix fun and(other: SpdxExpression) =
takeIf { this == other } ?: SpdxCompoundExpression(SpdxOperator.AND, listOf(this, other))
takeIf { this == other } ?: SpdxCompoundExpression(SpdxOperator.AND, setOf(this, other))

/**
* Concatenate [this][SpdxExpression] and [other] using [SpdxOperator.OR].
*/
infix fun or(other: SpdxExpression) =
takeIf { this == other } ?: SpdxCompoundExpression(SpdxOperator.OR, listOf(this, other))
takeIf { this == other } ?: SpdxCompoundExpression(SpdxOperator.OR, setOf(this, other))
}

/**
Expand All @@ -224,14 +224,14 @@ class SpdxCompoundExpression(
* Create a compound expression with the provided [operator] and the [left] and [right] child expressions.
*/
constructor(left: SpdxExpression, operator: SpdxOperator, right: SpdxExpression) :
this(operator, listOf(left, right))
this(operator, setOf(left, right))

/**
* Create a compound expression with the provided [operator], the [first] and [second] child expressions, and an
* arbitrary number of [other] child expressions.
*/
constructor(operator: SpdxOperator, first: SpdxExpression, second: SpdxExpression, vararg other: SpdxExpression) :
this(operator, listOf(first, second, *other))
this(operator, setOf(first, second, *other))

init {
require(children.size > 1) {
Expand Down

0 comments on commit 041eb86

Please sign in to comment.