Lecture 5 - Personal

Download Report

Transcript Lecture 5 - Personal

DCO20105 Data structures and algorithms
 Lecture
5:
Deque
Comparison of sequence containers
Deque
 Applications considerations
 Comparison of sequence containers

-- By Rossella Lau
Rossella Lau
Lecture 5, DCO20105, Semester A,2005-6
Deque
 Doubly-ended-queue
allows objects to be added to the front or back efficiently
 compromises the advantages and disadvantages of list and
vector

 An

example of deque: Deque.h
a linked list of blocks which are arrays (or vectors)
……
Rossella Lau
……
……
……
Lecture 5, DCO20105, Semester A,2005-6
The example structure of DequeBlock
 DequeBlock<T>
similar to Node<T>
 use of a static array is to avoid unwanted array re-size in
STL’s vector

template <class T>
class DequeBlock {
T
elm[BLOCK_SIZE];
friend class Deque<T>;
};
Rossella Lau
Lecture 5, DCO20105, Semester A,2005-6
The example structure of Deque<T>
template <class T>
class Deque {
list<DequeBlock<T>>
blockList;
size_t length;
size_t indexInHead;
size_t indexInTail;
};
use STL’s list which is a doubly linked list
 length stores the number of elements in Deque
 indexInHead and IndexInTail store the first position of the
first block and the last index of the last block

Rossella Lau
Lecture 5, DCO20105, Semester A,2005-6
Constraints of Deque<T>
Middle blocks are full
 The first block usually stores data at its last part while the
last block usually stores data at its beginning part

……
Rossella Lau
……
……
Lecture 5, DCO20105, Semester A,2005-6
Adding elements into deque
……
push_back()
……
push_front()
Rossella Lau
……
……
push_back()
……
push_back()
……
……
……
……
……
Lecture 5, DCO20105, Semester A,2005-6
Identifying an element in deque
……
……
[0]
……
[1] [1+BLOCK_SIZE-1]
posInBlock() for deque[i]
= i – [sizeInHead() +
(previous blocks – 1) * BLOCK_SIZE ]
dequeBlock[pos] is posInBlock() and its
corresponding deque [i]
= sizeInHead() + (n-2) * BLOCK_SIZE +
posInBlock() - 1
Rossella Lau
Lecture 5, DCO20105, Semester A,2005-6
Exercises on using Deque<T>
 Assume
that a deque has the structures as in
Deque.h and the BLOCK_SIZE is 3 (instead of 10
as in the program). Depict the diagrams, or write
down the answer, after each set of the following
operations is performed:





push_back("a"), push_back("b"), push_back("c"), push_back("d"),
push_front("e"), push_front("f"), push_front("g"), push_front("h"),
push_back("i")
identify the value of deque[5] , the block number of the value located, and the
array index of the block
pop_back(), pop_front(), pop_front()
identify the value of deque[5], the block number of the value located, and the
array index of the block.
pop_back(), pop_back(), pop_front(), pop_front(), pop_front()
Rossella Lau
Lecture 5, DCO20105, Semester A,2005-6
Another implementation of deque
 Collin’s
5:82 : An array (map) of blocks with pointers
map
start
yes
true
now
good
love
clear
right
finish
Rossella Lau
Lecture 5, DCO20105, Semester A,2005-6
The iterator class
map
start
yes
true
now
good
right
love
clear
finish
 Collin’s
Rossella Lau
5:85-86: first, last, current, node
Lecture 5, DCO20105, Semester A,2005-6
Sequence containers
 Vector,
 deque
list, and deque are sequence containers
is a compromise of vector and list
Creation on demand for a block to save the overhead of
creation for each node
 Shift operations involve only a block or at most two
blocks for Collin’s structure
 Allow random access and thus allow for efficient search
 It also has the disadvantages of vector and list but they
are limited to a lower degree with some space and
computation overhead

Rossella Lau
Lecture 5, DCO20105, Semester A,2005-6
Container for order re-visit
 Will
