/************************************************************************* * ModernUO * * Copyright (C) 2019-2021 - ModernUO Development Team * * Email: hi@modernuo.com * * File: OrderedHashSet.ValueCollection.cs * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see . * *************************************************************************/ using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; namespace Server.Collections { public partial class OrderedHashSet { [DebuggerDisplay("Count = {Count}")] public sealed class ValueCollection : IList, IReadOnlyList { private readonly OrderedHashSet _orderedHashSet; private const string NotSupported_ValueCollectionSet = "Mutating a key collection derived from a hash set is not allowed."; public int Count => _orderedHashSet.Count; public TValue this[int index] => ((IList)_orderedHashSet)[index]; TValue IList.this[int index] { get => this[index]; set => throw new NotSupportedException(NotSupported_ValueCollectionSet); } bool ICollection.IsReadOnly => true; internal ValueCollection(OrderedHashSet OrderedHashSet) => _orderedHashSet = OrderedHashSet; public Enumerator GetEnumerator() => new Enumerator(_orderedHashSet); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); int IList.IndexOf(TValue item) => _orderedHashSet.IndexOf(item); void IList.Insert(int index, TValue item) => throw new NotSupportedException(NotSupported_ValueCollectionSet); void IList.RemoveAt(int index) => throw new NotSupportedException(NotSupported_ValueCollectionSet); void ICollection.Add(TValue item) => throw new NotSupportedException(NotSupported_ValueCollectionSet); void ICollection.Clear() => throw new NotSupportedException(NotSupported_ValueCollectionSet); bool ICollection.Contains(TValue item) => _orderedHashSet.Contains(item); void ICollection.CopyTo(TValue[] array, int arrayIndex) { if (array == null) { throw new ArgumentNullException(nameof(array)); } if ((uint)arrayIndex > (uint)array.Length) { throw new ArgumentOutOfRangeException(nameof(arrayIndex), ArgumentOutOfRange_NeedNonNegNum); } int count = Count; if (array.Length - arrayIndex < count) { throw new ArgumentException(Arg_ArrayPlusOffTooSmall); } Entry[] entries = _orderedHashSet._entries; for (int i = 0; i < count; ++i) { array[i + arrayIndex] = entries[i].Value; } } bool ICollection.Remove(TValue item) => throw new NotSupportedException(NotSupported_ValueCollectionSet); public struct Enumerator : IEnumerator { private readonly OrderedHashSet _orderedHashSet; private readonly int _version; private int _index; private TValue _current; public TValue Current => _current; object IEnumerator.Current => _current; internal Enumerator(OrderedHashSet OrderedHashSet) { _orderedHashSet = OrderedHashSet; _version = OrderedHashSet._version; _index = 0; _current = default; } public void Dispose() { } public bool MoveNext() { if (_version != _orderedHashSet._version) { throw new InvalidOperationException(InvalidOperation_EnumFailedVersion); } if (_index < _orderedHashSet.Count) { _current = _orderedHashSet._entries[_index].Value; ++_index; return true; } _current = default; return false; } void IEnumerator.Reset() { if (_version != _orderedHashSet._version) { throw new InvalidOperationException(InvalidOperation_EnumFailedVersion); } _index = 0; _current = default; } } } } }