1 /*--------------------------------------------------------------------------
2   Project:  HorizonSDK
3   File:     rdt_Queue.h
4 
5   Copyright 2009 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2010-04-30#$
14   $Rev: 15289 $
15   $Author: hiratsu_daisuke $
16  *-------------------------------------------------------------------------*/
17 
18 #include "stdafx.h"
19 
20 #ifndef NN_RDT_QUEUE_H_
21 #define NN_RDT_QUEUE_H_
22 
23 #include "rdt_Utility.h"
24 
25 namespace nn { namespace rdt { namespace CTR {
26 
27 
28 /* Please see man pages for details
29 
30 
31 
32 */
33 template <class T, size_t N>
34 class Queue{
35 public:
36 /* Please see man pages for details
37 
38 */
39     Queue(void);
40 
41 /* Please see man pages for details
42 
43 */
44    ~Queue(void);
45 
46 /* Please see man pages for details
47 
48 */
49     bool IsEmpty(void) const;
50 
51 /* Please see man pages for details
52 
53 */
54     size_t Size(void) const;
55 
56 /* Please see man pages for details
57 
58 */
59     void Push(const T &value);
60 
61 /* Please see man pages for details
62 
63 */
64     void Pop(void);
65 
66 /* Please see man pages for details
67 
68 */
69     const T& Front(void) const;
70 
71 /* Please see man pages for details
72 
73 */
74     const T& Back(void) const;
75 
76 /* Please see man pages for details
77 
78 */
79     void Clear(void);
80 
81 /* Please see man pages for details
82 
83 */
84     void PrintDebugInfo(void) const;
85 
86 private:
87 /* Please see man pages for details
88 
89 */
90     Queue           (const Queue&);
91 
92 /* Please see man pages for details
93 
94 */
95     Queue& operator=(const Queue&);
96 
97     // Lower numbers are on the start-side.
98     T m_array[N];
99     size_t m_front;  // Index indicating start elements
100     size_t m_back;   // Index indicating position when packing from back
101 
102     // If m_front and m_back are equal, the queue is "empty."
103 };
104 
105 template <class T, size_t N>
Queue(void)106 Queue<T, N>::Queue(void)
107 {
108     Clear();
109 }
110 
111 // Destructor
112 template <class T, size_t N>
~Queue(void)113 Queue<T, N>::~Queue(void)
114 {
115 }
116 
117 
118 template <class T, size_t N>
IsEmpty(void)119 bool Queue<T, N>::IsEmpty(void) const
120 {
121     return m_front==m_back;
122 }
123 
124 
125 template <class T, size_t N>
Size(void)126 size_t Queue<T, N>::Size(void) const
127 {
128     return (m_back - m_front + N) % N;
129 }
130 
131 
132 template <class T, size_t N>
Push(const T & value)133 void Queue<T, N>::Push(const T &value)
134 {
135     ASSERTMSG((m_back + 1) % N != m_front, "You can not Push() anymore because queue is full.");
136 
137     m_array[m_back] = value;
138     m_back = (m_back + 1) % N;
139 }
140 
141 
142 template <class T, size_t N>
Pop(void)143 void Queue<T, N>::Pop(void)
144 {
145     ASSERTMSG(!IsEmpty(), "You attempted to execute Pop(), but queue is empty.");
146 
147     m_front = (m_front + 1) % N;
148 }
149 
150 
151 template <class T, size_t N>
Front(void)152 const T& Queue<T, N>::Front(void) const
153 {
154     return m_array[m_front];
155 }
156 
157 
158 template <class T, size_t N>
Back(void)159 const T& Queue<T, N>::Back(void) const
160 {
161     return m_array[(m_back - 1 + N) % N];
162 }
163 
164 
165 template <class T, size_t N>
Clear(void)166 void Queue<T, N>::Clear(void)
167 {
168     m_front = 0;
169     m_back = 0;
170 }
171 
172 
173 // If T is not a basic type, then this PrintDebugInfo function will not work
174 template <class T, size_t N>
PrintDebugInfo(void)175 void Queue<T, N>::PrintDebugInfo(void) const
176 {
177     if(IsEmpty())
178     {
179         LOG("Queue is empty.\n");
180     }
181     else
182     {
183         for(u32 i=0; i<Size(); ++i)
184         {
185             LOG("[%d] : %d\n", i, m_array[(m_front + i) % N]);
186         }
187     }
188 }
189 
190 }}} // namespace nn::rdt::CTR
191 
192 #endif  // end of NN_RDT_QUEUE_H_
193