Main Page   Class List   Class Members  

  • Main Page
  • User's Guide
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

sdk/common/NvBlastFixedPriorityQueue.h

Go to the documentation of this file.
00001 // This code contains NVIDIA Confidential Information and is disclosed to you
00002 // under a form of NVIDIA software license agreement provided separately to you.
00003 //
00004 // Notice
00005 // NVIDIA Corporation and its licensors retain all intellectual property and
00006 // proprietary rights in and to this software and related documentation and
00007 // any modifications thereto. Any use, reproduction, disclosure, or
00008 // distribution of this software and related documentation without an express
00009 // license agreement from NVIDIA Corporation is strictly prohibited.
00010 //
00011 // ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
00012 // NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
00013 // THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
00014 // MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
00015 //
00016 // Information and code furnished is believed to be accurate and reliable.
00017 // However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
00018 // information or for any infringement of patents or other rights of third parties that may
00019 // result from its use. No license is granted by implication or otherwise under any patent
00020 // or patent rights of NVIDIA Corporation. Details are subject to change without notice.
00021 // This code supersedes and replaces all information previously supplied.
00022 // NVIDIA Corporation products are not authorized for use as critical
00023 // components in life support devices or systems without express written approval of
00024 // NVIDIA Corporation.
00025 //
00026 // Copyright (c) 2016-2020 NVIDIA Corporation. All rights reserved.
00027 
00028 
00029 #ifndef NVBLASTFIXEDPRIORITYQUEUE_H
00030 #define NVBLASTFIXEDPRIORITYQUEUE_H
00031 
00032 #include "NvBlastAssert.h"
00033 #include "NvBlastMemory.h"
00034 
00035 namespace Nv
00036 {
00037 
00038 namespace Blast
00039 {
00040 
00062 template <typename A>
00063 struct Less
00064 {
00065     bool operator()(const A& a, const A& b) const
00066     {
00067         return a < b;
00068     }
00069 };
00070 
00071 
00072 template<class Element, class Comparator = Less<Element> >
00073 class FixedPriorityQueue : protected Comparator // inherit so that stateless comparators take no space
00074 {
00075 public:
00076     FixedPriorityQueue(const Comparator& less = Comparator()) : Comparator(less), mHeapSize(0)
00077     {
00078     }
00079         
00080     ~FixedPriorityQueue()
00081     {
00082     }
00083 
00084     static size_t requiredMemorySize(uint32_t capacity)
00085     {
00086         return align16(sizeof(FixedPriorityQueue<Element, Comparator>)) + align16(capacity * sizeof(Element));
00087     }
00088         
00090     const Element top() const
00091     {
00092         return data()[0];
00093     }
00094 
00096     Element top()
00097     {
00098         return data()[0];
00099     }
00100         
00102     bool empty() const
00103     {
00104         return (mHeapSize == 0);
00105     }
00106         
00108     void clear()
00109     {
00110         mHeapSize = 0;
00111     }  
00112 
00114     void push(const Element& value)
00115     {
00116         uint32_t newIndex;
00117         uint32_t parentIndex = parent(mHeapSize);
00118 
00119         for (newIndex = mHeapSize; newIndex > 0 && compare(value, data()[parentIndex]); newIndex = parentIndex, parentIndex= parent(newIndex)) 
00120         {
00121             data()[ newIndex ] = data()[parentIndex];
00122         }
00123         data()[newIndex] = value; 
00124         mHeapSize++;
00125         NVBLAST_ASSERT(valid());
00126     }
00127 
00129     Element pop()
00130     {
00131         NVBLAST_ASSERT(mHeapSize > 0);
00132         uint32_t i, child;
00133         //try to avoid LHS
00134         uint32_t tempHs = mHeapSize-1;
00135         mHeapSize = tempHs;
00136         Element min = data()[0];
00137         Element last = data()[tempHs];
00138             
00139         for (i = 0; (child = left(i)) < tempHs; i = child) 
00140         {
00141             /* Find highest priority child */
00142             const uint32_t rightChild = child + 1;
00143             
00144             child += ((rightChild < tempHs) & compare((data()[rightChild]), (data()[child]))) ? 1 : 0;
00145 
00146             if(compare(last, data()[child]))
00147                 break;
00148 
00149             data()[i] = data()[child];
00150         }
00151         data()[ i ] = last;
00152             
00153         NVBLAST_ASSERT(valid());
00154         return min;
00155     } 
00156 
00158     bool valid() const
00159     {
00160         const Element& min = data()[0];
00161         for(uint32_t i=1; i<mHeapSize; ++i)
00162         {
00163             if(compare(data()[i], min))
00164                 return false;
00165         }
00166 
00167         return true;
00168     }
00169 
00171     uint32_t size() const
00172     {
00173         return mHeapSize;
00174     }
00175 
00176 private:
00177     uint32_t mHeapSize;
00178 
00179     NV_FORCE_INLINE Element* data()
00180     {
00181         return (Element*)((char*)this + sizeof(FixedPriorityQueue<Element, Comparator>));
00182     }
00183 
00184     NV_FORCE_INLINE Element* data() const
00185     {
00186         return (Element*)((char*)this + sizeof(FixedPriorityQueue<Element, Comparator>));
00187     }
00188 
00189     bool compare(const Element& a, const Element& b) const
00190     {
00191         return Comparator::operator()(a,b);
00192     }
00193 
00194     static uint32_t left(uint32_t nodeIndex) 
00195     {
00196         return (nodeIndex << 1) + 1;
00197     }
00198         
00199     static uint32_t parent(uint32_t nodeIndex) 
00200     {
00201         return (nodeIndex - 1) >> 1;
00202     }
00203 
00204     FixedPriorityQueue<Element, Comparator>& operator = (const FixedPriorityQueue<Element, Comparator>);
00205 };
00206 
00207 } // namespace Blast
00208 } // namespace Nv
00209 
00210 #endif // ifndef NVBLASTFIXEDPRIORITYQUEUE_H
Copyright © 2015-2017 NVIDIA Corporation, 2701 San Tomas Expressway, Santa Clara, CA 95050 U.S.A. All rights reserved. www.nvidia.com