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

nl-safeids.h « include « rvtl « hhmm « synlm « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 50837c36689babced7e8e0be1b54d6e226d856a9 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// This file is part of ModelBlocks. Copyright 2009, ModelBlocks developers. //
//                                                                           //
//    ModelBlocks 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 3 of the License, or      //
//    (at your option) any later version.                                    //
//                                                                           //
//    ModelBlocks 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 ModelBlocks.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                           //
//    ModelBlocks developers designate this particular file as subject to    //
//    the "Moses" exception as provided by ModelBlocks developers in         //
//    the LICENSE file that accompanies this code.                           //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

#ifndef _NL_SAFE_IDS__
#define _NL_SAFE_IDS__

#include "nl-const.h"
#include "nl-stringindex.h"
//#include "nl-string.h"
#include "nl-stream.h"

#include <iostream>
using namespace std; 

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//  StaticSafeArray<T,I>
//
////////////////////////////////////////////////////////////////////////////////

template <int I, class T> 
class StaticSafeArray {
 private:
  // Data members...
  T at[I];
 public:
  typedef T ElementType;
  template<class P> class Iterator : public StaticSafeArray<I,typename T::template Iterator<P> > {
   public:
    //operator StaticSafeArray<I,T>() const { }
  };
  // Constructor / destructor methods...
  StaticSafeArray ( )                               { }
  StaticSafeArray ( const T& t )                    { for(int i=0;i<I;i++) at[i]=t; }
  StaticSafeArray ( const StaticSafeArray<I,T>& a ) { for(int i=0;i<I;i++) at[i]=a.at[i]; }
  // Static extraction methods...
  static const int SIZE = I;
  static const int getSize ( )       { return I; }
  // Math methods...
  StaticSafeArray<I,T> operator+ ( const StaticSafeArray<I,T>& a ) const { StaticSafeArray<I,T> aOut; for(int i=0;i<I;i++) aOut.at[i]=at[i]+a.at[i]; return aOut; }
  StaticSafeArray<I,T> operator- ( const StaticSafeArray<I,T>& a ) const { StaticSafeArray<I,T> aOut; for(int i=0;i<I;i++) aOut.at[i]=at[i]-a.at[i]; return aOut; }
  //StaticSafeArray<I,T> operator+ ( const T& t ) const { StaticSafeArray<I,T> aOut; for(int i=0;i<I;i++) aOut.at[i]=at[i]+t; return aOut; }
  //StaticSafeArray<I,T> operator- ( const T& t ) const { StaticSafeArray<I,T> aOut; for(int i=0;i<I;i++) aOut.at[i]=at[i]-t; return aOut; }
  // Specification methods...
  T&         set       (int i)       { assert(0<=i); assert(i<I); return at[i]; }
  T&         operator[](int i)       { assert(0<=i); assert(i<I); return at[i]; }
  // Extraction methods...
  const T&   get       (int i) const { assert(NULL!=this); assert(0<=i); assert(i<I); return at[i]; }
  const T&   operator[](int i) const { assert(NULL!=this); assert(0<=i); assert(i<I); return at[i]; }
  bool       operator< ( const StaticSafeArray<I,T>& a ) const {
    int i;
    for ( i=0; at[i]==a.at[i] && i<I; i++ ) ;
    return ( i<I && at[i]<a.at[i] ) ;
  }
  bool       operator== ( const StaticSafeArray<I,T>& a ) const {
    int i;
    for ( i=0; at[i]==a.at[i] && i<I; i++ ) ;
    return ( i==I ) ;
  }
  size_t getHashKey   ( ) const { size_t k=0; for(int i=0;i<I;i++){k=rotLeft(k,3); k^=get(i).getHashKey(); } return k; }
//  friend ostream& operator<< ( ostream& os, const StaticSafeArray<I,T>& a ) { for(int i=0;i<I;i++) os<<((i==0)?"":",")<<a.get(i); return os; }
};

////////////////////////////////////////////////////////////////////////////////

template <int I, char* SD, class T> 
class DelimitedStaticSafeArray : public StaticSafeArray<I,T> {
 public:
  DelimitedStaticSafeArray ( )                               : StaticSafeArray<I,T>()  { }
  DelimitedStaticSafeArray ( const T& t )                    : StaticSafeArray<I,T>(t) { }
  DelimitedStaticSafeArray ( const StaticSafeArray<I,T>& a ) : StaticSafeArray<I,T>(a) { }

