コード例 #1
0
ファイル: Enums.cs プロジェクト: 0xCM/arrows
        /// <summary>
        /// Produces a stream values sampled from an enum
        /// </summary>
        /// <param name="random">The random source</param>
        /// <typeparam name="E">The enum type</typeparam>
        public static IEnumerable <E> EnumStream <E>(this IPolyrand random, Func <E, bool> filter = null)
            where E : struct, Enum
        {
            IEnumerable <E> produce()
            {
                var names  = Enum.GetNames(typeof(E)).Mapi((index, name) => (index, name)).ToDictionary();
                var domain = closed(0, names.Count);
                var stream = random.Stream(domain);

                while (true)
                {
                    var name  = names[stream.First()];
                    var value = Enum.Parse <E>(name);
                    if (filter != null)
                    {
                        if (filter(value))
                        {
                            yield return(value);
                        }
                    }
                    else
                    {
                        yield return(value);
                    }
                }
            }

            return(stream(produce(), random.RngKind));
        }
コード例 #2
0
        /// <summary>
        /// Fills a caller-allocated target with a specified number of values from the source
        /// </summary>
        /// <param name="random">The random source</param>
        /// <param name="count">The number of values to send to the target</param>
        /// <param name="dst">A reference to the target location</param>
        /// <typeparam name="T">The element type</typeparam>
        public static void StreamTo <T>(this IPolyrand random, int count, ref T dst)
            where T : struct
        {
            var it      = random.Stream <T>().Take(count).GetEnumerator();
            var counter = 0;

            while (it.MoveNext())
            {
                Unsafe.Add(ref dst, counter++) = it.Current;
            }
        }
コード例 #3
0
        /// <summary>
        /// Fills a caller-allocated target with a specified number of values from the source
        /// </summary>
        /// <param name="random">The random source</param>
        /// <param name="domain">The domain of the random variable</param>
        /// <param name="count">The number of values to send to the target</param>
        /// <param name="dst">A reference to the target location</param>
        /// <param name="filter">If specified, values that do not satisfy the predicate are excluded from the stream</param>
        /// <typeparam name="T">The element type</typeparam>
        public static void StreamTo <T>(this IPolyrand random, Interval <T> domain, int count, ref T dst, Func <T, bool> filter = null)
            where T : struct
        {
            var it      = random.Stream <T>(domain, filter).Take(count).GetEnumerator();
            var counter = 0;

            while (it.MoveNext())
            {
                Unsafe.Add(ref dst, counter++) = it.Current;
            }
        }
コード例 #4
0
ファイル: Distinct.cs プロジェクト: 0xCM/arrows
        /// <summary>
        /// Takes a specified number of distinct points from a source
        /// </summary>
        /// <param name="random">The random source</param>
        /// <param name="count">The number of points to take</param>
        /// <typeparam name="T">The element type</typeparam>
        public static HashSet <T> SampleDistinct <T>(this IPolyrand random, int count)
            where T : struct
        {
            var stream = random.Stream <T>();
            var set    = stream.Take(count).ToHashSet();

            while (set.Count < count)
            {
                set.AddRange(stream.Take(set.Count - count));
            }
            return(set);
        }
コード例 #5
0
ファイル: Distinct.cs プロジェクト: 0xCM/arrows
        public static HashSet <T> SampleDistinct <T>(this IPolyrand random, T pool, int count)
            where T : struct
        {
            var src = random.Stream(default(T), pool);
            var set = src.Take(count).ToHashSet();

            while (set.Count < count)
            {
                set.AddRange(src.Take(count / 2));
            }
            return(set);
        }
コード例 #6
0
        /// <summary>
        /// Counts the number of sample points that lie within a specified interval
        /// </summary>
        /// <param name="random">The distribution to analyze</param>
        /// <param name="domain">The interval within which points will be counted</param>
        /// <param name="count">The number of sample points to use</param>
        /// <typeparam name="T">The distribution point type</typeparam>
        public static Ratio <double> SamplesWithin <T>(IPolyrand random, Interval <T> domain, int count)
            where T : unmanaged
        {
            var src     = random.Stream(domain).Take((uint)count);
            var counter = 0;

            foreach (var sample in src)
            {
                if (domain.Contains(sample))
                {
                    counter++;
                }
            }
            return(new Ratio <double>(counter, count));
        }
コード例 #7
0
ファイル: Memory.cs プロジェクト: 0xCM/arrows
 public static Memory <T> Memory <T>(this IPolyrand random, int length, Interval <T> domain)
     where T : struct
 => random.Stream <T>(domain).TakeMemory(length);
コード例 #8
0
ファイル: Memory.cs プロジェクト: 0xCM/arrows
 public static Memory <T> Memory <T>(this IPolyrand random, int length, Interval <T>?domain = null, Func <T, bool> filter = null)
     where T : struct
 => (domain != null ? random.Stream <T>(domain.Value) : random.Stream <T>()).TakeMemory(length);
コード例 #9
0
ファイル: Spans.cs プロジェクト: 0xCM/arrows
 public static Span256 <T> Span256 <T>(this IPolyrand random, int blocks = 1, Interval <T>?domain = null, Func <T, bool> filter = null)
     where T : struct
 => random.Stream(domain, filter).TakeSpan(Z0.Span256.BlockLength <T>(blocks)).ToSpan256();
コード例 #10
0
ファイル: Arrays.cs プロジェクト: 0xCM/arrows
 public static T[] NonZeroArray <T>(this IPolyrand random, int length, Interval <T>?domain = null)
     where T : struct
 => random.Stream(domain, gmath.nonzero).TakeArray(length);
コード例 #11
0
ファイル: Arrays.cs プロジェクト: 0xCM/arrows
 public static T[] Array <T>(this IPolyrand random, int length, Interval <T>?domain = null, Func <T, bool> filter = null)
     where T : struct
 => random.Stream(domain, filter).TakeArray(length);