bucket_sorter.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. //=======================================================================
  3. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  4. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //=======================================================================
  10. //
  11. //
  12. // Revision History:
  13. // 13 June 2001: Changed some names for clarity. (Jeremy Siek)
  14. // 01 April 2001: Modified to use new <boost/limits.hpp> header. (JMaddock)
  15. //
  16. #ifndef BOOST_GRAPH_DETAIL_BUCKET_SORTER_HPP
  17. #define BOOST_GRAPH_DETAIL_BUCKET_SORTER_HPP
  18. #include <vector>
  19. #include <cassert>
  20. #include <boost/limits.hpp>
  21. namespace boost {
  22. template <class BucketType, class ValueType, class Bucket,
  23. class ValueIndexMap>
  24. class bucket_sorter {
  25. public:
  26. typedef BucketType bucket_type;
  27. typedef ValueType value_type;
  28. typedef typename std::vector<value_type>::size_type size_type;
  29. bucket_sorter(size_type _length, bucket_type _max_bucket,
  30. const Bucket& _bucket = Bucket(),
  31. const ValueIndexMap& _id = ValueIndexMap())
  32. : head(_max_bucket, invalid_value()),
  33. next(_length, invalid_value()),
  34. prev(_length, invalid_value()),
  35. id_to_value(_length),
  36. bucket(_bucket), id(_id) { }
  37. void remove(const value_type& x) {
  38. const size_type i = get(id, x);
  39. const size_type& next_node = next[i];
  40. const size_type& prev_node = prev[i];
  41. //check if i is the end of the bucket list
  42. if ( next_node != invalid_value() )
  43. prev[next_node] = prev_node;
  44. //check if i is the begin of the bucket list
  45. if ( prev_node != invalid_value() )
  46. next[prev_node] = next_node;
  47. else //need update head of current bucket list
  48. head[ bucket[x] ] = next_node;
  49. }
  50. void push(const value_type& x) {
  51. id_to_value[get(id, x)] = x;
  52. (*this)[bucket[x]].push(x);
  53. }
  54. void update(const value_type& x) {
  55. remove(x);
  56. (*this)[bucket[x]].push(x);
  57. }
  58. // private:
  59. // with KCC, the nested stack class is having access problems
  60. // despite the friend decl.
  61. static size_type invalid_value() {
  62. return (std::numeric_limits<size_type>::max)();
  63. }
  64. typedef typename std::vector<size_type>::iterator Iter;
  65. typedef typename std::vector<value_type>::iterator IndexValueMap;
  66. public:
  67. friend class stack;
  68. class stack {
  69. public:
  70. stack(bucket_type _bucket_id, Iter h, Iter n, Iter p, IndexValueMap v,
  71. const ValueIndexMap& _id)
  72. : bucket_id(_bucket_id), head(h), next(n), prev(p), value(v), id(_id) {}
  73. // Avoid using default arg for ValueIndexMap so that the default
  74. // constructor of the ValueIndexMap is not required if not used.
  75. stack(bucket_type _bucket_id, Iter h, Iter n, Iter p, IndexValueMap v)
  76. : bucket_id(_bucket_id), head(h), next(n), prev(p), value(v) {}
  77. void push(const value_type& x) {
  78. const size_type new_head = get(id, x);
  79. const size_type current = head[bucket_id];
  80. if ( current != invalid_value() )
  81. prev[current] = new_head;
  82. prev[new_head] = invalid_value();
  83. next[new_head] = current;
  84. head[bucket_id] = new_head;
  85. }
  86. void pop() {
  87. size_type current = head[bucket_id];
  88. size_type next_node = next[current];
  89. head[bucket_id] = next_node;
  90. if ( next_node != invalid_value() )
  91. prev[next_node] = invalid_value();
  92. }
  93. value_type& top() { return value[ head[bucket_id] ]; }
  94. const value_type& top() const { return value[ head[bucket_id] ]; }
  95. bool empty() const { return head[bucket_id] == invalid_value(); }
  96. private:
  97. bucket_type bucket_id;
  98. Iter head;
  99. Iter next;
  100. Iter prev;
  101. IndexValueMap value;
  102. ValueIndexMap id;
  103. };
  104. stack operator[](const bucket_type& i) {
  105. assert(i < head.size());
  106. return stack(i, head.begin(), next.begin(), prev.begin(),
  107. id_to_value.begin(), id);
  108. }
  109. protected:
  110. std::vector<size_type> head;
  111. std::vector<size_type> next;
  112. std::vector<size_type> prev;
  113. std::vector<value_type> id_to_value;
  114. Bucket bucket;
  115. ValueIndexMap id;
  116. };
  117. }
  118. #endif