  bool operator==(const DelimitedStaticSafeArray<I,SD,T>& a) const { return StaticSafeArray<I,T>::operator==(a); }
  bool operator< (const DelimitedStaticSafeArray<I,SD,T>& a) const { return StaticSafeArray<I,T>::operator<(a); }

  friend ostream& operator<< ( ostream& os, const DelimitedStaticSafeArray<I,SD,T>& a ) { for(int i=0;i<I;i++) os<<((i==0)?"":SD)<<a.get(i); return os;  }
  friend String&  operator<< ( String& str, const DelimitedStaticSafeArray<I,SD,T>& a ) { for(int i=0;i<I;i++)str<<((i==0)?"":SD)<<a.get(i); return str; }
  friend IStream operator>> ( pair<IStream,DelimitedStaticSafeArray<I,SD,T>*> is_x, const char* psDlm ) {
    IStream&                          is =  is_x.first;
    DelimitedStaticSafeArray<I,SD,T>& x  = *is_x.second;
    if (IStream()==is) return IStream();
    for(int i=0;i<I;i++)
      is = pair<IStream,T*>(is,&x.set(i))>>((i<I-1)?SD:psDlm);
    return is;
  }


  // OBSOLETE!
  friend pair<StringInput,DelimitedStaticSafeArray<I,SD,T>*> operator>> ( StringInput ps, DelimitedStaticSafeArray<I,SD,T>& a ) { return pair<StringInput,DelimitedStaticSafeArray<I,SD,T>*>(ps,&a); }
  friend StringInput operator>> ( pair<StringInput,DelimitedStaticSafeArray<I,SD,T>*> delimbuff, const char* psDlm ) {
    if (StringInput(NULL)==delimbuff.first) return delimbuff.first;
    StringInput psIn = delimbuff.first;
    for(int i=0;i<I;i++)
      psIn = pair<StringInput,T*>(psIn,&delimbuff.second->set(i))>>((i<I-1)?SD:psDlm);
    return psIn;
  }

  /*
  void read  ( char* ps )       { char* psT; for(int i=0;i<I;i++){char* z=strtok_r((0==i)?ps:NULL,SD,&psT); assert(z);
                                                                  StaticSafeArray<I,T>::set(i).read(z);} }
  void read  ( char* ps, ReaderContext& rc ) { read(ps); }
  void write ( FILE* pf ) const { for(int i=0;i<I;i++){fprintf(pf,(0==i)?"":SD); StaticSafeArray<I,T>::get(i).write(pf);} }
  void write ( FILE* pf, ReaderContext& rc ) const { write(pf); }
  */
};

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//  Delimited pair
//
////////////////////////////////////////////////////////////////////////////////

template<char* SD1,class V1,char* SD2,class V2,char* SD3>
class DelimitedPair : public pair<V1,V2> {
 public:
  // Constructor / destructor methods...
  DelimitedPair ( )                             : pair<V1,V2>()      { }
  DelimitedPair ( const V1& v1, const V2& v2 )  : pair<V1,V2>(v1,v2) { }

  // Input / output methods...
  friend ostream& operator<< ( ostream& os, const DelimitedPair<SD1,V1,SD2,V2,SD3>& rv ) { return  os<<SD1<<rv.first<<SD2<<rv.second<<SD3; }
  friend String&  operator<< ( String& str, const DelimitedPair<SD1,V1,SD2,V2,SD3>& rv ) { return str<<SD1<<rv.first<<SD2<<rv.second<<SD3; }
  friend IStream operator>> ( pair<IStream,DelimitedPair<SD1,V1,SD2,V2,SD3>*> is_x, const char* psDlm ) {
    IStream&                               is =  is_x.first;
    DelimitedPair<SD1,V1,SD2,V2,SD3>& x  = *is_x.second;
    // Propagate fail...
    if ( IStream()==is ) return is;
    IStream is1 = (is>>SD1>>x.first>>SD2);
    IStream is2 = (is>>SD1>>x.first>>SD2>>x.second>>psDlm);
    // Use last delimiter only if not empty (otherwise it will immediately trivially match)...
    return ( (SD3[0]=='\0') ? is>>SD1>>x.first>>SD2>>x.second>>psDlm
                            : is>>SD1>>x.first>>SD2>>x.second>>SD3>>psDlm );
  }
};

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//  Id -- generalize int to any symbol value
//
////////////////////////////////////////////////////////////////////////////////

