Advertisement
Vifon

My queue

Dec 4th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. // File: queue.hpp
  2. #ifndef _VFN_QUEUE_H_
  3. #define _VFN_QUEUE_H_
  4.  
  5. namespace vfn
  6. {
  7.     class queueEmpty : public std::exception {};
  8.     class queueFull  : public std::exception {};
  9.  
  10.     template <typename T, unsigned int N>
  11.     class queue
  12.     {
  13.       private:
  14.         T            buffer[N];
  15.         unsigned int write;
  16.         unsigned int read;
  17.         bool         empty;
  18.         bool         full;
  19.       public:
  20.         queue() : write(0),
  21.                   read(0),
  22.                   empty(true),
  23.                   full(false) {}
  24.  
  25.         T pop() throw(queueEmpty)
  26.         {
  27.             if (empty)
  28.                 throw queueEmpty();
  29.             T& tmp = buffer[read++];
  30.             read  %= N;
  31.             empty  = (read == write);
  32.             full   = false;
  33.  
  34.             return tmp;
  35.         }
  36.         T rpop() throw(queueEmpty)
  37.         {
  38.             if (empty)
  39.                 throw queueEmpty();
  40.             write  = (write-1 + N) % N;
  41.             T& tmp = buffer[write];
  42.             empty  = (read == write);
  43.             full   = false;
  44.  
  45.             return tmp;
  46.         }
  47.         void push(T a) throw(queueFull)
  48.         {
  49.             if (full)
  50.                 throw queueFull();
  51.             buffer[write++] = a;
  52.             write          %= N;
  53.             full            = (read == write);
  54.             empty           = false;
  55.         }
  56.         void rpush(T a) throw(queueFull)
  57.         {
  58.             if (full)
  59.                 throw queueFull();
  60.             read         = (read-1 + N) % N;
  61.             buffer[read] = a;
  62.             full         = (read == write);
  63.             empty        = false;
  64.         }
  65.     };
  66. }
  67.  
  68. #endif
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement