File: Serialize.cpp

package info (click to toggle)
freemat 4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 174,736 kB
  • ctags: 67,053
  • sloc: cpp: 351,060; ansic: 255,892; sh: 40,590; makefile: 4,323; perl: 4,058; asm: 3,313; pascal: 2,718; fortran: 1,722; ada: 1,681; ml: 1,360; cs: 879; csh: 795; python: 430; sed: 162; lisp: 160; awk: 5
file content (702 lines) | stat: -rw-r--r-- 17,861 bytes parent folder | download | duplicates (2)
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
 * Copyright (c) 2002-2006 Samit Basu
 *
 * 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
 *
 */

#include "Serialize.hpp"
#include "Exception.hpp"
#include "Algorithms.hpp"
#include "Struct.hpp"
#include <cstdio>
#include "SparseCCS.hpp"
#include <QtEndian>

Serialize::Serialize(QIODevice *sck) {
  endianSwap = false;
  s = sck;
}

Serialize::~Serialize() {
}

void Serialize::handshakeServer() {
  try {
    char hand = 'A';
    s->write((const char*) &hand,sizeof(char));
    hand = 'Y';
    s->write((const char*) &hand,sizeof(char));
    hand = 'B';
    s->write((const char*) &hand,sizeof(char));
    hand = 'S';
    s->write((const char*) &hand,sizeof(char));
    unsigned short t;
    t = 1;
    s->write((const char *)&t,sizeof(short));
  } catch (Exception& e) {
    throw;
  }
}

void Serialize::handshakeClient() {
  try {
    char hand;
    s->read((char*) &hand,sizeof(char));
    if (hand != 'A')
      throw Exception("Handshaking error! Unable to establish serialization");
    s->read((char*) &hand,sizeof(char));
    if (hand != 'Y')
      throw Exception("Handshaking error! Unable to establish serialization");
    s->read((char*) &hand,sizeof(char));
    if (hand != 'B')
      throw Exception("Handshaking error! Unable to establish serialization");
    s->read((char*) &hand,sizeof(char));
    if (hand != 'S')
      throw Exception("Handshaking error! Unable to establish serialization");
    unsigned short t = 1;
    s->read((char*) &t,sizeof(short));
    if (t == 1)
      endianSwap = false;
    else if (t == 256)
      endianSwap = true;
    else
      throw Exception("Handshaking error! Unable to resolve byte ordering between server/client");    
  } catch (Exception &e) {
    throw ;
  }
}

void Serialize::sendSignature(char type, int count) {
  s->write((const char*) &type,1);
  int netcount;
  netcount = qToBigEndian(count);
  s->write((const char*) &netcount,sizeof(int));
}

void Serialize::checkSignature(char type, int count) {
  char rtype;
  int rcount;
  s->read((char*) &rtype,1);
  s->read((char*) &rcount,sizeof(int));
  rcount = qFromBigEndian(rcount);
  if (!((type == rtype) && (count == rcount))) {
    char buffer[1000];
    sprintf(buffer,"Serialization Mismatch: expected <%c,%d>, got <%c,%d>",
	    type,count,rtype,rcount);
    throw Exception(buffer);
  }
}

// Send a stream of base objects
void Serialize::putBytes(const char *ptr, int count) {
  sendSignature('c',count);
  s->write((const char*) ptr,count*sizeof(char));
}

void Serialize::putShorts(const short *ptr, int count) {
  sendSignature('s',count);
  s->write((const char*) ptr,count*sizeof(short));
}

void Serialize::putI64s(const int64 *ptr, int count) {
  sendSignature('z',count);
  s->write((const char*) ptr,count*sizeof(int64));
}

void Serialize::putInts(const int *ptr, int count) {
  sendSignature('i',count);
  s->write((const char*) ptr,count*sizeof(int));
}

void Serialize::putFloats(const float *ptr, int count) {
  sendSignature('f',count);
  s->write((const char*) ptr,count*sizeof(float));
}

void Serialize::putDoubles(const double *ptr, int count) {
  sendSignature('d',count);
  s->write((const char*) ptr,count*sizeof(double));
}

void Serialize::putString(QString p) {
  unsigned int len;
  sendSignature('x',0);
  len = p.size()+1;
  putInts((int*)&len,1);
  putBytes(qPrintable(p),len);
}

