From e1ea09d014e9b1f87fbb9986ee4ee27ceb1bd617 Mon Sep 17 00:00:00 2001 From: towsey Date: Tue, 5 May 2020 12:08:55 +1000 Subject: [PATCH] Wirte new class EventExtensions.cs Issue #297 To contain methods that help with dealing with lists of mixed event types. --- .../Events/EventExtentions.cs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/AudioAnalysisTools/Events/EventExtentions.cs diff --git a/src/AudioAnalysisTools/Events/EventExtentions.cs b/src/AudioAnalysisTools/Events/EventExtentions.cs new file mode 100644 index 000000000..54517ba65 --- /dev/null +++ b/src/AudioAnalysisTools/Events/EventExtentions.cs @@ -0,0 +1,44 @@ +// +// All code in this file and all associated files are the copyright and property of the QUT Ecoacoustics Research Group (formerly MQUTeR, and formerly QUT Bioacoustics Research Group). +// + +namespace AudioAnalysisTools.Events +{ + using System; + using System.Collections.Generic; + using System.Text; + + public static class EventExtentions + { + //NOTES on SYNTAX: + //Select is a transform - fails if it encounters anything that is not of type SpectralEvent. + //var spectralEvents = events.Select(x => (SpectralEvent)x).ToList(); + + //Where is a FILTER - only returns spectral events. + //var spectralEvents = events.Where(x => x is SpectralEvent).Cast().ToList(); + //var spectralEvents = events.Where(x => x is ChirpEvent).ToList(); + //var chirpEvents = events.Cast().ToList(); + + public static (List TargetEvents, List OtherEvents) FilterForEventType(this List events) + where U : EventCommon + where T : EventCommon + { + var target = new List(events.Count); + var other = new List(events.Count); + + foreach (var @event in events) + { + if (@event is T t) + { + target.Add(t); + } + else + { + other.Add(@event); + } + } + + return (target, other); + } + } +}