CIS 403503 Accelerated DataFile Structures - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

CIS 403503 Accelerated DataFile Structures

Description:

Radix sort. Queue Implementation. STL queue implementation. Array-based queue ... apply the radix sort and output the sorted vector. radixSort(intVector, 5) ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 29
Provided by: yanz
Category:

less

Transcript and Presenter's Notes

Title: CIS 403503 Accelerated DataFile Structures


1
CIS 403/503Accelerated Data-File Structures
  • Lecture 16
  • Queue Implementation

2
Objectives
  • In this lecture, we will learn
  • Queue Application
  • Bank Teller Simulation
  • Radix sort
  • Queue Implementation
  • STL queue implementation
  • Array-based queue implementation
  • Pointer-based queue implementation

3
Queue Application
4
Queue Scheduling
  • Bank Teller Simulation
  • Duration 1 hour
  • A new customer is coming in every minute with
    probability 0.9
  • Single queue multiple tellers (5) whenever a
    teller is free, take the next customer from the
    queue.
  • Compute the average wait time

5
The Customer
class Customer protected int arrivalTime
int processTime public Customer (int at
0) arrivalTime (at), processTime (2
(int)rand() 7) // are we done with our
transaction? bool done () return
--processTime lt 0 int arrival() return
arrivalTime int process() return
processTime // order by arrival time bool
operatorlt (const Customer c) const return
arrivalTime lt c.arrivalTime
  • Arrival time
  • Process time

6
The Teller
class Teller public Teller()free (true)
bool isFree () // are we free to service
new customer? if (free) return true
if (customer.done()) free true
return free // start serving new
customer void addCustomer (const Customer c)
customer c free false
private bool free Customer
customer
  • free
  • customer

7
Put it All Together
  • include ltiostreamgt
  • include ltqueuegt
  • include ltcstdlibgt
  • include lttime.hgt
  • using namespace std
  • class Customer
  • protected
  • int arrivalTime
  • int processTime
  • public
  • Customer (int at 0)
  • arrivalTime (at),
  • processTime (2 (int)rand() 7)
  • // are we done with our transaction?

class Teller public Teller()free (true)
bool isFree () // are we free to service
new customer? if (free) return true
if (customer.done()) free true
return free // start serving new
customer void addCustomer (const Customer c)
customer c free false
private bool free Customer
customer
8
Put it All Together
int main () srand(time(NULL)) const int
numberOfTellers 5 const int numberOfMinutes
60 double totalWait 0 int
numberOfCustomers 0 vectorltTellergt
teller (numberOfTellers) queueltCustomergt
line for (int t 0 t lt numberOfMinutes
t) if ((int)rand() 10 lt 9)
line.push (Customer (t)) for (int i 0 i
lt numberOfTellers i) if
(telleri.isFree () !line.empty ())
Customer frontCustomer line.front
() numberOfCustomers totalWait t -
frontCustomer.arrival()
telleri.addCustomer (frontCustomer)
line.pop () cout ltlt
"average wait " ltlt (totalWait /
numberOfCustomers) ltlt stdendl
9
Radix Sort
  • Sort d-digit numbers based on radix
  • First the ones digit
  • The tens digit
  • The 10d-1 digit

10
Radix Sort
  • void radixSort(vectorltintgt v, int d)
  • // support function for radixSort()
  • // distribute vector elements into one of 10
    queues
  • // using the digit corresponding to power
  • // power 1 gt 1's digit
  • // power 10 gt 10's digit
  • // power 100 gt 100's digit
  • // ...
  • void distribute(const vectorltintgt v, queueltintgt
    digitQueue, int power)
  • int i
  • // loop through the vector, inserting
    each element into
  • // the queue (vi / power) 10
  • for (i 0 i lt v.size() i)
  • digitQueue(vi / power)
    10.push(vi)

11
Radix Sort
  • // support function for radixSort()
  • // gather elements from the queues and copy back
    to the vector
  • void collect(queueltintgt digitQueue,
    vectorltintgt v)
  • int i 0, digit
  • // scan the vector of queues using
    indices 0, 1, 2, etc.
  • for (digit 0 digit lt 10 digit)
  • // collect items until queue
    empty and copy items back
  • // to the vector
  • while (!digitQueuedigit.empty())
  • vi digitQueuedigit.f
    ront()
  • digitQueuedigit.pop()
  • i

12
Radix Sort
  • void radixSort(vectorltintgt v, int d)
  • int i
  • int power 1
  • queueltintgt digitQueue10
  • for (i0i lt di)
  • distribute(v, digitQueue, power)
  • collect(digitQueue, v)
  • power 10

13
Test
  • include ltiostreamgt
  • include ltiomanipgt
  • include ltvectorgt
  • using namespace std
  • // output v, 6 elements per line
  • void displayVector(const vectorltintgt v)
  • int main()
  • // vector to hold the data that is sorted
  • vectorltintgt intVector
  • randomNumber rnd
  • int i
  • // initialize vector with 50 random
    numbers in range 0 - 99999
  • for (i 0 i lt 50 i)
  • intVector.push_back(rnd.random(100
    000))
  • // apply the radix sort and output the
    sorted vector
  • radixSort(intVector, 5)

void displayVector(const vectorltintgt v)
int i for (i0 i lt v.size() i)
// output each element in
12 spaces cout ltlt setw(12) ltlt
vi if ((i1) 6 0)
// newline every 6 numbers
cout ltlt endl cout ltlt
endl
14
Queue Implementation
15
STL-based Implementation
  • STL list as the internal data structure

