1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     rdt_Queue.h
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  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   $Rev: 46347 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "stdafx.h"
17 
18 #ifndef NN_LIBRARIES_RDT_CTR_RDT_QUEUE_H_
19 #define NN_LIBRARIES_RDT_CTR_RDT_QUEUE_H_
20 
21 #include "rdt_Utility.h"
22 
23 namespace nn { namespace rdt { namespace CTR {
24 
25 
26 /* Please see man pages for details
27 
28 
29 
30 */
31 template <class T, size_t N>
32 class Queue{
33 public:
34 /* Please see man pages for details
35 
36 */
37     Queue(void);
38 
39 /* Please see man pages for details
40 
41 */
42    ~Queue(void);
43 
44 /* Please see man pages for details
45 
46 */
47     bool IsEmpty(void) const;
48 
49 /* Please see man pages for details
50 
51 */
52     size_t Size(void) const;
53 
54 /* Please see man pages for details
55 
56 */
57     void Push(const T &value);
58 
59 /* Please see man pages for details
60 
61 */
62     void Pop(void);
63 
64 /* Please see man pages for details
65 
66 */
67     const T& Front(void) const;
68 
69 /* Please see man pages for details
70 
71 */
72     const T& Back(void) const;
73 
74 /* Please see man pages for details
75 
76 */
77     void Clear(void);
78 
79 /* Please see man pages for details
80 
81 */
82     void PrintDebugInfo(void) const;
83 
84 private:
85 /* Please see man pages for details
86 
87 */
88     Queue           (const Queue&);
89 
90 /* Please see man pages for details
91 
92 */
93     Queue& operator=(const Queue&);
94 
95     // The earlier numbers are at the start.
96     T m_array[N];
97     size_t m_front;  // Index indicating the start element.
98     size_t m_back;   // Index indicating the position when filling the array.
99 
100     // When m_front and m_back are equal, the queue is "empty."
101 };
102 
103 template <class T, size_t N>
Queue(void)104 Queue<T, N>::Queue(void)
105 {
106     Clear();
107 }
108 
109 // Destructor
110 template <class T, size_t N>
~Queue(void)111 Queue<T, N>::~Queue(void)
112 {
113 }
114 
115 
116 template <class T, size_t N>
IsEmpty(void)117 bool Queue<T, N>::IsEmpty(void) const
118 {
119     return m_front==m_back;
120 }
121 
122 
123 template <class T, size_t N>
Size(void)124 size_t Queue<T, N>::Size(void) const
125 {
126     return (m_back - m_front + N) % N;
127 }
128 
129 
130 template <class T, size_t N>
Push(const T & value)131 void Queue<T, N>::Push(const T &value)
132 {
133     ASSERTMSG((m_back + 1) % N != m_front, "You can not Push() anymore because queue is full.");
134 
135     m_array[m_back] = value;
136     m_back = (m_back + 1) % N;
137 }
138 
139 
140 template <class T, size_t N>
Pop(void)141 void Queue<T, N>::Pop(void)
142 {
143     ASSERTMSG(!IsEmpty(), "You attempted to execute Pop(), but the queue is empty.");
144 
145     m_front = (m_front + 1) % N;
146 }
147 
148 
149 template <class T, size_t N>
Front(void)150 const T& Queue<T, N>::Front(void) const
151 {
152     return m_array[m_front];
153 }
154 
155 
156 template <class T, size_t N>
Back(void)157 const T& Queue<T, N>::Back(void) const
158 {
159     return m_array[(m_back - 1 + N) % N];
160 }
161 
162 
163 template <class T, size_t N>
Clear(void)164 void Queue<T, N>::Clear(void)
165 {
166     m_front = 0;
167     m_back = 0;
168 }
169 
170 
171 // If T is not a basic type, then this PrintDebugInfo function will not work
172 template <class T, size_t N>
PrintDebugInfo(void)173 void Queue<T, N>::PrintDebugInfo(void) const
174 {
175     if(IsEmpty())
176     {
177         LOG("Queue is empty.\n");
178     }
179     else
180     {
181         for(u32 i=0; i<Size(); ++i)
182         {
183             LOG("[%d] : %d\n", i, m_array[(m_front + i) % N]);
184         }
185     }
186 }
187 
188 }}} // namespace nn::rdt::CTR
189 
190 #endif  // end of NN_LIBRARIES_RDT_CTR_RDT_QUEUE_H_
191