Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mystl.h « src « mgizapp - github.com/moses-smt/mgiza.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aa915ef23c2a68e955a623b8e820b36cdccaa108 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* ---------------------------------------------------------------- */
/* Copyright 1998 (c) by RWTH Aachen - Lehrstuhl fuer Informatik VI */
/* Franz Josef Och                                                  */
/* ---------------------------------------------------------------- */
#ifndef MY_STL_H_DEFINED
#define MY_STL_H_DEFINED

#include <vector>
#include <string>
using namespace std;
#ifdef USE_STLPORT
#ifdef __STL_DEBUG
using namespace _STLD;
#else
using namespace _STL;
#endif
#endif

#include "myassert.h"
#include <string>
#include <utility>
#if __GNUC__>2
#include <ext/hash_map>
using __gnu_cxx::hash_map;
#else
#include <hash_map>
#endif
#include <iostream>
#include "mymath.h"
#include "Array2.h"

#define over_string(a,i) for(unsigned int i=0;i<a.length();i++)
#define over_array(a,i) for(i=(a).low();i<=(a).high();i++)
#define backwards_array(a,i) for(i=(a).high();i>=(a).low();i--)
#define over_arr(a,i) for(int i=(a).low();i<=(a).high();i++)
#define over_arrMAX(a,i,max) for(int i=(a).low();i<=min((a).high(),max-1);i++)
#define backwards_arr(a,i) for(int i=(a).high();i>=(a).low();i--)

extern double n1mult,n2mult,n3mult;

inline double realProb(int n1,int n2)
{
  massert(n1<=n2);
  iassert(n1>=0&&n2>0);
  if(n2==0)n2=1;
  return ((double)n1)/(double)n2;
}

inline double verfProb(int n1,int n2)
{
  double prob = realProb(n1,n2);
  if( n1==1 )return prob*n1mult;
  else if( n1==2 )return prob*n2mult;
  else if( n1==3 )return prob*n3mult;
  else
    return prob;
}

inline bool prefix(const string&x,const string&y)
{
  if(y.size()>x.size() )
    return 0;
  for(unsigned int i=0; i<y.size(); ++i)
    if( y[i]!=x[i] )
      return 0;
  return 1;
}


/*template<class T>
int lev(const T&s1,const T&s2)
{
  Array2<int,std::vector<int> > a(s1.size()+1,s2.size()+1,1000);
  Array2<pair<int,int>,std::vector<pair<int,int> > > back(s1.size()+1,s2.size()+1,pair<int,int>(0,0));
  for(unsigned int i=0;i<=s1.size();i++)
    for(unsigned int j=0;j<=s2.size();j++)
      {
	if( i==0&&j==0 )
	  a(i,j)=0;
	else
	  {
	    int aDEL=100,aINS=100,aSUB=100;
	    if(i>0)
	      aDEL=a(i-1,j)+1;
	    if(j>0)
	      aINS=a(i,j-1)+1;
	    if(i>0&&j>0)
	      aSUB=a(i-1,j-1)+ !(s1[i-1]==s2[j-1]);
	    if( aSUB<=aDEL && aSUB<=aINS )
	      {
		a(i,j)=aSUB;
		back(i,j)=pair<int,int>(i-1,j-1);
	      }
	    else if( aDEL<=aSUB && aDEL<=aINS )
	      {
		a(i,j)=aDEL;
		back(i,j)=pair<int,int>(i-1,j);
	      }
	    else
	      {
		a(i,j)=aINS;
		back(i,j)=pair<int,int>(i,j-1);
	      }
	  }
      }
  return a(s1.size(),s2.size());
}

template<class T>
float rel_lev(const T&s1,const T&s2)
{
  if( s1.size()==0 )
    return s2.size()==0;
  else
    return min(1.0,lev(s1,s2)/(double)s1.size());
}*/

template<class V> int Hash(const pair<V,V>&a)
{
  return Hash(a.first)+13001*Hash(a.second);
}

template<class T1,class T2>
ostream& operator<<(ostream &out,const pair<T1,T2> &ir)
{
  out << "(" << ir.first << "," << ir.second << ")";
  return out;
}

inline int Hash(const string& s)
{
  int sum=0;
  string::const_iterator i=s.begin(),end=s.end();
  for(; i!=end; i++)sum=5*sum+(*i);
  return sum;
}
template<class A,class B,class C>
class tri
{
public:
  A a;
  B b;
  C c;
  tri() {};
  tri(const A&_a,const B&_b,const C&_c)
    : a(_a),b(_b),c(_c) {}
};
template<class A,class B,class C>
bool operator==(const tri<A,B,C>&x,const tri<A,B,C>&y)
{
  return x.a==y.a&&x.b==y.b&&x.c==y.c;
}

