-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #297 To contain methods that help with dealing with lists of mixed event types.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// <copyright file="EventExtentions.cs" company="QutEcoacoustics"> | ||
// 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). | ||
// </copyright> | ||
|
||
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<SpectralEvent>().ToList(); | ||
//var spectralEvents = events.Where(x => x is ChirpEvent).ToList(); | ||
//var chirpEvents = events.Cast<ChirpEvent>().ToList(); | ||
|
||
public static (List<T> TargetEvents, List<U> OtherEvents) FilterForEventType<T, U>(this List<U> events) | ||
where U : EventCommon | ||
where T : EventCommon | ||
{ | ||
var target = new List<T>(events.Count); | ||
var other = new List<U>(events.Count); | ||
|
||
foreach (var @event in events) | ||
{ | ||
if (@event is T t) | ||
{ | ||
target.Add(t); | ||
} | ||
else | ||
{ | ||
other.Add(@event); | ||
} | ||
} | ||
|
||
return (target, other); | ||
} | ||
} | ||
} |