16
Class Declaration
  • include ltlistgt // list class used by object
    composition
  • using namespace std
  • // implements a queue
  • template lttypename Tgt
  • class miniQueue
  • public
  • miniQueue()
  • // constructor create an
    empty queue
  • void push(const T item)
  • // insert an element at
    the back of the queue.
  • // Postcondition the
    queue has one more element
  • void pop()
  • // remove an element from
    the front of the queue.
  • // Precondition the
    queue is not empty. if the
  • // queue is empty, the
    function throws the

T front()
// return a reference to the front of the
queue. // Preconditon
the queue is not empty. if the
// the queue is empty, the function throws
the // underflowError
exception const T front()
const // constant
version of front() int size() const
// return the queue size
bool empty() const
// is the queue empty? private
listltTgt qlist //
a list object maintains the queue items and
size
17
Implementation
  • // the constructor has nothing to do. the default
  • // constructor for the list class initializes
  • // qlist to be empty
  • template lttypename Tgt
  • miniQueueltTgtminiQueue()
  • // insert item into the queue by inserting it at
  • // the back of the list
  • template lttypename Tgt
  • void miniQueueltTgtpush(const T item)
  • qlist.push_back(item)

18
Implementation
  • // remove the item at the front of the queue
  • // by erasing the front of the list
  • template lttypename Tgt
  • void miniQueueltTgtpop()
  • // if queue is empty, throw
    underflowError
  • if (qlist.size() 0)
  • throw underflowError("miniQueue
    pop() empty queue")
  • // erase the front
  • qlist.pop_front()
  • template lttypename Tgt
  • T miniQueueltTgtfront()
  • // if queue is empty, throw
    underflowError
  • if (qlist.size() 0)
  • throw underflowError("miniQueue
    front() empty queue")

19
Implementation
  • template lttypename Tgt
  • const T miniQueueltTgtfront() const
  • // if queue is empty, throw
    underflowError
  • if (qlist.size() 0)
  • throw underflowError("miniQueue
    front() empty queue")
  • return qlist.front()
  • template lttypename Tgt
  • T miniQueueltTgtback()
  • // if queue is empty, throw
    underflowError
  • if (qlist.size() 0)
  • throw underflowError("miniQueue
    back() empty queue")
  • return qlist.back()

20
Implementation
  • template lttypename Tgt
  • const T miniQueueltTgtback() const
  • // if queue is empty, throw
    underflowError
  • if (qlist.size() 0)
  • throw underflowError("miniQueue
    back() empty queue")
  • return qlist.back()
  • // return the queue size
  • template lttypename Tgt
  • int miniQueueltTgtsize() const
  • return qlist.size()
  • // is the queue empty?
  • template lttypename Tgt

21
Array-Based Queue
  • Finite queue
  • Test if queue is full

22
The Declaration
  • using namespace std
  • const int MAXQSIZE 50
  • template lttypename Tgt
  • class bqueue
  • public
  • bqueue()
  • // constructor. create an
    empty queue with MAXQSIZE available slots
  • void push(const T item)
  • // insert an element at
    the back of the queue.
  • // Precondition count lt
    MAXQSIZE. throws exception
  • // overflowError if the
    queue is full
  • void pop()
  • // remove an element from
    the front of the queue.
  • // Precondition the
    queue is not empty. throws

int size() const //
return the queue size bool
empty() const // is the
queue empty? bool full()
const private // array
holding the queue elements T
queueArrayMAXQSIZE // index of
the front and back of the queue
int qfront, qback // the number
of elements in the queue, 0 lt count lt MAXQSIZE
int count
23
Implementation
  • template lttypename Tgt
  • bqueueltTgtbqueue()
  • qfront(0), qback(0), count(0)
  • template lttypename Tgt
  • void bqueueltTgtpush(const T item)
  • // is the array filled up? if so, throw
    overflowError
  • if (count MAXQSIZE)
  • throw overflowError("bqueue
    push() queue full")
  • // perform a circular queue insertion
  • queueArrayqback item
  • qback (qback1) MAXQSIZE
  • // increment the queue size
  • count

24
Implementation
  • template lttypename Tgt
  • void bqueueltTgtpop()
  • // if queue is empty, throw
    underflowError
  • if (count 0)
  • throw underflowError("bqueue
    pop() empty queue")
  • // perform a circular queue deletion
  • qfront (qfront1) MAXQSIZE
  • // decrement the queue size
  • count--
  • template lttypename Tgt
  • T bqueueltTgtfront()
  • // if queue is empty, throw
    underflowError
  • if (count 0)

25
Implementation
  • template lttypename Tgt
  • const T bqueueltTgtfront() const
  • // if queue is empty, throw
    underflowError
  • if (count 0)
  • throw underflowError("bqueue
    front() empty queue")
  • return queueArrayqfront
  • template lttypename Tgt
  • int bqueueltTgtsize() const
  • return count

26
Implementation
  • template lttypename Tgt
  • bool bqueueltTgtempty() const
  • return count 0
  • template lttypename Tgt
  • bool bqueueltTgtfull() const
  • return count MAXQSIZE

27
Summary
  • Queue application
  • Bank teller
  • Radix sort
  • Queue implementation
  • STL-based
  • Array based

28
Possible Quiz Questions
  • Whats time complexity of radix sort?
  • Use STL queue
  • StL/Array-based queue data structure
Write a Comment
User Comments (0)
About PowerShow.com