Skip to content

Commit cf68a1c

Browse files
some optimization not needed for now
1 parent 88750ef commit cf68a1c

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

tags3/expr.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const (
2424
exprHashDelimiter rune = ';'
2525
)
2626

27+
// var hashWildcardCharSet string = "[^" + regexp.QuoteMeta(string(exprAnd)+string(exprOr)+string(exprNot)+string(exprBracesOpen)+string(exprBracesClose)+string(exprHashDelimiter)) + "]"
28+
2729
type Expr interface {
2830
fmt.Stringer
2931
Eval(tags Tags) bool
@@ -51,6 +53,17 @@ var _ Expr = AndExpr{}
5153
var _ Expr = OrExpr{}
5254
var _ Expr = NotExpr{}
5355

56+
// func ExprSubsumes(expr1, expr2 Expr) bool {
57+
// hash1, hash2 := expr1.hash(), expr2.hash()
58+
59+
// hash1 = regexp.QuoteMeta(hash1)
60+
// hash1 = strings.ReplaceAll(hash1, regexp.QuoteMeta(exprWildcards), hashWildcardCharSet+"*")
61+
// hash1 = strings.ReplaceAll(hash1, regexp.QuoteMeta(exprWildcard), hashWildcardCharSet)
62+
63+
// regexpr := regexp.MustCompile("^" + hash1 + "$")
64+
// return regexpr.MatchString(hash2)
65+
// }
66+
5467
func (e NotExpr) tryResolve() Expr {
5568
switch expr := e.Expr.(type) {
5669
case AndExpr:
@@ -157,6 +170,18 @@ func (e AndExpr) Optimize() Expr {
157170

158171
// deduplicate nested expressions
159172
e.Exprs = sliceDeduplicateFunc(e.Exprs, func(expr Expr) string { return expr.hash() })
173+
// e.Exprs = slicest.FilterI(e.Exprs, func(i1 int, expr Expr) bool {
174+
// // is expression not contained by any other expression in its parent expression
175+
// // return !slicest.ContainsI(e.Exprs, func(i2 int, otherExpr Expr) bool { return i1 != i2 && ExprSubsumes(otherExpr, expr) })
176+
// return !slicest.ContainsI(e.Exprs, func(i2 int, otherExpr Expr) bool {
177+
// differentIndex := i1 != i2
178+
// subsumes := ExprSubsumes(otherExpr, expr)
179+
// if i1 > i2 {
180+
// return differentIndex && subsumes && !ExprSubsumes(expr, otherExpr)
181+
// }
182+
// return differentIndex && subsumes
183+
// })
184+
// })
160185

161186
// remove redundant nested or expressions
162187
e.Exprs = slicest.Filter(e.Exprs, func(expr Expr) bool {
@@ -165,6 +190,7 @@ func (e AndExpr) Optimize() Expr {
165190
return !slices.ContainsFunc(orExpr.Exprs, func(orSubExpr Expr) bool {
166191
// ... wich is contained in the and expression
167192
return slices.ContainsFunc(e.Exprs, func(andSubExpr Expr) bool {
193+
// return ExprSubsumes(andSubExpr, orSubExpr)
168194
return orSubExpr.hash() == andSubExpr.hash()
169195
})
170196
})
@@ -197,6 +223,18 @@ func (e OrExpr) Optimize() Expr {
197223

198224
// deduplicate nested expressions
199225
e.Exprs = sliceDeduplicateFunc(e.Exprs, func(expr Expr) string { return expr.hash() })
226+
// e.Exprs = slicest.FilterI(e.Exprs, func(i1 int, expr Expr) bool {
227+
// // is expression not contained by any other expression in its parent expression
228+
// // return !slicest.ContainsI(e.Exprs, func(i2 int, otherExpr Expr) bool { return i1 != i2 && ExprSubsumes(otherExpr, expr) })
229+
// return !slicest.ContainsI(e.Exprs, func(i2 int, otherExpr Expr) bool {
230+
// differentIndex := i1 != i2
231+
// subsumes := ExprSubsumes(otherExpr, expr)
232+
// if i1 > i2 {
233+
// return differentIndex && subsumes && !ExprSubsumes(expr, otherExpr)
234+
// }
235+
// return differentIndex && subsumes
236+
// })
237+
// })
200238

201239
// remove redundant nested and expressions
202240
e.Exprs = slicest.Filter(e.Exprs, func(expr Expr) bool {
@@ -205,6 +243,7 @@ func (e OrExpr) Optimize() Expr {
205243
return !slices.ContainsFunc(andExpr.Exprs, func(andSubExpr Expr) bool {
206244
// ... wich is contained in the or expression
207245
return slices.ContainsFunc(e.Exprs, func(orSubExpr Expr) bool {
246+
// return ExprSubsumes(orSubExpr, andSubExpr)
208247
return andSubExpr.hash() == orSubExpr.hash()
209248
})
210249
})

0 commit comments

Comments
 (0)