Ejemplo n.º 1
0
        public NativeQuery <T> Take(int count)
        {
            if (_length == 0)
            {
                return(new NativeQuery <T>(GetAllocator()));
            }

            if (count == 0)
            {
                var emptyQuery = new NativeQuery <T>(GetAllocator());
                Dispose();
                return(emptyQuery);
            }

            if (count < 0)
            {
                Dispose();
                throw new ArgumentException(count.ToString(), nameof(count));
            }

            int       length    = count >= _length ? _length : count;
            Allocator allocator = GetAllocator() !;
            void *    src       = _buffer;
            void *    dst       = allocator.Allocate <T>(length);

            Unsafe.CopyBlockUnaligned(dst, src, (uint)(sizeof(T) * length));
            Dispose();
            return(new NativeQuery <T>(dst, length, allocator));
        }
Ejemplo n.º 2
0
        public NativeQuery <T> Where(Predicate <T> predicate)
        {
            if (_length == 0)
            {
                return(new NativeQuery <T>(GetAllocator()));
            }

            Allocator      allocator = GetAllocator() !;
            NativeList <T> list      = new NativeList <T>(_length, allocator);

            foreach (ref var e in this)
            {
                if (predicate(e))
                {
                    list.Add(e);
                }
            }

            if (list.Length == 0)
            {
                list.Dispose();
                var emptyQuery = new NativeQuery <T>(allocator);
                return(emptyQuery);
            }

            list.TrimExcess();
            var q = new NativeQuery <T>(list.GetUnsafePointer(), list.Length, allocator);

            return(q);
        }
        public static T Min <T>(this NativeQuery <T> query, Comparison <T> comparison) where T : unmanaged
        {
            T?min = null;

            foreach (ref var e in query)
            {
                if (min == null)
                {
                    min = e;
                }
                else
                {
                    int c = comparison(e, min.Value);
                    if (c < 0)
                    {
                        min = e;
                    }
                }
            }

            if (min == null)
            {
                throw new InvalidOperationException("Cannot find the min value.");
            }

            return(min.Value);
        }
        public static T Max <T>(this NativeQuery <T> query) where T : unmanaged, IComparable <T>
        {
            T?max = null;

            foreach (ref var e in query)
            {
                if (max == null)
                {
                    max = e;
                }
                else
                {
                    int c = e.CompareTo(max.Value);
                    if (c > 0)
                    {
                        max = e;
                    }
                }
            }

            if (max == null)
            {
                throw new InvalidOperationException("Cannot find the min value.");
            }

            return(max.Value);
        }
        /// <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);
        }
        public static ulong Sum(this NativeQuery <ulong> query)
        {
            ulong total = 0;

            foreach (ref var e in query)
            {
                total += e;
            }

            return(total);
        }
        public static decimal Sum(this NativeQuery <decimal> query)
        {
            decimal total = 0;

            foreach (ref var e in query)
            {
                total += e;
            }

            return(total);
        }
        public static double Sum(this NativeQuery <double> query)
        {
            double total = 0;

            foreach (ref var e in query)
            {
                total += e;
            }

            return(total);
        }
        public static float Sum(this NativeQuery <float> query)
        {
            float total = 0;

            foreach (ref var e in query)
            {
                total += e;
            }

            return(total);
        }
        public static uint Sum(this NativeQuery <uint> query)
        {
            uint total = 0;

            foreach (ref var e in query)
            {
                total += e;
            }

            return(total);
        }
Ejemplo n.º 12
0
        public NativeQueryDebugView(NativeQuery <T> query)
        {
            T[] array      = new T[query.Length];
            var enumerator = query.GetEnumerator(false);
            int i          = 0;

            while (enumerator.MoveNext())
            {
                array[i++] = enumerator.Current;
            }

            Items = array;
        }
        /// <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);
        }
Ejemplo n.º 14
0
        internal NativeQuery(Allocator?allocator)
        {
            if (allocator == null)
            {
                this = new NativeQuery <T>(Allocator.Default);
                return;
            }

            if (!Allocator.IsCached(allocator))
            {
                throw new ArgumentException("The allocator is not in cache");
            }

            _buffer      = null;
            _length      = 0;
            _allocatorID = allocator.ID;
        }
        public static ulong Average(this NativeQuery <ulong> query)
        {
            if (query.IsEmpty)
            {
                throw new InvalidOperationException("NativeQuery is empty.");
            }

            ulong total  = 0;
            int   length = query.Length;

            foreach (ref var e in query)
            {
                total += e;
            }

            return(total / (ulong)length);
        }
        public static decimal Average(this NativeQuery <decimal> query)
        {
            if (query.IsEmpty)
            {
                throw new InvalidOperationException("NativeQuery is empty.");
            }

            decimal total  = 0;
            int     length = query.Length;

            foreach (ref var e in query)
            {
                total += e;
            }

            return(total / length);
        }
        /// <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 new <see cref="NativeQuery{T}"/> which elements are the result of operate with the elements of this
 /// query and the specified <see cref="Span{T}"/>.
 /// </summary>
 /// <typeparam name="TFirst">The type of the first element.</typeparam>
 /// <typeparam name="TSecond">The type of the second element.</typeparam>
 /// <typeparam name="TResult">The type of the result elements.</typeparam>
 /// <param name="query">The query.</param>
 /// <param name="elements">The elements.</param>
 /// <param name="operation">The operation.</param>
 /// <returns>The resulting query.</returns>
 unsafe public static NativeQuery <TResult> Zip <TFirst, TSecond, TResult>(this NativeQuery <TFirst> query, in Span <TSecond> elements, Func <TFirst, TSecond, TResult> operation) where TFirst : unmanaged where TSecond : unmanaged where TResult : unmanaged