“order” use deque as its container better?
Use vector
• A small vector causes resizing for “long” orders
• A large vector results in wasting space for “short” orders
 Using list solves the above problem but creation for each node is
not efficient
 Use deque
• The one in Deque.h avoids resizing the array by simply creating
another block and each block can be defined as a “short” block
• The one in Collin’s is similar with more flexibility in
insertion/removal but more space overhead

Rossella Lau
Lecture 5, DCO20105, Semester A,2005-6
Application considerations
 A typical
order system in a restaurant usually includes
2 containers:
Menu: All the dishes provided in the restaurant. Typical
contents of each item are the dish name and its price.
 Table Order: All the dishes ordered by a table/customer.
Typical contents of a Table Order includes the table id or a
customer name and all the orders of the table. Each order
includes at least the dish name and the quantity.

 Before

analysis, assumptions should be made for
Types of operations and their frequency
Rossella Lau
Lecture 5, DCO20105, Semester A,2005-6
Analysis of using which containers
Define (Make the assumptions) the attributes of
the containers in the following areas:
Elements adding frequency
 Elements removing frequency
 Search frequency
 Update frequency

Rossella Lau
Lecture 5, DCO20105, Semester A,2005-6
Comparisons – Storage
Deque
Vector (or array)
List
For Collin’s
Unused slots are wasted
structure, some space
in the front/back
blocks is wasted
Efficient because of
create on demand
Overhead is required No overhead for each
for links or the class element
of Iterator
A pointer is the
overhead of each
element
Rossella Lau
Lecture 5, DCO20105, Semester A,2005-6
Comparisons – Adding/Removing elements
Deque
Vector (or array)
List
Efficient for both
adding/removing at
the end or at the
front
Efficient when appending
while there is still room
Adding to the front causes
lots of operations
fast when adding an
element in any position
if the position is set
Insert involves fewer
“shift” operations
than vector that
depend on the size of
a block for Collin’s
structure
Insert may involve a lot of
shift operations which
depend on number of data
Insert/remove causes
only a fixed number of
operations
Rossella Lau
Lecture 5, DCO20105, Semester A,2005-6
Comparisons – Access
Deque
Vector (or array)
List
Random access
Random assess
No random access
Can apply Binary
Search
Can apply Binary search
Can only apply linear
search
Traversal is in
between Vector
and List
Traversal is simple and
efficient since data are stored
contiguous
Traversal is not
complicated but a bit
slower than vector
Rossella Lau
Lecture 5, DCO20105, Semester A,2005-6
Comparisons – Memory allocation
Deque
Vector (or array)
List
Actions are required
when a block is full
Actions are only required
when the array needs to
be re-sized
Actions are required for
each add/delete
operation on a list
Size of a new block is
fixed and usually
much smaller than
the one in vector
Size of a new block
depends on number of
data
Size of a new node is
fixed and usually the
size is small
No additional action
is required for
creation of a new
block
Copying of the original
elements is required
No additional action is
required for creation of
a new node
Rossella Lau
Lecture 5, DCO20105, Semester A,2005-6
No appropriate container?
 It
seems that it is difficult to find a suitable
container for “order”
Possible

application re-design?
Consider an "Order" in a local super market, it only
allows for an item to be added to an order and there is
no modification. Although an order may consist of more
than one identical item, the only “append” operation
simplifies the requirements in choosing an efficient
container for the application
Rossella Lau
Lecture 5, DCO20105, Semester A,2005-6
Summary
 Two
typical deque structures are studied: a list of
vector and a map of blocks
 Deque
compromises the advantages and
disadvantages of linked list and array and uses a node
to store an array to reduce the times of using
new/delete
 There
is not a sequence container that can always be
the best for the frequent operations: add, remove,
update, and search
Rossella Lau
Lecture 5, DCO20105, Semester A,2005-6
Reference
 Ford:
6, 9.1-4, Collin: 5.4
 STL online
references

http://www.sgi.com/tech/stl

http://www.cppreference.com/
 Example
programs: Deque.h (v.8)
-- END -Rossella Lau
Lecture 5, DCO20105, Semester A,2005-6