Rossella Lau Lecture 5, DCO20105, Semester A, DCO 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, 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, The example structure of DequeBlock DequeBlock similar to Node use of a static array is to avoid unwanted array re-size in STL’s vector template class DequeBlock { T elm[BLOCK_SIZE]; friend class Deque ; };
Rossella Lau Lecture 5, DCO20105, Semester A, The example structure of Deque 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 template class Deque { list > blockList; size_t length; size_t indexInHead; size_t indexInTail; };
Rossella Lau Lecture 5, DCO20105, Semester A, Constraints of Deque 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, Adding elements into deque push_back() …… push_back() …… push_front()
Rossella Lau Lecture 5, DCO20105, Semester A, 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, Exercises on using Deque 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, Another implementation of deque Collin’s 5:82 : An array (map) of blocks with pointers
Rossella Lau Lecture 5, DCO20105, Semester A, The iterator class Collin’s 5:85-86: first, last, current, node
Rossella Lau Lecture 5, DCO20105, Semester A, Sequence containers Vector, list, and deque are sequence containers deque 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, 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, 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, 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, Comparisons – Storage DequeVector (or array)List For Collin’s structure, some space in the front/back blocks is wasted Unused slots are wastedEfficient because of create on demand Overhead is required for links or the class of Iterator No overhead for each element A pointer is the overhead of each element
Rossella Lau Lecture 5, DCO20105, Semester A, Comparisons – Adding/Removing elements DequeVector (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, Comparisons – Access DequeVector (or array)List Random accessRandom assessNo random access Can apply Binary Search Can apply Binary searchCan 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, Comparisons – Memory allocation DequeVector (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, 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, 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, Reference Ford: 6, 9.1-4, Collin: 5.4 STL online references Example programs: Deque.h (v.8) -- END --