template<class A,class B,class C>
bool operator<(const tri<A,B,C>&x,const tri<A,B,C>&y)
{
  if(x.a<y.a)return 1;
  if(y.a<x.a)return 0;
  if(x.b<y.b)return 1;
  if(y.b<x.b)return 0;
  if(x.c<y.c)return 1;
  if(y.c<x.c)return 0;
  return 0;
}

double used_time();


template<class T ,class _Pr = less<T> >
class my_hash
{
public:
  int operator()(const T&t)const {
    return Hash(t);
  }
#ifdef WIN32
  enum {
    // parameters for hash table
    bucket_size = 1		// 0 < bucket_size
  };
  my_hash()
    : comp() {
    // construct with default comparator
  }

  my_hash(_Pr _Pred)
    : comp(_Pred) {
    // construct with _Pred comparator
  }
protected:
  _Pr comp;
public:
  int operator()(const T&t , const T&t1)const {
    return comp(t,t1);
  }
#endif
};

inline int Hash(int value)
{
  return value;
}
#define MY_HASH_BASE hash_map<A,B,my_hash<A> >

template<class A,class B>
class leda_h_array : public MY_HASH_BASE
{
private:
  B init;
public:
  leda_h_array() : MY_HASH_BASE() {}
  leda_h_array(const B&_init)
    : MY_HASH_BASE(),init(_init) {}
  bool defined(const A&a) const {
    return find(a)!=this->end();
  }
  const B&operator[](const A&a)const {
    typename MY_HASH_BASE::const_iterator pos=find(a);
    if( pos==this->end() )
      return init;
    else
      return pos->second;
  }
  B&operator[](const A&a) {
    typename MY_HASH_BASE::iterator pos=find(a);
    if( pos==this->end() ) {
      insert(MY_HASH_BASE::value_type(a,init));
      pos=find(a);
      iassert(pos!=this->end());
    }
    return pos->second;
  }
  const B&initValue()const {
    return init;
  }
};

#define forall_defined_h(a,b,c,d) for(typename leda_h_array<a,b>::const_iterator __jj__=(d).begin();__jj__!=(d).end()&&((c=__jj__->first),1); ++__jj__)
template<class T,class U>
ostream & operator<<(ostream&out,const leda_h_array<T,U>&w)
{
  T t;
  bool makeNl=0;
  out << "h_array{";
  forall_defined_h(T,U,t,w) {
    if( makeNl )
      out << "\n       ";
    out << "EL:" << t << " INH:" << w[t] << ".";
    makeNl=1;
  }
  return out << "}\n";
}

template<class T,class U>
istream & operator>>(istream&in,leda_h_array<T,U>&)
{
  return in;
}

template<class A,class B>
bool operator==(const leda_h_array<A,B>&p1,const leda_h_array<A,B>&p2)
{
  A v;
  forall_defined_h(A,B,v,p1)
  if( !( p1[v]==p2[v]) ) return 0;
  forall_defined_h(A,B,v,p2)
  if( !( p1[v]==p2[v]) ) return 0;
  return 1;
}

template<class T>
int count_elements(T a,T b)
{
  int c=0;
  while(a!=b) {
    a++;
    c++;
  }
  return c;
}

template<class T>
T normalize_if_possible_with_increment(T*a,T*b,int increment)
{
  T sum=0;
  for(T*i=a; i!=b; i+=increment)
    sum+=*i;
  if( sum )
    for(T*i=a; i!=b; i+=increment)
      *i/=sum;
  else {
    T factor=increment/(b-a);
    for(T*i=a; i!=b; i+=increment)
      *i=factor;
  }
  return sum;
}

template<class T>
inline int m_comp_3way(T a,T b,int n)
{
  int _n=0;
  while((_n++<n) && a && b) {
    const typename T::value_type &aa=*a;
    const typename T::value_type &bb=*b;
    if( aa<bb )return 1;
    if( bb<aa )return -1;
    ++a;
    ++b;
  }
  return 0;
}

template<class T>
void smooth_standard(T*a,T*b,double p)
{
  int n=b-a;
  if( n==0 )
    return;
  double pp=p/n;
  for(T*i=a; i!=b; ++i)
    *i = (1.0-p)*(*i)+pp;
}

template<class T>
const T *conv(typename std::vector<T>::const_iterator i)
{
  return &(*i);
}
#if __GNUC__>2
template<class T>
T *conv(typename std::vector<T>::iterator i)
{
  return &(*i);
}
#endif

/*template<class T>
const T *conv(const T*x)
{
  return x;
}*/
template<class T>
T *conv(T*x)
{
  return x;
}

#endif