using System; using System.Collections; using System.Collections.Generic; namespace MemoryPools.Collections.Specialized { public partial class PoolingDictionary { internal class ValuesCollection : ICollection { private PoolingDictionary _src; public ValuesCollection Init(PoolingDictionary src) { _src = src; return this; } public IEnumerator GetEnumerator() { return Pool.Get().Init(_src); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); public void Add(TValue item) => throw new NotImplementedException(); public void Clear() => throw new NotImplementedException(); public bool Contains(TValue item) { foreach (var entry in _src._entries) { if (entry.Equals(item)) return true; } return false; } public void CopyTo(TValue[] array, int arrayIndex) { if (array.Length - arrayIndex < _src._entries.Count) { throw new IndexOutOfRangeException("Cannot copy keys into array. Target array is too small."); } for (var index = 0; index < _src._entries.Count; index++) { var entry = _src._entries[index]; array[arrayIndex + index] = entry.value; } } public bool Remove(TValue item) => throw new NotImplementedException("Removal ops are disallowed."); public int Count => _src.Count; public bool IsReadOnly => _src.IsReadOnly; private class Enumerator : IEnumerator { private PoolingDictionary _src; private int _pos; public Enumerator Init(PoolingDictionary src) { _pos = -1; _src = src; return this; } public bool MoveNext() { if (_pos >= _src.Count) return false; _pos++; return _pos < _src.Count; } public void Reset() { _pos = -1; } public TValue Current => _src._entries[_pos].value; object IEnumerator.Current => Current; public void Dispose() { _src = default; Pool.Return(this); } } } } }