summaryrefslogtreecommitdiff
path: root/chapter8/queue1.template
blob: 5feee782d485b4b5ddd3cccefc6f7c6044c32494 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// FILE: queue1.template
// TEMPLATE CLASS IMPLEMENTED: queue<Item> (see queue1.h for documentation)
// This file is included in the header file, and not compiled separately.
// INVARIANT for the queue class:
//   1. The number of items in the queue is in the member variable count;
//   2. For a non-empty queue, the items are stored in a circular array
//      beginning at data[front] and continuing through data[rear].
//      The array's total capacity of the array is CAPACITY.
//   3. For an empty array, rear is some valid index, and front is
//      always equal to next_index(rear).
//

#include <cassert>  // Provides assert

namespace main_savitch_8B
{
    template <class Item>
    const typename queue<Item>::size_type queue<Item>::CAPACITY;

    template <class Item>
    queue<Item>::queue( )
    {
        count = 0;
        first = 0;
        last = CAPACITY - 1;
    }

    template <class Item>
    Item queue<Item>::front( ) const
    // Library facilities used: cassert
    {
        assert(!empty( ));
        return data[first];
    }

    template <class Item>
    void queue<Item>::pop( )
    // Library facilities used: cassert
    {
        assert(!empty( ));
        first = next_index(first);
        --count;
    }

    template <class Item>
    void queue<Item>::push(const Item& entry)
    // Library facilities used: cassert
    {
        assert(size( ) < CAPACITY);
        last = next_index(last);
        data[last] = entry;
        ++count;
    }

}