-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #290 from Belphemur/fix_performance
Fix performance
- Loading branch information
Showing
5 changed files
with
193 additions
and
23 deletions.
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
SoundSwitch/Framework/Configuration/Device/DeviceInfoCollection.cs
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,150 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using NAudio.CoreAudioApi; | ||
|
||
namespace SoundSwitch.Framework.Configuration.Device | ||
{ | ||
public class DeviceInfoCollection : ICollection<DeviceInfo> | ||
{ | ||
private readonly Dictionary<string, DeviceInfo> _deviceByName = new Dictionary<string, DeviceInfo>(); | ||
private readonly Dictionary<string, DeviceInfo> _deviceById = new Dictionary<string, DeviceInfo>(); | ||
|
||
private readonly HashSet<DeviceInfo> _original; | ||
|
||
public DeviceInfoCollection(HashSet<DeviceInfo> devices) | ||
{ | ||
_original = devices; | ||
foreach (var deviceInfo in devices) | ||
{ | ||
AddItem(deviceInfo); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Intersect with MMDevices | ||
/// </summary> | ||
/// <param name="devices"></param> | ||
/// <returns></returns> | ||
public IEnumerable<MMDevice> IntersectWith(IEnumerable<MMDevice> devices) | ||
{ | ||
var devicesResult = new Dictionary<string, MMDevice>(); | ||
foreach (var mmDevice in devices) | ||
{ | ||
if (devicesResult.ContainsKey(mmDevice.ID)) | ||
continue; | ||
|
||
if (!_deviceById.ContainsKey(mmDevice.ID) && !_deviceByName.ContainsKey(mmDevice.FriendlyName)) | ||
continue; | ||
|
||
devicesResult.Add(mmDevice.ID, mmDevice); | ||
|
||
} | ||
|
||
return devicesResult.Values; | ||
|
||
} | ||
|
||
public IEnumerator<DeviceInfo> GetEnumerator() | ||
{ | ||
return _deviceById.Values.GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
|
||
public void Add(DeviceInfo item) | ||
{ | ||
AddItem(item, true); | ||
} | ||
|
||
/// <summary> | ||
/// Add Item and update or not the original | ||
/// </summary> | ||
/// <param name="item"></param> | ||
/// <param name="updateOriginal"></param> | ||
private void AddItem(DeviceInfo item, bool updateOriginal = false) | ||
{ | ||
if (item == null) | ||
{ | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
_deviceById.Add(item.Id, item); | ||
} | ||
catch (ArgumentException) | ||
{ | ||
} | ||
|
||
try | ||
{ | ||
_deviceByName.Add(item.Name, item); | ||
} | ||
catch (ArgumentException) | ||
{ | ||
} | ||
|
||
if (updateOriginal) | ||
{ | ||
_original.Add(item); | ||
} | ||
} | ||
|
||
public void Clear() | ||
{ | ||
_deviceById.Clear(); | ||
_deviceByName.Clear(); | ||
_original.Clear(); | ||
} | ||
|
||
public bool Contains(DeviceInfo item) | ||
{ | ||
return item != null && (_deviceById.ContainsKey(item.Id) || _deviceByName.ContainsKey(item.Name)); | ||
} | ||
|
||
public void CopyTo(DeviceInfo[] array, int arrayIndex) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public bool Remove(DeviceInfo item) | ||
{ | ||
if (item == null) | ||
{ | ||
return false; | ||
} | ||
|
||
var result = _original.RemoveWhere((info => info?.Id == item.Id || info?.Name == item.Name)) > 0; | ||
|
||
try | ||
{ | ||
result &= _deviceById.Remove(item.Id); | ||
} | ||
catch (ArgumentException) | ||
{ | ||
|
||
} | ||
|
||
try | ||
{ | ||
result &= _deviceByName.Remove(item.Name); | ||
} | ||
catch (ArgumentException) | ||
{ | ||
|
||
} | ||
|
||
return result; | ||
} | ||
|
||
public int Count => _deviceById.Count; | ||
|
||
public bool IsReadOnly => false; | ||
} | ||
} |
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
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
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
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