NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Deque.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 #include "base/Array.hpp"
30 
31 namespace FW
32 {
33 //------------------------------------------------------------------------
34 
35 template <class T> class Deque // Generalization of stack & queue.
36 {
37 private:
38  struct Item
39  {
40  T value;
41  S32 prev;
42  S32 next;
43  };
44 
45 public:
46  Deque (void) { clear(); }
47  explicit Deque (const T& item) { clear(); addLast(item); }
48  Deque (const T* ptr, int size) { set(ptr, size); }
49  Deque (const Deque<T>& other) { set(other); }
50  explicit Deque (const Array<T>& other) { set(other); }
51  ~Deque (void) {}
52 
53  int getSize (void) const { return m_size; }
54  const T& getFirst (void) const { FW_ASSERT(m_size); return m_items[m_first].value; }
55  T& getFirst (void) { FW_ASSERT(m_size); return m_items[m_first].value; }
56  const T& getLast (void) const { FW_ASSERT(m_size); return m_items[m_last].value; }
57  T& getLast (void) { FW_ASSERT(m_size); return m_items[m_last].value; }
58 
59  void reset (void) { clear(); m_items.reset(); }
60  void clear (void) { m_items.clear(); m_size = 0; m_free = -1; }
61  void setCapacity (int capacity) { m_items.setCapacity(capacity); }
62  void compact (void) { set(getAll()); }
63 
64  void set (const T* ptr, int size);
65  void set (const Deque<T>& other);
66  void set (const Array<T>& other) { set(other.getPtr(), other.getSize()); }
67  void getRange (Array<T>& res, int start, int end) const;
68  Array<T> getRange (int start, int end) const;
69  void getAll (Array<T>& res) const { getRange(res, 0, getSize()); }
70  Array<T> getAll (void) const { return getRange(0, getSize()); }
71 
72  T& addFirst (void);
73  T& addFirst (const T& item) { T& slot = addFirst(); slot = item; return slot; }
74  T& addLast (void);
75  T& addLast (const T& item) { T& slot = addLast(); slot = item; return slot; }
76 
77  T& removeFirst (void);
78  T& removeLast (void);
79 
80  Deque<T>& operator= (const Deque<T>& other) { set(other); return *this; }
81  Deque<T>& operator= (const Array<T>& other) { set(other); return *this; }
82  bool operator== (const Deque<T>& other) const;
83  bool operator!= (const Deque<T>& other) const { return (!operator==(other)); }
84 
85 private:
86  int allocItem (void);
87  void freeItem (int idx);
88 
89  Array<Item> m_items;
90  S32 m_size;
91  S32 m_first;
92  S32 m_last;
93  S32 m_free;
94 };
95 
96 //------------------------------------------------------------------------
97 
98 template <class T> void Deque<T>::set(const T* ptr, int size)
99 {
100  FW_ASSERT(size >= 0);
101 
102  m_items.reset(size);
103  for (int i = 0; i < size; i++)
104  {
105  if (ptr)
106  m_items[i].value = ptr[i];
107  m_items[i].prev = i - 1;
108  m_items[i].next = i + 1;
109  }
110 
111  m_first = 0;
112  m_last = size - 1;
113  m_free = -1;
114  m_size = size;
115 }
116 
117 //------------------------------------------------------------------------
118 
119 template <class T> void Deque<T>::set(const Deque<T>& other)
120 {
121  if (&other != this)
122  {
123  m_items = other.m_items;
124  m_size = other.m_size;
125  m_first = other.m_first;
126  m_last = other.m_last;
127  m_free = other.m_free;
128  }
129 }
130 
131 //------------------------------------------------------------------------
132 
133 template <class T> void Deque<T>::getRange(Array<T>& res, int start, int end) const
134 {
135  FW_ASSERT(start >= 0 && start <= end && end <= getSize());
136 
137  int src = m_first;
138  for (int i = 0; i < start; i++)
139  src = m_items[src].next;
140 
141  T* dst = res.add(NULL, end - start);
142  for (int i = start; i < end; i++)
143  {
144  *dst++ = m_items[src].value;
145  src = m_items[src].next;
146  }
147 }
148 
149 //------------------------------------------------------------------------
150 
151 template <class T> Array<T> Deque<T>::getRange(int start, int end) const
152 {
153  FW_ASSERT(start >= 0 && start <= end && end <= getSize());
154  Array<T> res;
155  res.setCapacity(end - start);
156  getRange(res, start, end);
157  return res;
158 }
159 
160 //------------------------------------------------------------------------
161 
162 template <class T> T& Deque<T>::addFirst(void)
163 {
164  int idx = allocItem();
165  if (m_size == 1)
166  m_last = idx;
167  else
168  {
169  m_items[idx].next = m_first;
170  m_items[m_first].prev = idx;
171  }
172  m_first = idx;
173  return m_items[idx].value;
174 }
175 
176 //------------------------------------------------------------------------
177 
178 template <class T> T& Deque<T>::addLast(void)
179 {
180  int idx = allocItem();
181  if (m_size == 1)
182  m_first = idx;
183  else
184  {
185  m_items[idx].prev = m_last;
186  m_items[m_last].next = idx;
187  }
188  m_last = idx;
189  return m_items[idx].value;
190 }
191 
192 //------------------------------------------------------------------------
193 
194 template <class T> T& Deque<T>::removeFirst(void)
195 {
196  FW_ASSERT(m_size);
197  int idx = m_first;
198  m_first = m_items[idx].next;
199  freeItem(idx);
200  return m_items[idx].value;
201 }
202 
203 //------------------------------------------------------------------------
204 
205 template <class T> T& Deque<T>::removeLast(void)
206 {
207  FW_ASSERT(m_size);
208  int idx = m_last;
209  m_last = m_items[idx].prev;
210  freeItem(idx);
211  return m_items[idx].value;
212 }
213 
214 //------------------------------------------------------------------------
215 
216 template <class T> bool Deque<T>::operator==(const Deque<T>& other) const
217 {
218  if (m_size != other.m_size)
219  return false;
220 
221  int idxA = m_first;
222  int idxB = other.m_first;
223  for (int i = 0; i < m_size; i++)
224  {
225  if (m_items[idxA].value != other.m_items[idxB].value)
226  return false;
227  idxA = m_items[idxA].next;
228  idxB = other.m_items[idxB].next;
229  }
230  return true;
231 }
232 
233 //------------------------------------------------------------------------
234 
235 template <class T> int Deque<T>::allocItem(void)
236 {
237  int idx = m_free;
238  if (idx != -1)
239  m_free = m_items[idx].next;
240  else
241  {
242  idx = m_items.getSize();
243  m_items.add();
244  }
245  m_size++;
246  return idx;
247 }
248 
249 //------------------------------------------------------------------------
250 
251 template <class T> void Deque<T>::freeItem(int idx)
252 {
253  m_items[idx].next = m_free;
254  m_free = idx;
255  m_size--;
256 }
257 
258 //------------------------------------------------------------------------
259 }
#define NULL
Definition: Defs.hpp:39
void clear(void)
Definition: Deque.hpp:60
Deque(const Array< T > &other)
Definition: Deque.hpp:50
Deque(void)
Definition: Deque.hpp:46
void ** ptr
Definition: DLLImports.cpp:74
Deque(const Deque< T > &other)
Definition: Deque.hpp:49
bool operator==(const Deque< T > &other) const
Definition: Deque.hpp:216
int getSize(void) const
Definition: Deque.hpp:53
void reset(void)
Definition: Deque.hpp:59
void clear(void)
Definition: Array.hpp:359
T & addLast(void)
Definition: Deque.hpp:178
T & addFirst(const T &item)
Definition: Deque.hpp:73
const T & getFirst(void) const
Definition: Deque.hpp:54
Array< T > getAll(void) const
Definition: Deque.hpp:70
T & getLast(void)
Definition: Deque.hpp:57
void setCapacity(int capacity)
Definition: Deque.hpp:61
T & addFirst(void)
Definition: Deque.hpp:162
void reset(S size=0)
Definition: Array.hpp:317
Deque< T > & operator=(const Deque< T > &other)
Definition: Deque.hpp:80
bool operator!=(const Deque< T > &other) const
Definition: Deque.hpp:83
#define FW_ASSERT(X)
Definition: Defs.hpp:67
signed int S32
Definition: Defs.hpp:88
T & addLast(const T &item)
Definition: Deque.hpp:75
void setCapacity(S numElements)
Definition: Array.hpp:326
void getAll(Array< T > &res) const
Definition: Deque.hpp:69
~Deque(void)
Definition: Deque.hpp:51
Deque(const T *ptr, int size)
Definition: Deque.hpp:48
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value
Definition: DLLImports.inl:84
void getRange(Array< T > &res, int start, int end) const
Definition: Deque.hpp:133
void set(const T *ptr, int size)
Definition: Deque.hpp:98
Deque(const T &item)
Definition: Deque.hpp:47
T & removeFirst(void)
Definition: Deque.hpp:194
void set(const Array< T > &other)
Definition: Deque.hpp:66
const T & getLast(void) const
Definition: Deque.hpp:56
const T * getPtr(S32idx=0) const
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr size
Definition: DLLImports.inl:319
S32 getSize(void) const
T & removeLast(void)
Definition: Deque.hpp:205
T & getFirst(void)
Definition: Deque.hpp:55
void compact(void)
Definition: Deque.hpp:62