void Serialize::putBool(bool b) {
  if (b)
    putByte(1);
  else
    putByte(0);
}

bool Serialize::getBool() {
  char b;
  b = getByte();
  return (b == 1);
}

void Serialize::putByte(char t) {
  putBytes(&t,1);
}

void Serialize::putStringVector(StringVector t) {
  sendSignature('S',1);
  putInt(t.size());
  for (int i=0;i<t.size();i++)
    putString(t[i]);
}

StringVector Serialize::getStringVector() {
  checkSignature('S',1);
  int L = getInt();
  int i;
  StringVector N;
  for (i=0;i<L;i++)
    N.push_back(getString());
  return N;
}

void Serialize::putShort(short t) {
  putShorts(&t,1);
}

void Serialize::putInt(int t) {
  putInts(&t,1);
}

void Serialize::putFloat(float t) {
  putFloats(&t,1);
}

void Serialize::putDouble(double t) {
  putDoubles(&t,1);
}

void Serialize::getBytes(char *ptr, int count) {
  checkSignature('c',count);
  s->read((char*) ptr,count*sizeof(char));
}

#define SWAP(a,b) {tmp = a; a = b; b = tmp;}
void Serialize::getShorts(short *ptr, int count) {
  checkSignature('s',count);
  s->read((char*) ptr,count*sizeof(short));
  if (endianSwap) {
    char *cptr = (char *) ptr;
    char tmp;
    for (int i=0;i<2*count;i+=2)
      SWAP(cptr[i],cptr[i+1]);
  }
}

void Serialize::getI64s(int64 *ptr, int count) {
  checkSignature('z',count);
  s->read((char*) ptr,count*sizeof(int64));
  if (endianSwap) {
    char *cptr = (char *) ptr;
    char tmp;
    for (int i=0;i<8*count;i+=8) {
      SWAP(cptr[i],cptr[i+7]);
      SWAP(cptr[i+1],cptr[i+6]);
      SWAP(cptr[i+2],cptr[i+5]);
      SWAP(cptr[i+3],cptr[i+4]);
    }
  }
}

void Serialize::getInts(int *ptr, int count) {
  checkSignature('i',count);
  s->read((char*) ptr,count*sizeof(int));
  if (endianSwap) {
    char *cptr = (char *) ptr;
    char tmp;
    for (int i=0;i<4*count;i+=4) {
      SWAP(cptr[i],cptr[i+3]);
      SWAP(cptr[i+1],cptr[i+2]);
    }
  }
}

void Serialize::getFloats(float *ptr, int count) {
  checkSignature('f',count);
  s->read((char*) ptr,count*sizeof(float));
  if (endianSwap) {
    char *cptr = (char *) ptr;
    char tmp;
    for (int i=0;i<4*count;i+=4) {
      SWAP(cptr[i],cptr[i+3]);
      SWAP(cptr[i+1],cptr[i+2]);
    }
  }
}

void Serialize::getDoubles(double *ptr, int count) {
  checkSignature('d',count);
  s->read((char*)ptr,count*sizeof(double));
  if (endianSwap) {
    char *cptr = (char *) ptr;
    char tmp;
    for (int i=0;i<8*count;i+=8) {
      SWAP(cptr[i],cptr[i+7]);
      SWAP(cptr[i+1],cptr[i+6]);
      SWAP(cptr[i+2],cptr[i+5]);
      SWAP(cptr[i+3],cptr[i+4]);
    }
  }
}

QString Serialize::getString() {
  checkSignature('x',0);
  unsigned int len;
  getInts((int*) &len,1);
  if (len == 0) return QString();
  char *cp = (char*) malloc(len*sizeof(char));
  getBytes(cp,len);
  QString ret(cp);
  free(cp);
  return ret;
}

char Serialize::getByte() {
  char t;
  getBytes(&t,1);
  return t;
}

short Serialize::getShort() {
  short t;
  getShorts(&t,1);
  return t;
}

int Serialize::getInt() {
  int t;
  getInts(&t,1);
  return t;
}

float Serialize::getFloat() {
  float t;
  getFloats(&t,1);
  return t;
}

double Serialize::getDouble() {
  double t;
  getDoubles(&t,1);
  return t;
}

