multi_index_container.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Boost.Assign library
  2. //
  3. // Copyright Thorsten Ottosen 2003-2004. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see http://www.boost.org/libs/assign/
  9. //
  10. #include <boost/detail/workaround.hpp>
  11. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  12. # pragma warn -8091 // suppress warning in Boost.Test
  13. # pragma warn -8057 // unused argument argc/argv in Boost.Test
  14. #endif
  15. #include <boost/assign/list_of.hpp>
  16. #include <boost/assign/list_inserter.hpp>
  17. #include <boost/multi_index_container.hpp>
  18. #include <boost/multi_index/identity.hpp>
  19. #include <boost/multi_index/member.hpp>
  20. #include <boost/multi_index/ordered_index.hpp>
  21. #include <boost/multi_index/sequenced_index.hpp>
  22. #include <boost/test/test_tools.hpp>
  23. #include <boost/test/unit_test.hpp>
  24. #include <cstddef>
  25. #include <ostream>
  26. #include <string>
  27. using namespace boost;
  28. using namespace boost::multi_index;
  29. namespace ba = boost::assign;
  30. //
  31. // Define a classical multi_index_container for employees
  32. //
  33. struct employee
  34. {
  35. int id;
  36. std::string name;
  37. int age;
  38. employee(int id_,std::string name_,int age_):id(id_),name(name_),age(age_){}
  39. bool operator==(const employee& x)const
  40. {
  41. return id==x.id&&name==x.name&&age==x.age;
  42. }
  43. bool operator<(const employee& x)const
  44. {
  45. return id<x.id;
  46. }
  47. bool operator!=(const employee& x)const{return !(*this==x);}
  48. bool operator> (const employee& x)const{return x<*this;}
  49. bool operator>=(const employee& x)const{return !(*this<x);}
  50. bool operator<=(const employee& x)const{return !(x<*this);}
  51. struct comp_id
  52. {
  53. bool operator()(int x,const employee& e2)const{return x<e2.id;}
  54. bool operator()(const employee& e1,int x)const{return e1.id<x;}
  55. };
  56. friend std::ostream& operator<<(std::ostream& os,const employee& e)
  57. {
  58. os<<e.id<<" "<<e.name<<" "<<e.age<<std::endl;
  59. return os;
  60. }
  61. };
  62. struct name{};
  63. struct by_name{};
  64. struct age{};
  65. struct as_inserted{};
  66. typedef
  67. multi_index_container<
  68. employee,
  69. indexed_by<
  70. ordered_unique<
  71. identity<employee> >,
  72. ordered_non_unique<
  73. tag<name,by_name>,
  74. BOOST_MULTI_INDEX_MEMBER(employee,std::string,name)>,
  75. ordered_non_unique<
  76. tag<age>,
  77. BOOST_MULTI_INDEX_MEMBER(employee,int,age)>,
  78. sequenced<
  79. tag<as_inserted> > > >
  80. employee_set;
  81. #if defined(BOOST_NO_MEMBER_TEMPLATES)
  82. typedef nth_index<
  83. employee_set,1>::type employee_set_by_name;
  84. #else
  85. typedef employee_set::nth_index<1>::type employee_set_by_name;
  86. #endif
  87. typedef boost::multi_index::index<
  88. employee_set,age>::type employee_set_by_age;
  89. typedef boost::multi_index::index<
  90. employee_set,as_inserted>::type employee_set_as_inserted;
  91. //
  92. // Define a multi_index_container with a list-like index and an ordered index
  93. //
  94. typedef multi_index_container<
  95. std::string,
  96. indexed_by<
  97. sequenced<>, // list-like index
  98. ordered_non_unique<identity<std::string> > // words by alphabetical order
  99. >
  100. > text_container;
  101. void test_multi_index_container()
  102. {
  103. employee_set eset = ba::list_of< employee >(1,"Franz",30)(2,"Hanz",40)(3,"Ilse",50);
  104. BOOST_CHECK( eset.size() == 3u );
  105. //
  106. // This container is associative, hence we can use 'insert()'
  107. //
  108. ba::insert( eset )(4,"Kurt",55)(5,"Bjarne",77)(7,"Thorsten",24);
  109. BOOST_CHECK( eset.size() == 6u );
  110. employee_set_by_name& name_index = boost::multi_index::get<name>(eset);
  111. employee_set_by_name::iterator i = name_index.find("Ilse");
  112. BOOST_CHECK( i->id == 3 );
  113. BOOST_CHECK( i->age == 50 );
  114. text_container text = ba::list_of< std::string >("Have")("you")("ever")("wondered")("how")("much")("Boost")("rocks?!");
  115. BOOST_CHECK_EQUAL( text.size(), 8u );
  116. BOOST_CHECK_EQUAL( *text.begin(), "Have" );
  117. //
  118. // This container is a sequence, hence we can use 'push_back()' and 'push_font()'
  119. //
  120. ba::push_back( text )("Well")(",")("A")("LOT")(",")("obviously!");
  121. BOOST_CHECK_EQUAL( text.size(), 14u );
  122. BOOST_CHECK_EQUAL( *--text.end(), "obviously!" );
  123. ba::push_front( text ) = "question:", "simple", "A";
  124. BOOST_CHECK_EQUAL( text.size(), 17u );
  125. BOOST_CHECK_EQUAL( text.front(), "A" );
  126. }
  127. using boost::unit_test::test_suite;
  128. test_suite* init_unit_test_suite( int argc, char* argv[] )
  129. {
  130. test_suite* test = BOOST_TEST_SUITE( "List Test Suite" );
  131. test->add( BOOST_TEST_CASE( &test_multi_index_container ) );
  132. return test;
  133. }