template<class T>
class Id {
 private:
  // Data members...
  T val;
 public:
  typedef T IntType;
  // Constructor / destructor methods...
  Id ()            { val=0; }
  Id (T t)         { val=t; }
//  Id (int i)       { val=i; }
  Id (const Id& i) { val=i.val; }
  // Specification methods...
  Id& operator=  (const T& i)  { val=i; return *this; }
//  Id& operator=  (int i)       { val=i; return *this; }
  Id& operator=  (const Id& i) { val=i.val; return *this; }
  Id& operator+= (const Id& i) { val+=i.val; return *this; }
  Id& operator++ ( )           { ++val; return *this; }
  Id& setFirst   ( )           { val=0; return *this; }
  Id& setFirst   (const Id& i) { val=0; return *this; }
  Id& setNext    ( )           { ++val; return *this; }
  Id& setNext    (const Id& i) { ++val; return *this; }
  // Extraction methods...
  //int         getHashConst ( const int m ) const { return (sizeof(T)%m); }
  //int         getHashKey   ( const int m ) const { return (((int)*this)%m);  }
  size_t getHashKey ( ) const { return (size_t)val; }
  bool operator== ( const Id& i ) const { return val==i.val; }
  bool operator!= ( const Id& i ) const { return val!=i.val; }
  bool operator<  ( const Id& i ) const { return val<i.val; }
  bool operator<= ( const Id& i ) const { return val<=i.val; }
  bool operator>  ( const Id& i ) const { return val>i.val; }
  bool operator>= ( const Id& i ) const { return val>=i.val; }
  Id   operator+  ( const Id& i ) const { return Id(val+i.val); }
  Id   operator-  ( const Id& i ) const { return Id(val-i.val); }
  bool operator== ( const T t )   const { return val==t; }
  bool operator<  ( const T t )   const { return val<t; }
  bool operator<= ( const T t )   const { return val<=t; }
  bool operator>  ( const T t )   const { return val>t; }
  bool operator>= ( const T t )   const { return val>=t; }
  Id   operator+  ( const T t )   const { return Id(val+t); }
  Id   operator-  ( const T t )   const { return Id(val-t); }
//  operator T() const { return val; };

  T     toInt     ( )     const { return val; }
  Id<T> set       (T t)         { val=t; return *this; }
//  void setInt    (int i)       { val=i; }

  // Input / output methods...
  friend ostream& operator<< ( ostream& os, const Id<T>& i ) { return os<<i.val; }

  /*
  void write ( FILE* pf, const ReaderContext& rc=ReaderContext() ) const { (sizeof(T)<sizeof(long long)) ? fprintf(pf,"%d",val) : fprintf(pf,"%lld",val); }
  */
} ;

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//  Safe pointers..
//
////////////////////////////////////////////////////////////////////////////////

template <class R>
class SafePtr {
 private:
  R* pr;
  static const R rDummy;
 public:
  SafePtr<R> ( )      : pr(NULL) { }
  SafePtr<R> ( R& r ) : pr(&r)   { }
  bool operator== ( const SafePtr<R>& spr ) const { return(pr==spr.pr); }
  bool operator!= ( const SafePtr<R>& spr ) const { return(!(pr==spr.pr)); }
  //friend void delete ( SafePtr<R>& sp )  { delete sp.pr; }
  void     del    ( )       { delete pr; pr=NULL; }
  //R&       setRefV ( )      { assert(pr); return *pr; }
  R&       setRef  ( )      { assert(pr); return (pr!=NULL) ? *pr : rDummy; }
  const R& getRef ( ) const { return (pr!=NULL) ? *pr : rDummy; }
};
template <class R>
const R SafePtr<R>::rDummy = R();

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//  Safe static array templates for small arrays...
//
////////////////////////////////////////////////////////////////////////////////

template <class T>
class Doub {
 private:
  T at[2];
 public:
  T&       set ( int i )       { assert(0<=i&&i<2); return at[i]; }
  const T& get ( int i ) const { assert(0<=i&&i<2); return at[i]; }
};

template <class T>
class Trip {
 private:
  T at[3];
 public:
  T&       set ( int i )       { assert(0<=i&&i<3); return at[i]; }
  const T& get ( int i ) const { assert(0<=i&&i<3); return at[i]; }
};

