46 BinaryHeap (
void) : m_hasBeenBuilt(false), m_freelist(-1) {}
53 bool contains (
int idx)
const {
return (idx >= 0 && idx < m_items.
getSize() && m_items[idx].slot != -1); }
56 void clear (
void) { m_items.
clear(); m_slots.
clear(); m_hasBeenBuilt =
false; m_freelist = -1; }
57 void reset (
void) { m_items.
reset(); m_slots.
reset(); m_hasBeenBuilt =
false; m_freelist = -1; }
72 bool heapify (
int parentSlot);
73 void adjust (
int idx,
bool increased);
86 m_items = other.m_items;
87 m_slots = other.m_slots;
88 m_hasBeenBuilt = other.m_hasBeenBuilt;
89 m_freelist = other.m_freelist;
100 while (idx >= m_items.getSize())
102 Item& item = m_items.add();
104 item.nextInFreelist = m_freelist;
105 m_freelist = m_items.getSize() - 1;
110 bool increased =
false;
111 if (m_items[idx].slot != -1)
112 increased = (m_items[idx].value <
value);
115 m_items[idx].slot = m_slots.getSize();
118 m_items[idx].value =
value;
123 adjust(idx, increased);
138 idx = m_items.getSize();
141 m_freelist = m_items[idx].nextInFreelist;
142 m_items[idx].nextInFreelist = -2;
144 while (m_items[idx].slot != -1);
163 if (m_slots.getSize() <= 2)
164 m_hasBeenBuilt =
false;
168 Item item = m_items[idx];
169 m_items[idx].slot = -1;
171 if (item.nextInFreelist == -2)
173 m_items[idx].nextInFreelist = m_freelist;
179 int last = m_slots.removeLast();
182 m_items[last].slot = item.slot;
183 m_slots[item.slot] = last;
188 adjust(last, (item.value < m_items[last].value));
204 if (!m_hasBeenBuilt && m_slots.getSize() >= 2)
206 for (
int i = (m_slots.getSize() - 2) >> 1; i >= 0; i--)
208 int idx = m_slots[i];
209 while (heapify(m_items[idx].slot));
211 m_hasBeenBuilt =
true;
223 FW_ASSERT(parentSlot >= 0 && parentSlot < m_slots.getSize());
228 int childSlot = (parentSlot << 1) + 1;
229 if (childSlot >= m_slots.getSize())
232 int child = m_slots[childSlot];
236 if (childSlot + 1 < m_slots.getSize())
238 int other = m_slots[childSlot + 1];
239 if (m_items[other].
value < m_items[child].
value)
248 int parent = m_slots[parentSlot];
249 if (!(m_items[child].
value < m_items[parent].
value))
254 m_items[child].slot = parentSlot;
255 m_items[parent].slot = childSlot;
256 m_slots[parentSlot] = child;
257 m_slots[childSlot] = parent;
263 template <
class T>
void BinaryHeap<T>::adjust(
int idx,
bool increased)
267 while (heapify(m_items[idx].slot));
269 while (m_items[idx].slot != 0 && heapify((m_items[idx].slot - 1) >> 1));
void set(const BinaryHeap< T > &other)
BinaryHeap(const BinaryHeap< T > &other)
const T & operator[](int idx) const
int numIndices(void) const
void add(int idx, const T &value)
bool contains(int idx) 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
BinaryHeap< T > & operator=(const BinaryHeap< T > &other)