aboutsummaryrefslogtreecommitdiff
path: root/enginedata.cpp
blob: a1e8da43572a6add0e129c9ea0918dca6264c275 (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
#include "enginedata.h"
#include "util.h"
#include <iostream>
#include <list>
#include <cassert>

using namespace std;

RowItem RowItem::copy(const ColHeader &header){
	RowItem ri;
	switch(header.type){
	case RH_INT32:
		ri.v.rh_int32=v.rh_int32;
		break;
	case RH_UINT32:
		ri.v.rh_uint32=v.rh_uint32;
		break;
	case RH_BYTES:
		ri.v.rh_bytes=new unsigned char[header.arg];
		memcpy(ri.v.rh_bytes,v.rh_bytes,header.arg);
		break;
	}
	return ri;
}

Row::Row(void):nc(0),table(NULL){}
Row::Row(const int _nc,Table *_t):nc(_nc),table(_t){
	items=new RowItem[nc];
}
Row::~Row(void){
	if(items)delete[] items;
}
Row::Row(Row &&other):nc(other.nc),table(other.table),items(other.items){ //move constr
	other.items=nullptr;
}
Row::Row(const Row &other):nc(other.nc),table(other.table){ //copy constr
	items=new RowItem[nc];
	for(int i=0;i<nc;i++){
		items[i]=other.items[i].copy(table->header[i]);
	}
}
Row& Row::operator=(const Row &other){ //copy assignment
	nc=other.nc; table=other.table;
	items=new RowItem[nc];
	for(int i=0;i<nc;i++){
		items[i]=other.items[i].copy(table->header[i]);
	}
	return *this;
}
ostream& operator<<(ostream &os,const Row &r){
	os<<'[';
	for(int i=0;i<r.nc;i++){
		if(i)os<<", ";
		os<<serialise(r.table->header[i],r.items[i]);
	}
	os<<']';
	return os;
}


Table::Table(const string &_n,const int _nc,const ColHeader *const _hd):name(_n),nc(_nc){
	header=new ColHeader[nc];
	memcpy(header,_hd,nc*sizeof(ColHeader));
}
Table::~Table(void){
	if(header)delete[] header;
}
Table::Table(const Table &other):name(other.name),nc(other.nc),rows(other.rows){
	memcpy(header,other.header,nc*sizeof(ColHeader));
}
Table::Table(Table &&other):name(move(other.name)),nc(other.nc),header(other.header),rows(move(other.rows)){
	other.header=nullptr;
}



string serialise(const ColHeader &header,const RowItem &rowitem){
	switch(header.type){
	case RH_INT32:  return serialise(rowitem.v.rh_int32);
	case RH_UINT32: return serialise(rowitem.v.rh_uint32);
	case RH_BYTES:  return serialise(rowitem.v.rh_bytes,header.arg);
	default: assert(false);
	}
}
string serialise(int32_t v){return to_string(v);}
string serialise(uint32_t v){return to_string(v);}
string serialise(unsigned char *v,int len){return string((char*)v,len);}

int riCompare(const ColHeader &header,const RowItem &ri1,const RowItem &ri2){
	switch(header.type){
	case RH_INT32:  return riCompare(ri1.v.rh_int32,ri2.v.rh_int32);
	case RH_UINT32: return riCompare(ri1.v.rh_uint32,ri2.v.rh_uint32);
	case RH_BYTES:  return riCompare(ri1.v.rh_bytes,ri2.v.rh_bytes,header.arg);
	default: assert(false);
	}
}
int riCompare(int32_t v1,int32_t v2){return v1-v2;}
int riCompare(uint32_t v1,uint32_t v2){return v1-v2;}
int riCompare(unsigned char *v1,unsigned char *v2,int len){
	int i;
	// cerr<<"Comparing "<<(void*)v1<<" ("<<v1<<") and "<<(void*)v2<<" ("<<v2<<')'<<endl;
	for(i=0;i<len;i++){
		if(v1[i]!=v2[i])return v1[i]-v2[i];
	}
	return 0;
}