doc_list_algorithms.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-2013
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. //[doc_list_algorithms_code
  13. #include <boost/intrusive/circular_list_algorithms.hpp>
  14. #include <cassert>
  15. struct my_node
  16. {
  17. my_node *next_, *prev_;
  18. //other members...
  19. };
  20. //Define our own list_node_traits
  21. struct my_list_node_traits
  22. {
  23. typedef my_node node;
  24. typedef my_node * node_ptr;
  25. typedef const my_node * const_node_ptr;
  26. static node_ptr get_next(const_node_ptr n) { return n->next_; }
  27. static void set_next(node_ptr n, node_ptr next) { n->next_ = next; }
  28. static node *get_previous(const_node_ptr n) { return n->prev_; }
  29. static void set_previous(node_ptr n, node_ptr prev){ n->prev_ = prev; }
  30. };
  31. int main()
  32. {
  33. typedef boost::intrusive::circular_list_algorithms<my_list_node_traits> algo;
  34. my_node one, two, three;
  35. //Create an empty doubly linked list container:
  36. //"one" will be the first node of the container
  37. algo::init_header(&one);
  38. assert(algo::count(&one) == 1);
  39. //Now add a new node before "one"
  40. algo::link_before(&one, &two);
  41. assert(algo::count(&one) == 2);
  42. //Now add a new node after "two"
  43. algo::link_after(&two, &three);
  44. assert(algo::count(&one) == 3);
  45. //Now unlink the node after one
  46. algo::unlink(&three);
  47. assert(algo::count(&one) == 2);
  48. //Now unlink two
  49. algo::unlink(&two);
  50. assert(algo::count(&one) == 1);
  51. //Now unlink one
  52. algo::unlink(&one);
  53. assert(algo::count(&one) == 1);
  54. return 0;
  55. }
  56. //]