Given:
To generate N events we need to generate N/2 connection
events and N/2 corresponding disconnection events. The time stamps
(ts) for the N/2 connection events should be a random floating
point number between 0 and (N/2)/p. For every connection event,
a disonnection event should be generated. The time stamp for the
disconnection even should be ts +d, where ts is the time
stamp of the connection event and d is the random duration (0 -
2000).
Example
Imagine that the six random connection events are:
Connection event 1 <276-9345, <276-1238, 16.562> >
Connection event 2 <276-9901, <276-1843, 43.124> >
Connection event 3 <276-1333, <276-9998, 55.267> >
Connection event 4 <276-1234, <276-5432, 2.348>
>
Connection event 5 <276-1846, <276-1046, 20.341> >
Connection event 6 <276-2643, <276-2304, 37.123> >
Note that the time stamps are between 0 and 60 which follows the probability model defined by p.
The disconnection events will be duplicates of the six connection events above except that the time stamp will be increased by a random floationg point number between 0 and 2000.
Afterwards, all 12 events must be sorted by the time stamp. The
discrete event simulator simply processes the events in order and finishes
after the last event has been processed.
A priority queue is the perfect implementation structure for maintaining
the events.
typedef map<double, pair<double, float> > table_type; ... table_type connections; table_type::iterator i; double number = .... i = connections.find(number); if (i != connections.end()) ... action to take if the number was found else ... action to take if the number wasn't foundIn case (i != connections.end()), it is valid to use *i to retrieve the key and its associated value, which are stored as a pair.
string number1; string number2; float start_time; ... code to give values to number1, number2, start_time connections[number1] = pair<double, float>(number2, start_time);This means store the value created by the pair<string, float> constructor in the table so that it can be found using key number1. On the left hand side of the assignment, it looks like you are treating connections as an array; this works because the map class overloads the definition of operator[].
typedef pair<double, float> associated_type; typedef pair<const double, associated_type> entry_type; typedef pair<table_type::iterator, bool> pib; pib p = connections.insert( entry_type(number1, associated_type(number_2, start_time))); if (p.second) ... action to take if the insertion was successful, ... using p.first to refer to the entry with key == number1 else ... action to take if there was already an entry with key == number1, ... using p.first to refer to that entryThus insert allows you to check if a key is in the table and, if not, put a new entry with that key into the table in one step--that is, with one search of the table. This is faster than using find and then operator[], which requires searching the map twice, but either way is acceptable for this assignment.