#if ZLINQ_UNITY_COLLECTIONS_SUPPORT #pragma warning disable CS9074 #nullable enable using System; using System.ComponentModel; using System.Runtime.InteropServices; using Unity.Collections; using Unity.Collections.LowLevel.Unsafe; using ZLinq.Internal; using ZLinq.Linq; namespace ZLinq { public static class UnityCollectionsExtensions { public static ValueEnumerable, T> AsValueEnumerable(this NativeList source) where T : unmanaged { return new(new(source)); } public static ValueEnumerable, T> AsValueEnumerable(this NativeQueue.ReadOnly source) where T : unmanaged { return new(new(source)); } public static ValueEnumerable, T> AsValueEnumerable(this NativeHashSet source) where T : unmanaged, IEquatable { return new(new(source.AsReadOnly())); } public static ValueEnumerable, T> AsValueEnumerable(this NativeHashSet.ReadOnly source) where T : unmanaged, IEquatable { return new(new(source)); } public static ValueEnumerable, KVPair> AsValueEnumerable(this NativeHashMap source) where TKey : unmanaged, IEquatable where TValue : unmanaged { return new(new(source.AsReadOnly())); } public static ValueEnumerable, KVPair> AsValueEnumerable(this NativeHashMap.ReadOnly source) where TKey : unmanaged, IEquatable where TValue : unmanaged { return new(new(source)); } public static ValueEnumerable AsValueEnumerable(this NativeText source) { return new(new(source.AsReadOnly())); } public static ValueEnumerable AsValueEnumerable(this NativeText.ReadOnly source) { return new(new(source)); } public static ValueEnumerable, T> AsValueEnumerable(this FixedList32Bytes source) where T : unmanaged { return new(new(source)); } public static ValueEnumerable, T> AsValueEnumerable(this FixedList64Bytes source) where T : unmanaged { return new(new(source)); } public static ValueEnumerable, T> AsValueEnumerable(this FixedList128Bytes source) where T : unmanaged { return new(new(source)); } public static ValueEnumerable, T> AsValueEnumerable(this FixedList512Bytes source) where T : unmanaged { return new(new(source)); } public static ValueEnumerable, T> AsValueEnumerable(this FixedList4096Bytes source) where T : unmanaged { return new(new(source)); } public static ValueEnumerable AsValueEnumerable(this FixedString32Bytes source) { return new(new(source)); } public static ValueEnumerable AsValueEnumerable(this FixedString64Bytes source) { return new(new(source)); } public static ValueEnumerable AsValueEnumerable(this FixedString128Bytes source) { return new(new(source)); } public static ValueEnumerable AsValueEnumerable(this FixedString512Bytes source) { return new(new(source)); } public static ValueEnumerable AsValueEnumerable(this FixedString4096Bytes source) { return new(new(source)); } } } namespace ZLinq.Linq { [StructLayout(LayoutKind.Auto)] [EditorBrowsable(EditorBrowsableState.Never)] public struct FromNativeList : IValueEnumerator where T : unmanaged { NativeList source; int index; public FromNativeList(NativeList source) { this.source = source; this.index = 0; } public void Dispose() { } public unsafe bool TryCopyTo(Span destination, Index offset) { if (EnumeratorHelper.TryGetSlice(new ReadOnlySpan(source.GetUnsafePtr(), source.Length), offset, destination.Length, out var slice)) { slice.CopyTo(destination); return true; } return false; } public bool TryGetNext(out T current) { if ((uint)index < (uint)source.Length) { current = source[index++]; return true; } current = default!; return false; } public bool TryGetNonEnumeratedCount(out int count) { count = source.Length; return true; } public unsafe bool TryGetSpan(out ReadOnlySpan span) { span = new ReadOnlySpan(source.GetUnsafePtr(), source.Length); return true; } } [StructLayout(LayoutKind.Auto)] [EditorBrowsable(EditorBrowsableState.Never)] public struct FromNativeQueue : IValueEnumerator where T : unmanaged { NativeQueue.ReadOnly source; NativeQueue.Enumerator enumerator; public FromNativeQueue(NativeQueue.ReadOnly source) { this.source = source; this.enumerator = source.GetEnumerator(); } public void Dispose() { } public unsafe bool TryCopyTo(Span destination, Index offset) => false; public bool TryGetNext(out T current) { if (enumerator.MoveNext()) { current = enumerator.Current; return true; } current = default!; return false; } public bool TryGetNonEnumeratedCount(out int count) { count = source.Count; return true; } public unsafe bool TryGetSpan(out ReadOnlySpan span) { span = default; return false; } } [StructLayout(LayoutKind.Auto)] [EditorBrowsable(EditorBrowsableState.Never)] public struct FromNativeHashSet : IValueEnumerator where T : unmanaged, IEquatable { NativeHashSet.ReadOnly source; NativeHashSet.Enumerator enumerator; public FromNativeHashSet(NativeHashSet.ReadOnly source) { this.source = source; this.enumerator = source.GetEnumerator(); } public void Dispose() { } public unsafe bool TryCopyTo(Span destination, Index offset) => false; public bool TryGetNext(out T current) { if (enumerator.MoveNext()) { current = enumerator.Current; return true; } current = default!; return false; } public bool TryGetNonEnumeratedCount(out int count) { count = source.Count; return true; } public unsafe bool TryGetSpan(out ReadOnlySpan span) { span = default; return false; } } [StructLayout(LayoutKind.Auto)] [EditorBrowsable(EditorBrowsableState.Never)] public struct FromNativeHashMap : IValueEnumerator> where TKey : unmanaged, IEquatable where TValue : unmanaged { NativeHashMap.ReadOnly source; NativeHashMap.Enumerator enumerator; public FromNativeHashMap(NativeHashMap.ReadOnly source) { this.source = source; this.enumerator = source.GetEnumerator(); } public void Dispose() { } public unsafe bool TryCopyTo(Span> destination, Index offset) => false; public bool TryGetNext(out KVPair current) { if (enumerator.MoveNext()) { current = enumerator.Current; return true; } current = default!; return false; } public bool TryGetNonEnumeratedCount(out int count) { count = source.Count; return true; } public unsafe bool TryGetSpan(out ReadOnlySpan> span) { span = default; return false; } } [StructLayout(LayoutKind.Auto)] [EditorBrowsable(EditorBrowsableState.Never)] public struct FromNativeText : IValueEnumerator { NativeText.Enumerator enumerator; public FromNativeText(NativeText.ReadOnly source) { this.enumerator = source.GetEnumerator(); } public void Dispose() { } public unsafe bool TryCopyTo(Span destination, Index offset) => false; public bool TryGetNext(out Unicode.Rune current) { if (enumerator.MoveNext()) { current = enumerator.Current; return true; } current = default!; return false; } public bool TryGetNonEnumeratedCount(out int count) { count = default; return false; } public unsafe bool TryGetSpan(out ReadOnlySpan span) { span = default; return false; } } public struct FromFixedList32Bytes : IValueEnumerator where T : unmanaged { FixedList32Bytes source; int index; public FromFixedList32Bytes(FixedList32Bytes source) { this.source = source; this.index = 0; } public void Dispose() { } public unsafe bool TryCopyTo(Span destination, Index offset) => false; public bool TryGetNext(out T current) { if ((uint)index < (uint)source.Length) { current = source[index++]; return true; } current = default!; return false; } public bool TryGetNonEnumeratedCount(out int count) { count = source.Length; return true; } public unsafe bool TryGetSpan(out ReadOnlySpan span) { span = default; return false; } } public struct FromFixedList64Bytes : IValueEnumerator where T : unmanaged { FixedList64Bytes source; int index; public FromFixedList64Bytes(FixedList64Bytes source) { this.source = source; this.index = 0; } public void Dispose() { } public unsafe bool TryCopyTo(Span destination, Index offset) => false; public bool TryGetNext(out T current) { if ((uint)index < (uint)source.Length) { current = source[index++]; return true; } current = default!; return false; } public bool TryGetNonEnumeratedCount(out int count) { count = source.Length; return true; } public unsafe bool TryGetSpan(out ReadOnlySpan span) { span = default; return false; } } public struct FromFixedList128Bytes : IValueEnumerator where T : unmanaged { FixedList128Bytes source; int index; public FromFixedList128Bytes(FixedList128Bytes source) { this.source = source; this.index = 0; } public void Dispose() { } public unsafe bool TryCopyTo(Span destination, Index offset) => false; public bool TryGetNext(out T current) { if ((uint)index < (uint)source.Length) { current = source[index++]; return true; } current = default!; return false; } public bool TryGetNonEnumeratedCount(out int count) { count = source.Length; return true; } public unsafe bool TryGetSpan(out ReadOnlySpan span) { span = default; return false; } } public struct FromFixedList512Bytes : IValueEnumerator where T : unmanaged { FixedList512Bytes source; int index; public FromFixedList512Bytes(FixedList512Bytes source) { this.source = source; this.index = 0; } public void Dispose() { } public unsafe bool TryCopyTo(Span destination, Index offset) => false; public bool TryGetNext(out T current) { if ((uint)index < (uint)source.Length) { current = source[index++]; return true; } current = default!; return false; } public bool TryGetNonEnumeratedCount(out int count) { count = source.Length; return true; } public unsafe bool TryGetSpan(out ReadOnlySpan span) { span = default; return false; } } public struct FromFixedList4096Bytes : IValueEnumerator where T : unmanaged { FixedList4096Bytes source; int index; public FromFixedList4096Bytes(FixedList4096Bytes source) { this.source = source; this.index = 0; } public void Dispose() { } public unsafe bool TryCopyTo(Span destination, Index offset) => false; public bool TryGetNext(out T current) { if ((uint)index < (uint)source.Length) { current = source[index++]; return true; } current = default!; return false; } public bool TryGetNonEnumeratedCount(out int count) { count = source.Length; return true; } public unsafe bool TryGetSpan(out ReadOnlySpan span) { span = default; return false; } } [StructLayout(LayoutKind.Auto)] [EditorBrowsable(EditorBrowsableState.Never)] public struct FromFixedString32Bytes : IValueEnumerator { FixedString32Bytes.Enumerator enumerator; public FromFixedString32Bytes(FixedString32Bytes source) { this.enumerator = source.GetEnumerator(); } public void Dispose() { } public unsafe bool TryCopyTo(Span destination, Index offset) => false; public bool TryGetNext(out Unicode.Rune current) { if (enumerator.MoveNext()) { current = enumerator.Current; return true; } current = default!; return false; } public bool TryGetNonEnumeratedCount(out int count) { count = default; return false; } public unsafe bool TryGetSpan(out ReadOnlySpan span) { span = default; return false; } } [StructLayout(LayoutKind.Auto)] [EditorBrowsable(EditorBrowsableState.Never)] public struct FromFixedString64Bytes : IValueEnumerator { FixedString64Bytes.Enumerator enumerator; public FromFixedString64Bytes(FixedString64Bytes source) { this.enumerator = source.GetEnumerator(); } public void Dispose() { } public unsafe bool TryCopyTo(Span destination, Index offset) => false; public bool TryGetNext(out Unicode.Rune current) { if (enumerator.MoveNext()) { current = enumerator.Current; return true; } current = default!; return false; } public bool TryGetNonEnumeratedCount(out int count) { count = default; return false; } public unsafe bool TryGetSpan(out ReadOnlySpan span) { span = default; return false; } } [StructLayout(LayoutKind.Auto)] [EditorBrowsable(EditorBrowsableState.Never)] public struct FromFixedString128Bytes : IValueEnumerator { FixedString128Bytes.Enumerator enumerator; public FromFixedString128Bytes(FixedString128Bytes source) { this.enumerator = source.GetEnumerator(); } public void Dispose() { } public unsafe bool TryCopyTo(Span destination, Index offset) => false; public bool TryGetNext(out Unicode.Rune current) { if (enumerator.MoveNext()) { current = enumerator.Current; return true; } current = default!; return false; } public bool TryGetNonEnumeratedCount(out int count) { count = default; return false; } public unsafe bool TryGetSpan(out ReadOnlySpan span) { span = default; return false; } } [StructLayout(LayoutKind.Auto)] [EditorBrowsable(EditorBrowsableState.Never)] public struct FromFixedString512Bytes : IValueEnumerator { FixedString512Bytes.Enumerator enumerator; public FromFixedString512Bytes(FixedString512Bytes source) { this.enumerator = source.GetEnumerator(); } public void Dispose() { } public unsafe bool TryCopyTo(Span destination, Index offset) => false; public bool TryGetNext(out Unicode.Rune current) { if (enumerator.MoveNext()) { current = enumerator.Current; return true; } current = default!; return false; } public bool TryGetNonEnumeratedCount(out int count) { count = default; return false; } public unsafe bool TryGetSpan(out ReadOnlySpan span) { span = default; return false; } } [StructLayout(LayoutKind.Auto)] [EditorBrowsable(EditorBrowsableState.Never)] public struct FromFixedString4096Bytes : IValueEnumerator { FixedString4096Bytes.Enumerator enumerator; public FromFixedString4096Bytes(FixedString4096Bytes source) { this.enumerator = source.GetEnumerator(); } public void Dispose() { } public unsafe bool TryCopyTo(Span destination, Index offset) => false; public bool TryGetNext(out Unicode.Rune current) { if (enumerator.MoveNext()) { current = enumerator.Current; return true; } current = default!; return false; } public bool TryGetNonEnumeratedCount(out int count) { count = default; return false; } public unsafe bool TryGetSpan(out ReadOnlySpan span) { span = default; return false; } } } #pragma warning restore CS9074 #endif