From c1b6ffe70bd281c6c230fd63fabcaac2aff47514 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 7 Apr 2024 23:18:32 -0700 Subject: feat: initial commit --- chapter8/queue1.template | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 chapter8/queue1.template (limited to 'chapter8/queue1.template') diff --git a/chapter8/queue1.template b/chapter8/queue1.template new file mode 100644 index 0000000..5feee78 --- /dev/null +++ b/chapter8/queue1.template @@ -0,0 +1,55 @@ +// FILE: queue1.template +// TEMPLATE CLASS IMPLEMENTED: queue (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 // Provides assert + +namespace main_savitch_8B +{ + template + const typename queue::size_type queue::CAPACITY; + + template + queue::queue( ) + { + count = 0; + first = 0; + last = CAPACITY - 1; + } + + template + Item queue::front( ) const + // Library facilities used: cassert + { + assert(!empty( )); + return data[first]; + } + + template + void queue::pop( ) + // Library facilities used: cassert + { + assert(!empty( )); + first = next_index(first); + --count; + } + + template + void queue::push(const Item& entry) + // Library facilities used: cassert + { + assert(size( ) < CAPACITY); + last = next_index(last); + data[last] = entry; + ++count; + } + +} -- cgit v1.2.3