storage.cpp 9.46 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
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
#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();
		size_t bytes = t.GetBytesSize();
		if( data_size == ENUMUNDEF ) //variable size array
		{
			DataType type = t.GetDataType();
			if( adata != NULL ) DestroyVariableData(t,adata);
			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
64
65
66
67
68
	{
		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();
			INMOST_DATA_ENUM_TYPE from = record_size*local_id, to = record_size*new_local_id;
			memcpy(&arr[to],&arr[from],record_size);
			memset(&arr[from],0,record_size);
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
69
70
71
72
73
74
75
	void Storage::SwapSparseData(INMOST_DATA_INTEGER_TYPE new_local_id)
	{
		Mesh * m = GetMeshLink();
		if( m == NULL ) return;
		inner_data.swap(m->ElementByLocalID(GetElementType(),new_local_id)->inner_data);
	}
	void Storage::SwapDenseData(INMOST_DATA_INTEGER_TYPE new_local_id)
Kirill Terekhov's avatar
Kirill Terekhov committed
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
	{
		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();
			INMOST_DATA_ENUM_TYPE from = record_size*local_id, to = record_size*new_local_id;
			
			//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);
		markers = 0;
		m_link = m;
		if( m != NULL ) m->TieElement(this);
	}
	Storage::Storage(Mesh * m, INMOST_DATA_ENUM_TYPE lid, const Storage & other)
	{
		etypenum = other.etypenum;
		markers = other.markers;
		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);
				CopyData(*t,a,b);
			}
		}
	}

	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;
		markers = other.markers;
		
		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);
				CopyData(*t,a,b);
			}
		}
		
		return *this;
	}
	
	
	size_t Storage::GetDataSize(Tag tag) const
	{
		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();
	}
	void Storage::SetDataSize(Tag tag,size_t new_size)
	{
		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;
	}
	void Storage::GetData(Tag tag,size_t shift, size_t size, void * data_out) const
	{
		assert( tag.GetMeshLink() == GetMeshLink() );
		void * adata = GetLink(tag);
		if( adata == NULL ) throw NoData;
		INMOST_DATA_ENUM_TYPE data_size = tag.GetSize();
		size_t bytes = tag.GetBytesSize();
		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;
	}
	void Storage::SetData(Tag tag,size_t shift, size_t size, void * data_in)
	{
		assert( tag.GetMeshLink() == GetMeshLink() );
		void * adata = GetLink(tag);
		if( adata == NULL ) throw NoData;
		INMOST_DATA_ENUM_TYPE data_size = tag.GetSize();
		size_t bytes = tag.GetBytesSize();
		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);
	}
	void Storage::DelData(Tag tag)
	{
		assert( tag.GetMeshLink() == GetMeshLink() );
		if( tag.isSparse(GetElementType()) )
		{
			/* old sparse data
			TagManager::sparse_sub_type & arr = tag.GetTagManager()->GetSparseData(tag.GetPosition(GetElementType()));
			TagManager::sparse_sub_type::iterator it = arr.find(this);
			if( it != arr.end() )
			{
				if( tag.GetSize() == ENUMUNDEF ) DestroyVariableData(tag,it->second);
				free(it->second);
				arr.erase(it);
			}
			*/
			for(sparse_data_array_type::iterator it = inner_data.begin(); it != inner_data.end(); ++it)
				if( it->first == tag ) 
				{
					if( tag.GetSize() == ENUMUNDEF ) DestroyVariableData(tag,it->second);
					free(it->second);
					it = inner_data.erase(it);
					break;
				}
		}
		else 
		{
			void * data = GetLink(tag);
			if( data != NULL )
			{
				if( tag.GetSize() == ENUMUNDEF )
					DestroyVariableData(tag,data);
				else memset(data,0,tag.GetRecordSize());
			}
		}
	}

	

}

#endif