storage.cpp 11.4 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1
2
3
4
5
6
7
8
9
10
11
#include "inmost.h"

#if defined(USE_MESH)
#include <new>

namespace INMOST
{
	void Storage::CopyData(Tag t, void * adata, void * bdata)
	{
		
		INMOST_DATA_ENUM_TYPE data_size = t.GetSize();
Kirill Terekhov's avatar
Kirill Terekhov committed
12
		INMOST_DATA_ENUM_TYPE bytes = t.GetBytesSize();
Kirill Terekhov's avatar
Kirill Terekhov committed
13
14
15
		if( data_size == ENUMUNDEF ) //variable size array
		{
			DataType type = t.GetDataType();
Kirill Terekhov's avatar
Kirill Terekhov committed
16
			if( adata != NULL ) Storage::DestroyVariableData(t,adata);
Kirill Terekhov's avatar
Kirill Terekhov committed
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
			if( type == DATA_REAL )           new (adata) inner_real_array     (*static_cast< inner_real_array      * >(bdata));
			else if( type == DATA_INTEGER )   new (adata) inner_integer_array  (*static_cast< inner_integer_array   * >(bdata));
			else if( type == DATA_BULK )      new (adata) inner_bulk_array     (*static_cast< inner_bulk_array      * >(bdata));
			else if( type == DATA_REFERENCE ) new (adata) inner_reference_array(*static_cast< inner_reference_array * >(bdata));
		}
		else // fixed size array
			memcpy(adata,bdata,data_size*bytes);
	}
	void Storage::DestroyVariableData(Tag t, void * adata)
	{
		INMOST_DATA_ENUM_TYPE data_size = t.GetSize();
		if( data_size == ENUMUNDEF && adata != NULL ) //variable size array
		{
			DataType type = t.GetDataType();
			if( type == DATA_REAL ) 
			{
				(*static_cast<inner_real_array *> (adata)).~inner_real_array();
				new (adata) inner_real_array(); //reinitialize for reuse
			}
			else if( type == DATA_INTEGER ) 
			{
				(*static_cast<inner_integer_array *> (adata)).~inner_integer_array();
				new (adata) inner_integer_array();
			}
			else if( type == DATA_BULK ) 
			{
				(*static_cast<inner_bulk_array *> (adata)).~inner_bulk_array();
				new (adata) inner_bulk_array();
			}
			else if( type == DATA_REFERENCE ) 
			{
				(*static_cast<inner_reference_array *> (adata)).~inner_reference_array();
				new (adata) inner_reference_array();
			}
		}
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
54
	void Storage::MoveData(INMOST_DATA_INTEGER_TYPE new_local_id)
Kirill Terekhov's avatar
Kirill Terekhov committed
55
56
57
58
59
60
61
62
63
	{
		Mesh * m = GetMeshLink();
		if( m == NULL ) return;
		for(Mesh::iteratorTag t = m->BeginTag(); t != m->EndTag(); t++) if( !t->isSparse(GetElementType()) )
		{
			INMOST_DATA_ENUM_TYPE data_pos = t->GetPosition(GetElementType());
			if( data_pos == ENUMUNDEF ) continue;
			TagManager::dense_sub_type & arr = t->GetTagManager()->GetDenseData(data_pos);
			INMOST_DATA_ENUM_TYPE record_size = t->GetRecordSize();
Kirill Terekhov's avatar
Kirill Terekhov committed
64
			INMOST_DATA_ENUM_TYPE from = local_id, to = new_local_id;
Kirill Terekhov's avatar
Kirill Terekhov committed
65
66
67
68
			memcpy(&arr[to],&arr[from],record_size);
			memset(&arr[from],0,record_size);
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
69
70
71
72
	void Storage::SwapSparseData(INMOST_DATA_INTEGER_TYPE new_local_id)
	{
		Mesh * m = GetMeshLink();
		if( m == NULL ) return;
Kirill Terekhov's avatar
Kirill Terekhov committed
73
74
75
#if defined(NEW_SPARSE)
		SLink().swap(m->ElementByLocalID(GetElementType(),new_local_id)->SLink());
#else
Kirill Terekhov's avatar
Kirill Terekhov committed
76
		inner_data.swap(m->ElementByLocalID(GetElementType(),new_local_id)->inner_data);
Kirill Terekhov's avatar
Kirill Terekhov committed
77
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
78
79
	}
	void Storage::SwapDenseData(INMOST_DATA_INTEGER_TYPE new_local_id)
Kirill Terekhov's avatar
Kirill Terekhov committed
80
81
82
83
84
85
86
87
88
89
90
	{
		Mesh * m = GetMeshLink();
		if( m == NULL ) return;
		dynarray<bulk,2048> temp;
		for(Mesh::iteratorTag t = m->BeginTag(); t != m->EndTag(); t++) if( !t->isSparse(GetElementType()) )
		{

			INMOST_DATA_ENUM_TYPE data_pos = t->GetPosition(GetElementType());
			if( data_pos == ENUMUNDEF ) continue;
			TagManager::dense_sub_type & arr = t->GetTagManager()->GetDenseData(data_pos);
			INMOST_DATA_ENUM_TYPE record_size = t->GetRecordSize();
Kirill Terekhov's avatar
Kirill Terekhov committed
91
			INMOST_DATA_ENUM_TYPE from = local_id, to = new_local_id;
Kirill Terekhov's avatar
Kirill Terekhov committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
			
			//std::cout << "swap " << t->GetTagName() << " from " << local_id << " to " << new_local_id << " record size: " << record_size << " bytes: " << bytes << " data size: " << data_size << std::endl;
			
			temp.resize(record_size);
			memcpy(&temp[0],&arr[to],record_size);
			memcpy(&arr[to],&arr[from],record_size);
			memcpy(&arr[from],&temp[0],record_size);
		}
	}
	Storage::Storage(Mesh * m, ElementType _etype)
	{
		if( sizeof(INMOST_DATA_BULK_TYPE) != 1 ) throw BadBulkType;
		INMOST_DATA_ENUM_TYPE i = 0;
#if !defined(NDEBUG)
		for(ElementType e = NODE; e <= MESH; e = e << 1 ) if( _etype & e ) i++;
		assert( i != 0 );
		assert( i == 1 );
#endif
		etypenum = ElementNum(_etype);
Kirill Terekhov's avatar
Kirill Terekhov committed
111
#if !defined(NEW_MARKERS)
Kirill Terekhov's avatar
Kirill Terekhov committed
112
		markers = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
113
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
114
115
116
117
118
119
		m_link = m;
		if( m != NULL ) m->TieElement(this);
	}
	Storage::Storage(Mesh * m, INMOST_DATA_ENUM_TYPE lid, const Storage & other)
	{
		etypenum = other.etypenum;
Kirill Terekhov's avatar
Kirill Terekhov committed
120
#if !defined(NEW_MARKERS)
Kirill Terekhov's avatar
Kirill Terekhov committed
121
		markers = other.markers;
Kirill Terekhov's avatar
Kirill Terekhov committed
122
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
		m_link = m;
		local_id = lid;
		//if( m != NULL ) m->TieElement(this);
		Mesh * other_mesh = other.GetMeshLink();
		void * a, * b;
		if( m == NULL || other_mesh == NULL ) return;

		for(Mesh::iteratorTag t = m->BeginTag(); t != m->EndTag(); t++) if( t->isDefined(GetElementType()) )
		{
			Mesh::iteratorTag other_t = other_mesh->BeginTag() + (t-m->BeginTag());
			b = other.GetLink(*other_t);
			if( b != NULL )
			{
				a = GetLink(*t);
Kirill Terekhov's avatar
Kirill Terekhov committed
137
				Storage::CopyData(*t,a,b);
Kirill Terekhov's avatar
Kirill Terekhov committed
138
139
140
141
142
143
144
145
146
147
148
			}
		}
	}

	Storage::Storage(const Storage & other) {throw NotImplemented;}

	
	Storage & Storage::assign(Mesh * m, INMOST_DATA_ENUM_TYPE lid, Storage const & other)
	{
		Mesh * other_mesh = other.GetMeshLink();
		etypenum = other.etypenum;
Kirill Terekhov's avatar
Kirill Terekhov committed
149
150
151
152
153
154
155
#if defined(NEW_MARKERS)
		Storage::bulk marker_space[MarkerFields];
		other.GetMarkerSpace(marker_space);
		SetMarkerSpace(marker_space);
#else
		markers = other.GetMarkerSpace();
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
		
		m_link = m;
		local_id = lid;
		
		void * a, * b;
		if( m == NULL || other_mesh == NULL ) return *this;


		for(Mesh::iteratorTag t = m->BeginTag(); t != m->EndTag(); t++) if( t->isDefined(GetElementType()) )
		{
			Mesh::iteratorTag other_t = other_mesh->BeginTag() + (t-m->BeginTag());
			b = other.GetLink(*other_t);
			if( b != NULL )
			{
				a = GetLink(*t);
Kirill Terekhov's avatar
Kirill Terekhov committed
171
				Storage::CopyData(*t,a,b);
Kirill Terekhov's avatar
Kirill Terekhov committed
172
173
174
175
176
177
178
			}
		}
		
		return *this;
	}
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
179
	INMOST_DATA_ENUM_TYPE Storage::GetDataSize(const Tag & tag) const
Kirill Terekhov's avatar
Kirill Terekhov committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
	{
		if( tag.GetSize() == ENUMUNDEF )
		{
			void * adata = GetLink(tag);
			if( adata == NULL ) throw NoTagPosition;
			switch(tag.GetDataType())
			{
				case DATA_REAL:     return static_cast<inner_real_array     *>(adata)->size();
				case DATA_INTEGER:  return static_cast<inner_integer_array  *>(adata)->size();
				case DATA_BULK:     return static_cast<inner_bulk_array     *>(adata)->size();
				case DATA_REFERENCE:return static_cast<inner_reference_array*>(adata)->size();
			}
			throw BadTag;
		}
		return tag.GetSize();
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
196
	void Storage::SetDataSize(const Tag & tag,INMOST_DATA_ENUM_TYPE new_size)
Kirill Terekhov's avatar
Kirill Terekhov committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
	{
		assert( tag.GetMeshLink() == GetMeshLink() );
		void * adata = GetLink(tag);
		if( adata == NULL ) throw NoTagPosition;
		if( tag.GetSize() == ENUMUNDEF )
		{
			switch(tag.GetDataType())
			{
				case DATA_REAL:     static_cast<inner_real_array      *>(adata)->resize(new_size); break;
				case DATA_INTEGER:  static_cast<inner_integer_array   *>(adata)->resize(new_size); break;
				case DATA_BULK:     static_cast<inner_bulk_array      *>(adata)->resize(new_size); break;
				case DATA_REFERENCE:static_cast<inner_reference_array *>(adata)->resize(new_size); break;
			}
			return;
		}
		else if( tag.GetSize() == new_size )
			return;
		throw BadTag;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
216
	void Storage::GetData(const Tag & tag,INMOST_DATA_ENUM_TYPE shift, INMOST_DATA_ENUM_TYPE size, void * data_out) const
Kirill Terekhov's avatar
Kirill Terekhov committed
217
218
219
220
221
	{
		assert( tag.GetMeshLink() == GetMeshLink() );
		void * adata = GetLink(tag);
		if( adata == NULL ) throw NoData;
		INMOST_DATA_ENUM_TYPE data_size = tag.GetSize();
Kirill Terekhov's avatar
Kirill Terekhov committed
222
		INMOST_DATA_ENUM_TYPE bytes = tag.GetBytesSize();
Kirill Terekhov's avatar
Kirill Terekhov committed
223
224
225
226
227
228
229
230
231
232
233
234
235
		if( data_size == ENUMUNDEF )
		{
			switch(tag.GetDataType())
			{
				case DATA_REAL:     memcpy(data_out,&(*static_cast<inner_real_array      *>(adata))[shift],bytes*size); break;
				case DATA_INTEGER:  memcpy(data_out,&(*static_cast<inner_integer_array   *>(adata))[shift],bytes*size); break;
				case DATA_BULK:     memcpy(data_out,&(*static_cast<inner_bulk_array      *>(adata))[shift],bytes*size); break;
				case DATA_REFERENCE:memcpy(data_out,&(*static_cast<inner_reference_array *>(adata))[shift],bytes*size); break;
			}
		}
		else memcpy(data_out,static_cast<INMOST_DATA_BULK_TYPE *>(adata)+shift*bytes,size*bytes);
		return;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
236
	void Storage::SetData(const Tag & tag,INMOST_DATA_ENUM_TYPE shift, INMOST_DATA_ENUM_TYPE size, void * data_in)
Kirill Terekhov's avatar
Kirill Terekhov committed
237
238
239
240
241
	{
		assert( tag.GetMeshLink() == GetMeshLink() );
		void * adata = GetLink(tag);
		if( adata == NULL ) throw NoData;
		INMOST_DATA_ENUM_TYPE data_size = tag.GetSize();
Kirill Terekhov's avatar
Kirill Terekhov committed
242
		INMOST_DATA_ENUM_TYPE bytes = tag.GetBytesSize();
Kirill Terekhov's avatar
Kirill Terekhov committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
		if( data_size == ENUMUNDEF )
		{
			switch(tag.GetDataType())
			{
				case DATA_REAL:     memcpy(&(*static_cast<inner_real_array     *>(adata))[shift],data_in,bytes*size); break;
				case DATA_INTEGER:  memcpy(&(*static_cast<inner_integer_array  *>(adata))[shift],data_in,bytes*size); break;
				case DATA_BULK:     memcpy(&(*static_cast<inner_bulk_array     *>(adata))[shift],data_in,bytes*size); break;
				case DATA_REFERENCE:memcpy(&(*static_cast<inner_reference_array*>(adata))[shift],data_in,bytes*size); break;
			}
		}
		else memcpy(static_cast<INMOST_DATA_BULK_TYPE *>(adata)+shift*bytes,data_in,size*bytes);
		return;
	}
	Storage::~Storage()
	{
		Mesh * m = GetMeshLink();
		if( m == NULL ) return;
		for(Mesh::iteratorTag t = m->BeginTag(); t != m->EndTag(); t++)
			if( t->isDefined(GetElementType()) ) DelData(*t);
		
		if( !(GetElementType() & MESH) )
			m->UntieElement(this);
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
266
	void Storage::DelData(const Tag & tag)
Kirill Terekhov's avatar
Kirill Terekhov committed
267
268
269
270
	{
		assert( tag.GetMeshLink() == GetMeshLink() );
		if( tag.isSparse(GetElementType()) )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
271
272
273
274
#if defined(NEW_SPARSE)
			sparse_type & s = SLink();
			INMOST_DATA_ENUM_TYPE tag_size = tag.GetSize();
			for(int i = 0; i < s.size(); ++i) if( s[i].tag == tag.mem )
Kirill Terekhov's avatar
Kirill Terekhov committed
275
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
276
277
278
279
				if( tag_size == ENUMUNDEF ) Storage::DestroyVariableData(tag,s[i].rec);
				free(s[i].rec);
				s.erase(s.begin()+i);
				break;
Kirill Terekhov's avatar
Kirill Terekhov committed
280
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
281
#else
Kirill Terekhov's avatar
Kirill Terekhov committed
282
283
284
			for(sparse_data_array_type::iterator it = inner_data.begin(); it != inner_data.end(); ++it)
				if( it->first == tag ) 
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
285
					if( tag.GetSize() == ENUMUNDEF ) Storage::DestroyVariableData(tag,it->second);
Kirill Terekhov's avatar
Kirill Terekhov committed
286
287
288
289
					free(it->second);
					it = inner_data.erase(it);
					break;
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
290
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
291
292
293
294
295
296
297
		}
		else 
		{
			void * data = GetLink(tag);
			if( data != NULL )
			{
				if( tag.GetSize() == ENUMUNDEF )
Kirill Terekhov's avatar
Kirill Terekhov committed
298
					Storage::DestroyVariableData(tag,data);
Kirill Terekhov's avatar
Kirill Terekhov committed
299
300
301
302
303
				else memset(data,0,tag.GetRecordSize());
			}
		}
	}

Kirill Terekhov's avatar
Kirill Terekhov committed
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
#if defined(NEW_MARKERS)
	void Storage::SetMarker(MarkerType n) 
	{
		assert( (n >> MarkerShift) < MarkerFields );
		static_cast<Storage::bulk *>(GetDenseLink(GetMeshLink()->MarkersTag()))[n >> MarkerShift] |= static_cast<Storage::bulk>(n & MarkerMask);
	}
	bool Storage::GetMarker(MarkerType n) const  
	{
		assert( (n >> MarkerShift) < MarkerFields );
		return (static_cast<Storage::bulk *>(GetDenseLink(GetMeshLink()->MarkersTag()))[n >> MarkerShift] & static_cast<Storage::bulk>(n & MarkerMask)) != 0;
	}
	void Storage::RemMarker(MarkerType n) 
	{
		assert( (n >> MarkerShift) < MarkerFields );
		static_cast<Storage::bulk *>(GetDenseLink(GetMeshLink()->MarkersTag()))[n >> MarkerShift] &= ~static_cast<Storage::bulk>(n & MarkerMask);
	}
	void Storage::ClearMarkerSpace() 
	{
		Storage::bulk * marker_space = static_cast<Storage::bulk *>(GetDenseLink(GetMeshLink()->MarkersTag()));
		for(INMOST_DATA_ENUM_TYPE k = 0; k < MarkerFields; ++k) marker_space[k] = 0;
	}
	void Storage::GetMarkerSpace(Storage::bulk copy[MarkerFields]) const 
	{
		Storage::bulk * marker_space = static_cast<Storage::bulk *>(GetDenseLink(GetMeshLink()->MarkersTag()));
		for(INMOST_DATA_ENUM_TYPE k = 0; k < MarkerFields; ++k) copy[k] = marker_space[k];
	}
	void Storage::SetMarkerSpace(Storage::bulk source[MarkerFields]) 
	{
		Storage::bulk * marker_space = static_cast<Storage::bulk *>(GetDenseLink(GetMeshLink()->MarkersTag()));
		for(INMOST_DATA_ENUM_TYPE k = 0; k < MarkerFields; ++k) marker_space[k] = source[k];
	}
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
336
337
338
339

}

#endif