template <class T>
class Quad {
 private:
  T at[4];
 public:
  T&       set ( int i )       { assert(0<=i&&i<4); return at[i]; }
  const T& get ( int i ) const { assert(0<=i&&i<4); return at[i]; }
};


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//  SafeArrays -- prevents overflow memory leaks
//
////////////////////////////////////////////////////////////////////////////////

template <class X1, class T>
class SafeArray1D {
 private:
  // Data members;
  int xSize;
  T*  at;
 public:
  // Constructor / destructor methods...
  ~SafeArray1D( )                    { delete[] at; }
  SafeArray1D ( )                    { xSize=0; at=NULL; }
  SafeArray1D (int x)                { xSize=x; at=new T[x]; }
  SafeArray1D (int x, const T& t)    { xSize=x; at=new T[x];
                                          for(int i=0;i<x;++i) at[i]=t; }
  SafeArray1D ( const SafeArray1D<X1,T>& sat ) { delete[] at; xSize=sat.xSize; at=new T[xSize];
                                                 for(int i=0;i<xSize;i++) at[i]=sat.at[i]; }
  // Specification methods...
  SafeArray1D& operator= ( const SafeArray1D<X1,T>& sat )
    { delete[] at; xSize=sat.xSize; at=new T[xSize];
      for(int i=0;i<xSize;i++) at[i]=sat.at[i]; return *this; }
  void  init ( int x )               { delete[] at; xSize=x; at=new T[x];}
  void  init ( int x, const T& t )   { delete[] at; xSize=x; at=new T[x];
                                             for(int i=0;i<x;++i) at[i]=t; }
  void  reset()                      { delete[] at; xSize=0; at=NULL; }
  T&    set  ( const X1& x )         { assert(x.toInt()>=0); assert(x.toInt()<xSize);
                                       return at[x.toInt()];}
  // Extraction methods...
  int  getSize ( ) const             { return xSize; }
  const T& get ( const X1& x ) const { assert(x.toInt()>=0); assert(x.toInt()<xSize);
                                       return at[x.toInt()];}
};

////////////////////////////////////////////////////////////////////////////////

template <class X1, class X2, class T>
class SafeArray2D {
 private:
  // Data members;
  int xSize;
  int ySize;
  T*  at;
 public:
  // Constructor / destructor methods...
  ~SafeArray2D( )                           { delete[] at; }
  SafeArray2D ( )                           { xSize=0; ySize=0; at=NULL; }
  SafeArray2D (int x, int y)                { xSize=x; ySize=y; at=new T[x*y]; }
  SafeArray2D (int x, int y, const T& t)    { xSize=x; ySize=y; at=new T[x*y];
                                              for(int i=0;i<x*y;++i) at[i]=t; }
  //SafeArray2D ( const SafeArray2D& a )      { xSize=a.xSize; ySize=a.ySize;
  //                                            for(int i=0;i<xSize;i++) for(int j=0;j<ySize;j++) at[i*ySize+j]=a.at[i*ySize+j]; }
  // Specification methods...
  SafeArray2D& operator= ( const SafeArray2D<X1,X2,T>& sat )
    { delete[] at; xSize=sat.xSize; ySize=sat.ySize; at=new T[xSize*ySize];
      for(int i=0;i<xSize*ySize;i++) at[i]=sat.at[i]; return *this; }
  void  init ( int x,int y )                   { delete[] at; xSize=x; ySize=y; at=new T[x*y];}
  void  init ( int x,int y,const T& t )        { delete[] at; xSize=x; ySize=y; at=new T[x*y];
                                                       for(int i=0;i<x*y;++i) at[i]=t; }
  void  reset()                                { delete[] at; xSize=0; ySize=0; at=NULL; }
  T&    set  ( const X1& x,const X2& y)        { assert(at!=NULL);
                                                 assert(x.toInt()>=0); assert(x.toInt()<xSize);
                                                 assert(y.toInt()>=0); assert(y.toInt()<ySize);
                                                 return at[x.toInt()*ySize + y.toInt()];}
  // Extraction methods...
  const T& get (const X1& x,const X2& y) const { assert(at!=NULL);
                                                 assert(x.toInt()>=0); assert(x.toInt()<xSize);
                                                 assert(y.toInt()>=0); 
//this assert failed when compile without -DNDEBUG (needed for debugging). Have to figure out why before adding this assert back in
//assert(y.toInt()<ySize);
                                                 return at[x.toInt()*ySize + y.toInt()];}
  int getxSize( ) const { return xSize; }
  int getySize( ) const { return ySize; }
};