DataClass Serialize::getDataClass(bool& sparseflag, StringVector& className, bool& complexflag) {
  checkSignature('a',1);
  char a = getByte();
  sparseflag = (a & 16) > 0;
  complexflag = false;
  // For compatibility reasons, the sparse flag is stuck at
  // 16.  Which is binary:
  //   0001 0000
  // To mask out this bit, we need the following number:
  //   1110 1111 = 255 - 16 = 239
  a = a & 239;
  switch (a) {
  case 1:
    return CellArray;
  case 2:
    return Struct;
  case 3:
    return Bool;
  case 4:
    return UInt8;
  case 5:
    return Int8;
  case 6:
    return UInt16;
  case 7:
    return Int16;
  case 8:
    return UInt32;
  case 9:
    return Int32;
  case 32:
    return UInt64;
  case 33:
    return Int64;
  case 10:
    return Float;
  case 11:
    return Double;
  case 12:
    complexflag = true;
    return Float;
  case 13:
    complexflag = true;
    return Double;
  case 14:
    return StringArray;
  case 128: {
    int cnt(getInt());
    for (int i=0;i<cnt;i++)
      className.push_back(getString());
    return Struct;
  }    
  default:
    throw Exception("Unrecognized array type received!");
  }
}

void Serialize::putDataClass(DataClass cls, bool issparse, bool isuserclass, StringVector className, bool complexflag) {
  char sparseval;
  sparseval = issparse ? 16 : 0;
  sendSignature('a',1);
  switch (cls) {
  default:
    throw Exception("Unhandled type in putDataClass.");
  case CellArray:
    putByte(1);
    return;
  case Struct:
    if (!isuserclass)
      putByte(2);
    else {
      putByte(128);
      putInt(className.size());
      for (int i=0;i<className.size();i++)
	putString(className.at(i));
    }
    return;
  case Bool:
    putByte(3);
    return;
  case UInt8:
    if (complexflag) throw Exception("complex uint8 not supported");
    if (sparseval) throw Exception("sparse uint8 not supported");
    putByte(4);
    return;
  case Int8:
    if (complexflag) throw Exception("complex int8 not supported");
    if (sparseval) throw Exception("sparse int8 not supported");
    putByte(5);
    return;
  case UInt16:
    if (complexflag) throw Exception("complex uint16 not supported");
    if (sparseval) throw Exception("sparse uint16 not supported");
    putByte(6);
    return;
  case Int16:
    if (complexflag) throw Exception("complex int16 not supported");
    if (sparseval) throw Exception("sparse int16 not supported");
    putByte(7);
    return;
  case UInt32:
    if (complexflag) throw Exception("complex uint32 not supported");
    if (sparseval) throw Exception("sparse uint32 not supported");
    putByte(8);
    return;
  case Int32:
    if (complexflag) throw Exception("complex int32 not supported");
    putByte(9 | sparseval);
    return;
  case UInt64:
    if (complexflag) throw Exception("complex uint64 not supported");
    if (sparseval) throw Exception("sparse uint64 not supported");
    putByte(32);
    return;
  case Int64:
    if (complexflag) throw Exception("complex int64 not supported");
    putByte(33 | sparseval);
    return;
  case Float:
    if (complexflag) {
      putByte(12 | sparseval);
    } else {
      putByte(10 | sparseval);
    }
    return;
  case Double:
    if (complexflag) {
      putByte(13 | sparseval);
    } else {
      putByte(11 | sparseval);
    }
    return;
  case StringArray:
    putByte(14);
    return;
  }
}

void Serialize::putDimensions(const NTuple& dim) {
  sendSignature('D',1);
  putInt(dim.lastNotOne());
  for (int i=0;i<dim.lastNotOne();i++)
    putInt(int(dim[i]));
}

NTuple Serialize::getDimensions() {
  checkSignature('D',1);
  NTuple dim;
  int len;
  len = getInt();
  for (int i=0;i<len;i++)
    dim[i] = getInt();
  return dim;
}

#define MacroPutCase(func,ctype,class,ftype)				\
  {									\
    const Array &t(dat.asDenseArray().toClass(class));			\
    func((const ctype*)t.constReal<ftype>().constData(),int(dat.length())); \
    return;								\
  }


