Title: Generic Positional Containers and Double-Ended Queues
1Generic Positional Containers and Double-Ended
Queues
- Andy Wang
- Data Structures, Algorithms, and Generic
Programming
2Generic Positional Containers
- A generic container C
- Stores elements by position
- vectori a
- list.insert(I, a)
- A template class for a proper type T
- CltTgt is a proper type
- CltTgt is capable of storing a number of T objs
- CltTgt supports an iterater class CltTgtIterator
3More on Generic pContainers
- Organized and accessed by position
- Can insert any T object at any position in CltTgt
- Can remove any T object at any position in CltTgt
- Can support
- PushFront(), PopFront(), Front()
- PushBack(), PopBack(), Back()
4Traditional Double-Ended Queue Class
- Deque (pronounced deck)
- Deque operations
- Push/Pop at either end
- Retrieve data from either end
- Proper type
5Traditional Double-Ended Queue Class (2)
- Assumptions on element type T (proper type)
- Constructor T() and destructor T()
- Copy constructor
- Assignment operator
6Specifying TDequeltTgt
- Traditional assumptions, plus
- O(1) average runtime, O(Size()) space
- PushFront(t), PopFront(), Front()
- PushBack(t), PopBack(), Back()
- O(1) time and space for iterator opeartions
- Random access iterators (, pointer arithmetics)
7TDequeltTgt Implementation Plan
- Circular array
- Protected array content of size content_size
- Illusion contentcontent_size content0
8TDequeltTgt D Illustrated
- content_size 8
- D.Empty() true
0 1 2 3 4 5 6 7
content
9TDequeltchargt D Illustrated (2)
- content_size 8
- D.PushBack(M)
0 1 2 3 4 5 6 7
M
content
10TDequeltchargt D Illustrated (3)
- content_size 8
- D.PushBack(e)
0 1 2 3 4 5 6 7
M e
content
11TDequeltchargt D Illustrated (4)
- content_size 8
- D.PushBack(r)
0 1 2 3 4 5 6 7
M e r
content
12TDequeltchargt D Illustrated (5)
- content_size 8
- D.PushBack(r)
0 1 2 3 4 5 6 7
M e r r
content
13TDequeltchargt D Illustrated (6)
- content_size 8
- D.PushBack(y)
0 1 2 3 4 5 6 7
M e r r y
content
14TDequeltchargt D Illustrated (7)
- content_size 8
- D.PopFront()
- O(1)
0 1 2 3 4 5 6 7
e r r y
content
15TDequeltchargt D Illustrated (8)
- content_size 8
- D.PopFront()
0 1 2 3 4 5 6 7
r r y
content
16TDequeltchargt D Illustrated (9)
- content_size 8
- D.PushBack(G)
0 1 2 3 4 5 6 7
r r y G
content
17TDequeltchargt D Illustrated (10)
- content_size 8
- D.PushBack(o)
- D.Size() (7 2 8) 8
0 1 2 3 4 5 6 7
r r y G o
content
18TDequeltchargt D Illustrated (11)
- content_size 8
- D.PushBack(A)
- D.Size() (0 2 8) 8
0 1 2 3 4 5 6 7
r r y G o A
content
19TDequeltchargt D Illustrated (12)
- content_size 8
- D.PushBack(r)
0 1 2 3 4 5 6 7
r r r y G o A
content
20TDequeltchargt D Illustrated (13)
- D.Size() content_size 1
- Return full or
- Double the capacity
0 1 2 3 4 5 6 7
r r r y G o A
content
21TDequeltTgt Implementation Plan (2)
- Relative Indexing
- Protected integers begin, end
- Element position relative to begin
- Front element is contentbegin
- Back element is contentend 1
- Size is (end begin content_size)
content_size
22TDequeltTgt Implementation Plan (3)
- Class Bracket Operator
- Similar to TVector
- Distinguished from TDeque Iterator bracket
operation
23TDequeltTgtIterator Implementation Plan
- Public interface
- Start with the public interface of
TListltTgtIterator - Add bracket operator
- Add pointer arithmetic
- Protected data
- Pointer to a specific TDequeltTgt object
- A deque index value
24Defining TDequeltTgt
- template lttypename Tgt
- class TDeque
- public
- typedef T value_type // type definitions
- typedef TDequeIteratorltTgt Iterator
- TDeque() // constructors and deconstructor
- TDeque(size_t, const T)
- TDeque(const TDequeltTgt)
- TDeque()
- // display functions
- void Display(ostream os, char ofc \0)
const - void Dump(ostream os) const
25Defining TDequeltTgt (2)
- int Empty() const // container read-only
routines - size_t Size() const
- T Front() const
- T Back() const
- T operator (size_t) const
- int PushFront(const T) // container write
routines - int PopFront()
- int PushBack(const T)
- int PopBack()
- TDequeltTgt operator(const TDequeltTgt)
26Defining TDequeltTgt (3)
- friend class TDequeIteratorltTgt // iterator
support - Iterator Begin() const
- Iterator End() const
-
- protected
- T content
- size_t content_size, begin, end
-
27Defining TDequeltTgt (4)
- // operator overloads (friend status not
required) - templateltclass Tgt
- ostream operatorltlt(ostream os, const TDequeltTgt
a) - templateltclass Tgt
- int operator(const TDequeltTgt, const
TDequeltTgt) - templateltclass Tgt
- int operator!(const TDequeltTgt, const
TDequeltTgt)
28Defining TDequeIteratorltTgt
- template lttypename Tgt
- class TDequeIterator
- friend class TDequeltTgt
- public
- typedef T value_type // terminology support
- TDequeIterator() // constructors
- TDequeIterator(const TDequeltTgt I)
- TDequeIterator(const TDequeIteratorltTgt I)
- TDequeIterator(const size_t i)
- T Retrieve() const // return ptr to current
Tval - int Valid() const // cursor is valid element
29Defining TDequeIteratorltTgt (2)
- // various operators
- int operator(const TDequeIteratorltTgt I2)
const - int operator!(const TDequeIteratorltTgt I2)
const - T operator() const // return reference to
current Tval - T operator (size_t i) const // return ref
to Tval at index - TDequeIteratorltTgt operator(const
TDequeIteratorltTgt I) - TDequeIteratorltTgt operator() // prefix
- TDequeIteratorltTgt operator(int) // postfix
- TDequeIteratorltTgt operator--() // prefix
- TDequeIteratorltTgt operator--(int) // postfix
-
30Defining TDequeIteratorltTgt (3)
- // pointer arithmetic
- long operator-(const TDequeIteratorltTgt I2)
const - TDequeIteratorltTgt operator(long n)
- TDequeIteratorltTgt operator-(long n)
- TDequeIteratorltTgt operator(long n) const
- TDequeIteratorltTgt operator(int n)
- TDequeIteratorltTgt operator-(int n)
- TDequeIteratorltTgt operator(int n) const
- TDequeIteratorltTgt operator(unsigned long n)
- TDequeIteratorltTgt operator-(unsigned long n)
- TDequeIteratorltTgt operator(unsigned long n)
const - TDequeIteratorltTgt operator(unsigned int n)
- TDequeIteratorltTgt operator-(unsigned int n)
- TDequeIteratorltTgt operator(unsigned int n)
const -
31Defining TDequeIteratorltTgt (3)
- protected
- const TDequeltTgt Qptr
- size_t index
-
32Implementing TDequeltTgt
- Default constructor
- template lttypename Tgt
- TDequeltTgtTDeque() content(0), begin(0),
end(0), content_size(0) - content new Tdefault_content_size
- if (content 0)
- // error
-
- content_size default_content_size
-
33Implementing TDequeltTgt (2)
- Copy constructor
- template lttypename Tgt
- TDequeltTgtTDeque(const TDequeltTgt Q)
content_size(Q.content_size), begin(Q.begin),
end(Q.end) - content new Tcontent_size
- if (content 0)
- // error
-
- for (size_t j 0 j lt content_size j)
- contentj Q.contentj
-
-
34Implementing TDequeltTgt (3)
- Read-only functions
- template lttypename Tgt
- size_t TDequeltTgtSize() const
- return (end begin content_size)
content_size -
- template lttypename Tgt
- T TDequeltTgtoperator (size_t i) const
- if (Size() lt i)
- // error
-
- return (i begin) content_size
-
35Implementing TDequeltTgt (4)
- Display functions
- template lttypename Tgt
- void TDequeltTgtDisplay(ostream os, char ofc)
const - for (size_t j 0 j lt Size() j)
- os ltlt operator(j)
- if (ofc ! \0)
- os ltlt ofc
-
-
-
- template lttypename Tgt
- void TDequeltTgtDump(ostream os) const
- for (size_t j 0 j lt content_size j) //
print -
36Implementing TDequeltTgt (5)
- Read-only operator overloads
- template lttypename Tgt
- ostream operatorltlt(ostream os, const TDequeltTgt
Q) - Q.Display(os)
- return(os)
-
- template lttypename Tgt
- int operator(const TDequeltTgt Q1, const
TDequeltTgt Q2) - if (Q1.Size() ! Q2.Size()) return 0
- for (size_t j 0 j lt Q1.Size() j)
- if (Q1j ! Q2j) return 0
-
- return 1
37Implementing TDequeltTgt (6)
- Read-only operator overloads
- template lttypename Tgt
- int operator!(const TDequeltTgt Q1, const
TDequeltTgt Q2) - return !(Q1 Q2)
38Implementing TDequeltTgt (7)
- Read-only functions
- template lttypename Tgt
- int TDequeltTgtEmpty() const
- return begin end
-
- template lttypename Tgt
- void TDequeltTgtClear()
- begin end 0
-
39Implementing TDequeltTgt (8)
- Read-only functions
- template lttypename Tgt
- T TDequeltTgtFront() const
- // check for empty TDeque
- return contentbegin
-
- template lttypename Tgt
- T TDequeltTgtBack() const
- // check for empty TDeque
- return (end 1 content_size) content_size
40Implementing TDequeltTgt (9)
- Iterator support
- template lttypename Tgt
- TDequeIteratorltTgt TDequeltTgtBegin() const
- TDequeltTgtIterator I
- I.Qptr this
- I.index 0
- return I
-
- template lttypename Tgt
- TDequeIteratorltTgt TDequeltTgtEnd() const
- TDequeltTgtIterator I
- I.Qptr this
- I.index Size()
- return I
41Implementing TDequeltTgt (10)
- Assignment
- template lttypename Tgt
- TDequeltTgt TDequeltTgtoperator(const TDequeltTgt
Q) - if (this ! Q)
- T newcontent new TQ.content_size
- // check for allocation
- delete content
- content newcontent
- content_size Q.content_size
- begin Q.begin
- end Q.end
- // copy queue elements
-
- return this
42Implementing TDequeltTgt (11)
- PushBack
- template lttypename Tgt
- int TDequeltTgtPushBack(const T Tval)
- if (Size() 1 gt content_size) // deque is
full - unsigned j, k
- size_t newcontent_size 2 content_size
- if (content_size 0) newcontent_size 2
- T newcontent new Tnewcontent_size
- // check for allocation error
- for (j k begin
- j ! end
- j (j 1) content_size, k)
- newcontentk contentj
-
-
43Implementing TDequeltTgt (12)
- PushBack
- if (begin lt end) begin content_size
- delete content
- content newcontent
- content_size newcontent_size
-
- contentend Tval
- end (end 1) content_size
- return 1
44Implementing TDequeltTgt (13)
- PushFront
- template lttypename Tgt
- int TDequeltTgtPushFront(const T Tval)
- if (Size() 1 gt content_size) // deque is
full - unsigned j, k
- size_t newcontent_size 2 content_size
- if (content_size 0) newcontent_size 2
- T newcontent new Tnewcontent_size
- // check for allocation error
- for (j k begin
- j ! end
- j (j 1) content_size, k)
- newcontentk contentj
-
-
45Implementing TDequeltTgt (14)
- PushFront
- if (begin lt end) begin content_size
- delete content
- content newcontent
- content_size newcontent_size
-
- begin (begin 1 content_size)
content_size - contentbegin Tval
- return 1
46Implementing TDequeltTgt (15)
- Pop routines
- template lttypename Tgt
- int TDequeltTgtPopFront()
- if (begin end) return 0
- begin (begin 1) content_size
- return 1
-
- template lttypename Tgt
- int TDequeltTgtPopBack()
- if (begin end) return 0
- end (end 1 content_size) content_size
- return 1
47Implementing TDequeIteratorltTgt
- Constructors
- template lttypename Tgt
- TDequeIteratorltTgtTDequeIterator() Qptr(0),
index(0) - template lttypename Tgt
- TDequeIteratorltTgtTDequeIterator(const
TDequeltTgt Q) Qptr(Q), index(0) - template lttypename Tgt
- TDequeIteratorltTgtTDequeIterator(const
TDequeIteratorltTgt I) Qptr(I.Qptr),
index(I.index)
48Implementing TDequeIteratorltTgt (2)
- Initialization routines
- template lttypename Tgt
- void TDequeIteratorltTgtInitialize(const
TDequeltTgt Q) - Qptr Q
- index 0
-
- template lttypename Tgt
- void TDequeIteratorltTgtrInitialize(const
TDequeltTgt Q) - Qptr Q
- index Q.Size() 1
49Implementing TDequeIteratorltTgt (3)
- Helper functions
- template lttypename Tgt
- int TDequeIteratorltTgtValid() const
- if (Qptr 0) return 0
- if (index gt Qptr-gtSize()) return 0
- return 1
-
- template lttypename Tgt
- T TDequeIteratorltTgtoperator (size_t i) const
- if (!Qptr) // error
- return Qptr-gtoperator(index i)
-
50Implementing TDequeIteratorltTgt (4)
- Helper functions
- template lttypename Tgt
- T TDequeIteratorltTgtRetrieve() const
- // check for validity
- return Qptr-gtoperator(index)
-
- template lttypename Tgt
- T TDequeIteratorltTgtoperator () const
- if (Qptr 0) // error
- if (Qptr-gtSize() 0) // error
- return Qptr-gtoperator(index)
-
51Implementing TDequeIteratorltTgt (5)
- Comparators
- template lttypename Tgt
- int TDequeIteratorltTgtoperator(const
TDequeIteratorltTgt I2) const - if (Qptr ! I2.Qptr) return 0
- if (index ! I2.index) return 0
- return 1
-
- template lttypename Tgt
- int TDequeIteratorltTgtoperator!(const
TDequeIteratorltTgt I2) const - return !(this I2)
-
52Implementing TDequeIteratorltTgt (6)
- Assignment
- template lttypename Tgt
- TDequeIteratorltTgt TDequeIteratorltTgtoperator(co
nst TDequeIteratorltTgt I) - if (this ! I)
- Qptr I.Qptr
- index I.index
-
- return this
53Implementing TDequeIteratorltTgt (7)
- Various operators
- template lttypename Tgt
- TDequeIteratorltTgt TDequeIteratorltTgtoperator()
- index
- return this
-
- template lttypename Tgt
- TDequeIteratorltTgt TDequeIteratorltTgtoperator(in
t) - TDequeIteratorltTgt I(this)
- operator ()
- return I
54Implementing TDequeIteratorltTgt (8)
- Various operators
- template lttypename Tgt
- TDequeIteratorltTgt TDequeIteratorltTgtoperator--()
- --index
- return this
-
- template lttypename Tgt
- TDequeIteratorltTgt TDequeIteratorltTgtoperator--(in
t) - TDequeIteratorltTgt I(this)
- operator --()
- return I
55Implementing TDequeIteratorltTgt (9)
- Various operators
- template lttypename Tgt
- long TDequeIteratorltTgtoperator-(const
TDequeIteratorltTgt I2) const - return index I2.index
-
- template lttypename Tgt
- TDequeIteratorltTgt TDequeIteratorltTgtoperator(lon
g n) const - TDequeIteratorltTgt I(this)
- return I n
56Implementing TDequeIteratorltTgt (10)
- Various operators
- template lttypename Tgt
- TDequeIteratorltTgt TDequeIteratorltTgtoperator(l
ong n) - index n
- return this
-
- template lttypename Tgt
- TDequeIteratorltTgt TDequeIteratorltTgtoperator-(l
ong n) - index - n
- return this
-