bucket_sorter.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. #include <boost/config.hpp>
  10. #include <vector>
  11. #include <iostream>
  12. #include <stdlib.h>
  13. #include <boost/pending/bucket_sorter.hpp>
  14. int main() {
  15. using namespace std;
  16. using boost::bucket_sorter;
  17. const std::size_t N = 10;
  18. vector<std::size_t> bucket(N);
  19. for (std::size_t i=0; i<N; i++) {
  20. bucket[i] = rand() % N;
  21. cout.width(6);
  22. cout << "Number " << i << " is in bucket " << bucket[i] << endl;
  23. }
  24. typedef boost::identity_property_map ID;
  25. typedef bucket_sorter<std::size_t, int,
  26. vector<std::size_t>::iterator, ID> BS;
  27. BS my_bucket_sorter(N, N, bucket.begin());
  28. for (std::size_t ii=0; ii<N; ii++)
  29. my_bucket_sorter.push(ii);
  30. std::size_t j;
  31. for (j=0; j<N; j++) {
  32. cout << "The bucket " << j;
  33. if ( ! my_bucket_sorter[j].empty() ) {
  34. cout << " has number ";
  35. do {
  36. int v = my_bucket_sorter[j].top();
  37. my_bucket_sorter[j].pop();
  38. cout << v << " ";
  39. } while ( ! my_bucket_sorter[j].empty() );
  40. cout << endl;
  41. } else {
  42. cout << " has no number associated with it." << endl;
  43. }
  44. }
  45. for (std::size_t k=0; k<N; k++)
  46. my_bucket_sorter.push(k);
  47. my_bucket_sorter.remove(5);
  48. my_bucket_sorter.remove(7);
  49. cout << "After removing numbers 5 and 7, check correctness again." << endl;
  50. for (j=0; j<N; j++) {
  51. cout << "The bucket " << j;
  52. if ( ! my_bucket_sorter[j].empty() ) {
  53. cout << " has number ";
  54. do {
  55. int v = my_bucket_sorter[j].top();
  56. my_bucket_sorter[j].pop();
  57. cout << v << " ";
  58. } while ( ! my_bucket_sorter[j].empty() );
  59. cout << endl;
  60. } else {
  61. cout << " has no number associated with it." << endl;
  62. }
  63. }
  64. std::size_t iii;
  65. for (iii=0; iii<N; iii++) {
  66. std::size_t current = rand() % N;
  67. if ( ! my_bucket_sorter[current].empty() ) {
  68. int v = my_bucket_sorter[current].top();
  69. my_bucket_sorter[current].pop();
  70. bucket[v] = rand() % N;
  71. my_bucket_sorter.push(v);
  72. }
  73. }
  74. for (iii=0; iii<N; iii++) {
  75. std::size_t current = rand() % N;
  76. if ( ! my_bucket_sorter[current].empty() ) {
  77. int v = my_bucket_sorter[current].top();
  78. bucket[v] = rand() % N;
  79. my_bucket_sorter.update(v);
  80. }
  81. }
  82. return 0;
  83. }