miniQueue - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

miniQueue

Description:

Client/Server Music Player. Football 'Depth Charting' ollehtO. Jeopardy. Jigsaw Puzzles. Texas Hold-Em. AIM Bot Creator. 2. Main Index ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 21
Provided by: alaneu
Category:

less

Transcript and Presenter's Notes

Title: miniQueue


1
CSE 331 Lecture 12 Queue Implementations
Simulation
miniQueue() class API member
functions Bounded Queue circular
nature indexing class API member
fuctions Simulation Time driven Drive up
tellers Traffic intersection
Review Topics to date Projects
Proposed Texas Hold-Em Twixt Client/Server
Music Player Football Depth Charting ollehtO
Jeopardy Jigsaw Puzzles Texas Hold-Em AIM Bot
Creator
2
(No Transcript)
3
miniQueue d_queue.h
  • template
  • class miniQueue
  • public
  • miniQueue() // create an empty queue
  • void push(const T item) // append item to
    queue
  • void pop() // remove item from front of
    queue
  • T front() // return reference to front
    queue item
  • const T front() const // const version of
    front()
  • int size() const // return queue size
  • bool empty() const // is the queue empty?
  • private
  • list qlist // maintain queue items
    and size

4
miniQueue d_queue.h
  • template
  • miniQueueminiQueue()
  • // append item to back of queue
  • template
  • void miniQueuepush(const T item)
  • qlist.push_back(item)
  • // remove the item from front queue
  • template
  • void miniQueuepop()
  • if (qlist.size() 0)
  • throw underflowError("miniQueue pop()
    empty q")
  • qlist.pop_front()

5
miniQueue d_queue.h
  • // return reference to front item in queue
  • template
  • T miniQueuefront()
  • if (qlist.size() 0)
  • throw underflowError("miniQueue front()
    empty q")
  • return qlist.front()
  • // return const reference to front item in queue
  • template
  • const T miniQueuefront() const
  • // if queue is empty, throw underflowError
  • if (qlist.size() 0)
  • throw underflowError("miniQueue front()
    empty q")
  • return qlist.front()

6
miniQueue d_queue.h
  • // return the queue size
  • template
  • int miniQueuesize() const
  • return qlist.size()
  • // is the queue empty?
  • template
  • bool miniQueueempty() const
  • return qlist.empty()

7
The Bounded queue
8
Indexing into circular array
  • Incrementing of indices wraps-around to
    beginning
  • Method using a count of queue items items in
    range front .. back)
  • Initially, count 0, front 0, back 0
  • Queue is full if count MAXQSIZE
  • Queue is empty if count 0
  • To pop, if not empty
  • front (front 1) MAXQSIZE
  • To push, if not full
  • Itemsback newitem
  • back (back 1) MAXQSIZE
  • Front returns itemsfront
  • Alternate method without count items in range
    (front .. back
  • Queue is empty if front back
  • queue is full if (back1)MAXQSIZE front
  • To push, if not full
  • Back (back 1) MAXQSIZE
  • itemsback newitem

9
Bounded Queue d_bqueue.h
  • const int MAXQSIZE 50
  • template
  • class bqueue
  • public
  • bqueue() // create empty queue of
    MAXQSIZE
  • void push(const T item) // append item
    at back
  • // Pre count exception
  • void pop() // remove front item from
    queue
  • // Pre queue is not empty
  • T front() // return ref to front queue
    item
  • const T front() const // ditto
  • // Pre queue not empty
  • int size() const // return the queue size
  • bool empty() const // is the queue empty?
  • bool full() const // is the queue full?
  • private
  • T queueArrayMAXQSIZE // queue items
    (circular array)

10
Bounded Queue d_bqueue.h
  • template
  • bqueuebqueue() qfront(0), qback(0),
    count(0)
  • template
  • void bqueuepush(const T item)
  • if (count MAXQSIZE) // array is
    full
  • throw overflowError("bqueue push() queue
    full")
  • queueArrayqback item // insert item
  • qback (qback1) MAXQSIZE // move qback
    along
  • count // increment
    size
  • template
  • void bqueuepop()
  • if (count 0) // array is
    full
  • throw underflowError("bqueue pop() empty
    queue")

11
Bounded Queue d_bqueue.h
  • template
  • T bqueuefront()
  • if (count 0) // queue is empty
  • throw underflowError("bqueue front() empty
    q")
  • return queueArrayqfront
  • template
  • const T bqueuefront() const
  • if (count 0) // queue is empty
  • throw underflowError("bqueue front() empty
    q")
  • return queueArrayqfront

12
Bounded Queue d_bqueue.h
  • template
  • int bqueuesize() const
  • return count
  • template
  • bool bqueueempty() const
  • return count 0
  • template
  • bool bqueuefull() const
  • return count MAXQSIZE

13
Simulation
  • Simulations of interest to us are time driven
  • Events are typically arrivals of entities seeking
    some service, benefit or access to a resource
  • Arrival events over time may be generated
    randomly, uniformly or in accordance with a
    scripted sequence
  • What we measure is waiting time, service time,
    resource utilization, etc
  • Over time and multiple simulations using
    different constraints, the individual stats are
    summarized and compared to assess the best
    situation

14
Simulation
  • Example Customers at the drive-up bank teller
  • Variables
  • How many tellers are on duty
  • How many drive-up lanes are open
  • Time taken for a teller to serve a customer
  • Use of Queues
  • Each drive-up lane is represented by a queue with
    time of arrival stored for each customer waiting
  • Tellers (vector of count-down timers)
  • Tellerk is available if timer 0
  • Tellerk is put to work by setting timer to time
    required for a given job

15
Simulation main loop
  • For each time interval
  • Number of customer arrivals is determined
    (probabilistically)
  • Lane placement of each arrival is determined
    (random or shortest-line or )
  • Current time is pushed onto each queue getting a
    new arrival (this is the customers arrival time)
  • For each available teller and waiting customer
  • Customer in queue waiting longest is popped and
    assigned to teller
  • Tellers counter is set to timer required
  • Waiting time and total time for service are
    computed
  • Current time is updated and all teller times are
    decremented by the interval

16
Traffic Simulation
  • Queues
  • One for each lane of traffic entering
    intersection
  • Arrivals determined by random chance, but based
    on know frequencies for time of day and real
    intersection
  • Time of arrival kept in queue for each vehicle
  • Rules of the road
  • Assume no accidents
  • Assume all drivers are law abiding
  • Order of progression through intersection based
    on traffic rules (e.g., left turn yields to
    oncomming traffic unless left turn arrow, etc)

17
Traffic Simulation
  • Traffic lights
  • No lights
  • Lights, but no special turn arrows
  • Lights with turn arrows
  • Different light timing sequences
  • Statistics
  • Longest waiting time
  • Average waiting time (each lane and overall)
  • Average number of cars waiting at red light
  • Number of cars through intersection
  • ..

18
Simulation
  • Advanced options
  • Light changes keyed to presence of cars in lane
  • Simulation of grids of connected intersections
  • Coordinated timing sequences at different
    intersections
  • Simulations run for different times of day and
    different days of week (traffic arrival
    frequencies differ)
  • Simulation of highway segment through multiple
    lights

19
Review Topics to Date
  • ADT, API, C classes
  • Software design cycle
  • Operator overloading
  • Template functions and classes
  • Sorting (Selection, Insertion, Shell, Merge,
    Quick, Radix, )
  • Recursion (hanoi, fib, fact, knights, 8queens,)
  • Containers (Vector, List, Dequeue)
  • Containers (Sequence, adaptor, associative)
  • Stacks Queues
  • Dynamic Memory (new delete)
  • Copy constructors, destructors, shallow vs. deep
  • Singly-linked lists (will cover Tuesday)

20
Test - Mechanics
  • Closed book closed note in class
  • EXCEPT
  • You may use a standard C reference syntax sheet
    I will give you (and which will be posted on the
    web site)
  • You may bring to the test and use while taking it
    any single 8.5 x 11 sheet of paper with
    anything you want written on it AS LONG AS it is
    hand written (NO TYPED AND REDUCED NOTES ALLOWED)
  • Test will be short answer, code writing, code
    interpreting and analysis, maybe some T/F or
    multiple choice
Write a Comment
User Comments (0)
About PowerShow.com