////////////////////////////////////////////////////////////////////////////////

template <class X1, class X2, class X3, class T>
class SafeArray3D {
 private:
  // Data members;
  int xSize;
  int ySize;
  int zSize;
  T*  at;
 public:
  // Constructor / destructor methods...
  ~SafeArray3D()                           { delete[] at; }
  SafeArray3D()                            { xSize=0; ySize=0; zSize=0; at=NULL; }
  SafeArray3D(int x,int y,int z)           { xSize=x; ySize=y; zSize=z; at=new T[x*y*z];}
  SafeArray3D(int x,int y,int z,const T& t){ xSize=x; ySize=y; zSize=z; at=new T[x*y*z];
                                                for(int i=0;i<x*y*z;++i) at[i]=t; }
  // Specification methods...
  SafeArray3D& operator= ( const SafeArray3D<X1,X2,X3,T>& sat )
    { delete[] at; xSize=sat.xSize; ySize=sat.ySize; zSize=sat.zSize;
      at=new T[xSize*ySize*zSize];
      for(int i=0;i<xSize*ySize*zSize;i++) at[i]=sat.at[i]; return *this; }
  void init(int x,int y,int z)           { delete[] at; xSize=x; ySize=y; zSize=z; at=new T[x*y*z]; }
  void init(int x,int y,int z,const T& t){ delete[] at; xSize=x; ySize=y; zSize=z; at=new T[x*y*z];
                                                 for(int i=0;i<x*y*z;++i) at[i]=t; }
  void reset()                           { delete[] at; xSize=0; ySize=0; zSize=0; at=NULL; }
  T&   set(const X1& x,const X2& y,const X3& z)
                                         { assert(at!=NULL);
                                           assert(x.toInt()>=0); assert(x.toInt()<xSize);
                                           assert(y.toInt()>=0); assert(y.toInt()<ySize);
                                           assert(z.toInt()>=0); assert(z.toInt()<zSize);
                                           return at[(x.toInt()*ySize+y.toInt())*zSize+z.toInt()];}
  // Extraction methods...
  const T& get(const X1& x,const X2& y,const X3& z) const
                                         { assert(at!=NULL);
                                           assert(x.toInt()>=0); assert(x.toInt()<xSize);
                                           assert(y.toInt()>=0); assert(y.toInt()<ySize);
                                           assert(z.toInt()>=0); assert(z.toInt()<zSize);
                                           return at[(x.toInt()*ySize+y.toInt())*zSize+z.toInt()];}
};

////////////////////////////////////////////////////////////////////////////////

template <class X1, class X2, class X3, class X4, class T>
class SafeArray4D {
 private:
  // Data members;
  int wSize;
  int xSize;
  int ySize;
  int zSize;
  T*  at;
 public:
  // Constructor / destructor methods...
  ~SafeArray4D( )  { delete[] at; }
  SafeArray4D ( )  { wSize=0; xSize=0; ySize=0; zSize=0; at=NULL; }
  SafeArray4D(int w, int x,int y,int z)
    { wSize=w; xSize=x; ySize=y; zSize=z; at=new T[w*x*y*z];}
  SafeArray4D(int w, int x,int y,int z,const T& t)
    { wSize=w; xSize=x; ySize=y; zSize=z; at=new T[w*x*y*z];
      for(int i=0;i<w*x*y*z;++i) at[i]=t; }
  // Specification methods...
  SafeArray4D& operator= ( const SafeArray4D<X1,X2,X3,X4,T>& sat )
    { delete[] at; wSize=sat.wSize; xSize=sat.xSize; ySize=sat.ySize;
      zSize=sat.zSize; at=new T[wSize*xSize*ySize*zSize];
      for(int i=0;i<wSize*xSize*ySize*zSize;i++) at[i]=sat.at[i]; return *this; }
  void init (int w,int x,int y,int z)        
    { delete[] at; wSize=w; xSize=x; ySize=y; zSize=z; at=new T[w*x*y*z]; }
  void init (int w,int x,int y,int z,const T& t)
    { delete[] at; wSize=w; xSize=x; ySize=y; zSize=z; at=new T[w*x*y*z];
      for(int i=0;i<w*x*y*z;++i) at[i]=t; }
  void reset() { delete[] at; wSize=0; xSize=0; ySize=0; zSize=0; at=NULL; }
  T&   set(const X1& w,const X2& x,const X3& y,const X4& z)
    { assert(at!=NULL);
      assert(w.toInt()>=0); assert(w.toInt()<wSize);
      assert(x.toInt()>=0); assert(x.toInt()<xSize);
      assert(y.toInt()>=0); assert(y.toInt()<ySize);
      assert(z.toInt()>=0); assert(z.toInt()<zSize);
      return at[((w.toInt()*xSize+x.toInt())*ySize+y.toInt())*zSize+z.toInt()];}
  // Extraction methods...
  const T& get(const X1& w,const X2& x,const X3& y,const X4& z) const
    { assert(at!=NULL);
      assert(w.toInt()>=0); assert(w.toInt()<wSize);
      assert(x.toInt()>=0); assert(x.toInt()<xSize);
      assert(y.toInt()>=0); assert(y.toInt()<ySize);
      assert(z.toInt()>=0); assert(z.toInt()<zSize);
      return at[((w.toInt()*xSize+x.toInt())*ySize+y.toInt())*zSize+z.toInt()];}
};

