SimSharp-3.0.7 An implementation of a min-Priority Queue using a heap. Has O(1) .Contains()! See https://bitbucket.org/BlueRaja/high-speed-priority-queue-for-c/wiki/Getting%20Started for more information There are modifications so that the type is not generic anymore and can only hold values of type EventQueueNode Instantiate a new Priority Queue EventQueueNodehe max nodes ever allowed to be enqueued (going over this will cause an exception) Removes every node from the queue. O(n) (So, don't do this often!) Returns (in O(1)!) whether the given node is in the queue. O(1) Enqueue a node - .Priority must be set beforehand! O(log n) Returns true if 'higher' has higher priority than 'lower', false otherwise. Note that calling HasHigherPriority(node, node) (ie. both arguments the same node) will return false Removes the head of the queue (node with highest priority; ties are broken by order of insertion), and returns it. O(log n) This method must be called on a node every time its priority changes while it is in the queue. Forgetting to call this method will result in a corrupted queue! O(log n) Removes a node from the queue. Note that the node does not need to be the head of the queue. O(log n) Should not be called in production code. Checks to make sure the queue is still in a valid state. Used for testing/debugging the queue. Returns the number of nodes in the queue. O(1) Returns the maximum number of items that can be enqueued at once in this queue. Once you hit this number (ie. once Count == MaxSize), attempting to enqueue another item will throw an exception. O(1) Returns the head of the queue, without removing it (use Dequeue() for that). O(1) The Priority to insert this node at. Must be set BEFORE adding a node to the queue Used by the priority queue - do not edit this value. Represents the order the node was inserted in Used by the priority queue - do not edit this value. Represents the current position in the queue Conditions are events that execute when any or all of its sub-events are executed. The base class for all events in SimSharp. An event can be in one of three states at any time: - Alive: The event object exists, but is neither scheduled to be executed, nor is it already executed. - Triggered: The event has been put in the event queue and is going to be executed. - Processed: The event has been executed. Usually, the event is alive until its Trigger, Succeed, or Fail method have been called. Then it becomes triggered. When the Environment progresses to the event and executes its callbacks the event becomes processed. This method schedules the event right now. It takes the IsOk state and uses the of the given . Thus if the given event fails, this event will also be triggered as failing. Thrown when the event has already been triggered. The signature of this method allows it to be used as a callback. The event that triggers this event. This method schedules the event right now. It sets IsOk state to true and optionally uses also the value. If urgent is given, the event may be scheduled as urgent. Urgent events are placed in a separate event queue. The callbacks of urgent events are executed before normal events. Thrown when the event has already been triggered. The value that the event should use. Whether the event should be scheduled urgently. This is ususally not required and should be reserved for very special cases. This method schedules the event right now. It sets IsOk state to false and optionally uses also the value. If urgent is given, the event may be scheduled as urgent. Urgent events are placed in a separate event queue. The callbacks of urgent events are executed before normal events. Thrown when the event has already been triggered. The value that the event should use. Whether the event should be scheduled urgently. This is ususally not required and should be reserved for very special cases. This method adds a callback to the list of callbacks. Callbacks will be executed in the order they have been added. The callback to execute when the event is being processed. This method adds a range of callbacks to the list of callbacks. Callbacks will be executed in the order they have been added. The callbacks to execute when the event is being processed. This method removes a callback to the list of callbacks. It is not checked if the callback has actually been added before and no exception will be thrown if it had not been present. The callback to remove. This method processes the event, that is, it calls all the callbacks. When it finishes it will be marked IsProcessed and cannot be processed again. When the event has already been processed. The value property can be used to return arbitrary data from a process or an event. It also represents the interrupt cause to a process. The IsOk flag indicates if the event succeeded or failed. An event that failed indicates to a waiting process that the action could not be performed and that the faulting situation must be handled. Typically, interrupting a process sets the IsOk flag to false. An event is alive when it is not triggered and not processed. That is, when it exists in memory without being scheduled. Typically, a Process is alive until its last event has been processed and the process event itself is to be processed. An event becomes processed when its callbacks have been executed. Events may only be processed once and an exception will be thrown if they are to be processed multiple times. An event becomes triggered when it is placed into the event queue. That is, when its callbacks are going to be executed. An even that is triggered may later not be failed or retriggered. Environments hold the event queues, schedule and process events. Returns a number that is exponentially distributed given a certain mean. Unlike in other APIs here the mean should be given and not the lambda parameter. The mean(!) of the distribution is 1 / lambda. A number that is exponentially distributed Returns a timespan that is exponentially distributed given a certain mean. Unlike in other APIs here the mean should be given and not the lambda parameter. The mean(!) of the distribution is 1 / lambda. A number that is exponentially distributed Describes the number of seconds that a logical step of 1 in the *D-API takes. Calculates the logical date of the simulation by the amount of default steps that have passed. The current simulation time as a calendar date. The calendar date when the simulation started. This defaults to 1970-1-1 if no other date has been specified in the overloaded constructor. The random number generator that is to be used in all events in order to produce reproducible results. A Process handles the iteration of events. Processes may define steps that a certain entity in the simulation has to perform. Each time the process should wait it yields an event and will be resumed when that event is processed. Since an iterator method does not have access to its process, the method can retrieve the associated Process through the ActiveProcess property of the environment. Each Process sets and resets that property during Resume. This interrupts a process and causes the IsOk flag to be set to false. If a process is interrupted the iterator method needs to call HandleFault() before continuing to yield further events. This is thrown in three conditions: - If the process has already been triggered. - If the process attempts to interrupt itself. - If the process continues to yield events despite being faulted. The cause of the interrupt. This method must be called to reset the IsOk flag of the process back to true. The IsOk flag may be set to false if the process waited on an event that failed. In SimPy a faulting process would throw an exception which is then catched and chained. In SimSharp catching exceptions from a yield is not possible as a yield return statement may not throw an exception. If a processes faulted the Value property may indicate a cause for the fault. True if a faulting situation needs to be handled, false if the process is okay and the last yielded event succeeded. Target is the event that is expected to be executed next in the process. Timeouts are simple events that are executed after a certain timespan has passed. A timeout is an event that is executed after a certain timespan has passed. Timeout events are scheduled when they are created. They are always triggered when they are created. The environment in which it is scheduled. The timespan for the timeout. The value of the timeout. Whether the timeout should succeed or fail. An exception that is thrown to stop the simulation. A fast random number generator for .NET Colin Green, January 2005 Key points: 1) Based on a simple and fast xor-shift pseudo random number generator (RNG) specified in: Marsaglia, George. (2003). Xorshift RNGs. http://www.jstatsoft.org/v08/i14/paper This particular implementation of xorshift has a period of 2^128-1. See the above paper to see how this can be easily extened if you need a longer period. At the time of writing I could find no information on the period of System.Random for comparison. 2) Faster than System.Random. Up to 8x faster, depending on which methods are called. 3) Direct replacement for System.Random. This class implements all of the methods that System.Random does plus some additional methods. The like named methods are functionally equivalent. 4) Allows fast re-initialisation with a seed, unlike System.Random which accepts a seed at construction time which then executes a relatively expensive initialisation routine. This provides a vast speed improvement if you need to reset the pseudo-random number sequence many times, e.g. if you want to re-generate the same sequence of random numbers many times. An alternative might be to cache random numbers in an array, but that approach is limited by memory capacity and the fact that you may also want a large number of different sequences cached. Each sequence can be represented by a single seed value (int) when using FastRandom. Notes. A further performance improvement can be obtained by declaring local variables as static, thus avoiding re-allocation of variables on each call. However care should be taken if multiple instances of FastRandom are in use or if being used in a multi-threaded environment. Colin Green, September 4th 2005 - Added NextBytesUnsafe() - commented out by default. - Fixed bug in Reinitialise() - y,z and w variables were not being reset. Colin Green, December 2008. - Fix to Next() - Was previously able to return int.MaxValue, contrary to the method's contract and comments. - Modified NextBool() to use _bitMask instead of a count of remaining bits. Also reset the bit buffer in Reinitialise(). Colin Green, 2011-08-31 - Added NextByte() method. - Added new statically declared seedRng FastRandom to allow easy creation of multiple FastRandoms with different seeds within a single clock tick. Colin Green, 2011-10-04 - Seeds are now hashed. Without this the first random sample for nearby seeds (1,2,3, etc.) are very similar (have a similar bit pattern). Thanks to Francois Guibert for identifying this problem. A static RNG that is used to generate seed values when constructing new instances of FastRandom. This overcomes the problem whereby multiple FastRandom instances are instantiated within the same tick count and thus obtain the same seed, that approach can result in extreme biases occuring in some cases depending on how the RNG is used. Initialises a new instance using a seed generated from the class's static seed RNG. Initialises a new instance using an int value as seed. This constructor signature is provided to maintain compatibility with System.Random Reinitialises using an int value as a seed. Generates a random int over the range 0 to int.MaxValue-1. MaxValue is not generated in order to remain functionally equivalent to System.Random.Next(). This does slightly eat into some of the performance gain over System.Random, but not much. For better performance see: Call NextInt() for an int over the range 0 to int.MaxValue. Call NextUInt() and cast the result to an int to generate an int over the full Int32 value range including negative values. Generates a random int over the range 0 to upperBound-1, and not including upperBound. Generates a random int over the range lowerBound to upperBound-1, and not including upperBound. upperBound must be >= lowerBound. lowerBound may be negative. Generates a random double. Values returned are over the range [0, 1). That is, inclusive of 0.0 and exclusive of 1.0. Fills the provided byte array with random bytes. This method is functionally equivalent to System.Random.NextBytes(). Generates a uint. Values returned are over the full range of a uint, uint.MinValue to uint.MaxValue, inclusive. This is the fastest method for generating a single random number because the underlying random number generator algorithm generates 32 random bits that can be cast directly to a uint. Generates a random int over the range 0 to int.MaxValue, inclusive. This method differs from Next() only in that the range is 0 to int.MaxValue and not 0 to int.MaxValue-1. The slight difference in range means this method is slightly faster than Next() but is not functionally equivalent to System.Random.Next(). Generates a random double. Values returned are over the range (0, 1). That is, exclusive of both 0.0 and 1.0. Generates a single random bit. This method's performance is improved by generating 32 bits in one operation and storing them ready for future calls. Generates a signle random byte with range [0,255]. This method's performance is improved by generating 4 bytes in one operation and storing them ready for future calls.