37 #ifndef GMM_ALGOBASE_H__
38 #define GMM_ALGOBASE_H__
52 inline int operator()(
const T& x,
const T& y)
const
53 {
return (x < y) ? -1 : ((y < x) ? 1 : 0); }
56 template<>
struct less<int> {
57 int operator()(
int x,
int y)
const {
return x-y; } };
58 template<>
struct less<char> {
59 int operator()(
char x,
char y)
const {
return int(x-y); } };
60 template<>
struct less<short> {
61 int operator()(
short x,
short y)
const {
return int(x-y); } };
62 template<>
struct less<unsigned char> {
63 int operator()(
unsigned char x,
unsigned char y)
const
64 {
return int(x)-int(y); }
70 inline int operator()(
const T& x,
const T& y)
const
71 {
return (y < x) ? -1 : ((x < y) ? 1 : 0); }
74 template<>
struct greater<int> {
75 int operator()(
int x,
int y)
const {
return y-x; } };
76 template<>
struct greater<char> {
77 int operator()(
char x,
char y)
const {
return int(y-x); } };
78 template<>
struct greater<short> {
79 int operator()(
short x,
short y)
const {
return int(y-x); } };
80 template<>
struct greater<unsigned char> {
81 int operator()(
unsigned char x,
unsigned char y)
const
82 {
return int(y)-int(x); }
85 template <
typename T>
inline T my_abs(T a) {
return (a < T(0)) ? T(-a) : a; }
90 inline int operator()(
const T &x,
const T &y)
const
91 {
if (my_abs(x - y) <= eps)
return 0;
if (x < y)
return -1;
return 1; }
92 approx_less(
double e = 1E-13) { eps = e; }
96 struct approx_greater {
98 inline int operator()(
const T &x,
const T &y)
const
99 {
if (my_abs(x - y) <= eps)
return 0;
if (x > y)
return -1;
return 1; }
100 approx_greater(
double e = 1E-13) { eps = e; }
103 template<
class ITER1,
class ITER2,
class COMP>
104 int lexicographical_compare(ITER1 b1,
const ITER1 &e1,
105 ITER2 b2,
const ITER2 &e2,
const COMP &c) {
107 for ( ; b1 != e1 && b2 != e2; ++b1, ++b2)
108 if ((i = c(*b1, *b2)) != 0)
return i;
109 if (b1 != e1)
return 1;
110 if (b2 != e2)
return -1;
114 template<
class CONT,
class COMP = gmm::less<
typename CONT::value_type> >
115 struct lexicographical_less {
117 int operator()(
const CONT &x,
const CONT &y)
const {
118 return gmm::lexicographical_compare(x.begin(), x.end(),
119 y.begin(), y.end(), c);
121 lexicographical_less(
const COMP &d = COMP()) { c = d; }
124 template<
class CONT,
class COMP = gmm::less<
typename CONT::value_type> >
125 struct lexicographical_greater {
127 int operator()(
const CONT &x,
const CONT &y)
const {
128 return -gmm::lexicographical_compare(x.begin(), x.end(),
129 y.begin(), y.end(), c);
131 lexicographical_greater(
const COMP &d = COMP()) { c = d; }
140 template<
class T>
struct sequence_iterator {
142 typedef T value_type;
143 typedef value_type* pointer;
144 typedef value_type& reference;
145 typedef const value_type& const_reference;
146 typedef std::forward_iterator_tag iterator_category;
150 sequence_iterator(T U0 = T(0)) { Un = U0; }
152 sequence_iterator &operator ++()
153 { ++Un;
return *
this; }
154 sequence_iterator operator ++(
int)
155 { sequence_iterator tmp = *
this; (*this)++;
return tmp; }
157 const_reference operator *()
const {
return Un; }
158 reference operator *() {
return Un; }
160 bool operator ==(
const sequence_iterator &i)
const {
return (i.Un==Un);}
161 bool operator !=(
const sequence_iterator &i)
const {
return (i.Un!=Un);}
168 template <
class ITER1,
class SIZE,
class ITER2>
169 ITER2 copy_n(ITER1 first, SIZE count, ITER2 result) {
170 for ( ; count > 0; --count, ++first, ++result) *result = *first;
175 typename std::iterator_traits<ITER>::value_type
176 mean_value(ITER first,
const ITER &last) {
177 GMM_ASSERT2(first != last,
"mean value of empty container");
179 typename std::iterator_traits<ITER>::value_type res = *first++;
180 while (first != last) { res += *first; ++first; ++n; }
186 typename CONT::value_type
187 mean_value(
const CONT &c) {
return mean_value(c.begin(), c.end()); }
190 void minmax_box(
typename std::iterator_traits<ITER>::value_type &pmin,
191 typename std::iterator_traits<ITER>::value_type &pmax,
192 ITER first,
const ITER &last) {
193 typedef typename std::iterator_traits<ITER>::value_type PT;
194 if (first != last) { pmin = pmax = *first; ++first; }
195 while (first != last) {
196 typename PT::const_iterator b = (*first).begin(), e = (*first).end();
197 typename PT::iterator b1 = pmin.begin(), b2 = pmax.begin();
199 { *b1 = std::min(*b1, *b); *b2 = std::max(*b2, *b); ++b; ++b1; ++b2; }
203 template<
typename VEC>
struct sorted_indexes_aux {
206 sorted_indexes_aux(
const VEC& v_) : v(v_) {}
207 template <
typename IDX>
208 bool operator()(
const IDX &ia,
const IDX &ib)
const
209 {
return v[ia] < v[ib]; }
212 template<
typename VEC,
typename IVEC>
213 void sorted_indexes(
const VEC &v, IVEC &iv) {
214 iv.clear(); iv.resize(v.size());
215 for (
size_t i=0; i < v.size(); ++i) iv[i] = i;
216 std::sort(iv.begin(), iv.end(), sorted_indexes_aux<VEC>(v));
Definition of basic exceptions.
basic setup for gmm (includes, typedefs etc.)