//----------------------------------------------------------------------------- // Copyright (c) 2012 GarageGames, LLC // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to // deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS // IN THE SOFTWARE. //----------------------------------------------------------------------------- #ifndef _MRANDOMDECK_H_ #define _MRANDOMDECK_H_ #ifndef _MRANDOM_H_ #include "math/mRandom.h" #endif template class MRandomDeck { protected: MRandomLCG *mRandGen; Vector mDeck; Vector mPile; public: MRandomDeck( MRandomLCG *randGen = &gRandGen ); void addToPile( const T &item ); void addToPile( const Vector &items ); void shuffle(); S32 draw( T *item, bool reshuffle = true ); void removeAll( Vector *outItems ); }; template inline MRandomDeck::MRandomDeck( MRandomLCG *randGen ) : mRandGen( randGen ) { } template inline void MRandomDeck::shuffle() { // Move everything to the pile. mPile.merge( mDeck ); if ( mPile.empty() ) return; T& last = mPile.last(); mDeck.clear(); // Randomly draw from the pile // and place them in the deck. while ( !mPile.empty() ) { U32 i = mRandGen->randI( 0, mPile.size() - 1 ); mDeck.push_back( mPile[i] ); mPile.erase_fast( i ); } // Make sure that the first drawn item // is not the same as the last drawn item. if ( mDeck.last() == last ) { mDeck.pop_back(); mDeck.push_front( last ); } } template inline S32 MRandomDeck::draw( T *item, bool reshuffle ) { if ( mDeck.size() == 0 ) { if ( mPile.size() == 0 ) return -1; if ( reshuffle ) shuffle(); else return -1; } *item = mDeck.last(); mPile.push_back( *item ); mDeck.pop_back(); return mDeck.size(); } template inline void MRandomDeck::addToPile( const T &item ) { mPile.push_back( item ); } template inline void MRandomDeck::addToPile( const Vector &items ) { mPile.merge( items ); } template inline void MRandomDeck::removeAll( Vector *outItems ) { if ( outItems ) { outItems->merge( mPile ); outItems->merge( mDeck ); } mPile.clear(); mDeck.clear(); } #endif //_MRANDOMDECK_H_