Skip to content

Instantly share code, notes, and snippets.

@veikkoeeva
Last active May 10, 2018 18:06
Show Gist options
  • Save veikkoeeva/50c8f38ec46b0a3ce70467d16af00dc1 to your computer and use it in GitHub Desktop.
Save veikkoeeva/50c8f38ec46b0a3ce70467d16af00dc1 to your computer and use it in GitHub Desktop.
RecordSet
/// <summary>
/// A set of records.
/// </summary>
/// <typeparam name="TKey">The key type of records.</typeparam>
/// <typeparam name="TValue">The value type of records.</typeparam>
[Serializable]
[DebuggerTypeProxy(typeof(CollectionDebugView<>))]
[DebuggerDisplay("Count = {Count}")]
public class RecordSet<TKey, TValue>: ICollection<KeyValuePair<TKey, TValue>>
{
/// <summary>
/// The set of stored records.
/// </summary>
private List<KeyValuePair<TKey, TValue>> Records { get; }
/// <summary>
/// If this collection is readonly or not.
/// </summary>
public bool IsReadOnly { get; } = false;
/// <summary>
/// A constructor for a set of records.
/// </summary>
/// <param name="records">The raw records.</param>
/// <exception cref="ArgumentNullException"/>.
public RecordSet(IEnumerable<KeyValuePair<TKey, TValue>> records)
{
Records = new List<KeyValuePair<TKey, TValue>>(records ?? throw new ArgumentNullException(nameof(records)));
}
/// <summary>
/// Gets the number of elements contained in the <see cref="RecordSet{TKey, TValue}"/>.
/// </summary>
public int Count => Records.Count;
/// <summary>
/// Adds an object to the end of <see cref="RecordSet{TKey, TValue}"/>.
/// </summary>
/// <param name="item">The object to be added to the end of the <see cref="RecordSet{TKey, TValue}"/>. The value can be <c>null</c> for reference types.</param>
public void Add(KeyValuePair<TKey, TValue> item)
{
Records.Add(item);
}
/// <summary>
/// Removes all elements from <see cref="RecordSet{TKey, TValue}"/>.
/// </summary>
public void Clear()
{
Records.Clear();
}
/// <summary>
/// Determines whether element is in the <see cref="RecordSet{TKey, TValue}"/>.
/// </summary>
/// <param name="item">The object to locate in the <see cref="RecordSet{TKey, TValue}"/>. The value can be <c>null</c> for reference types.</param>
/// <returns><em>True</em> if the item was contained. <em>False</em> otherwise.</returns>
public bool Contains(KeyValuePair<TKey, TValue> item)
{
return Records.Contains(item);
}
/// <summary>
/// Copies the entire <see cref="RecordSet{TKey, TValue}"/> to a compatible one-dimensional recordset,
/// starting at the specified index of the target array.
/// </summary>
/// <param name="array">The one-dimensional <see cref="Array"/> that is the destination of the elements copied from <see cref="RecordSet{TKey, TValue}"/>.
/// The array must have zero-based indexing.</param>
/// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="ArgumentOutOfRangeException"/>
/// <exception cref="ArgumentException"/>
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
//The exceptions come from the underlying type check.
Records.CopyTo(array, arrayIndex);
}
/// <summary>
/// Removes the first occurence of a specific item from the <see cref="RecordSet{TKey, TValue}"/>.
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Remove(KeyValuePair<TKey, TValue> item)
{
return Records.Remove(item);
}
/// <summary>
/// Returns an enumerator that iterates through the <see cref="RecordSet{TKey, TValue}"/>.
/// </summary>
/// <returns></returns>
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return Records.GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through the <see cref="RecordSet{TKey, TValue}"/>.
/// </summary>
/// <returns></returns>
IEnumerator IEnumerable.GetEnumerator()
{
return Records.GetEnumerator();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment