2
0

IceRevisitedRadix.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /**
  3. * Contains source code from the article "Radix Sort Revisited".
  4. * \file IceRevisitedRadix.h
  5. * \author Pierre Terdiman
  6. * \date April, 4, 2000
  7. */
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // Include Guard
  11. #ifndef __ICERADIXSORT_H__
  12. #define __ICERADIXSORT_H__
  13. //! Allocate histograms & offsets locally
  14. #define RADIX_LOCAL_RAM
  15. enum RadixHint
  16. {
  17. RADIX_SIGNED, //!< Input values are signed
  18. RADIX_UNSIGNED, //!< Input values are unsigned
  19. RADIX_FORCE_DWORD = 0x7fffffff
  20. };
  21. class ICECORE_API RadixSort
  22. {
  23. public:
  24. // Constructor/Destructor
  25. RadixSort();
  26. ~RadixSort();
  27. // Sorting methods
  28. RadixSort& Sort(const udword* input, udword nb, RadixHint hint=RADIX_SIGNED);
  29. RadixSort& Sort(const float* input, udword nb);
  30. //! Access to results. mRanks is a list of indices in sorted order, i.e. in the order you may further process your data
  31. inline_ const udword* GetRanks() const { return mRanks; }
  32. //! mIndices2 gets trashed on calling the sort routine, but otherwise you can recycle it the way you want.
  33. inline_ udword* GetRecyclable() const { return mRanks2; }
  34. // Stats
  35. udword GetUsedRam() const;
  36. //! Returns the total number of calls to the radix sorter.
  37. inline_ udword GetNbTotalCalls() const { return mTotalCalls; }
  38. //! Returns the number of eraly exits due to temporal coherence.
  39. inline_ udword GetNbHits() const { return mNbHits; }
  40. private:
  41. #ifndef RADIX_LOCAL_RAM
  42. udword* mHistogram; //!< Counters for each byte
  43. udword* mOffset; //!< Offsets (nearly a cumulative distribution function)
  44. #endif
  45. udword mCurrentSize; //!< Current size of the indices list
  46. udword* mRanks; //!< Two lists, swapped each pass
  47. udword* mRanks2;
  48. // Stats
  49. udword mTotalCalls; //!< Total number of calls to the sort routine
  50. udword mNbHits; //!< Number of early exits due to coherence
  51. // Internal methods
  52. void CheckResize(udword nb);
  53. bool Resize(udword nb);
  54. };
  55. #endif // __ICERADIXSORT_H__