STXXL  1.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pq_losertree.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/containers/pq_losertree.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 1999 Peter Sanders <[email protected]>
7  * Copyright (C) 2003, 2004, 2007 Roman Dementiev <[email protected]>
8  * Copyright (C) 2007-2009 Johannes Singler <[email protected]>
9  * Copyright (C) 2007, 2008 Andreas Beckmann <[email protected]>
10  *
11  * Distributed under the Boost Software License, Version 1.0.
12  * (See accompanying file LICENSE_1_0.txt or copy at
13  * http://www.boost.org/LICENSE_1_0.txt)
14  **************************************************************************/
15 
16 #ifndef STXXL_CONTAINERS_PQ_LOSERTREE_HEADER
17 #define STXXL_CONTAINERS_PQ_LOSERTREE_HEADER
18 
20 
22 
23 //! \addtogroup stlcontinternals
24 //!
25 //! \{
26 
27 /*! \internal
28  */
29 namespace priority_queue_local {
30 
31 //////////////////////////////////////////////////////////////////////
32 // The data structure from Knuth, "Sorting and Searching", Section 5.4.1
33 /*!
34  * Loser tree from Knuth, "Sorting and Searching", Section 5.4.1
35  * \param MaxArity maximum arity of loser tree, has to be a power of two
36  */
37 template <class ValueType, class CompareType, unsigned MaxArity>
38 class loser_tree : private noncopyable
39 {
40 public:
41  typedef ValueType value_type;
42  typedef CompareType comparator_type;
44  enum { max_arity = MaxArity };
45 
46 private:
47 #if STXXL_PQ_INTERNAL_LOSER_TREE
48  struct Entry
49  {
50  value_type key; // Key of Loser element (winner for 0)
51  unsigned_type index; // number of losing segment
52  };
53 #endif //STXXL_PQ_INTERNAL_LOSER_TREE
54 
56  // stack of free segment indices
58 
59  unsigned_type size_; // total number of elements stored
60  unsigned_type logK; // log of current tree size
61  unsigned_type k; // invariant (k == 1 << logK), always a power of two
62 
63  Element sentinel; // target of free segment pointers
64 
65 #if STXXL_PQ_INTERNAL_LOSER_TREE
66  // upper levels of loser trees
67  // entry[0] contains the winner info
68  Entry entry[MaxArity];
69 #endif //STXXL_PQ_INTERNAL_LOSER_TREE
70 
71  // leaf information
72  // note that Knuth uses indices k..k-1
73  // while we use 0..k-1
74  Element* current[MaxArity]; // pointer to current element
75  Element* current_end[MaxArity]; // pointer to end of block for current element
76  Element* segment[MaxArity]; // start of Segments
77  unsigned_type segment_size[MaxArity]; // just to count the internal memory consumption, in bytes
78 
80 
81  // private member functions
82  unsigned_type initWinner(unsigned_type root);
83  void update_on_insert(unsigned_type node, const Element& newKey, unsigned_type newIndex,
84  Element* winnerKey, unsigned_type* winnerIndex, unsigned_type* mask);
85  void deallocate_segment(unsigned_type slot);
86  void doubleK();
87  void compactTree();
88  void rebuildLoserTree();
89  bool is_segment_empty(unsigned_type slot);
90  void multi_merge_k(Element* target, unsigned_type length);
91 
92 #if STXXL_PQ_INTERNAL_LOSER_TREE
93  template <int LogK>
94  void multi_merge_f(Element* target, unsigned_type length)
95  {
96  //Entry *currentPos;
97  //Element currentKey;
98  //int currentIndex; // leaf pointed to by current entry
99  Element* done = target + length;
100  Entry* regEntry = entry;
101  Element** regStates = current;
102  unsigned_type winnerIndex = regEntry[0].index;
103  Element winnerKey = regEntry[0].key;
104  Element* winnerPos;
105  //Element sup = sentinel; // supremum
106 
107  assert(logK >= LogK);
108  while (target != done)
109  {
110  winnerPos = regStates[winnerIndex];
111 
112  // write result
113  *target = winnerKey;
114 
115  // advance winner segment
116  ++winnerPos;
117  regStates[winnerIndex] = winnerPos;
118  winnerKey = *winnerPos;
119 
120  // remove winner segment if empty now
121  if (is_sentinel(winnerKey))
122  {
123  deallocate_segment(winnerIndex);
124  }
125  ++target;
126 
127  // update loser tree
128 #define TreeStep(L) \
129  if (1 << LogK >= 1 << L) { \
130  Entry* pos ## L = regEntry + ((winnerIndex + (1 << LogK)) >> ((LogK - L + 1 >= 0) ? (LogK - L + 1) : 0)); \
131  Element key ## L = pos ## L->key; \
132  if (cmp(winnerKey, key ## L)) { \
133  unsigned_type index ## L = pos ## L->index; \
134  pos ## L->key = winnerKey; \
135  pos ## L->index = winnerIndex; \
136  winnerKey = key ## L; \
137  winnerIndex = index ## L; \
138  } \
139  }
140  TreeStep(10);
141  TreeStep(9);
142  TreeStep(8);
143  TreeStep(7);
144  TreeStep(6);
145  TreeStep(5);
146  TreeStep(4);
147  TreeStep(3);
148  TreeStep(2);
149  TreeStep(1);
150 #undef TreeStep
151  }
152  regEntry[0].index = winnerIndex;
153  regEntry[0].key = winnerKey;
154  }
155 #endif //STXXL_PQ_INTERNAL_LOSER_TREE
156 
157 public:
158  bool is_sentinel(const Element& a)
159  {
160  return !(cmp(cmp.min_value(), a));
161  }
162  bool not_sentinel(const Element& a)
163  {
164  return cmp(cmp.min_value(), a);
165  }
166 
167 public:
168  loser_tree();
169  ~loser_tree();
170  void init();
171 
172  void swap(loser_tree& obj)
173  {
174  std::swap(cmp, obj.cmp);
175  std::swap(free_slots, obj.free_slots);
176  std::swap(size_, obj.size_);
177  std::swap(logK, obj.logK);
178  std::swap(k, obj.k);
179  std::swap(sentinel, obj.sentinel);
180 #if STXXL_PQ_INTERNAL_LOSER_TREE
181  swap_1D_arrays(entry, obj.entry, MaxArity);
182 #endif //STXXL_PQ_INTERNAL_LOSER_TREE
183  swap_1D_arrays(current, obj.current, MaxArity);
184  swap_1D_arrays(current_end, obj.current_end, MaxArity);
185  swap_1D_arrays(segment, obj.segment, MaxArity);
186  swap_1D_arrays(segment_size, obj.segment_size, MaxArity);
187  std::swap(mem_cons_, obj.mem_cons_);
188  }
189 
190  void multi_merge(Element* begin, Element* end)
191  {
192  multi_merge(begin, end - begin);
193  }
194  void multi_merge(Element*, unsigned_type length);
195 
196  unsigned_type mem_cons() const { return mem_cons_; }
197 
198  bool is_space_available() const // for new segment
199  {
200  return (k < MaxArity) || !free_slots.empty();
201  }
202 
203  //! insert segment beginning at target
204  void insert_segment(Element * target, unsigned_type length);
205 
206  unsigned_type size() const { return size_; }
207 };
208 
209 ///////////////////////// LoserTree ///////////////////////////////////
210 template <class ValueType, class CompareType, unsigned MaxArity>
212  : size_(0), logK(0), k(1), mem_cons_(0)
213 {
214  free_slots.push(0);
215  segment[0] = NULL;
216  current[0] = &sentinel;
217  current_end[0] = &sentinel;
218  // entry and sentinel are initialized by init
219  // since they need the value of supremum
220  init();
221 }
222 
223 template <class ValueType, class CompareType, unsigned MaxArity>
225 {
226  assert(!cmp(cmp.min_value(), cmp.min_value())); // verify strict weak ordering
227  sentinel = cmp.min_value();
228  rebuildLoserTree();
229 #if STXXL_PQ_INTERNAL_LOSER_TREE
230  assert(current[entry[0].index] == &sentinel);
231 #endif //STXXL_PQ_INTERNAL_LOSER_TREE
232 }
233 
234 // rebuild loser tree information from the values in current
235 template <class ValueType, class CompareType, unsigned MaxArity>
237 {
238 #if STXXL_PQ_INTERNAL_LOSER_TREE
239  // MaxArity needs to be a power of two
241  unsigned_type winner = initWinner(1);
242  entry[0].index = winner;
243  entry[0].key = *(current[winner]);
244 #endif //STXXL_PQ_INTERNAL_LOSER_TREE
245 }
246 
247 #if STXXL_PQ_INTERNAL_LOSER_TREE
248 // given any values in the leaves this
249 // routing recomputes upper levels of the tree
250 // from scratch in linear time
251 // initialize entry[root].index and the subtree rooted there
252 // return winner index
253 template <class ValueType, class CompareType, unsigned MaxArity>
255 {
256  if (root >= k) { // leaf reached
257  return root - k;
258  } else {
259  unsigned_type left = initWinner(2 * root);
260  unsigned_type right = initWinner(2 * root + 1);
261  Element lk = *(current[left]);
262  Element rk = *(current[right]);
263  if (!(cmp(lk, rk))) { // right subtree loses
264  entry[root].index = right;
265  entry[root].key = rk;
266  return left;
267  } else {
268  entry[root].index = left;
269  entry[root].key = lk;
270  return right;
271  }
272  }
273 }
274 
275 // first go up the tree all the way to the root
276 // hand down old winner for the respective subtree
277 // based on new value, and old winner and loser
278 // update each node on the path to the root top down.
279 // This is implemented recursively
280 template <class ValueType, class CompareType, unsigned MaxArity>
282  unsigned_type node,
283  const Element& newKey,
284  unsigned_type newIndex,
285  Element* winnerKey,
286  unsigned_type* winnerIndex, // old winner
287  unsigned_type* mask) // 1 << (ceil(log KNK) - dist-from-root)
288 {
289  if (node == 0) { // winner part of root
290  *mask = (unsigned_type)(1) << (logK - 1);
291  *winnerKey = entry[0].key;
292  *winnerIndex = entry[0].index;
293  if (cmp(entry[node].key, newKey))
294  {
295  entry[node].key = newKey;
296  entry[node].index = newIndex;
297  }
298  } else {
299  update_on_insert(node >> 1, newKey, newIndex, winnerKey, winnerIndex, mask);
300  Element loserKey = entry[node].key;
301  unsigned_type loserIndex = entry[node].index;
302  if ((*winnerIndex & *mask) != (newIndex & *mask)) { // different subtrees
303  if (cmp(loserKey, newKey)) { // newKey will have influence here
304  if (cmp(*winnerKey, newKey)) { // old winner loses here
305  entry[node].key = *winnerKey;
306  entry[node].index = *winnerIndex;
307  } else { // new entry loses here
308  entry[node].key = newKey;
309  entry[node].index = newIndex;
310  }
311  }
312  *winnerKey = loserKey;
313  *winnerIndex = loserIndex;
314  }
315  // note that nothing needs to be done if
316  // the winner came from the same subtree
317  // a) newKey <= winnerKey => even more reason for the other tree to lose
318  // b) newKey > winnerKey => the old winner will beat the new
319  // entry further down the tree
320  // also the same old winner is handed down the tree
321 
322  *mask >>= 1; // next level
323  }
324 }
325 #endif //STXXL_PQ_INTERNAL_LOSER_TREE
326 
327 // make the tree two times as wide
328 template <class ValueType, class CompareType, unsigned MaxArity>
330 {
331  STXXL_VERBOSE3("loser_tree::doubleK (before) k=" << k << " logK=" << logK << " MaxArity=" << MaxArity << " #free=" << free_slots.size());
332  assert(k > 0);
333  assert(k < MaxArity);
334  assert(free_slots.empty()); // stack was free (probably not needed)
335 
336  // make all new entries free
337  // and push them on the free stack
338  for (unsigned_type i = 2 * k - 1; i >= k; i--) // backwards
339  {
340  current[i] = &sentinel;
341  current_end[i] = &sentinel;
342  segment[i] = NULL;
343  free_slots.push(i);
344  }
345 
346  // double the size
347  k *= 2;
348  logK++;
349 
350  STXXL_VERBOSE3("loser_tree::doubleK (after) k=" << k << " logK=" << logK << " MaxArity=" << MaxArity << " #free=" << free_slots.size());
351  assert(!free_slots.empty());
352 
353  // recompute loser tree information
354  rebuildLoserTree();
355 }
356 
357 // compact nonempty segments in the left half of the tree
358 template <class ValueType, class CompareType, unsigned MaxArity>
360 {
361  STXXL_VERBOSE3("loser_tree::compactTree (before) k=" << k << " logK=" << logK << " #free=" << free_slots.size());
362  assert(logK > 0);
363 
364  // compact all nonempty segments to the left
365  unsigned_type pos = 0;
366  unsigned_type last_empty = 0;
367  for ( ; pos < k; pos++)
368  {
369  if (not_sentinel(*(current[pos])))
370  {
371  segment_size[last_empty] = segment_size[pos];
372  current[last_empty] = current[pos];
373  current_end[last_empty] = current_end[pos];
374  segment[last_empty] = segment[pos];
375  last_empty++;
376  } /*
377  else
378  {
379  if(segment[pos])
380  {
381  STXXL_VERBOSE2("loser_tree::compactTree() deleting segment "<<pos<<
382  " address: "<<segment[pos]<<" size: "<<segment_size[pos]);
383  delete [] segment[pos];
384  segment[pos] = 0;
385  mem_cons_ -= segment_size[pos];
386  }
387  }*/
388  }
389 
390  // half degree as often as possible
391  while ((k > 1) && ((k / 2) >= last_empty))
392  {
393  k /= 2;
394  logK--;
395  }
396 
397  // overwrite garbage and compact the stack of free segment indices
398  free_slots.clear(); // none free
399  for ( ; last_empty < k; last_empty++)
400  {
401  current[last_empty] = &sentinel;
402  current_end[last_empty] = &sentinel;
403  free_slots.push(last_empty);
404  }
405 
406  STXXL_VERBOSE3("loser_tree::compactTree (after) k=" << k << " logK=" << logK << " #free=" << free_slots.size());
407 
408  // recompute loser tree information
409  rebuildLoserTree();
410 }
411 
412 // insert segment beginning at target
413 // require: is_space_available() == 1
414 template <class ValueType, class CompareType, unsigned MaxArity>
417 {
418  STXXL_VERBOSE2("loser_tree::insert_segment(" << target << "," << length << ")");
419  //std::copy(target,target + length,std::ostream_iterator<ValueType>(std::cout, "\n"));
420 
421  if (length > 0)
422  {
423  assert(not_sentinel(target[0]));
424  assert(not_sentinel(target[length - 1]));
425  assert(is_sentinel(target[length]));
426 
427  // get a free slot
428  if (free_slots.empty())
429  { // tree is too small
430  doubleK();
431  }
432  assert(!free_slots.empty());
433  unsigned_type index = free_slots.top();
434  free_slots.pop();
435 
436  // link new segment
437  current[index] = segment[index] = target;
438  current_end[index] = target + length;
439  segment_size[index] = (length + 1) * sizeof(value_type);
440  mem_cons_ += (length + 1) * sizeof(value_type);
441  size_ += length;
442 
443 #if STXXL_PQ_INTERNAL_LOSER_TREE
444  // propagate new information up the tree
445  Element dummyKey;
446  unsigned_type dummyIndex;
447  unsigned_type dummyMask;
448  update_on_insert((index + k) >> 1, *target, index,
449  &dummyKey, &dummyIndex, &dummyMask);
450 #endif //STXXL_PQ_INTERNAL_LOSER_TREE
451  } else {
452  // immediately deallocate
453  // this is not only an optimization
454  // but also needed to keep free segments from
455  // clogging up the tree
456  delete[] target;
457  }
458 }
459 
460 template <class ValueType, class CompareType, unsigned MaxArity>
462 {
463  STXXL_VERBOSE1("loser_tree::~loser_tree()");
464  for (unsigned_type i = 0; i < k; ++i)
465  {
466  if (segment[i])
467  {
468  STXXL_VERBOSE2("loser_tree::~loser_tree() deleting segment " << i);
469  delete[] segment[i];
470  mem_cons_ -= segment_size[i];
471  }
472  }
473  // check whether we have not lost any memory
474  assert(mem_cons_ == 0);
475 }
476 
477 // free an empty segment .
478 template <class ValueType, class CompareType, unsigned MaxArity>
481 {
482  // reroute current pointer to some empty sentinel segment
483  // with a sentinel key
484  STXXL_VERBOSE2("loser_tree::deallocate_segment() deleting segment " <<
485  slot << " address: " << segment[slot] << " size: " << (segment_size[slot] / sizeof(value_type)) - 1);
486  current[slot] = &sentinel;
487  current_end[slot] = &sentinel;
488 
489  // free memory
490  delete[] segment[slot];
491  segment[slot] = NULL;
492  mem_cons_ -= segment_size[slot];
493 
494  // push on the stack of free segment indices
495  free_slots.push(slot);
496 }
497 
498 // delete the length smallest elements and write them to target
499 // empty segments are deallocated
500 // require:
501 // - there are at least length elements
502 // - segments are ended by sentinels
503 template <class ValueType, class CompareType, unsigned MaxArity>
506 {
507  STXXL_VERBOSE3("loser_tree::multi_merge(target=" << target << ", len=" << length << ") k=" << k);
508 
509  if (length == 0)
510  return;
511 
512  assert(k > 0);
513  assert(length <= size_);
514 
515  //This is the place to make statistics about internal multi_merge calls.
516 
517 #if STXXL_PARALLEL && STXXL_PARALLEL_PQ_MULTIWAY_MERGE_INTERNAL
519 #endif
520  switch (logK) {
521  case 0:
522  assert(k == 1);
523 #if STXXL_PQ_INTERNAL_LOSER_TREE
524  assert(entry[0].index == 0);
525 #endif //STXXL_PQ_INTERNAL_LOSER_TREE
526  assert(free_slots.empty());
527  memcpy(target, current[0], length * sizeof(Element));
528  //std::copy(current[0], current[0] + length, target);
529  current[0] += length;
530 #if STXXL_PQ_INTERNAL_LOSER_TREE
531  entry[0].key = **current;
532 #endif //STXXL_PQ_INTERNAL_LOSER_TREE
533  if (is_segment_empty(0))
534  deallocate_segment(0);
535 
536  break;
537  case 1:
538  assert(k == 2);
539 #if STXXL_PARALLEL && STXXL_PARALLEL_PQ_MULTIWAY_MERGE_INTERNAL
540  {
541  std::pair<Element*, Element*> seqs[2] =
542  {
543  std::make_pair(current[0], current_end[0]),
544  std::make_pair(current[1], current_end[1])
545  };
546  parallel::multiway_merge_sentinel(seqs, seqs + 2, target, inv_cmp, length);
547  current[0] = seqs[0].first;
548  current[1] = seqs[1].first;
549  }
550 #else
551  merge_iterator(current[0], current[1], target, length, cmp);
552  rebuildLoserTree();
553 #endif
554  if (is_segment_empty(0))
555  deallocate_segment(0);
556 
557  if (is_segment_empty(1))
558  deallocate_segment(1);
559 
560  break;
561  case 2:
562  assert(k == 4);
563 #if STXXL_PARALLEL && STXXL_PARALLEL_PQ_MULTIWAY_MERGE_INTERNAL
564  {
565  std::pair<Element*, Element*> seqs[4] =
566  {
567  std::make_pair(current[0], current_end[0]),
568  std::make_pair(current[1], current_end[1]),
569  std::make_pair(current[2], current_end[2]),
570  std::make_pair(current[3], current_end[3])
571  };
572  parallel::multiway_merge_sentinel(seqs, seqs + 4, target, inv_cmp, length);
573  current[0] = seqs[0].first;
574  current[1] = seqs[1].first;
575  current[2] = seqs[2].first;
576  current[3] = seqs[3].first;
577  }
578 #else
579  if (is_segment_empty(3))
580  merge3_iterator(current[0], current[1], current[2], target, length, cmp);
581  else
582  merge4_iterator(current[0], current[1], current[2], current[3], target, length, cmp);
583 
584  rebuildLoserTree();
585 #endif
586  if (is_segment_empty(0))
587  deallocate_segment(0);
588 
589  if (is_segment_empty(1))
590  deallocate_segment(1);
591 
592  if (is_segment_empty(2))
593  deallocate_segment(2);
594 
595  if (is_segment_empty(3))
596  deallocate_segment(3);
597 
598  break;
599 #if !(STXXL_PARALLEL && STXXL_PARALLEL_PQ_MULTIWAY_MERGE_INTERNAL)
600  case 3: multi_merge_f<3>(target, length);
601  break;
602  case 4: multi_merge_f<4>(target, length);
603  break;
604  case 5: multi_merge_f<5>(target, length);
605  break;
606  case 6: multi_merge_f<6>(target, length);
607  break;
608  case 7: multi_merge_f<7>(target, length);
609  break;
610  case 8: multi_merge_f<8>(target, length);
611  break;
612  case 9: multi_merge_f<9>(target, length);
613  break;
614  case 10: multi_merge_f<10>(target, length);
615  break;
616 #endif
617  default:
618 #if STXXL_PARALLEL && STXXL_PARALLEL_PQ_MULTIWAY_MERGE_INTERNAL
619  {
620  std::vector<std::pair<Element*, Element*> > seqs;
621  std::vector<int_type> orig_seq_index;
622  for (unsigned int i = 0; i < k; ++i)
623  {
624  if (current[i] != current_end[i] && !is_sentinel(*current[i]))
625  {
626  seqs.push_back(std::make_pair(current[i], current_end[i]));
627  orig_seq_index.push_back(i);
628  }
629  }
630 
631  parallel::multiway_merge_sentinel(seqs.begin(), seqs.end(), target, inv_cmp, length);
632 
633  for (unsigned int i = 0; i < seqs.size(); ++i)
634  {
635  int_type seg = orig_seq_index[i];
636  current[seg] = seqs[i].first;
637  }
638 
639  for (unsigned int i = 0; i < k; ++i)
640  if (is_segment_empty(i))
641  {
642  STXXL_VERBOSE3("deallocated " << i);
643  deallocate_segment(i);
644  }
645  }
646 #else
647  multi_merge_k(target, length);
648 #endif
649  break;
650  }
651 
652  size_ -= length;
653 
654  // compact tree if it got considerably smaller
655  {
656  const unsigned_type num_segments_used = k - free_slots.size();
657  const unsigned_type num_segments_trigger = k - (3 * k / 5);
658  // using k/2 would be worst case inefficient (for large k)
659  // for k \in {2, 4, 8} the trigger is k/2 which is good
660  // because we have special mergers for k \in {1, 2, 4}
661  // there is also a special 3-way-merger, that will be
662  // triggered if k == 4 && is_segment_empty(3)
663  STXXL_VERBOSE3("loser_tree compact? k=" << k << " #used=" << num_segments_used
664  << " <= #trigger=" << num_segments_trigger << " ==> "
665  << ((k > 1 && num_segments_used <= num_segments_trigger) ? "yes" : "no ")
666  << " || "
667  << ((k == 4 && !free_slots.empty() && !is_segment_empty(3)) ? "yes" : "no ")
668  << " #free=" << free_slots.size());
669  if (k > 1 && ((num_segments_used <= num_segments_trigger) ||
670  (k == 4 && !free_slots.empty() && !is_segment_empty(3))))
671  {
672  compactTree();
673  }
674  }
675  //std::copy(target,target + length,std::ostream_iterator<ValueType>(std::cout, "\n"));
676 }
677 
678 // is this segment empty and does not point to sentinel yet?
679 template <class ValueType, class CompareType, unsigned MaxArity>
682 {
683  return (is_sentinel(*(current[slot])) && (current[slot] != &sentinel));
684 }
685 
686 #if STXXL_PQ_INTERNAL_LOSER_TREE
687 // multi-merge for arbitrary K
688 template <class ValueType, class CompareType, unsigned MaxArity>
690 multi_merge_k(Element* target, unsigned_type length)
691 {
692  Entry* currentPos;
693  Element currentKey;
694  unsigned_type currentIndex; // leaf pointed to by current entry
695  unsigned_type kReg = k;
696  Element* done = target + length;
697  unsigned_type winnerIndex = entry[0].index;
698  Element winnerKey = entry[0].key;
699  Element* winnerPos;
700 
701  while (target != done)
702  {
703  winnerPos = current[winnerIndex];
704 
705  // write result
706  *target = winnerKey;
707 
708  // advance winner segment
709  ++winnerPos;
710  current[winnerIndex] = winnerPos;
711  winnerKey = *winnerPos;
712 
713  // remove winner segment if empty now
714  if (is_sentinel(winnerKey)) //
715  deallocate_segment(winnerIndex);
716 
717  // go up the entry-tree
718  for (unsigned_type i = (winnerIndex + kReg) >> 1; i > 0; i >>= 1) {
719  currentPos = entry + i;
720  currentKey = currentPos->key;
721  if (cmp(winnerKey, currentKey)) {
722  currentIndex = currentPos->index;
723  currentPos->key = winnerKey;
724  currentPos->index = winnerIndex;
725  winnerKey = currentKey;
726  winnerIndex = currentIndex;
727  }
728  }
729 
730  ++target;
731  }
732  entry[0].index = winnerIndex;
733  entry[0].key = winnerKey;
734 }
735 #endif // STXXL_PQ_INTERNAL_LOSER_TREE
736 
737 } // namespace priority_queue_local
738 
739 //! \}
740 
742 
743 #endif // !STXXL_CONTAINERS_PQ_LOSERTREE_HEADER
744 // vim: et:ts=4:sw=4
Inverts the order of a comparison functor by swapping its arguments.
Definition: pq_helpers.h:187
bool is_segment_empty(unsigned_type slot)
Definition: pq_losertree.h:681
void merge3_iterator(InputIterator &source0, InputIterator &source1, InputIterator &source2, OutputIterator target, SizeType length, CompareType &cmp)
Definition: pq_mergers.h:75
void swap_1D_arrays(Type *a, Type *b, unsigned_type size)
Definition: utils.h:246
#define STXXL_VERBOSE2(x)
Definition: verbose.h:107
Loser tree from Knuth, &quot;Sorting and Searching&quot;, Section 5.4.1.
Definition: pq_losertree.h:38
void multi_merge_k(Element *target, unsigned_type length)
unsigned_type initWinner(unsigned_type root)
#define STXXL_VERBOSE3(x)
Definition: verbose.h:113
choose_int_types< my_pointer_size >::int_type int_type
Definition: types.h:63
void multi_merge(Element *begin, Element *end)
Definition: pq_losertree.h:190
unsigned_type segment_size[MaxArity]
Definition: pq_losertree.h:77
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
#define STXXL_VERBOSE1(x)
Definition: verbose.h:99
void update_on_insert(unsigned_type node, const Element &newKey, unsigned_type newIndex, Element *winnerKey, unsigned_type *winnerIndex, unsigned_type *mask)
void insert_segment(Element *target, unsigned_type length)
insert segment beginning at target
Definition: pq_losertree.h:416
void deallocate_segment(unsigned_type slot)
Definition: pq_losertree.h:480
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:64
internal_bounded_stack< unsigned_type, MaxArity > free_slots
Definition: pq_losertree.h:57
#define TreeStep(L)
void merge_iterator(InputIterator &source0, InputIterator &source1, OutputIterator target, SizeType length, CompareType &cmp)
Definition: pq_mergers.h:42
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
void merge4_iterator(InputIterator &source0, InputIterator &source1, InputIterator &source2, InputIterator &source3, OutputIterator target, SizeType length, CompareType &cmp)
Definition: pq_mergers.h:143