Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // File: queue.hpp
- #ifndef _VFN_QUEUE_H_
- #define _VFN_QUEUE_H_
- namespace vfn
- {
- class queueEmpty : public std::exception {};
- class queueFull : public std::exception {};
- template <typename T, unsigned int N>
- class queue
- {
- private:
- T buffer[N];
- unsigned int write;
- unsigned int read;
- bool empty;
- bool full;
- public:
- queue() : write(0),
- read(0),
- empty(true),
- full(false) {}
- T pop() throw(queueEmpty)
- {
- if (empty)
- throw queueEmpty();
- T& tmp = buffer[read++];
- read %= N;
- empty = (read == write);
- full = false;
- return tmp;
- }
- T rpop() throw(queueEmpty)
- {
- if (empty)
- throw queueEmpty();
- write = (write-1 + N) % N;
- T& tmp = buffer[write];
- empty = (read == write);
- full = false;
- return tmp;
- }
- void push(T a) throw(queueFull)
- {
- if (full)
- throw queueFull();
- buffer[write++] = a;
- write %= N;
- full = (read == write);
- empty = false;
- }
- void rpush(T a) throw(queueFull)
- {
- if (full)
- throw queueFull();
- read = (read-1 + N) % N;
- buffer[read] = a;
- full = (read == write);
- empty = false;
- }
- };
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement