mRandomDeck.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _MRANDOMDECK_H_
  23. #define _MRANDOMDECK_H_
  24. #ifndef _MRANDOM_H_
  25. #include "math/mRandom.h"
  26. #endif
  27. template <class T>
  28. class MRandomDeck
  29. {
  30. protected:
  31. MRandomLCG *mRandGen;
  32. Vector<T> mDeck;
  33. Vector<T> mPile;
  34. public:
  35. MRandomDeck( MRandomLCG *randGen = &gRandGen );
  36. void addToPile( const T &item );
  37. void addToPile( const Vector<T> &items );
  38. void shuffle();
  39. S32 draw( T *item, bool reshuffle = true );
  40. void removeAll( Vector<T> *outItems );
  41. };
  42. template<class T>
  43. inline MRandomDeck<T>::MRandomDeck( MRandomLCG *randGen )
  44. : mRandGen( randGen )
  45. {
  46. }
  47. template<class T>
  48. inline void MRandomDeck<T>::shuffle()
  49. {
  50. // Move everything to the pile.
  51. mPile.merge( mDeck );
  52. if ( mPile.empty() )
  53. return;
  54. T& last = mPile.last();
  55. mDeck.clear();
  56. // Randomly draw from the pile
  57. // and place them in the deck.
  58. while ( !mPile.empty() )
  59. {
  60. U32 i = mRandGen->randI( 0, mPile.size() - 1 );
  61. mDeck.push_back( mPile[i] );
  62. mPile.erase_fast( i );
  63. }
  64. // Make sure that the first drawn item
  65. // is not the same as the last drawn item.
  66. if ( mDeck.last() == last )
  67. {
  68. mDeck.pop_back();
  69. mDeck.push_front( last );
  70. }
  71. }
  72. template<class T>
  73. inline S32 MRandomDeck<T>::draw( T *item, bool reshuffle )
  74. {
  75. if ( mDeck.size() == 0 )
  76. {
  77. if ( mPile.size() == 0 )
  78. return -1;
  79. if ( reshuffle )
  80. shuffle();
  81. else
  82. return -1;
  83. }
  84. *item = mDeck.last();
  85. mPile.push_back( *item );
  86. mDeck.pop_back();
  87. return mDeck.size();
  88. }
  89. template<class T>
  90. inline void MRandomDeck<T>::addToPile( const T &item )
  91. {
  92. mPile.push_back( item );
  93. }
  94. template<class T>
  95. inline void MRandomDeck<T>::addToPile( const Vector<T> &items )
  96. {
  97. mPile.merge( items );
  98. }
  99. template<class T>
  100. inline void MRandomDeck<T>::removeAll( Vector<T> *outItems )
  101. {
  102. if ( outItems )
  103. {
  104. outItems->merge( mPile );
  105. outItems->merge( mDeck );
  106. }
  107. mPile.clear();
  108. mDeck.clear();
  109. }
  110. #endif //_MRANDOMDECK_H_