File size: 4,561 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/// ------------------------------------------------------
/// RandomOps - (Pseudo) Random Number Generator For C#
/// Copyright (C) 2003-2010 Magnus Erik Hvass Pedersen.
/// Please see the file license.txt for license details.
/// RandomOps on the internet: http://www.Hvass-Labs.org/
/// ------------------------------------------------------

using System.Diagnostics;

namespace RandomOps
{
    /// <summary>
    /// A set of integers enumerated from zero and upwards that
    /// can be drawn at random. Creation or resetting takes time
    /// O(n) where n is the size of the set, and drawing a random
    /// element from the set takes constant time O(1). Not thread-safe.
    /// </summary>
    public class Set
    {
        #region Public properties.
        /// <summary>
        /// Number of elements available for drawing.
        /// </summary>
        public int Size
        {
            get;
            private set;
        }

        /// <summary>
        /// Maximum number of elements in the set.
        /// </summary>
        public int Capacity
        {
            get;
            private set;
        }
        #endregion

        #region Constructor.
        /// <summary>
        /// Creates but does not initialize the random set.
        /// </summary>
        /// <param name="rand">RNG object to use for random drawing.</param>
        /// <param name="capacity">Number of elements in the set.</param>
        public Set(Random rand, int capacity)
        {
            Rand = rand;

            Elms = new int[capacity];
            Capacity = capacity;
            Size = 0;
        }
        #endregion

        #region Internal variables.
        /// <summary>
        /// The RNG used for drawing elements from the set.
        /// </summary>
        Random Rand;

        /// <summary>
        /// Array used for holding the elements of the set.
        /// </summary>
        int[] Elms;
        #endregion

        #region Internal methods.
        /// <summary>
        /// Swap the i'th and j'th elements of the internal array.
        /// </summary>
        void Swap(int i, int j)
        {
            Debug.Assert(i >= 0 && i < Capacity);
            Debug.Assert(j >= 0 && j < Capacity);

            int temp = Elms[i];
            Elms[i] = Elms[j];
            Elms[j] = temp;
        }

        /// <summary>
        /// Remove an element from the set.
        /// </summary>
        /// <param name="index">Position of element to be removed.</param>
        void Remove(int index)
        {
            // Various assumptions.
            Debug.Assert(Size >= 1);
            Debug.Assert(index >= 0 && index < Size);

            // Decrease the number of elements in the set.
            Size--;

            // Swap element to be removed with the back of the array.
            Swap(index, Size);
        }
        #endregion

        #region Public methods.
        /// <summary>
        /// Draw and remove an element from the set.
        /// </summary>
        /// <returns>Randomly picked element.</returns>
        public int Draw()
        {
            // Local variables to be used.
            int index;
            int retVal;

            // Various assumptions.
            Debug.Assert(Size > 0);

            // Get random index from remainder of set.
            index = Rand.Index(Size);

            // Retrieve the element at that position.
            retVal = Elms[index];

            // Remove that element from the set.
            Remove(index);

            return retVal;
        }

        /// <summary>
        /// Reset by filling the set with integer elements enumerated
        /// from zero and upwards.
        /// </summary>
        public void Reset()
        {
            // Reset index-variable.
            Size = Capacity;

            // Initialize array.
            for (int i = 0; i < Size; i++)
            {
                Elms[i] = i;
            }
        }

        /// <summary>
        /// Same as Reset() only that it is being followed by a removal
        /// of the designated element.
        /// </summary>
        /// <param name="index">Element to be removed after resetting.</param>
        public void ResetExclude(int index)
        {
            Debug.Assert(index >= 0 && index < Capacity);

            Reset();
            Remove(index);
        }
        #endregion
    }
}