////////////////////////////////////////////////////////////////////////////////

template <class X1, class X2, class X3, class X4, class X5, class T>
class SafeArray5D {
 private:
  // Data members;
  int vSize;
  int wSize;
  int xSize;
  int ySize;
  int zSize;
  T*  at;
 public:
  // Constructor / destructor methods...
  ~SafeArray5D( )  { delete[] at; }
  SafeArray5D ( )  { vSize=0; wSize=0; xSize=0; ySize=0; zSize=0; at=NULL; }
  SafeArray5D(int v,int w, int x,int y,int z)
    { vSize=v; wSize=w; xSize=x; ySize=y; zSize=z; at=new T[v*w*x*y*z];}
  SafeArray5D(int v,int w, int x,int y,int z,const T& t)
    { vSize=v; wSize=w; xSize=x; ySize=y; zSize=z; at=new T[v*w*x*y*z];
      for(int i=0;i<v*w*x*y*z;++i) at[i]=t; }
  // Specification methods...
  SafeArray5D& operator= ( const SafeArray5D<X1,X2,X3,X4,X5,T>& sat )
    { delete[] at; vSize=sat.vSize; wSize=sat.wSize; xSize=sat.xSize;
      ySize=sat.ySize; zSize=sat.zSize; at=new T[vSize*wSize*xSize*ySize*zSize];
      for(int i=0;i<vSize*wSize*xSize*ySize*zSize;i++) at[i]=sat.at[i]; return *this; }
  void init(int v,int w,int x,int y,int z)        
    { delete[] at; vSize=v; wSize=w; xSize=x; ySize=y; zSize=z; at=new T[v*w*x*y*z]; }
  void init(int v,int w,int x,int y,int z,const T& t)
    { delete[] at; vSize=v; wSize=w; xSize=x; ySize=y; zSize=z; at=new T[v*w*x*y*z];
      for(int i=0;i<v*w*x*y*z;++i) at[i]=t; }
  void reset() { delete[] at; vSize=0; wSize=0; xSize=0; ySize=0; zSize=0; at=NULL; }
  T&   set(const X1& v,const X2& w,const X3& x,const X4& y,const X5& z)
    { assert(at!=NULL);
      assert(v.toInt()>=0); assert(v.toInt()<vSize);
      assert(w.toInt()>=0); assert(w.toInt()<wSize);
      assert(x.toInt()>=0); assert(x.toInt()<xSize);
      assert(y.toInt()>=0); assert(y.toInt()<ySize);
      assert(z.toInt()>=0); assert(z.toInt()<zSize);
      return at[(((v.toInt()*wSize+w.toInt())*xSize+x.toInt())*ySize+y.toInt())*zSize+z.toInt()];}
  // Extraction methods...
  const T& get(const X1& v,const X2& w,const X3& x,const X4& y,const X5& z) const
    { assert(at!=NULL);
      assert(v.toInt()>=0); assert(v.toInt()<vSize);
      assert(w.toInt()>=0); assert(w.toInt()<wSize);
      assert(x.toInt()>=0); assert(x.toInt()<xSize);
      assert(y.toInt()>=0); assert(y.toInt()<ySize);
      assert(z.toInt()>=0); assert(z.toInt()<zSize);
      return at[(((v.toInt()*wSize+w.toInt())*xSize+x.toInt())*ySize+y.toInt())*zSize+z.toInt()];}
};

////////////////////////////////////////////////////////////////////////////////


#endif //_NL_SAFE_IDS__