/* Boost.MultiIndex example of use of rearrange facilities. * * Copyright 2003-2015 Joaquin M Lopez Munoz. * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) * * See http://www.boost.org/libs/multi_index for library home page. */ #if !defined(NDEBUG) #define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING #define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE #endif #include #include #include #include #include #include #include #include #include #include #include #if !defined(BOOST_NO_CXX11_HDR_RANDOM) #include #endif using boost::multi_index_container; using namespace boost::multi_index; /* We model a card deck with a random access array containing * card numbers (from 0 to 51), supplemented with an additional * index which retains the start ordering. */ class deck { BOOST_STATIC_CONSTANT(std::size_t,num_cards=52); typedef multi_index_container< int, indexed_by< random_access<>, /* base index */ random_access<> /* "start" index */ > > container_type; container_type cont; public: deck() { cont.reserve(num_cards); get<1>(cont).reserve(num_cards); for(std::size_t i=0;i void rearrange(InputIterator it) { cont.rearrange(it); } void reset() { /* simply rearrange the base index like the start index */ cont.rearrange(get<1>(cont).begin()); } std::size_t position(int i)const { /* The position of a card in the deck is calculated by locating * the card through the start index (which is ordered), projecting * to the base index and diffing with the begin position. * Resulting complexity: constant. */ return project<0>(cont,get<1>(cont).begin()+i)-cont.begin(); } std::size_t rising_sequences()const { /* Iterate through all cards and increment the sequence count * when the current position is left to the previous. * Resulting complexity: O(n), n=num_cards. */ std::size_t s=1; std::size_t last_pos=0; for(std::size_t i=0;i class implicit_reference_wrapper:public boost::reference_wrapper { private: typedef boost::reference_wrapper super; public: implicit_reference_wrapper(T& t):super(t){} }; typedef std::vector > deck_view; /* Riffle shuffle is modeled like this: A cut is selected in the deck * following a binomial distribution. Then, cards are randomly selected * from one packet or the other with probability proportional to * packet size. */ template void riffle_shuffle( RandomAccessIterator first,RandomAccessIterator last, OutputIterator out) { static boost::mt19937 rnd_gen; typedef typename boost::detail::iterator_traits< RandomAccessIterator>::difference_type difference_type; typedef boost::binomial_distribution< difference_type> rnd_cut_select_type; typedef boost::uniform_real<> rnd_deck_select_type; rnd_cut_select_type cut_select(last-first); RandomAccessIterator middle=first+cut_select(rnd_gen); difference_type s0=middle-first; difference_type s1=last-middle; rnd_deck_select_type deck_select; while(s0!=0&&s1!=0){ if(deck_select(rnd_gen)<(double)s0/(s0+s1)){ *out++=*first++; --s0; } else{ *out++=*middle++; --s1; } } std::copy(first,first+s0,out); std::copy(middle,middle+s1,out); } struct riffle_shuffler { void operator()(deck& d)const { dv.clear(); dv.reserve(d.size()); riffle_shuffle( d.begin(),d.end(),std::back_inserter(dv)); /* do the shuffling */ d.rearrange(dv.begin()); /* apply to the deck */ } private: mutable deck_view dv; }; /* A truly random shuffle (up to stdlib implementation quality) using * std::shuffle. */ struct random_shuffler { void operator()(deck& d) { dv.clear(); dv.reserve(d.size()); std::copy(d.begin(),d.end(),std::back_inserter(dv)); shuffle_view(); d.rearrange(dv.begin()); /* apply to the deck */ } private: deck_view dv; #if !defined(BOOST_NO_CXX11_HDR_RANDOM) std::mt19937 e; void shuffle_view() { std::shuffle(dv.begin(),dv.end(),e); } #else /* for pre-C++11 compilers we use std::random_shuffle */ void shuffle_view() { std::random_shuffle(dv.begin(),dv.end()); } #endif }; /* Repeat a given shuffling algorithm repeats_num times * and obtain the resulting rising sequences number. Average * for tests_num trials. */ template double shuffle_test( unsigned int repeats_num,unsigned int tests_num BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Shuffler)) { deck d; Shuffler sh; unsigned long total=0; for(unsigned int n=0;n>rifs_num; std::cout<<"number of tests (vg 1000):"; std::cin>>tests_num; std::cout<<"shuffling..."<(rifs_num,tests_num) <(1,tests_num) <