rearrange.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* Boost.MultiIndex example of use of rearrange facilities.
  2. *
  3. * Copyright 2003-2015 Joaquin M Lopez Munoz.
  4. * Distributed under the Boost Software License, Version 1.0.
  5. * (See accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * See http://www.boost.org/libs/multi_index for library home page.
  9. */
  10. #if !defined(NDEBUG)
  11. #define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
  12. #define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE
  13. #endif
  14. #include <boost/config.hpp>
  15. #include <boost/detail/iterator.hpp>
  16. #include <boost/multi_index_container.hpp>
  17. #include <boost/multi_index/random_access_index.hpp>
  18. #include <boost/random/binomial_distribution.hpp>
  19. #include <boost/random/uniform_real.hpp>
  20. #include <boost/random/mersenne_twister.hpp>
  21. #include <algorithm>
  22. #include <iostream>
  23. #include <iterator>
  24. #include <vector>
  25. #if !defined(BOOST_NO_CXX11_HDR_RANDOM)
  26. #include <random>
  27. #endif
  28. using boost::multi_index_container;
  29. using namespace boost::multi_index;
  30. /* We model a card deck with a random access array containing
  31. * card numbers (from 0 to 51), supplemented with an additional
  32. * index which retains the start ordering.
  33. */
  34. class deck
  35. {
  36. BOOST_STATIC_CONSTANT(std::size_t,num_cards=52);
  37. typedef multi_index_container<
  38. int,
  39. indexed_by<
  40. random_access<>, /* base index */
  41. random_access<> /* "start" index */
  42. >
  43. > container_type;
  44. container_type cont;
  45. public:
  46. deck()
  47. {
  48. cont.reserve(num_cards);
  49. get<1>(cont).reserve(num_cards);
  50. for(std::size_t i=0;i<num_cards;++i)cont.push_back(i);
  51. }
  52. typedef container_type::iterator iterator;
  53. typedef container_type::size_type size_type;
  54. iterator begin()const{return cont.begin();}
  55. iterator end()const{return cont.end();}
  56. size_type size()const{return cont.size();}
  57. template<typename InputIterator>
  58. void rearrange(InputIterator it)
  59. {
  60. cont.rearrange(it);
  61. }
  62. void reset()
  63. {
  64. /* simply rearrange the base index like the start index */
  65. cont.rearrange(get<1>(cont).begin());
  66. }
  67. std::size_t position(int i)const
  68. {
  69. /* The position of a card in the deck is calculated by locating
  70. * the card through the start index (which is ordered), projecting
  71. * to the base index and diffing with the begin position.
  72. * Resulting complexity: constant.
  73. */
  74. return project<0>(cont,get<1>(cont).begin()+i)-cont.begin();
  75. }
  76. std::size_t rising_sequences()const
  77. {
  78. /* Iterate through all cards and increment the sequence count
  79. * when the current position is left to the previous.
  80. * Resulting complexity: O(n), n=num_cards.
  81. */
  82. std::size_t s=1;
  83. std::size_t last_pos=0;
  84. for(std::size_t i=0;i<num_cards;++i){
  85. std::size_t pos=position(i);
  86. if(pos<last_pos)++s;
  87. last_pos=pos;
  88. }
  89. return s;
  90. }
  91. };
  92. /* A vector of reference_wrappers to deck elements can be used
  93. * as a view to the deck container.
  94. * We use a special implicit_reference_wrapper having implicit
  95. * ctor from its base type, as this simplifies the use of generic
  96. * techniques on the resulting data structures.
  97. */
  98. template<typename T>
  99. class implicit_reference_wrapper:public boost::reference_wrapper<T>
  100. {
  101. private:
  102. typedef boost::reference_wrapper<T> super;
  103. public:
  104. implicit_reference_wrapper(T& t):super(t){}
  105. };
  106. typedef std::vector<implicit_reference_wrapper<const int> > deck_view;
  107. /* Riffle shuffle is modeled like this: A cut is selected in the deck
  108. * following a binomial distribution. Then, cards are randomly selected
  109. * from one packet or the other with probability proportional to
  110. * packet size.
  111. */
  112. template<typename RandomAccessIterator,typename OutputIterator>
  113. void riffle_shuffle(
  114. RandomAccessIterator first,RandomAccessIterator last,
  115. OutputIterator out)
  116. {
  117. static boost::mt19937 rnd_gen;
  118. typedef typename boost::detail::iterator_traits<
  119. RandomAccessIterator>::difference_type difference_type;
  120. typedef boost::binomial_distribution<
  121. difference_type> rnd_cut_select_type;
  122. typedef boost::uniform_real<> rnd_deck_select_type;
  123. rnd_cut_select_type cut_select(last-first);
  124. RandomAccessIterator middle=first+cut_select(rnd_gen);
  125. difference_type s0=middle-first;
  126. difference_type s1=last-middle;
  127. rnd_deck_select_type deck_select;
  128. while(s0!=0&&s1!=0){
  129. if(deck_select(rnd_gen)<(double)s0/(s0+s1)){
  130. *out++=*first++;
  131. --s0;
  132. }
  133. else{
  134. *out++=*middle++;
  135. --s1;
  136. }
  137. }
  138. std::copy(first,first+s0,out);
  139. std::copy(middle,middle+s1,out);
  140. }
  141. struct riffle_shuffler
  142. {
  143. void operator()(deck& d)const
  144. {
  145. dv.clear();
  146. dv.reserve(d.size());
  147. riffle_shuffle(
  148. d.begin(),d.end(),std::back_inserter(dv)); /* do the shuffling */
  149. d.rearrange(dv.begin()); /* apply to the deck */
  150. }
  151. private:
  152. mutable deck_view dv;
  153. };
  154. /* A truly random shuffle (up to stdlib implementation quality) using
  155. * std::shuffle.
  156. */
  157. struct random_shuffler
  158. {
  159. void operator()(deck& d)
  160. {
  161. dv.clear();
  162. dv.reserve(d.size());
  163. std::copy(d.begin(),d.end(),std::back_inserter(dv));
  164. shuffle_view();
  165. d.rearrange(dv.begin()); /* apply to the deck */
  166. }
  167. private:
  168. deck_view dv;
  169. #if !defined(BOOST_NO_CXX11_HDR_RANDOM)
  170. std::mt19937 e;
  171. void shuffle_view()
  172. {
  173. std::shuffle(dv.begin(),dv.end(),e);
  174. }
  175. #else
  176. /* for pre-C++11 compilers we use std::random_shuffle */
  177. void shuffle_view()
  178. {
  179. std::random_shuffle(dv.begin(),dv.end());
  180. }
  181. #endif
  182. };
  183. /* Repeat a given shuffling algorithm repeats_num times
  184. * and obtain the resulting rising sequences number. Average
  185. * for tests_num trials.
  186. */
  187. template<typename Shuffler>
  188. double shuffle_test(
  189. unsigned int repeats_num,unsigned int tests_num
  190. BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Shuffler))
  191. {
  192. deck d;
  193. Shuffler sh;
  194. unsigned long total=0;
  195. for(unsigned int n=0;n<tests_num;++n){
  196. for(unsigned m=0;m<repeats_num;++m)sh(d);
  197. total+=d.rising_sequences();
  198. d.reset();
  199. }
  200. return (double)total/tests_num;
  201. }
  202. int main()
  203. {
  204. unsigned rifs_num=0;
  205. unsigned tests_num=0;
  206. std::cout<<"number of riffle shuffles (vg 5):";
  207. std::cin>>rifs_num;
  208. std::cout<<"number of tests (vg 1000):";
  209. std::cin>>tests_num;
  210. std::cout<<"shuffling..."<<std::endl;
  211. std::cout<<"riffle shuffling\n"
  212. " avg number of rising sequences: "
  213. <<shuffle_test<riffle_shuffler>(rifs_num,tests_num)
  214. <<std::endl;
  215. std::cout<<"random shuffling\n"
  216. " avg number of rising sequences: "
  217. <<shuffle_test<random_shuffler>(1,tests_num)
  218. <<std::endl;
  219. return 0;
  220. }