using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace BotSharp.Algorithm.Bayesian
{
///
/// Class TrainingSet. This class cannot be inherited.
///
public sealed class TrainingSet : ITrainingSet
{
///
/// The data sets
///
private readonly ConcurrentDictionary _dataSets = new ConcurrentDictionary();
///
/// Initializes a new instance of the class.
///
public TrainingSet()
{
}
///
/// Initializes a new instance of the class.
///
/// The data sets.
public TrainingSet(IEnumerable dataSets)
{
Add(dataSets);
}
///
/// Initializes a new instance of the class.
///
/// The data set.
/// The additional data sets.
public TrainingSet(IDataSet dataSet, params IDataSet[] additionalDataSets)
{
Add(dataSet, additionalDataSets);
}
///
/// Gets the with the specified class.
///
/// The class.
/// IDataSet<IClass, IToken>.
/// No data set was registered for the given class;class
public IDataSet this[IClass @class]
{
get
{
IDataSet set;
if (_dataSets.TryGetValue(@class, out set)) return set;
throw new ArgumentException("No data set was registered for the given class", "class");
}
}
///
/// Adds the specified data set.
///
/// The data set.
/// The additional data sets.
/// dataSet
/// A data set for a given class was already registered.
public void Add(IDataSet dataSet, params IDataSet[] additionalDataSets)
{
if (ReferenceEquals(dataSet, null)) throw new ArgumentNullException("dataSet");
try
{
AddInternal(dataSet);
}
catch (ArgumentException e)
{
throw new ArgumentException("A data set for a given class was already registered.", e);
}
// may throw, that's anticipated
Add(additionalDataSets);
}
///
/// Adds the specified data sets.
///
/// The data sets.
/// dataSets
/// A data set for a given class was already registered.
public void Add(IEnumerable dataSets)
{
if (ReferenceEquals(dataSets, null)) throw new ArgumentNullException("dataSets");
try
{
foreach (var dataSet in dataSets)
{
AddInternal(dataSet);
}
}
catch (ArgumentException e)
{
throw new ArgumentException("A data set for a given class was already registered.", e);
}
}
///
/// Adds the data set internally.
///
/// The data set.
/// Data set for the given class was already registered.
private void AddInternal(IDataSet dataSet)
{
if (!_dataSets.TryAdd(dataSet.Class, dataSet))
{
throw new ArgumentException("Data set for the given class was already registered.");
}
}
///
/// Returns an enumerator that iterates through the collection.
///
/// A that can be used to iterate through the collection.
public IEnumerator GetEnumerator()
{
return _dataSets.Select(dataSet => dataSet.Value).GetEnumerator();
}
///
/// Returns an enumerator that iterates through a collection.
///
/// An object that can be used to iterate through the collection.
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}