23 #define mDoSort(extra_var,extra_action,sztype) \ 26 for ( sztype d=sz/2; d>0; d=d/2 ) \ 27 for ( sztype i=d; i<sz; i++ ) \ 28 for ( sztype j=i-d; j>=0 && arr[j]>arr[j+d]; j-=d ) \ 30 tmp = arr[j]; arr[j] = arr[j+d]; arr[j+d] = tmp; \ 36 template <
class T,
class I>
41 template <class T, class IT,class I>
43 mDoSort(IT itmp,itmp = idxs[j]; idxs[j] = idxs[j+d]; idxs[j+d] = itmp,I)
46 #define mDoSort(extra_var,extra_action,sztype) \ 49 for ( sztype d=sz/2; d>0; d=d/2 ) \ 50 for ( sztype i=d; i<sz; i++ ) \ 51 for ( sztype j=i-d; j>=0 && arr[j]>arr[j+d]; j-=d ) \ 53 Swap( arr[j], arr[j+d] ); \ 64 template <class T, class IT>
66 mDoSort(IT itmp,itmp = idxs[j]; idxs[j] = idxs[j+d]; idxs[j+d] = itmp,
int)
71 template <
class T,
class I>
76 for ( I idx=0; idx<sz; ++idx )
78 const int vidx = vals.
indexOf( arr[idx] );
81 if ( vals.
size()>maxnrvals )
95 for (
int idx=0; idx<vsize; idx++ )
100 for (
int idx=0; idx<vsize; ++idx )
102 for (
int idy=count[idxs[idx]]-1; idy>=0; --idy )
103 arr[++index] = vals[idx];
132 static bool mergeLists(
const T* vals, T* res,
133 int start0,
int start1,
int start2,
134 int stop,
int& totalsz );
165 const int localseed = (partsortglobalseed *
FA +
FC) %
FM;
171 partsortglobalseed = localseed;
173 return (
float) localseed;
177 template <
class T,
class I>
inline 179 I* jstart, I* jstop )
181 I ipivot, ileft, iright;
186 ipivot = (int)(istart + (istop-istart) * (float)localseed / (
float)
FM + .5);
187 if ( ipivot < istart ) ipivot = istart;
188 if ( ipivot > istop ) ipivot = istop;
189 pivotval = arr[ipivot];
191 for ( ileft=istart, iright=istop; ; )
193 while ( arr[ileft] <=pivotval && ileft<istop ) ileft++;
194 while ( arr[iright]>=pivotval && iright>istart ) iright--;
195 if ( ileft < iright )
198 arr[ileft++] = arr[iright];
204 if ( ileft < ipivot )
207 arr[ileft++] = arr[ipivot];
210 else if ( ipivot < iright )
213 arr[iright--] = arr[ipivot];
222 template <
class T,
class I>
inline 228 for ( i=istart+1; i<=istop; i++ )
230 for ( arr_i=arr[i],j=i; j>istart && arr[j-1]>arr_i; j-- )
237 template <
class T,
class I>
inline 243 I j, k, p = 0, q = sz-1;
249 if ( itarget <= j ) q = j;
250 else if ( itarget >= k ) p = k;
258 template <
class T,
class I>
inline 265 qstack[top++] = sz - 1;
294 template <
class T,
class IT>
inline 295 void partSort( T* arr, IT* iarr,
int istart,
int istop,
int* jstart,
int* jstop)
297 int ipivot, ileft, iright;
303 ipivot = (int)(istart + (istop-istart) * (float)localseed / (
float)
FM);
304 if ( ipivot < istart ) ipivot = istart;
305 if ( ipivot > istop ) ipivot = istop;
306 pivotval = arr[ipivot];
308 for ( ileft=istart, iright=istop; ; )
310 while ( arr[ileft] <=pivotval && ileft<istop ) ileft++;
311 while ( arr[iright]>=pivotval && iright>istart ) iright--;
312 if ( ileft < iright )
317 iarr[ileft] = iarr[iright];
318 arr[ileft++] = arr[iright];
326 if ( ileft < ipivot )
331 iarr[ileft] = iarr[ipivot];
332 arr[ileft++] = arr[ipivot];
337 else if ( ipivot < iright )
342 iarr[iright] = iarr[ipivot];
343 arr[iright--] = arr[ipivot];
354 template <
class T,
class IT>
inline 361 for ( i=istart+1; i<=istop; i++ )
363 for ( iarr_i=iarr[i],arr_i=arr[i],j=i; j>istart && arr[j-1]>arr_i; j-- )
374 template <
class T,
class IT>
375 void sortFor( T* arr, IT* iarr,
int sz,
int itarget )
377 int j, k, p = 0, q = sz-1;
381 partSort( arr, iarr, p, q, &j, &k );
383 if ( itarget <= j ) q = j;
384 else if ( itarget >= k ) p = k;
392 template <
class T,
class IT>
inline 398 qstack[top++] = sz - 1;
407 partSort( arr, iarr, p, q, &j, &k );
435 template <
class T>
inline 440 , barrier_( -1, false )
448 template <
class T>
inline 461 template <
class T>
inline 484 template <
class T>
inline 497 template <
class T>
inline 500 const int threadsize = stop-start+1;
501 if ( threadsize<100 )
569 curstart0, curstart1, curstart2, curstop, cursize) )
582 template <
class T>
inline 584 int start0,
int start1,
int start2,
585 int stop,
int& totalsz )
587 const int sz0 = start1-start0;
588 const int sz1 = start2==-1 ? stop-start1+1 : start2-start1;
589 const int sz2 = start2==-1 ? 0 : stop-start2+1;
590 totalsz = sz0+sz1+sz2;
592 const T* ptr0 = valptr + start0;
593 const T* stopptr0 = ptr0+sz0;
594 const T* ptr1 = valptr + start1;
595 const T* stopptr1 = ptr1+sz1;
596 const T* ptr2 = start2==-1 ? 0 : valptr + start2;
597 const T* stopptr2 = ptr2+sz2;
601 if ( ptr0 && (!ptr1 || *ptr0<*ptr1) && (!ptr2 || *ptr0<*ptr2 ) )
603 (*result++) = (*ptr0++);
604 if ( ptr0==stopptr0 )
607 else if ( ptr1 && ( !ptr2 || *ptr1<*ptr2 ) )
609 (*result++) = (*ptr1++);
610 if ( ptr1==stopptr1 )
615 (*result++) = (*ptr2++);
616 if ( ptr2==stopptr2 )
Is an object that faciliates many threads to wait for something to happen.
Definition: thread.h:108
void insertionSort(T *arr, I istart, I istop)
Definition: sorting.h:223
void partSort(T *arr, I istart, I istop, I *jstart, I *jstop)
Definition: sorting.h:178
void sort_idxabl(T &arr, int sz)
Definition: sorting.h:60
Mutex & mutex()
Definition: thread.h:262
int nrThreads() const
Definition: thread.h:245
const int nrvals_
Definition: sorting.h:144
#define mExtern(module)
Definition: commondefs.h:166
#define NSMALL
Definition: sorting.h:155
void sortFor(T *arr, I sz, I itarget)
Definition: sorting.h:238
void sort_array(T *arr, I sz)
Definition: sorting.h:37
#define FC
Definition: sorting.h:158
TypeSet< int > starts_
Definition: sorting.h:148
#define mCast(tp, v)
Definition: commondefs.h:124
#define od_int64
Definition: plftypes.h:36
Sorting in parallel. Code is still experimental.
Definition: sorting.h:120
int totalnr_
Definition: sorting.h:145
#define mDoSort(extra_var, extra_action, sztype)
Definition: sorting.h:46
virtual bool shouldContinue()
mExtern(Algo) Threads float getPartSortSeed()
Definition: sorting.h:163
od_int64 nrDone() const
May be -1, i.e. class does not report nrdone.
Definition: sorting.h:135
Generalization of a task that can be run in parallel.
Definition: paralleltask.h:66
virtual T * arr()
3rd party access
Definition: typeset.h:92
void quickSort(T *arr, I sz)
Definition: sorting.h:259
interface to threads that should be portable.
Definition: atomic.h:28
bool duplicate_sort(T *arr, I sz, int maxnrvals)
Definition: sorting.h:72
Set of (small) copyable elements.
Definition: commontypes.h:30
int minThreadSize() const
Definition: sorting.h:128
ArrPtrMan< T > tmpbuffer_
Definition: sorting.h:138
TypeSet< int > newstarts_
Definition: sorting.h:149
#define mTryAlloc(var, stmt)
Catches bad_alloc and sets ptr to null as normal.
Definition: commondefs.h:241
#define FA
Definition: sorting.h:157
bool doPrepare(int)
Definition: sorting.h:462
void sort_idxabl_coupled(T &arr, IT *idxs, int sz)
Definition: sorting.h:65
ParallelSorter(T *vals, int sz)
Definition: sorting.h:436
bool doFinish(bool)
Definition: sorting.h:485
#define NSTACK
Definition: sorting.h:159
Waits for a number of threads to reach a certain point (i.e. the call to Barrier::waitForAll). Once everyone has arrived, everyone is released.
Definition: thread.h:240
static bool mergeLists(const T *vals, T *res, int start0, int start1, int start2, int stop, int &totalsz)
Definition: sorting.h:583
bool doWork(od_int64, od_int64, int)
Definition: sorting.h:498
#define FM
Definition: sorting.h:156
od_int64 nrIterations() const
Definition: sorting.h:126
virtual void erase()
Definition: typeset.h:339
T * buf_
Definition: sorting.h:142
Threads::Barrier barrier_
Definition: sorting.h:151
Threads::ConditionVar condvar_
Definition: sorting.h:147
int * idxs_
Definition: sorting.h:140
T * vals_
Definition: sorting.h:137
bool waitForAll(bool unlock=true)
virtual void removeSingle(size_type, bool preserver_order=true)
Definition: typeset.h:500
#define mClass(module)
Definition: commondefs.h:164
virtual size_type indexOf(T, bool forward=true, size_type start=-1) const
void addToNrDone(int64_t increment)
T * curvals_
Definition: sorting.h:141
void sort_coupled(T *arr, IT *idxs, I sz)
Definition: sorting.h:42