HW10: Exploring Simulator Data Types

In looking over the simulator project we have constructed it is apparent that a different data structure might simplify the simulator code. There are two new C++ data types, both part of the C++ Standard Template Library that we can use to simplify the construction of the simulator.

For this homework, I want you to do some research into these data types, and build a simple program that demonstrates each at work. Google will turn up examples of both of these data type. Your task is to put together an example program that demonstrates both of these in action.

The C++ Map

A map is a new kind of array with a significant twist. The index you use to access this array is a string, not a number. The elements stored in the array can be any C++ object you want to use For this homework, I want you to explore using a string to index an array of simple integers.

Your program needs to do this:

1. Create a C++ map that sets up a collection of items called “pins”. The index will be a C++ string with the name we will use to identify the pin. The value stored at the position named by that string is a simple integer number.

  1. Add at least three entries in your collection of pins:

    • “PCO” -> 5
    • “MADDR” -> 3
    • “INST” -> 7

3. Make your program print all of the items in the map out, using the format shown in the above list. The order of the items in this output list is not important.

The Priority Queue

Warning

This part is a bit tough. The examples oyu will find are sometimes way too complex. Search for :priority queue event” and see what you find.

Supose you are working on a problem (like a simulator) where events occur that need to be processed at specific times. For our purposes, the events will trch a simple name (like for a pin) and the time when we need that pin to be processed. The data structure we need for this is called a “priority queue”, and such a thing is provided in the C++ Standard Template Library.

Here is a class that defines an event we can use:

class Event {
    public:
        Event(std::string pname, int t) {
          name=pname;
           int time;
        }
        friend bool operator<(const Event& e1, const Event& e2) {
            return e2.time < e1.time;
        }

        int time;
        std::string name;
};

The friend function defines an “operator” (the “<” symbol) that can be used to figure out which one of two Events comes before the other. The queue will use that method in its internal work.

Queue operations are similar to those we use for a “stack”. We “push” new items onto the queue, and we “pop” items off of the queue. Unlike the stack, items come off of the queue in an order defined by their “priority”.

To manage this queue, we will need a simple class called an “event” that looks

We will use this data structure to avoid sending a “tick” signal to every component in our simulator. Instead, we will “schedule” components for processing based on some time we will determine later. This will set us up for a simulation technique called “Event Driven SImulation”.

Note

This scheme is commonly used in games,a nd even in operting systems. Events there are such things as mouse clicks, or button presses on a game console.

You should be able to find examples of a priority queue of objects of this type.

Your job is to create a second program that does this:

  1. Create a priority queue that manages simple events as shown above in
    priority order. The data for this queue is the data from your first example program. In this case, the name string will be pin names. The numbers will be the “time” vlued t o store.
  2. Insert the three entries shown in part one above into the queue.
  3. Print out the items in the queue in priority order. You should “remove”
    the items from the queue as you print them out. You should use a loop that examines the queue, and “pops” off items until the queue is empty.

Doing this Work

Do not get carried away with this. All we want to do is get a basic chunk of code running that uses these data types and works. This kind of research, and test code building is a common way to experiment with an idea, without warping project code in the process. Once you have a good idea how some new idea works, then you add it to your project. If you have problems getting a working example of a priority queue, at lease build one that manages a set of integers There are many examples of that. We will be using the Event class shown above for the simulator.

The next time you see simulator code, we will be switching to these data types to manage simulator parts, and the simulation loop. We will discuss why we are making this switch in class.