Classes | Functions
qtl.h File Reference
#include "qtextstream.h"
#include "qstring.h"

Go to the source code of this file.

Classes

class  QTextOStreamIterator< T >
 

Functions

template<class InputIterator , class OutputIterator >
OutputIterator qCopy (InputIterator _begin, InputIterator _end, OutputIterator _dest)
 
template<class T >
void qSwap (T &_value1, T &_value2)
 
template<class InputIterator >
void qBubbleSort (InputIterator b, InputIterator e)
 
template<class Container >
void qBubbleSort (Container &c)
 
template<class Value >
void qHeapSortPushDown (Value *heap, int first, int last)
 
template<class InputIterator , class Value >
void qHeapSortHelper (InputIterator b, InputIterator e, Value, uint n)
 
template<class InputIterator >
void qHeapSort (InputIterator b, InputIterator e)
 
template<class Container >
void qHeapSort (Container &c)
 

Function Documentation

template<class InputIterator >
void qBubbleSort ( InputIterator  b,
InputIterator  e 
)
inline

Definition at line 89 of file qtl.h.

90 {
91  // Goto last element;
92  InputIterator last = e;
93  --last;
94  // only one element or no elements ?
95  if ( last == b )
96  return;
97 
98  // So we have at least two elements in here
99  while( b != last ) {
100  bool swapped = FALSE;
101  InputIterator swap_pos = b;
102  InputIterator x = e;
103  InputIterator y = x;
104  y--;
105  do {
106  --x;
107  --y;
108  if ( *x < *y ) {
109  swapped = TRUE;
110  qSwap( *x, *y );
111  swap_pos = y;
112  }
113  } while( y != b );
114  if ( !swapped )
115  return;
116  b = swap_pos;
117  b++;
118  }
119 }
const bool FALSE
Definition: qglobal.h:370
void qSwap(T &_value1, T &_value2)
Definition: qtl.h:80
const double e
static bool * b
Definition: config.cpp:1043
list x
Definition: train.py:276
const bool TRUE
Definition: qglobal.h:371
template<class Container >
void qBubbleSort ( Container &  c)
inline

Definition at line 123 of file qtl.h.

124 {
125  qBubbleSort( c.begin(), c.end() );
126 }
void qBubbleSort(InputIterator b, InputIterator e)
Definition: qtl.h:89
template<class InputIterator , class OutputIterator >
OutputIterator qCopy ( InputIterator  _begin,
InputIterator  _end,
OutputIterator  _dest 
)
inline

Definition at line 70 of file qtl.h.

72 {
73  while( _begin != _end )
74  *_dest++ = *_begin++;
75  return _dest;
76 }
template<class InputIterator >
void qHeapSort ( InputIterator  b,
InputIterator  e 
)
inline

Definition at line 192 of file qtl.h.

193 {
194  // Empty ?
195  if ( b == e )
196  return;
197 
198  // How many entries have to be sorted ?
199  InputIterator it = b;
200  uint n = 0;
201  while ( it != e ) {
202  ++n;
203  ++it;
204  }
205 
206  // The second last parameter is a hack to retrieve the value type
207  // Do the real sorting here
208  qHeapSortHelper( b, e, *b, n );
209 }
void qHeapSortHelper(InputIterator b, InputIterator e, Value, uint n)
Definition: qtl.h:161
const double e
std::void_t< T > n
static bool * b
Definition: config.cpp:1043
unsigned uint
Definition: qglobal.h:351
template<class Container >
void qHeapSort ( Container &  c)
inline

Definition at line 213 of file qtl.h.

214 {
215  if ( c.isEmpty() )
216  return;
217 
218  // The second last parameter is a hack to retrieve the value type
219  // Do the real sorting here
220  qHeapSortHelper( c.begin(), c.end(), *(c.begin()), c.count() );
221 }
void qHeapSortHelper(InputIterator b, InputIterator e, Value, uint n)
Definition: qtl.h:161
template<class InputIterator , class Value >
void qHeapSortHelper ( InputIterator  b,
InputIterator  e,
Value  ,
uint  n 
)
inline

Definition at line 161 of file qtl.h.

162 {
163  // Create the heap
164  InputIterator insert = b;
165  Value* realheap = new Value[ n ];
166  // Wow, what a fake. But I want the heap to be indexed as 1...n
167  Value* heap = realheap - 1;
168  int size = 0;
169  for( ; insert != e; ++insert ) {
170  heap[++size] = *insert;
171  int i = size;
172  while( i > 1 && heap[i] < heap[ i / 2 ] ) {
173  qSwap( heap[i], heap[ i / 2 ] );
174  i /= 2;
175  }
176  }
177 
178  // Now do the sorting
179  for( uint i = n; i > 0; i-- ) {
180  *b++ = heap[1];
181  if ( i > 1 ) {
182  heap[1] = heap[i];
183  qHeapSortPushDown( heap, 1, (int)i - 1 );
184  }
185  }
186 
187  delete[] realheap;
188 }
void qSwap(T &_value1, T &_value2)
Definition: qtl.h:80
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
const double e
std::void_t< T > n
static bool * b
Definition: config.cpp:1043
void qHeapSortPushDown(Value *heap, int first, int last)
Definition: qtl.h:130
unsigned uint
Definition: qglobal.h:351
template<class Value >
void qHeapSortPushDown ( Value *  heap,
int  first,
int  last 
)
inline

Definition at line 130 of file qtl.h.

131 {
132  int r = first;
133  while( r <= last/2 ) {
134  // Node r has only one child ?
135  if ( last == 2*r ) {
136  // Need for swapping ?
137  if ( heap[r] > heap[ 2*r ] )
138  qSwap( heap[r], heap[ 2*r ] );
139  // That's it ...
140  r = last;
141  } else { // Node has two children
142  if ( heap[r] > heap[ 2*r ] && heap[ 2*r ] <= heap[ 2*r+1 ] ) {
143  // Swap with left child
144  qSwap( heap[r], heap[ 2*r ] );
145  r *= 2;
146  } else if ( heap[r] > heap[ 2*r+1 ] &&
147  heap[ 2*r+1 ] < heap[ 2*r ] ) {
148  // Swap with right child
149  qSwap( heap[r], heap[ 2*r+1 ] );
150  r = 2*r+1;
151  } else {
152  // We are done
153  r = last;
154  }
155  }
156  }
157 }
void qSwap(T &_value1, T &_value2)
Definition: qtl.h:80
template<class T >
void qSwap ( T &  _value1,
T &  _value2 
)
inline

Definition at line 80 of file qtl.h.

81 {
82  T tmp = _value1;
83  _value1 = _value2;
84  _value2 = tmp;
85 }
string tmp
Definition: languages.py:63