BDE 4.14.0 Production release
|
Provide an in-place double-ended queue of T
values.
T
valuesThis component implements an efficient, in-place, indexable, double-ended queue of T
values, where T
is a templatized, user-defined type. The functionality of a bdlc::Queue
is relatively rich; it is almost a proper superset of a vector, with efficient implementations of front
, back
, pushFront
, and popBack
methods added. However, the queue does not provide a data
method (yielding direct access to the underlying memory), because its internal organization is not array-like.
Typical usage involves pushing (appending) values to the back of the queue, popping (removing) values from the front of the queue, and retrieving (operator[]) values from a specified index; unlike the O[n] runtime cost for an insert(0, v)
, however, a pushFront
has a constant average-case cost.
Note that appending, inserting, removing or pushing (back and front) elements potentially alters the memory address of other element in the queue, and there is no guarantee of contiguous storage of consecutive queued elements.
The logical organization of an indexable, in-place, double-ended bdlc::Queue
object q
is shown below, along with an illustration of some of its most common methods:
The following characterizes the performance of representative operations using big-oh notation, O[f(N,M)], where the names N
and M
also refer to the number of respective elements in each container (i.e., its length
). Here the average case, A[f(N)], is the amortized cost, which is defined as the cost of N
successive invocations of the operation divided by N
.
This section illustrates intended use of this component.
The following snippets of code illustrate how to create and use a queue. First, create an empty bdlc::Queue<double>
q
and populate it with two elements E1
and E2
.
Now, pop the first element (E1
) from q
and push the same value to the front of the queue.
Then, pop the last element (E2
) from the back of q
and push a new value E3
at the end of the queue.
Now, assign E2
to the first element (index position 0) of q
.
Then, insert a new value in the middle (index position 1) of q
.
Next, iterate over the elements in q
, printing them in increasing order of their index positions, [0 .. q.length() - 1]
,
which produces the following output on stdout
:
Finally, remove the elements from queue q
.
Note that, in general, specifying index positions greater than or equal to length() will result in undefined behavior.