#define MacroPutComplexCase(func,ctype,class,ftype)			\
  {									\
    const Array &t(dat.asDenseArray().toClass(class)); \
    func((const ctype*)t.fortran<ftype>().constData(),int(2*dat.length()));		\
    return;								\
  }


void Serialize::putArray(const Array& dat) {
  sendSignature('A',1);
  DataClass dclass(dat.dataClass());
  putDataClass(dclass,dat.isSparse(),dat.isUserClass(),StringVector(dat.className()),dat.isComplex());
  putDimensions(dat.dimensions());
  if (dat.isEmpty()) return;
  switch(dclass) {
  default: throw Exception("unhandled type in putArray");
  case CellArray: {
    const BasicArray<Array> &rp(dat.constReal<Array>());
    for (index_t i=1;i<=rp.length();i++)
      putArray(rp[i]);
    return;
  }
  case Struct: {    
    StringVector fnames(FieldNames(dat));
    int ncount(fnames.size());
    putInt(ncount);
    for (int i=0;i<ncount;i++)
      putString(fnames.at(i));
    const StructArray &rp(dat.constStructPtr());
    for (index_t j=1;j<=dat.length();j++)
      for (int i=0;i<ncount;i++) {
	const BasicArray<Array>& dp(rp[i]);
	putArray(dp[j]);
      }
    return;
  }
  case Bool: MacroPutCase(putBytes,char,Int8,int8);
  case StringArray: MacroPutCase(putBytes,char,UInt8,uint8);
  case UInt8: MacroPutCase(putBytes,char,UInt8,uint8);
  case UInt16: MacroPutCase(putShorts,short,UInt16,uint16);
  case UInt32: MacroPutCase(putInts,int,UInt32,uint32);
  case UInt64: MacroPutCase(putI64s,int64,UInt64,uint64);
  case Int8: MacroPutCase(putBytes,char,Int8,int8);
  case Int16: MacroPutCase(putShorts,short,Int16,int16);
  case Int32: 
    if (dat.isSparse()) {
      QVector<QVector<int32> > dp(SparseFM3(dat.constRealSparse<int32>()));
      for (int i=0;i<dat.cols();i++) {
	putInt(1+dp[i][0]);
	putInts((const int*) dp[i].constData(),int(1+dp[i][0]));
      } 
      return;
    } else
       MacroPutCase(putInts,int,Int32,int32);
  case Int64: MacroPutCase(putI64s,int64,Int64,int64);
  case Float: 
    if (dat.allReal()) {
      if (dat.isSparse()) {
	QVector<QVector<float> > dp(SparseFM3(dat.constRealSparse<float>()));
	for (int i=0;i<dat.cols();i++) {
	  putFloat(1+dp[i][0]);
	  putFloats((const float*) dp[i].constData(),int(1+dp[i][0]));
	} 
	return;
      } else
	MacroPutCase(putFloats,float,Float,float);
    } else {
      if (dat.isSparse()) {
	QVector<QVector<float> > dp(SparseFM3(dat.constRealSparse<float>(),dat.constImagSparse<float>()));
	for (int i=0;i<dat.cols();i++) {
	  putFloat(1+dp[i][0]);
	  putFloats((const float*) dp[i].constData(),int(1+dp[i][0]));
	} 	
      } else
	MacroPutComplexCase(putFloats,float,Float,float);
    }
  case Double:
    if (dat.allReal()) {
      if (dat.isSparse()) {
	QVector<QVector<double> > dp(SparseFM3(dat.constRealSparse<double>()));
	for (int i=0;i<dat.cols();i++) {
	  putDouble(1+dp[i][0]);
	  putDoubles((const double*) dp[i].constData(),int(1+dp[i][0]));
	} 
	return;
      } else
	MacroPutCase(putDoubles,double,Double,double);
    } else {
      if (dat.isSparse()) {
	QVector<QVector<double> > dp(SparseFM3(dat.constRealSparse<double>(),dat.constImagSparse<double>()));
	for (int i=0;i<dat.cols();i++) {
	  putDouble(1+dp[i][0]);
	  putDoubles((const double*) dp[i].constData(),int(1+dp[i][0]));
	} 	
      } else
	MacroPutComplexCase(putDoubles,double,Double,double);
    }
  }
}

