non_default_ctor.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Boost.MultiIndex example of use of multi_index_container::ctor_args_list.
  2. *
  3. * Copyright 2003-2008 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/multi_index_container.hpp>
  15. #include <boost/multi_index/identity.hpp>
  16. #include <boost/multi_index/ordered_index.hpp>
  17. #include <algorithm>
  18. #include <iostream>
  19. #include <iterator>
  20. using boost::multi_index_container;
  21. using namespace boost::multi_index;
  22. /* modulo_less order numbers according to their division residual.
  23. * For instance, if modulo==10 then 22 is less than 15 as 22%10==2 and
  24. * 15%10==5.
  25. */
  26. template<typename IntegralType>
  27. struct modulo_less
  28. {
  29. modulo_less(IntegralType m):modulo(m){}
  30. bool operator()(IntegralType x,IntegralType y)const
  31. {
  32. return (x%modulo)<(y%modulo);
  33. }
  34. private:
  35. IntegralType modulo;
  36. };
  37. /* multi_index_container of unsigned ints holding a "natural" index plus
  38. * an ordering based on modulo_less.
  39. */
  40. typedef multi_index_container<
  41. unsigned int,
  42. indexed_by<
  43. ordered_unique<identity<unsigned int> >,
  44. ordered_non_unique<identity<unsigned int>, modulo_less<unsigned int> >
  45. >
  46. > modulo_indexed_set;
  47. int main()
  48. {
  49. /* define a modulo_indexed_set with modulo==10 */
  50. modulo_indexed_set::ctor_args_list args_list=
  51. boost::make_tuple(
  52. /* ctor_args for index #0 is default constructible */
  53. nth_index<modulo_indexed_set,0>::type::ctor_args(),
  54. /* first parm is key_from_value, second is our sought for key_compare */
  55. boost::make_tuple(identity<unsigned int>(),modulo_less<unsigned int>(10))
  56. );
  57. modulo_indexed_set m(args_list);
  58. /* this could have be written online without the args_list variable,
  59. * left as it is for explanatory purposes. */
  60. /* insert some numbers */
  61. unsigned int numbers[]={0,1,20,40,33,68,11,101,60,34,88,230,21,4,7,17};
  62. const std::size_t numbers_length(sizeof(numbers)/sizeof(numbers[0]));
  63. m.insert(&numbers[0],&numbers[numbers_length]);
  64. /* lists all numbers in order, along with their "equivalence class", that is,
  65. * the equivalent numbers under modulo_less
  66. */
  67. for(modulo_indexed_set::iterator it=m.begin();it!=m.end();++it){
  68. std::cout<<*it<<" -> ( ";
  69. nth_index<modulo_indexed_set,1>::type::iterator it0,it1;
  70. boost::tie(it0,it1)=get<1>(m).equal_range(*it);
  71. std::copy(it0,it1,std::ostream_iterator<unsigned int>(std::cout," "));
  72. std::cout<<")"<<std::endl;
  73. }
  74. return 0;
  75. }