26 #ifndef MLN_UTIL_SOFT_HEAP_HH
27 # define MLN_UTIL_SOFT_HEAP_HH
59 # include <mln/core/concept/object.hh>
60 # include <mln/trait/value_.hh>
61 # include <mln/util/tracked_ptr.hh>
87 void set_key(
const T& key);
96 template <
typename T,
typename R>
104 node(
const T& ckey,
const R& rank,
109 const T& ckey()
const;
110 const R& rank()
const;
118 void set_ckey(
const T& ckey);
119 void set_rank(
const R& rank);
137 template <
typename T,
typename R>
151 const R& rank()
const;
156 void set_suffix_min(
head<T,R> *suffix_min);
157 void set_rank(
const R& rank);
177 template <
typename T,
typename R>
256 template <
typename L>
257 void clear_list(L *begin, L *end = 0);
269 template <
typename L>
270 void deep_clear_list(L *n);
273 void debug_head_list()
const;
276 void println_()
const;
289 # ifndef MLN_INCLUDE_ONLY
297 template <
typename T>
304 template <
typename T>
306 ilcell<T>::ilcell(
const T& key, ilcell_t next)
307 : key_(key), next_(next)
312 template <
typename T>
314 typename ilcell<T>::ilcell_t
315 ilcell<T>::next()
const
321 template <
typename T>
324 ilcell<T>::key()
const
330 template <
typename T>
333 ilcell<T>::set_next(ilcell_t next)
339 template <
typename T>
342 ilcell<T>::set_key(
const T& key)
355 template <
typename T,
typename R>
358 : il_(0), il_tail_(0)
363 template <
typename T,
typename R>
365 node<T,R>::node(
const T& ckey,
const R& rank,
366 node<T,R> *next, node<T,R> *child,
367 ilcell_t il, ilcell_t il_tail)
368 : ckey_(ckey), rank_(rank), next_(next), child_(child),
369 il_(il), il_tail_(il_tail)
374 template <
typename T,
typename R>
381 template <
typename T,
typename R>
384 node<T,R>::ckey()
const
390 template <
typename T,
typename R>
393 node<T,R>::rank()
const
399 template <
typename T,
typename R>
402 node<T,R>::next()
const
408 template <
typename T,
typename R>
411 node<T,R>::child()
const
417 template <
typename T,
typename R>
419 typename node<T,R>::ilcell_t
420 node<T,R>::il()
const
426 template <
typename T,
typename R>
428 typename node<T,R>::ilcell_t
429 node<T,R>::il_tail()
const
435 template <
typename T,
typename R>
438 node<T,R>::set_il(ilcell_t il)
444 template <
typename T,
typename R>
447 node<T,R>::set_il_tail(ilcell_t il_tail)
453 template <
typename T,
typename R>
456 node<T,R>::set_ckey(
const T& ckey)
462 template <
typename T,
typename R>
465 node<T,R>::set_rank(
const R& rank)
471 template <
typename T,
typename R>
474 node<T,R>::set_next(node<T,R> * next)
480 template <
typename T,
typename R>
483 node<T,R>::set_child(node<T,R> *child)
496 template <
typename T,
typename R>
503 template <
typename T,
typename R>
505 head<T,R>::head(
const R& rank, node<T,R> *queue, head<T,R> *next,
506 head<T,R> *prev, head<T,R> *suffix_min)
507 : queue_(queue), next_(next), prev_(prev),
508 suffix_min_(suffix_min), rank_(rank)
513 template <
typename T,
typename R>
520 template <
typename T,
typename R>
523 head<T,R>::queue()
const
529 template <
typename T,
typename R>
532 head<T,R>::next()
const
538 template <
typename T,
typename R>
541 head<T,R>::prev()
const
547 template <
typename T,
typename R>
550 head<T,R>::suffix_min()
const
556 template <
typename T,
typename R>
559 head<T,R>::rank()
const
565 template <
typename T,
typename R>
568 head<T,R>::set_queue(node<T,R> *queue)
574 template <
typename T,
typename R>
577 head<T,R>::set_next(head<T,R> *next)
583 template <
typename T,
typename R>
586 head<T,R>::set_prev(head<T,R> *prev)
592 template <
typename T,
typename R>
595 head<T,R>::set_suffix_min(head<T,R> *suffix_min)
597 suffix_min_ = suffix_min;
601 template <
typename T,
typename R>
604 head<T,R>::set_rank(
const R& rank)
617 template <
typename T,
typename R>
624 tail_ =
new head<T,R>(mln_max(R), 0, 0, header_);
625 header_->set_next(tail_);
629 template <
typename T,
typename R>
636 deep_clear_list(tmp->queue());
639 clear_list(header_, tail_->next());
643 template <
typename T,
typename R>
655 template <
typename T,
typename R>
663 if (head->queue() != 0)
672 template <
typename T,
typename R>
677 mln_precondition(is_valid());
683 head<T,R> *h = header_->next()->suffix_min();
684 while (h->queue()->il() == 0)
688 while (tmp->next() != 0)
694 if (childcount < h->rank() / 2)
696 h->prev()->set_next(h->next());
697 h->next()->set_prev(h->prev());
698 fix_minlist(h->prev());
700 while (tmp->next() != 0)
706 deep_clear_list(h->queue());
711 h->set_queue(sift(h->queue()));
713 if (h->queue()->ckey() == T::plus_infty())
715 h->prev()->set_next(h->next());
716 h->next()->set_prev(h->prev());
721 deep_clear_list(h_bak->queue());
726 h = header_->next()->suffix_min();
729 min = h->queue()->il()->key();
732 h->queue()->set_il(h->queue()->il()->next());
733 if (h->queue()->il() == 0)
734 h->queue()->set_il_tail(0);
741 template <
typename T,
typename R>
746 return header_ != 0 && tail_ != 0;
750 template <
typename T,
typename R>
755 return nelements_ == 0 ;
759 template <
typename T,
typename R>
768 template <
typename T,
typename R>
773 if (header_->next() == tail_)
778 prevtail->set_next(0);
780 header_->set_next(tail_);
781 tail_->set_prev(header_);
786 deep_clear_list(tmp->queue());
795 template <
typename T,
typename R>
800 clear_list(header_->next(), tail_);
801 header_->set_next(tail_);
802 header_->set_prev(0);
804 tail_->set_prev(header_);
809 template <
typename T,
typename R>
812 soft_heap<T,R>::head_hook_()
const
818 template <
typename T,
typename R>
821 soft_heap<T,R>::meld(node<T,R>* q)
823 head<T,R> *tohead = header_->next();
824 while (q->rank() > tohead->rank())
825 tohead = tohead->next();
826 head<T,R> *prevhead = tohead->prev();
828 node<T,R> *top, *bottom;
830 while (q->rank() == tohead->rank())
835 bottom = tohead->queue();
839 top = tohead->queue();
843 q =
new node<T,R>(top->ckey(), top->rank() + 1, top, bottom,
844 top->il(), top->il_tail());
846 tohead = tohead->next();
850 if (prevhead == tohead->prev())
852 h =
new head<T,R>(q->rank(), q, tohead, prevhead);
857 h = prevhead->next();
860 head<T,R> *head_del = h->next(), *tmp_del;
861 while (head_del != tohead)
863 tmp_del = head_del->next();
870 h->set_rank(q->rank());
871 h->set_prev(prevhead);
874 prevhead->set_next(h);
881 template <
typename T,
typename R>
884 soft_heap<T,R>::fix_minlist(head<T,R> *h)
887 if (h->next() == tail_)
890 tmpmin = h->next()->suffix_min();
896 h->set_suffix_min(tmpmin);
902 template <
typename T,
typename R>
905 soft_heap<T,R>::sift(node<T,R> *v)
913 if (v->next() == 0 && v->child() == 0)
916 v->set_ckey(T::plus_infty());
919 node<T,R> *v_next_bak = v->next();
920 v->set_next(sift(v->next()));
923 while (v_next_bak != v->next())
925 deep_clear_list(v_next_bak->next());
926 deep_clear_list(v_next_bak->child());
927 tmp = v_next_bak->next();
936 v->set_child(v->next());
940 v->set_il(v->next()->il());
941 v->set_il_tail(v->next()->il_tail());
942 v->set_ckey(v->next()->ckey());
944 if (v->rank() > r_ && (v->rank() % 2 == 1
945 || v->child()->rank() <
static_cast<unsigned>((v->rank() - 1))))
947 node<T,R> *v_next_bak = v->next();
948 v->set_next(sift(v->next()));
951 while (v_next_bak != v->next())
953 deep_clear_list(v_next_bak->next());
954 deep_clear_list(v_next_bak->child());
955 tmp = v_next_bak->next();
963 v->set_child(v->next());
967 if (v->next()->ckey() != T::plus_infty() && v->next()->il() != 0)
969 v->next()->il_tail()->set_next(v->il());
970 v->set_il(v->next()->il());
971 if (v->il_tail() == 0)
972 v->set_il_tail(v->next()->il_tail());
973 v->set_ckey(v->next()->ckey());
977 if (v->child()->ckey() == T::plus_infty())
979 if (v->next()->ckey() == T::plus_infty())
982 deep_clear_list(v->child());
983 deep_clear_list(v->next());
990 node<T,R> *next_bak = v->next();
993 deep_clear_list(v->child());
995 v->set_child(v->next()->child());
996 v->set_next(v->next()->next());
1005 template <
typename T,
typename R>
1006 template <
typename L>
1009 soft_heap<T,R>::clear_list(L *begin, L *end)
1012 while (begin != end)
1014 tmp = begin->next();
1021 template <
typename T,
typename R>
1022 template <
typename L>
1025 soft_heap<T,R>::deep_clear_list(L *n)
1027 L *current_node, *tmp;
1033 current_node = st.top();
1035 while (current_node != 0)
1037 if (current_node->child() != 0)
1038 st.push(current_node->child());
1040 tmp = current_node->next();
1041 delete current_node;
1049 template <
typename T,
typename R>
1052 soft_heap<T,R>::debug_head_list()
const
1054 head<T,R> *n = header_;
1055 std::cout <<
"Head list = " << std::endl;
1058 std::cout << n->id <<
"(";
1060 node<T,R> *current_node;
1061 std::stack< node<T,R> *> st;
1062 st.push(n->queue());
1065 current_node = st.top();
1067 while (current_node != 0)
1069 if (current_node->child() != 0)
1070 st.push(current_node->child());
1071 std::cout << current_node->id <<
",";
1072 current_node = current_node->next();
1076 std::cout <<
") - ";
1079 std::cout << std::endl;
1083 template <
typename T,
typename R>
1086 soft_heap<T,R>::println_()
const
1089 std::cout <<
"===============" << std::endl;
1090 head<T,R> *head = header_;
1093 std::cout <<
"<Head>" << std::endl;
1094 node<T,R> *node = head->queue(), *child;
1097 std::cout <<
" <node>" << std::endl;
1099 ilcell_t il(node->il());
1102 std::cout << il->item() << std::endl;
1106 child = node->child();
1109 std::cout <<
" <child>" << std::endl;
1110 ilcell_t il(child->il());
1113 std::cout << il->item() << std::endl;
1116 child = child->child();
1117 std::cout <<
" </child>" << std::endl;
1119 node = node->next();
1121 std::cout <<
" </node>" << std::endl;
1123 std::cout <<
"</Head>" << std::endl;
1124 head = head->next();
1129 # endif // ! MLN_INCLUDE_ONLY
1137 #endif // ! MLN_UTIL_SOFT_HEAP_HH