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

FixedArray.h « mkcls-v2 - github.com/moses-smt/giza-pp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 39da0b1de07421be4c42dcbbe7e27d75c5864edd (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
/*

Copyright (C) 1997,1998,1999,2000,2001  Franz Josef Och

mkcls - a program for making word classes .

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
USA.

*/




#ifndef FIXARRAY_H_DEFINED
#define FIXARRAY_H_DEFINED
#include <iostream>
#include <string>
#include <functional>

template<class T>
bool writeOb(ostream&out,const T&f)
{ 
  out << f << " "; 
  return 1;
}

template<class T>
bool readOb(istream&in,T&f)
{ 
  in >> f;
  char c;
  in.get(c);
  massert(c==' ');
  return 1;
}

template<class T>
bool writeOb(ostream&out,const string &s,const T&f)
{ 
  out << s << " " << f << " "; 
  return 1;
}
template<class T>
bool readOb(istream&in,const string&s,T&f)
{ 
  string ss;
  in >> ss;
  if( s!=ss )
    {
      cerr << "ERROR: readOb should be '" << s << "' and is '" << ss << "'" << endl;
      return 0;
    }
  in >> f; 
  char c;
  in.get(c);
  massert(c==' ');
  return 1;
}

template<class T> class FixedArray
{
 private:  
  void copy(T *aa,const T *bb,int nnn)
    {for(int iii=0;iii<nnn;iii++)aa[iii]=bb[iii];}
  
 public:
  T *p;                       	
  int realSize;
  FixedArray() 
    : p(0),realSize(0){}
  FixedArray(const FixedArray<T> &x)
    : p(new T[x.realSize]),realSize(x.realSize) {copy(p,x.p,realSize);}
  explicit FixedArray(int n)
    : p(new T[n]),realSize(n){}
  FixedArray(int n,const T&_init)
    : p(new T[n]),realSize(n){for(int z=0;z<n;z++)p[z]=_init;}
  FixedArray(const FixedArray&f,const T&t)
    : p(new T[f.size()+1]),realSize(f.size()+1){for(int z=0;z<f.size();z++)p[z]=f[z];p[f.size()]=t;}
  ~FixedArray() 
    { delete [] p;p=0;realSize=-1;}
  
  FixedArray<T>& operator=(const FixedArray<T>&x)
    {
      if( this!= &x )
	{
	  delete [] p;
	  realSize = x.realSize;
	  p = new T[x.realSize]; 
	  copy(p,x.p,realSize);
	}
      return *this;
    }
  void resize(int n)
    {
      if( n<=realSize )
	shrink(n);
      else
	{
	  T*np=new T[n];
	  copy(np,p,realSize);
	  delete []p;
	  p=np;
	  realSize=n;
	}
    }
  void shrink(int n)
    {
      assert(n<=realSize);
      realSize=n;
    }
  void init(int n,const T&_init)
    {
      delete []p;
      p=new T[n];
      realSize=n;
      for(int l=0;l<n;l++)p[l]=_init;
    }
  inline const T&top(int n=0) const
    {return (*this)[realSize-1-n];}
  inline int size() const 
    {return realSize;}

  inline T*begin(){ return p; }
  inline T*end(){ return p+realSize; }

  inline const T*begin()const{ return p; }
  inline const T*end()const{return p+realSize;}

  inline int low() const 
    {return 0;}
  inline int high() const 
    {return realSize-1;}
  const void errorAccess(int n) const;
  
  inline T& operator[](int n)
    { 
      return p[n];
    }
  inline const T& operator[](int n) const 
    { 
      return p[n]; 
    }
  bool writeTo(ostream&out) const
    {
      out << "FixedArray ";
      out << size() << " ";
      for(int a=0;a<size();a++)
	{
	  writeOb(out,(*this)[a]);
	  out << " ";
	}
      out << endl;
      return 1;
    }
  bool readFrom(istream&in)
    {
      string s;
      if( !in )
	{
	  cerr << "ERROR(FixedArray): file cannot be opened.\n";
	  return 0;
	}
      in >> s;
      if( !(s=="FixedArray") )
	{
	  cerr << "ERROR(FixedArray): FixedArray!='"<<s<<"'\n";
	  return 0;
	}
      int biggest;
      in >> biggest;
      resize(biggest);
      for(int a=0;a<size();a++)
	readOb(in,(*this)[a]);
      return 1;
    }
  void sort(int until=-1)
    {
      if( until== -1 ) until=size();
      std::sort(p,p+until);
    }
  void invsort(int until=-1)
    {
      if( until== -1 ) until=size();
      std::sort(p,p+until,greater<T>());      
    }
  int binary_locate(const T&t)
    {
      T*ppos=std::lower_bound(p,p+size(),t);
      int pos=ppos-p;
      if( pos>=-1&&pos<size() )
	return pos;
      else
	return -1;
    }
  int binary_search(const T&t)
    {
      T*ppos=std::lower_bound(p,p+size(),t);
      int pos=ppos-p;
      if( pos>=0&&pos<size()&& *ppos==t )
	return pos;
      else
	return -1;
    }
  typedef T* iterator;
  typedef const T* const_iterator;
};

template<class T> bool operator<(const FixedArray<T> &x, const FixedArray<T> &y)
{
  return lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
  
}


template<class T>  bool operator==(const FixedArray<T> &x, const FixedArray<T> &y)
{
  if( &x == &y )return 1;
  const int s = x.size();
  if( s !=y.size() )return 0;
  for(int iii=0;iii<s;iii++)
    if( !(x.p[iii]==y.p[iii]) )
      return 0;
  return 1;
}

template<class T> int Hash(const FixedArray<T>&a)
{
  int n=0;
  const int s=a.size();
  for(int iii=0;iii<s;iii++)
    n=13*n+Hash(a.p[iii]);
  return n;
}

template<class T> const void FixedArray<T>:: errorAccess(int n) const
{
  massert(0);
  cerr 	<< "ERROR: Access to array element " << n 
	<< " (" << realSize << "," << (void*)p << ")\n";
}
	
template<class T> ostream& operator<<(ostream&o,const FixedArray<T>&a)
{
  o << "FixedArray(" << a.size() << "){ ";
  for(int iii=0;iii<a.size();iii++)
    o << " " << iii<< ":" << a[iii]<<";";
  return o << "}\n";
}

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

template<class T> FixedArray<T> operator+(const FixedArray<T>&a,const FixedArray<T>&b)
{
  massert(a.size()==b.size());
  FixedArray<T> x(a.size());
  for(int iii=0;iii<a.size();iii++)
    x[iii]=a[iii]+b[iii];
  return x;
}
template<class T> FixedArray<T> operator|(const FixedArray<T>&aaa,const FixedArray<T>&bbb)
{
  iassert(aaa.size()==bbb.size());

  FixedArray<T> xxx(aaa.size());
  for(int iii=0;iii<aaa.size();iii++)
    xxx.p[iii]=aaa.p[iii]||bbb.p[iii];
  return xxx;
}

#endif