1 #if !defined HAVE_MINMAXIDXFUNC_H__
2 #define HAVE_MINMAXIDXFUNC_H__
3 // This file is part of the FXT library.
4 // Copyright (C) 2010, 2012 Joerg Arndt
5 // License: GNU General Public License version 3 or later,
6 // see the file COPYING.txt in the main directory.
7
8 #include "fxttypes.h"
9
10
11 template <typename Type>
idx_min(const Type * f,ulong n,const ulong * x,int (* cmp)(const Type &,const Type &))12 Type idx_min(const Type *f, ulong n, const ulong *x,
13 int (*cmp)(const Type &, const Type &))
14 // Return minimum (value) of array elements
15 // {f[x[0]], f[x[1]], ..., f[x[n-1]]}
16 // with respect to comparison function cmp()
17 {
18 Type v = f[x[0]];
19 while ( n-- ) { if ( cmp(f[x[n]], v) < 0 ) v = f[x[n]]; }
20 return v;
21 }
22 // -------------------------
23
24 template <typename Type>
idx_max(const Type * f,ulong n,const ulong * x,int (* cmp)(const Type &,const Type &))25 Type idx_max(const Type *f, ulong n, const ulong *x,
26 int (*cmp)(const Type &, const Type &))
27 // Return maximum (value) of array elements
28 // {f[x[0]], f[x[1]], ..., f[x[n-1]]}
29 // with respect to comparison function cmp()
30 {
31 Type v = f[x[0]];
32 while ( n-- ) { if ( cmp(f[x[n]], v) > 0 ) v = f[x[n]]; }
33 return v;
34 }
35 // -------------------------
36
37 template <typename Type>
is_idx_partitioned(const Type * f,ulong n,const ulong * x,ulong k,int (* cmp)(const Type &,const Type &))38 bool is_idx_partitioned(const Type *f, ulong n, const ulong *x, ulong k,
39 int (*cmp)(const Type &, const Type &))
40 {
41 ++k;
42 Type lmax = idx_max(f, k, x, cmp);
43 Type rmin = idx_min(f, n-k, x+k, cmp);
44 return ( cmp(lmax, rmin) <= 0 );
45 }
46 // -------------------------
47
48
49 #endif // !defined HAVE_MINMAXIDXFUNC_H__
50