我正在实现一个不可变集合,它提供 O(1) 堆栈操作和 O(log n) 列表操作。因此,在前面添加元素比在后面添加元素要快。可以有效实现System.Collections.Immutable.IImmutableList<T>.Add()
(https://nuget.org/packages/Microsoft.Bcl.Immutable,http://blogs.msdn.com/b/bclteam/archive/2012/12/18/preview-of-immutable- collections-released-on-nuget.aspx)将元素添加到集合的前面而不是后面?
以下是接口的定义方式:
[SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification = "Ignored")]
public interface IImmutableList<T> : IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
{
IEqualityComparer<T> ValueComparer { get; }
// Summary:
// Adds the specified value to this list.
//
// Parameters:
// value:
// The value to add.
//
// Returns:
// A new list with the element added, or this list if the element is already
// in this list.
IImmutableList<T> Add(T value);
//
// Summary:
// Adds the specified values to this list.
//
// Parameters:
// items:
// The values to add.
//
// Returns:
// A new list with the elements added, or this list if the elements are already
// in this list.
IImmutableList<T> AddRange(IEnumerable<T> items);
IImmutableList<T> Clear();
bool Contains(T value);
int IndexOf(T value);
IImmutableList<T> Insert(int index, T element);
IImmutableList<T> InsertRange(int index, IEnumerable<T> items);
IImmutableList<T> Remove(T value);
IImmutableList<T> RemoveAll(Predicate<T> match);
IImmutableList<T> RemoveAt(int index);
IImmutableList<T> RemoveRange(IEnumerable<T> items);
IImmutableList<T> RemoveRange(int index, int count);
IImmutableList<T> Replace(T oldValue, T newValue);
IImmutableList<T> SetItem(int index, T value);
IImmutableList<T> WithComparer(IEqualityComparer<T> equalityComparer);
}