/// <summary> /// Sorts the elements of this query. /// </summary> /// <typeparam name="T">Type of the elements.</typeparam> /// <param name="query">The query.</param> /// <param name="comparison">The comparison.</param> /// <returns>This query with the elements sorted.</returns> unsafe public static NativeQuery <T> Sorted <T>(this NativeQuery <T> query, Comparison <T> comparison) where T : unmanaged { if (query.IsEmpty) { return(new NativeQuery <T>(query.GetAllocator())); } NativeSortUtilities.Sort(query._buffer, query.Length, comparison); return(query); }
/// <summary> /// Sorts the elements of this query. /// </summary> /// <typeparam name="T">Type of the elements.</typeparam> /// <param name="query">The query.</param> /// <returns>This query with the elements sorted.</returns> unsafe public static NativeQuery <T> Sorted <T>(this NativeQuery <T> query) where T : unmanaged, IComparable <T> { if (query.IsEmpty) { return(new NativeQuery <T>(query.GetAllocator())); } NativeSortUtilities.Sort <T>(query._buffer, query.Length); return(query); }
/// <summary> /// Gets a <see cref="NativeQuery{T}"/> with the default values if this query is empty, otherwise get the same query. /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="query">The query.</param> /// <param name="defaultValues">The default values.</param> /// <returns>A query with the default value if empty, otherwise the same query will be returned.</returns> public static NativeQuery <T> DefaultIfEmpty <T>(this NativeQuery <T> query, Span <T> defaultValues) where T : unmanaged { if (query.IsEmpty) { if (defaultValues.IsEmpty) { return(new NativeQuery <T>(query.GetAllocator())); } unsafe { int length = defaultValues.Length; Allocator allocator = query.GetAllocator() ?? Allocator.Default; T * buffer = allocator.Allocate <T>(length); void * src = Unsafe.AsPointer(ref defaultValues[0]); Unsafe.CopyBlockUnaligned(buffer, src, (uint)(sizeof(T) * length)); return(new NativeQuery <T>(buffer, length, allocator)); } } return(query); }
/// <summary> /// Gets a <see cref="NativeQuery{T}"/> with the default value if this query is empty, otherwise get the same query. /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="query">The query.</param> /// <param name="defaultValue">The default value.</param> /// <returns>A query with the default value if empty, otherwise the same query will be returned.</returns> public static NativeQuery <T> DefaultIfEmpty <T>(this NativeQuery <T> query, T defaultValue) where T : unmanaged { if (query.IsEmpty) { unsafe { Allocator allocator = query.GetAllocator() ?? Allocator.Default; T * value = allocator.Allocate <T>(1); * value = defaultValue; return(new NativeQuery <T>(value, 1, allocator)); } } return(query); }