1 /*--------------------------------------------------------------------------
2   Project:  HorizonSDK
3   File:     rdt_Deque.h
4   Copyright 2009 Nintendo. All rights reserved.
5   These coded instructions, statements, and computer programs contain
6   proprietary information of Nintendo of America Inc. and/or Nintendo
7   Company Ltd., and are protected by Federal copyright law. They may
8   not be disclosed to third parties or copied or duplicated in any form,
9   in whole or in part, without the prior written consent of Nintendo.
10   $Date:: 2010-05-22#$
11   $Rev: 16612 $
12   $Author: hiratsu_daisuke $
13  *-------------------------------------------------------------------------
14 
15 
16 */
17 
18 #include "stdafx.h"
19 
20 #ifndef NN_RDT_DEQUE_H_
21 #define NN_RDT_DEQUE_H_
22 
23 #include "rdt_Utility.h"
24 
25 namespace nn { namespace rdt { namespace CTR {
26 
27 /* Please see man pages for details
28 
29 
30 
31 
32 */
33 template <class T, size_t N>
34 class Deque{
35 public:
36 /* Please see man pages for details
37 
38 */
39     Deque(void);
40 
41 /* Please see man pages for details
42 
43 */
44    ~Deque(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     bool IsFull(void) const;
55 
56 /* Please see man pages for details
57 
58 */
59     size_t Size(void) const;
60 
61 /* Please see man pages for details
62 
63 */
64     void PushBack(const T &value);
65 
66 /* Please see man pages for details
67 
68 */
69     void PopFront(void);
70 
71 /* Please see man pages for details
72 
73 */
74     const T& Front(void) const;
75 
76 /* Please see man pages for details
77 
78 */
79           T& Front(void);
80 
81 /* Please see man pages for details
82 
83 */
84     const T& Back(void) const;
85 
86 /* Please see man pages for details
87 
88 */
89           T& Back(void);
90 
91 /* Please see man pages for details
92 
93 */
94     const T& At(size_t n) const;
95 
96 /* Please see man pages for details
97 
98 */
99     void Clear(void);
100 
101 private:
102 /* Please see man pages for details
103 
104 */
105     Deque           (const Deque&);
106 
107 /* Please see man pages for details
108 
109 */
110     Deque& operator=(const Deque&);
111 
112     // Lower numbers are on the start-side.
113     T m_array[N];
114     size_t m_front;  // Index indicating start elements
115     size_t m_back;   // Index indicating position when packing from back
116 
117     // If m_front and m_back are equal, the queue is empty.
118 
119 };
120 
121 
122 template <class T, size_t N>
Deque(void)123 Deque<T, N>::Deque(void)
124 {
125     Clear();
126 }
127 
128 
129 // Destructor
130 template <class T, size_t N>
~Deque(void)131 Deque<T, N>::~Deque(void)
132 {
133 }
134 
135 
136 template <class T, size_t N>
IsEmpty(void)137 bool Deque<T, N>::IsEmpty(void) const
138 {
139     return m_front==m_back;
140 }
141 
142 
143 template <class T, size_t N>
IsFull(void)144 bool Deque<T, N>::IsFull(void) const
145 {
146     return (m_back + 1) % N == m_front;
147 }
148 
149 
150 template <class T, size_t N>
Size(void)151 size_t Deque<T, N>::Size(void) const
152 {
153     return (m_back - m_front + N) % N;
154 }
155 
156 
157 template <class T, size_t N>
PushBack(const T & value)158 void Deque<T, N>::PushBack(const T &value)
159 {
160     ASSERTMSG(!IsFull(), "You cannot PushBack() anymore because deque is full.");
161 
162     m_array[m_back] = value;
163     m_back = (m_back + 1) % N;
164 }
165 
166 
167 template <class T, size_t N>
PopFront(void)168 void Deque<T, N>::PopFront(void)
169 {
170     ASSERTMSG(!IsEmpty(), "You attempt to call PopFront(), but deque is empty.");
171 
172     m_front = (m_front + 1) % N;
173 }
174 
175 
176 template <class T, size_t N>
Front(void)177 const T& Deque<T, N>::Front(void) const
178 {
179     ASSERTMSG(!IsEmpty(), "You attempt to call const T& Front(), but deque is empty.");
180 
181     return m_array[m_front];
182 }
183 
184 
185 template <class T, size_t N>
Front(void)186 T& Deque<T, N>::Front(void)
187 {
188     ASSERTMSG(!IsEmpty(), "You attempt to call T& Front(), but deque is empty.");
189 
190     return m_array[m_front];
191 }
192 
193 
194 template <class T, size_t N>
Back(void)195 const T& Deque<T, N>::Back(void) const
196 {
197     ASSERTMSG(!IsEmpty(), "You attempt to call const T& Back(), but deque is empty.");
198 
199     return m_array[(m_back - 1 + N) % N];
200 }
201 
202 
203 template <class T, size_t N>
Back(void)204 T& Deque<T, N>::Back(void)
205 {
206     ASSERTMSG(!IsEmpty(), "You attempt to call T& Back(), but deque is empty.");
207 
208     return m_array[(m_back - 1 + N) % N];
209 }
210 
211 
212 template <class T, size_t N>
At(size_t n)213 const T& Deque<T, N>::At(size_t n) const
214 {
215     ASSERT(n < Size());
216 
217     return m_array[(m_front + n) % N];
218 }
219 
220 
221 template <class T, size_t N>
Clear(void)222 void Deque<T, N>::Clear(void)
223 {
224     m_front = 0;
225     m_back  = 0;
226 }
227 
228 }}} // namespace nn::rdt::CTR
229 
230 #endif  // end of NN_RDT_DEQUE_H_
231