From 21d72b2f67fb065d9071907c5c3307434aad3795 Mon Sep 17 00:00:00 2001 From: JP Appel Date: Thu, 19 Jun 2025 01:22:19 -0400 Subject: Add multiple clause and tree level optimizations Implement optimizations that can be called in parallel or serial. Optimizations occur mostly in place and result in a logically equivalent tree when used correctly. Optimizations ============= * Sort - sort all statements within a clause tree * Simplify - apply negation rules to all statements * Flatten - merge child clauses with parents * Compact - merge equivalent statements * Tidy^ - remove zero statements * Contradictions - zero contradicting statements and clauses * StrictEquality - zero fuzzy statements when exact statements are within clause * Tighten - combine multiple fuzzy statements to the least (AND) or most (OR) restrictive bounds ^: when used incorrectly can turn an invalid clause tree into a valid one --- pkg/util/util.go | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'pkg/util/util.go') diff --git a/pkg/util/util.go b/pkg/util/util.go index a1dc37c..a525bca 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -1,6 +1,10 @@ package util -import "time" +import ( + "iter" + "slices" + "time" +) func ParseDateTime(s string) (time.Time, error) { dateFormats := []string{ @@ -33,3 +37,41 @@ func ParseDateTime(s string) (time.Time, error) { return time.Time{}, err } + +// Create a copy of a slice with all values that satisfy cond +func Fitler[E any](s []E, cond func(e E) bool) []E { + filtered := make([]E, 0, len(s)) + for _, e := range s { + if cond(e) { + filtered = append(filtered, e) + } + } + + return filtered +} + +// Create an iterator of index and element for all values in a slice which satisfy cond. +func FilterIter[E any](s []E, cond func(e E) bool) iter.Seq2[int, E] { + return func(yield func(int, E) bool) { + for i, e := range s { + if cond(e) { + if !yield(i, e) { + return + } + } + } + } +} + +// FilterIter but backwards +func BackwardsFilterIter[E any](s []E, cond func(e E) bool) iter.Seq2[int, E] { + return func(yield func(int, E) bool) { + for i := len(s) - 1; i >= 0; i-- { + if cond(s[i]) { + if !yield(i, s[i]) { + return + } + } + } + } +} -- cgit v1.2.3