00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(_XRB_CIRCULARQUEUE_HPP_)
00012 #define _XRB_CIRCULARQUEUE_HPP_
00013
00014 #include "xrb.hpp"
00015
00016 namespace Xrb
00017 {
00018
00031 template <typename T, Uint32 queue_size>
00032 class CircularQueue
00033 {
00034 public:
00035
00039 CircularQueue ();
00042 ~CircularQueue ();
00043
00047 T const &Entry (Uint32 const index) const;
00051 inline Uint32 EntryCount () const { return m_entry_count; }
00054 inline bool IsEmpty () const { return m_entry_count == 0; }
00057 inline bool IsFull () const { return m_entry_count == queue_size; }
00058
00062 void Enqueue (T const &entry);
00070 T Dequeue ();
00075 void Clear ();
00076
00077 private:
00078
00079 inline Uint32 IncrementedIndex (Uint32 const index)
00080 {
00081 return (index + 1) % queue_size;
00082 }
00083
00084
00085 T m_queue[queue_size];
00086
00087 Uint32 m_head;
00088
00089 Uint32 m_tail;
00090
00091 Uint32 m_entry_count;
00092 };
00093
00094 template <typename T, Uint32 queue_size>
00095 CircularQueue<T, queue_size>::CircularQueue ()
00096 {
00097 ASSERT1(queue_size > 0);
00098 m_head = 0;
00099 m_tail = 0;
00100 m_entry_count = 0;
00101 }
00102
00103 template <typename T, Uint32 queue_size>
00104 CircularQueue<T, queue_size>::~CircularQueue ()
00105 {
00106 m_head = 0;
00107 m_tail = 0;
00108 m_entry_count = 0;
00109 }
00110
00111 template <typename T, Uint32 queue_size>
00112 T const &CircularQueue<T, queue_size>::Entry (Uint32 const index) const
00113 {
00114 ASSERT1(index < EntryCount());
00115 return m_queue[(m_head + index) % queue_size];
00116 }
00117
00118 template <typename T, Uint32 queue_size>
00119 void CircularQueue<T, queue_size>::Enqueue (T const &entry)
00120 {
00121 ASSERT0(!IsFull());
00122
00123
00124 m_queue[m_tail] = entry;
00125
00126 m_tail = IncrementedIndex(m_tail);
00127
00128 ++m_entry_count;
00129 }
00130
00131 template <typename T, Uint32 queue_size>
00132 T CircularQueue<T, queue_size>::Dequeue ()
00133 {
00134 ASSERT0(!IsEmpty());
00135
00136
00137 Uint32 retval_index = m_head;
00138
00139 m_head = IncrementedIndex(m_head);
00140
00141 --m_entry_count;
00142
00143
00144 return m_queue[retval_index];
00145 }
00146
00147 template <typename T, Uint32 queue_size>
00148 void CircularQueue<T, queue_size>::Clear ()
00149 {
00150
00151 m_head = 0;
00152
00153 m_tail = 0;
00154
00155 m_entry_count = 0;
00156 }
00157
00158 }
00159
00160 #endif // !defined(_XRB_CIRCULARQUEUE_HPP_)
00161