This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package readsb
|
||||
|
||||
import (
|
||||
"iter"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -9,19 +11,26 @@ import (
|
||||
// an aircraft is kept only if every filter passes (logical AND).
|
||||
type AircraftFilter func(*Aircraft) bool
|
||||
|
||||
// Filter returns the aircraft that satisfy all of the supplied filters. With no
|
||||
// filters it returns every aircraft. The receiver report is never mutated.
|
||||
// All yields each aircraft that satisfies every supplied filter, lazily and in
|
||||
// order. With no filters it yields every aircraft. Ranging may break early; the
|
||||
// receiver report is never mutated.
|
||||
func (r *AircraftReport) All(filters ...AircraftFilter) iter.Seq[Aircraft] {
|
||||
return func(yield func(Aircraft) bool) {
|
||||
for i := range r.Aircraft {
|
||||
if keep(&r.Aircraft[i], filters) && !yield(r.Aircraft[i]) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Filter is the eager form of All: it collects the matching aircraft into a
|
||||
// slice. With no filters it returns the report's own slice without copying.
|
||||
func (r *AircraftReport) Filter(filters ...AircraftFilter) []Aircraft {
|
||||
if len(filters) == 0 {
|
||||
return r.Aircraft
|
||||
}
|
||||
out := make([]Aircraft, 0, len(r.Aircraft))
|
||||
for i := range r.Aircraft {
|
||||
if keep(&r.Aircraft[i], filters) {
|
||||
out = append(out, r.Aircraft[i])
|
||||
}
|
||||
}
|
||||
return out
|
||||
return slices.Collect(r.All(filters...))
|
||||
}
|
||||
|
||||
func keep(a *Aircraft, filters []AircraftFilter) bool {
|
||||
|
||||
Reference in New Issue
Block a user