/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect;
using QuantConnect.Python;
namespace Common.Util
{
///
/// Provides a generic implementation of ExtendedDictionary with specific dictionary type
///
[PandasNonExpandable]
public class BaseExtendedDictionary : ExtendedDictionary, IDictionary
where TDictionary : IDictionary, new()
{
///
/// The dictionary instance
///
protected TDictionary Dictionary { get; }
///
/// Initializes a new instance of the BaseExtendedDictionary class that is empty
///
public BaseExtendedDictionary()
{
Dictionary = new TDictionary();
}
///
/// Initializes a new instance of the BaseExtendedDictionary class that contains elements copied from the specified dictionary
///
/// The dictionary whose elements are copied to the new dictionary
public BaseExtendedDictionary(TDictionary dictionary)
{
Dictionary = dictionary;
}
///
/// Initializes a new instance of the BaseExtendedDictionary class
/// using the specified as a data source
///
/// The data source for this dictionary
/// Delegate used to select a key from the value
public BaseExtendedDictionary(IEnumerable data, Func keySelector)
: this()
{
foreach (var datum in data)
{
Dictionary[keySelector(datum)] = datum;
}
}
///
/// Gets the number of elements contained in the dictionary
///
public override int Count => Dictionary.Count;
///
/// Gets a value indicating whether the dictionary is read-only
///
public override bool IsReadOnly => Dictionary.IsReadOnly;
///
/// Gets the value associated with the specified key
///
/// The key whose value to get
/// When this method returns, the value associated with the specified key
/// true if the key was found; otherwise, false
public override bool TryGetValue(TKey key, out TValue value)
{
return Dictionary.TryGetValue(key, out value);
}
///
/// Gets all the items in the dictionary
///
/// All the items in the dictionary
public override IEnumerable> GetItems()
{
return Dictionary;
}
///
/// Gets a collection containing the keys in the dictionary
///
protected override IEnumerable GetKeys => Dictionary.Keys;
///
/// Gets a collection containing the values in the dictionary
///
protected override IEnumerable GetValues => Dictionary.Values;
///
/// Gets a collection containing the keys of the dictionary
///
public virtual ICollection Keys => Dictionary.Keys;
///
/// Gets a collection containing the values of the dictionary
///
public virtual ICollection Values => Dictionary.Values;
///
/// Gets or sets the value associated with the specified key
///
/// The key of the value to get or set
/// The value associated with the specified key
public override TValue this[TKey key]
{
get => Dictionary[key];
set => Dictionary[key] = value;
}
///
/// Removes all items from the dictionary
///
public override void Clear()
{
Dictionary.Clear();
}
///
/// Removes the value with the specified key
///
/// The key of the element to remove
/// true if the element was successfully found and removed; otherwise, false
public override bool Remove(TKey key)
{
return Dictionary.Remove(key);
}
///
/// Adds an element with the provided key and value to the dictionary
///
/// The key of the element to add
/// The value of the element to add
public virtual void Add(TKey key, TValue value)
{
Dictionary.Add(key, value);
}
///
/// Adds an element with the provided key-value pair to the dictionary
///
/// The key-value pair to add
public virtual void Add(KeyValuePair item)
{
Dictionary.Add(item);
}
///
/// Determines whether the dictionary contains the specified key
///
/// The key to locate in the dictionary
/// true if the dictionary contains an element with the specified key; otherwise, false
public override bool ContainsKey(TKey key)
{
return Dictionary.ContainsKey(key);
}
///
/// Determines whether the dictionary contains a specific key-value pair
///
/// The key-value pair to locate
/// true if the key-value pair was found; otherwise, false
public virtual bool Contains(KeyValuePair item)
{
return Dictionary.Contains(item);
}
///
/// Copies the elements of the dictionary to an array, starting at a particular array index
///
/// The array to copy to
/// The starting index in the array
public void CopyTo(KeyValuePair[] array, int arrayIndex)
{
Dictionary.CopyTo(array, arrayIndex);
}
///
/// Removes the first occurrence of a specific object from the dictionary
///
/// The key-value pair to remove
/// true if the key-value pair was successfully removed; otherwise, false
public virtual bool Remove(KeyValuePair item)
{
return Dictionary.Remove(item);
}
///
/// Returns an enumerator that iterates through the dictionary
///
/// An enumerator for the dictionary
public virtual IEnumerator> GetEnumerator()
{
return Dictionary.GetEnumerator();
}
///
/// Returns an enumerator that iterates through the dictionary
///
/// An enumerator for the dictionary
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
///
/// Provides a default implementation of ExtendedDictionary using Dictionary{TKey, TValue}
///
[PandasNonExpandable]
public class BaseExtendedDictionary : BaseExtendedDictionary>
{
///
/// Initializes a new instance of the BaseExtendedDictionary class that is empty
///
public BaseExtendedDictionary() : base()
{
}
///
/// Initializes a new instance of the BaseExtendedDictionary class that contains elements copied from the specified dictionary
///
/// The dictionary whose elements are copied to the new dictionary
public BaseExtendedDictionary(IDictionary dictionary) : base(new Dictionary(dictionary))
{
}
///
/// Initializes a new instance of the BaseExtendedDictionary class
/// using the specified as a data source
///
/// The data source for this dictionary
/// Delegate used to select a key from the value
public BaseExtendedDictionary(IEnumerable data, Func keySelector) : base(data, keySelector)
{
}
}
}