示例#1
0
        /// <summary>
        /// Creates an unordered array of subsetCount int indices that
        /// constitute a subset of all ints in the range [0, count-1].
        /// O(subsetCount) for subsetCount &lt;&lt; count.
        /// NOTE: It is assumed that subsetCount is significantly smaller
        /// than count. If this is not the case, use
        /// CreateRandomSubsetOfSize instead.
        /// WARNING: As subsetCount approaches count execution time
        /// increases significantly.
        /// </summary>
        public static int[] CreateSmallRandomSubsetIndexArray(
            this IRandomUniform rnd, int subsetCount, int count)
        {
            Requires.That(subsetCount >= 0 && subsetCount <= count);
            var subsetIndices = new IntSet(subsetCount);

            for (int i = 0; i < subsetCount; i++)
            {
                int index;
                do
                {
                    index = rnd.UniformInt(count);
                }while (!subsetIndices.TryAdd(index));
            }
            return(subsetIndices.ToArray());
        }
示例#2
0
        /// <summary>
        /// Creates an unordered array of subsetCount int indices that
        /// constitute a subset of all ints in the range [0, count-1].
        /// O(subsetCount) for subsetCount &lt;&lt; count.
        /// NOTE: It is assumed that subsetCount is significantly smaller
        /// than count. If this is not the case, use
        /// CreateRandomSubsetOfSize instead.
        /// WARNING: As subsetCount approaches count execution time
        /// increases significantly.
        /// </summary>
        public static int[] CreateSmallRandomSubsetIndexArray(
            this IRandomUniform rnd, int subsetCount, int count)
        {
            if (!(subsetCount >= 0 && subsetCount <= count))
            {
                throw new ArgumentOutOfRangeException(nameof(subsetCount));
            }
            var subsetIndices = new IntSet(subsetCount);

            for (int i = 0; i < subsetCount; i++)
            {
                int index;
                do
                {
                    index = rnd.UniformInt(count);
                }while (!subsetIndices.TryAdd(index));
            }
            return(subsetIndices.ToArray());
        }
示例#3
0
        static void Main(string[] args)
        {
            IntSet mySet = new IntSet();
            HashSet<int> systemSet = new HashSet<int>();

            for (int i = 0; i < 10000000; i++)
            {
                int a = rand.Next(0, 100);
                mySet.Add(a);
                systemSet.Add(a);

                int b = rand.Next(0, 100);
                mySet.Remove(b);
                systemSet.Remove(b);
            }

            int[] systemSetArray = systemSet.ToArray();
            Array.Sort(systemSetArray);

            int[] mySetArray = mySet.ToArray();

            Console.WriteLine(systemSetArray.SequenceEqual(mySetArray) ?
                "test passed" : "test failed");
        }