1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     rdt_Deque.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_RDT_DEQUE_H_
19 #define NN_LIBRARIES_RDT_RDT_DEQUE_H_
20 
21 #include "rdt_Utility.h"
22 
23 namespace nn { namespace rdt { namespace CTR {
24 
25 /* Please see man pages for details
26 
27 
28 
29 
30 */
31 template <class T, size_t N>
32 class Deque{
33 public:
34 /* Please see man pages for details
35 
36 */
37     Deque(void);
38 
39 /* Please see man pages for details
40 
41 */
42    ~Deque(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     bool IsFull(void) const;
53 
54 /* Please see man pages for details
55 
56 */
57     size_t Size(void) const;
58 
59 /* Please see man pages for details
60 
61 */
62     void PushBack(const T &value);
63 
64 /* Please see man pages for details
65 
66 */
67     void PopFront(void);
68 
69 /* Please see man pages for details
70 
71 */
72     const T& Front(void) const;
73 
74 /* Please see man pages for details
75 
76 */
77           T& Front(void);
78 
79 /* Please see man pages for details
80 
81 */
82     const T& Back(void) const;
83 
84 /* Please see man pages for details
85 
86 */
87           T& Back(void);
88 
89 /* Please see man pages for details
90 
91 */
92     const T& At(size_t n) const;
93 
94 /* Please see man pages for details
95 
96 */
97     void Clear(void);
98 
99 private:
100 /* Please see man pages for details
101 
102 */
103     Deque           (const Deque&);
104 
105 /* Please see man pages for details
106 
107 */
108     Deque& operator=(const Deque&);
109 
110     // The earlier numbers are at the start.
111     T m_array[N];
112     size_t m_front;  // Index indicating the start element.
113     size_t m_back;   // Index indicating the position when filling the array.
114 
115     // If m_front and m_back are equal, the queue is empty.
116 
117 };
118 
119 
120 template <class T, size_t N>
Deque(void)121 Deque<T, N>::Deque(void)
122 {
123     Clear();
124 }
125 
126 
127 // Destructor
128 template <class T, size_t N>
~Deque(void)129 Deque<T, N>::~Deque(void)
130 {
131 }
132 
133 
134 template <class T, size_t N>
IsEmpty(void)135 bool Deque<T, N>::IsEmpty(void) const
136 {
137     return m_front==m_back;
138 }
139 
140 
141 template <class T, size_t N>
IsFull(void)142 bool Deque<T, N>::IsFull(void) const
143 {
144     return (m_back + 1) % N == m_front;
145 }
146 
147 
148 template <class T, size_t N>
Size(void)149 size_t Deque<T, N>::Size(void) const
150 {
151     return (m_back - m_front + N) % N;
152 }
153 
154 
155 template <class T, size_t N>
PushBack(const T & value)156 void Deque<T, N>::PushBack(const T &value)
157 {
158     ASSERTMSG(!IsFull(), "You cannot PushBack() anymore because deque is full.");
159 
160     m_array[m_back] = value;
161     m_back = (m_back + 1) % N;
162 }
163 
164 
165 template <class T, size_t N>
PopFront(void)166 void Deque<T, N>::PopFront(void)
167 {
168     ASSERTMSG(!IsEmpty(), "You attempt to call PopFront(), but deque is empty.");
169 
170     m_front = (m_front + 1) % N;
171 }
172 
173 
174 template <class T, size_t N>
Front(void)175 const T& Deque<T, N>::Front(void) const
176 {
177     ASSERTMSG(!IsEmpty(), "You attempt to call const T& Front(), but deque is empty.");
178 
179     return m_array[m_front];
180 }
181 
182 
183 template <class T, size_t N>
Front(void)184 T& Deque<T, N>::Front(void)
185 {
186     ASSERTMSG(!IsEmpty(), "You attempt to call T& Front(), but deque is empty.");
187 
188     return m_array[m_front];
189 }
190 
191 
192 template <class T, size_t N>
Back(void)193 const T& Deque<T, N>::Back(void) const
194 {
195     ASSERTMSG(!IsEmpty(), "You attempt to call const T& Back(), but deque is empty.");
196 
197     return m_array[(m_back - 1 + N) % N];
198 }
199 
200 
201 template <class T, size_t N>
Back(void)202 T& Deque<T, N>::Back(void)
203 {
204     ASSERTMSG(!IsEmpty(), "You attempt to call T& Back(), but deque is empty.");
205 
206     return m_array[(m_back - 1 + N) % N];
207 }
208 
209 
210 template <class T, size_t N>
At(size_t n)211 const T& Deque<T, N>::At(size_t n) const
212 {
213     ASSERT(n < Size());
214 
215     return m_array[(m_front + n) % N];
216 }
217 
218 
219 template <class T, size_t N>
Clear(void)220 void Deque<T, N>::Clear(void)
221 {
222     m_front = 0;
223     m_back  = 0;
224 }
225 
226 }}} // namespace nn::rdt::CTR
227 
228 #endif  // end of NN_LIBRARIES_RDT_RDT_DEQUE_H_
229