#define MacroGetCase(func,ctype,class,ftype)	\
  {						\
    BasicArray<ftype> rp(dims);			\
    func((ctype*) rp.data(),int(dims.count()));	\
    dat = Array(rp).toClass(class);		\
    return;					\
  }

#define MacroGetComplexCase(func,ctype,class,ftype)		\
  {								\
    BasicArray<ftype> rp(dims.replace(0,dims[0]*2));		\
    func((ctype*) rp.data(),int(dat.length()));			\
    dat = Array(SplitReal(rp),SplitImag(rp)).toClass(class);	\
    return;							\
  }

void Serialize::getArray(Array& dat) {
  checkSignature('A',1);
  bool sparseflag;
  StringVector className;
  bool complexflag;
  DataClass dclass(getDataClass(sparseflag,className,complexflag));
  NTuple dims(getDimensions());
  int elCount(int(dims.count()));
  if (elCount == 0) {
    dat = EmptyConstructor();
    return;
  }
  switch(dclass) {
  default:
    throw Exception("Unhandled type in getArray");
  case CellArray: {
    BasicArray<Array> rp(dims);
    for (index_t i=1;i<=rp.length();i++)
      getArray(rp[i]);
    dat = Array(rp);
    return;
  }
  case Struct: {
    StringVector fnames;
    int ncount(getInt());
    StructArray rp;
    for (int i=0;i<ncount;i++) 
      rp.insert(getString(),BasicArray<Array>(dims));
    rp.setClassPath(className);
    for (index_t j=1;j<=dims.count();j++)
      for (int i=0;i<ncount;i++)
	getArray(rp[i][j]);
    rp.updateDims();
    dat = Array(rp);
    return;
  }
  case Bool: MacroGetCase(getBytes,char,Bool,int8);
  case StringArray: MacroGetCase(getBytes,char,StringArray,uint8);
  case UInt8: MacroGetCase(getBytes,char,UInt8,uint8);
  case UInt16: MacroGetCase(getShorts,short,UInt16,uint16);
  case UInt32: MacroGetCase(getInts,int,UInt32,uint32);
  case UInt64: MacroGetCase(getI64s,int64,UInt64,uint64);
  case Int64: MacroGetCase(getI64s,int64,Int64,int64);
  case Int32:
    if (sparseflag) {
      QVector<QVector<int32> > dp;
      for (index_t i=1;i<=dims.cols();i++) {
	int len = getInt();
	QVector<int32> col(len);
	getInts(col.data(),len);
	dp << col;
      }
      dat = FM3Sparse(dp,dims);
      return;
    } else
      MacroGetCase(getInts,int,Int32,int32);
  case Float:
    if (!complexflag) {
      if (sparseflag) {
	QVector<QVector<float> > dp;
	for (index_t i=1;i<=dims.cols();i++) {
	  int len = int(getFloat());
	  QVector<float> col(len);
	  getFloats(col.data(),len);
	  dp << col;
	}
	dat = FM3Sparse(dp,dims);
	return;
      } else
	MacroGetCase(getFloats,float,Float,float);
    } else {
      if (sparseflag) {
	QVector<QVector<float> > dp;
	for (index_t i=1;i<=dims.cols();i++) {
	  int len = int(getFloat());
	  QVector<float> col(len);
	  getFloats(col.data(),len);
	  dp << col;
	}
	dat = FM3SparseComplex(dp,dims);
	return;
      } else
	MacroGetComplexCase(getFloats,float,Float,float);
    }
  case Double:
    if (!complexflag) {
      if (sparseflag) {
	QVector<QVector<double> > dp;
	for (index_t i=1;i<=dims.cols();i++) {
	  int len = int(getDouble());
	  QVector<double> col(len);
	  getDoubles(col.data(),len);
	  dp << col;
	}
	dat = FM3Sparse(dp,dims);
	return;
      } else
	MacroGetCase(getDoubles,double,Double,double);
    } else {
      if (sparseflag) {
	QVector<QVector<double> > dp;
	for (index_t i=1;i<=dims.cols();i++) {
	  int len = int(getDouble());
	  QVector<double> col(len);
	  getDoubles(col.data(),len);
	  dp << col;
	}
	dat = FM3SparseComplex(dp,dims);
	return;
      } else
	MacroGetComplexCase(getDoubles,double,Double,double);
    }
  }
}