38 #ifndef GMM_ALGOBASE_H__ 39 #define GMM_ALGOBASE_H__ 52 struct less :
public std::binary_function<T, T, int> {
53 inline int operator()(
const T& x,
const T& y)
const 54 {
return (x < y) ? -1 : ((y < x) ? 1 : 0); }
57 template<>
struct less<int> :
public std::binary_function<int, int, int>
58 {
int operator()(
int x,
int y)
const {
return x-y; } };
59 template<>
struct less<char> :
public std::binary_function<char, char, int>
60 {
int operator()(
char x,
char y)
const {
return int(x-y); } };
61 template<>
struct less<short> :
public std::binary_function<short,short,int>
62 {
int operator()(
short x,
short y)
const {
return int(x-y); } };
63 template<>
struct less<unsigned char>
64 :
public std::binary_function<unsigned char, unsigned char, int> {
65 int operator()(
unsigned char x,
unsigned char y)
const 66 {
return int(x)-int(y); }
71 struct greater :
public std::binary_function<T, T, int> {
72 inline int operator()(
const T& x,
const T& y)
const 73 {
return (y < x) ? -1 : ((x < y) ? 1 : 0); }
76 template<>
struct greater<int> :
public std::binary_function<int, int, int>
77 {
int operator()(
int x,
int y)
const {
return y-x; } };
78 template<>
struct greater<char> :
public std::binary_function<char,char,int>
79 {
int operator()(
char x,
char y)
const {
return int(y-x); } };
80 template<>
struct greater<short>
81 :
public std::binary_function<short, short, int>
82 {
int operator()(
short x,
short y)
const {
return int(y-x); } };
83 template<>
struct greater<unsigned char>
84 :
public std::binary_function<unsigned char, unsigned char, int> {
85 int operator()(
unsigned char x,
unsigned char y)
const 86 {
return int(y)-int(x); }
89 template <
typename T>
inline T my_abs(T a) {
return (a < T(0)) ? T(-a) : a; }
92 struct approx_less :
public std::binary_function<T, T, int> {
94 inline int operator()(
const T &x,
const T &y)
const 95 {
if (my_abs(x - y) <= eps)
return 0;
if (x < y)
return -1;
return 1; }
96 approx_less(
double e = 1E-13) { eps = e; }
100 struct approx_greater :
public std::binary_function<T, T, int> {
102 inline int operator()(
const T &x,
const T &y)
const 103 {
if (my_abs(x - y) <= eps)
return 0;
if (x > y)
return -1;
return 1; }
104 approx_greater(
double e = 1E-13) { eps = e; }
107 template<
class ITER1,
class ITER2,
class COMP>
108 int lexicographical_compare(ITER1 b1,
const ITER1 &e1,
109 ITER2 b2,
const ITER2 &e2,
const COMP &c) {
111 for ( ; b1 != e1 && b2 != e2; ++b1, ++b2)
112 if ((i = c(*b1, *b2)) != 0)
return i;
113 if (b1 != e1)
return 1;
114 if (b2 != e2)
return -1;
118 template<
class CONT,
class COMP = gmm::less<
typename CONT::value_type> >
119 struct lexicographical_less :
public std::binary_function<CONT, CONT, int>
122 int operator()(
const CONT &x,
const CONT &y)
const {
123 return gmm::lexicographical_compare(x.begin(), x.end(),
124 y.begin(), y.end(), c);
126 lexicographical_less(
const COMP &d = COMP()) { c = d; }
129 template<
class CONT,
class COMP = gmm::less<
typename CONT::value_type> >
130 struct lexicographical_greater
131 :
public std::binary_function<CONT, CONT, int> {
133 int operator()(
const CONT &x,
const CONT &y)
const {
134 return -gmm::lexicographical_compare(x.begin(), x.end(),
135 y.begin(), y.end(), c);
137 lexicographical_greater(
const COMP &d = COMP()) { c = d; }
146 template<
class T>
struct sequence_iterator {
148 typedef T value_type;
149 typedef value_type* pointer;
150 typedef value_type& reference;
151 typedef const value_type& const_reference;
152 typedef std::forward_iterator_tag iterator_category;
156 sequence_iterator(T U0 = T(0)) { Un = U0; }
158 sequence_iterator &operator ++()
159 { ++Un;
return *
this; }
160 sequence_iterator operator ++(
int)
161 { sequence_iterator tmp = *
this; (*this)++;
return tmp; }
163 const_reference operator *()
const {
return Un; }
164 reference operator *() {
return Un; }
166 bool operator ==(
const sequence_iterator &i)
const {
return (i.Un==Un);}
167 bool operator !=(
const sequence_iterator &i)
const {
return (i.Un!=Un);}
174 template <
class ITER1,
class SIZE,
class ITER2>
175 ITER2 copy_n(ITER1 first, SIZE count, ITER2 result) {
176 for ( ; count > 0; --count, ++first, ++result) *result = *first;
181 typename std::iterator_traits<ITER>::value_type
182 mean_value(ITER first,
const ITER &last) {
183 GMM_ASSERT2(first != last,
"mean value of empty container");
185 typename std::iterator_traits<ITER>::value_type res = *first++;
186 while (first != last) { res += *first; ++first; ++n; }
192 typename CONT::value_type
193 mean_value(
const CONT &c) {
return mean_value(c.begin(), c.end()); }
196 void minmax_box(
typename std::iterator_traits<ITER>::value_type &pmin,
197 typename std::iterator_traits<ITER>::value_type &pmax,
198 ITER first,
const ITER &last) {
199 typedef typename std::iterator_traits<ITER>::value_type PT;
200 if (first != last) { pmin = pmax = *first; ++first; }
201 while (first != last) {
202 typename PT::const_iterator b = (*first).begin(), e = (*first).end();
203 typename PT::iterator b1 = pmin.begin(), b2 = pmax.begin();
205 { *b1 = std::min(*b1, *b); *b2 = std::max(*b2, *b); ++b; ++b1; ++b2; }
209 template<
typename VEC>
struct sorted_indexes_aux {
212 sorted_indexes_aux(
const VEC& v_) : v(v_) {}
213 template <
typename IDX>
214 bool operator()(
const IDX &ia,
const IDX &ib)
const 215 {
return v[ia] < v[ib]; }
218 template<
typename VEC,
typename IVEC>
219 void sorted_indexes(
const VEC &v, IVEC &iv) {
220 iv.clear(); iv.resize(v.size());
221 for (
size_t i=0; i < v.size(); ++i) iv[i] = i;
222 std::sort(iv.begin(), iv.end(), sorted_indexes_aux<VEC>(v));
basic setup for gmm (includes, typedefs etc.)
Definition of basic exceptions.