DCL 4.0
Loading...
Searching...
No Matches
ArrayT.h
Go to the documentation of this file.
1#ifndef __DCL_ARRAY_T_H__
2#define __DCL_ARRAY_T_H__ 20110304
3
4#ifndef __DCL_CONFIG_H__
5 #include <dcl/Config.h>
6#endif
7
8#if !__DCL_DEBUG && !defined(__DCL_INCLUDED_STDLIB_H)
9 #include <stdlib.h> // malloc, free, realloc
10 #define __DCL_INCLUDED_STDLIB_H
11#endif
12#ifndef __DCL_INCLUDED_STRING_H
13 #include <string.h> // memset, memmove
14 #define __DCL_INCLUDED_STRING_H
15#endif
16
17#ifndef __DCL_OBJECT_H__
18#include <dcl/Object.h>
19#endif
20
21__DCL_BEGIN_NAMESPACE
22
23#if __DCL_HAVE_ALLOC_DEBUG
24 #undef __DCL_ALLOC_LEVEL
25 #define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
26 #undef new
27 #define new __DCL_DEBUG_NEW
28#endif
29
30#if __DCL_HAVE_THIS_FILE__
31 static const char_t __szArrayT_h__[] = __T("dcl/ArrayT.h");
32 #undef __THIS_FILE__
33 #define __THIS_FILE__ __szArrayT_h__
34#endif
35
39
40template<typename ELEMENT>
41class Array : public Object
42{
44public:
45 typedef const ELEMENT* ConstIterator;
46 typedef ELEMENT* Iterator;
47
48public:
49 virtual ~Array();
50 // ~Array();
51 Array(size_t _size = 0);
52 Array(const Array<ELEMENT>& _src);
54
55 ConstIterator begin() const;
56 ConstIterator end() const;
57 Iterator begin();
58 Iterator end();
59
60 void clear();
61 void shrink();
62 void resize(size_t _size);
63
64 // @return is inserted element Iterator
65 Iterator insert(Iterator _pos, const ELEMENT& _element);
66 Array<ELEMENT>& insert(size_t _index, const ELEMENT& _element);
67 Array<ELEMENT>& add(const ELEMENT& _element);
68 Array<ELEMENT>& add(const Array<ELEMENT>& _src);
69
70 Iterator find(const ELEMENT& _element);
71
72 // @return is next Iterator
73 Iterator erase(Iterator _pos);
74 Iterator erase(Iterator _first, Iterator _last);
75 Array<ELEMENT>& erase(size_t _index);
76 Array<ELEMENT>& erase(size_t _index, size_t _size);
77
78 size_t index(Iterator _pos) const;
79 size_t size() const;
80 bool isEmpty() const;
81
82 ELEMENT& operator[](size_t _index);
83 ELEMENT operator[](size_t _index) const;
84 ELEMENT* data() const;
85
86 size_t size(ConstIterator _first, ConstIterator _last) const;
87 size_t size(Iterator _first, Iterator _last) const;
88
89protected:
90 ELEMENT* __pData;
91
92 struct Buffer
93 {
94 size_t __size;
95 size_t __maxSize;
96 ELEMENT* data() { return (ELEMENT*) (this + 1); }
97 } ;
98
99 Buffer* __buf() const { return (Buffer*) __pData - 1; }
100 size_t& __size() const { return __buf()->__size; }
101 size_t& __maxSize() const { return __buf()->__maxSize; }
102
103 void constructElements(ELEMENT* _pElements, size_t _size);
104 void destructElements(ELEMENT* _pElements, size_t _size);
105};
106
108
109template<typename ELEMENT>
110inline
113{
114 return (const ELEMENT*) __pData;
115}
116
117template<typename ELEMENT>
118inline
121{
122 return (const ELEMENT*) __pData + size();
123}
124
125template<typename ELEMENT>
126inline
127typename Array<ELEMENT>::Iterator
129{
130 return __pData;
131}
132
133template<typename ELEMENT>
134inline
135typename Array<ELEMENT>::Iterator
137{
138 return __pData + size();
139}
140
141template<typename ELEMENT>
142inline
144Array<ELEMENT>::add(const ELEMENT& _element)
145{
146 return insert(size(), _element);
147}
148
149template<typename ELEMENT>
150inline
151typename Array<ELEMENT>::Iterator
153{
154 __DCL_ASSERT_PARAM(begin() <= _pos);
155 __DCL_ASSERT_PARAM(_pos < end());
156#if 0
157 erase(_pos - begin(), 1);
158 return _pos;
159#endif
160 size_t index = _pos - begin();
161 erase(index, 1);
162 return __pData + index;
163}
164
165template<typename ELEMENT>
166inline
167typename Array<ELEMENT>::Iterator
168Array<ELEMENT>::erase(Iterator _first, Iterator _last)
169{
170 __DCL_ASSERT_PARAM(begin() <= _first);
171 __DCL_ASSERT_PARAM(_last <= end());
172 erase(_first - begin(), _last - _first);
173 return _first;
174}
175
176template<typename ELEMENT>
177inline
180{
181 __DCL_ASSERT_PARAM(_index < size());
182 return erase(_index, 1);
183}
184
185template<typename ELEMENT>
186inline
187size_t
188Array<ELEMENT>::index(Iterator _pos) const
189{
191 return _pos - __pData;
192}
193
194template<typename ELEMENT>
195inline
196size_t
198{
199 return __buf()->__size;;
200}
201
202template<typename ELEMENT>
203inline
204bool
206{
207 return size() == 0;
208}
209
210template<typename ELEMENT>
211inline
212ELEMENT&
214{
215 __DCL_ASSERT_PARAM(_index < size());
216 return __pData[_index];
217}
218
219template<typename ELEMENT>
220inline
221ELEMENT
222Array<ELEMENT>::operator[] (size_t _index) const
223{
224 __DCL_ASSERT_PARAM(_index < size());
225 return __pData[_index];
226}
227
228template<typename ELEMENT>
229inline
230ELEMENT*
232{
233 return __pData;
234}
235
236template<typename ELEMENT>
237inline
238size_t
239Array<ELEMENT>::size(ConstIterator _first, ConstIterator _last) const
240{
241 __DCL_ASSERT_PARAM(_first <= _last);
242 return _last - _first;
243}
244
245template<typename ELEMENT>
246inline
247size_t
248Array<ELEMENT>::size(Iterator _first, Iterator _last) const
249{
250 __DCL_ASSERT_PARAM(_first <= _last);
251 return _last - _first;
252}
253
255
256// IMPLEMENT_CLASSINFO(Array<ELEMENT>, Object)
257#ifdef __DCL_NO_RTTI
258template<typename ELEMENT>
259const Object::ClassInfo Array<ELEMENT>::m_classInfo =
260{
261 __DCL_NAMESPACE_STRING __S(Array),
262 sizeof(class Array),
263 &Object::m_classInfo
264};
265
266template<typename ELEMENT>
267const Object::ClassInfo* Array<ELEMENT>::classInfo() const
268{
269 return CLASSINFO(class_name);
270}
271#else
272template<typename ELEMENT>
273const std::type_info& Array<ELEMENT>::typeInfo() const
274{
275 return typeid(Array);
276}
277#endif
278
279template<typename ELEMENT>
280inline
281void
282Array<ELEMENT>::constructElements(ELEMENT* _pElements, size_t _size)
283{
284 for(; _size; _size--, _pElements++)
285 {
286#if __DCL_HAVE_ALLOC_DEBUG
287#undef new
288#endif
289
290 new((void*)_pElements) ELEMENT;
291
292#if __DCL_HAVE_ALLOC_DEBUG
293#define new __DCL_DEBUG_NEW
294#endif
295 }
296}
297
298template<typename ELEMENT>
299inline
300void
301Array<ELEMENT>::destructElements(ELEMENT* _pElements, size_t _size)
302{
303 for( ; _size; _size--, _pElements++)
304 {
305 _pElements->~ELEMENT();
306 }
307}
308
309template<typename ELEMENT>
311{
312 __pData = NULL;
313 resize(_size);
314}
315
316template<typename ELEMENT>
318{
319 __pData = NULL;
320 *this = _src;
321}
322
323template<typename ELEMENT>
325{
326 clear();
327 free(__buf());
328}
329
330template<typename ELEMENT>
331const
334{
335 if (&_src != this)
336 {
337 resize(_src.size());
338 for(size_t i = 0; i < _src.size(); i++)
339 __pData[i] = _src.__pData[i];
340 }
341 return *this;
342}
343
344template<typename ELEMENT>
345void
347{
348 if (size() > 0)
349 {
351 __size() = 0;
352 }
353}
354
355template<typename ELEMENT>
356void
358{
359 if (size() <__maxSize())
360 {
361 Buffer* buf = (Buffer*) realloc(__buf(), sizeof(Buffer) + size() * sizeof(ELEMENT));
362 // FIXME!
363 // if (buf == NULL)
364 // throw new BadAllocException();
366
367 buf->__maxSize = buf->__size;
368 __pData = buf->data();
369 }
370}
371
372template<typename ELEMENT>
373void
375{
376 if (__pData != NULL)
377 {
378 if (size() == _size)
379 return;
380
381 if (size() > _size)
382 {
383 destructElements(__pData + _size, size() - _size);
384 __size() = _size;
385 return;
386 }
387 }
388
389 if (__pData == NULL || __maxSize() < _size)
390 {
391 Buffer* buf = NULL;
392 if (__pData == NULL)
393 {
394 buf = (Buffer*) malloc(sizeof(Buffer) + _size * sizeof(ELEMENT));
395 // FIXME!
396 // if (buf == NULL)
397 // throw new BadAllocException();
399
400 buf->__size = 0;
401 }
402 else
403 {
404 buf = (Buffer*) realloc(__buf(), sizeof(Buffer) + _size * sizeof(ELEMENT));
405 // FIXME!
406 // if (buf == NULL)
407 // throw new BadAllocException();
409 }
410
411 buf->__maxSize = _size;
412 __pData = buf->data();
413 memset((void*)(__pData + buf->__size), 0, (_size - buf->__size) * sizeof(ELEMENT));
414 }
415
416 constructElements(__pData + size(), _size - size());
417 __size() = _size;
418}
419
420template<typename ELEMENT>
421typename Array<ELEMENT>::Iterator
422Array<ELEMENT>::insert(Iterator _pos, const ELEMENT& _element)
423{
424 __DCL_ASSERT_PARAM(begin() <= _pos);
425 __DCL_ASSERT_PARAM(_pos <= end());
426
427 size_t index = _pos - __pData;
428 insert(index, _element);
429 return __pData + index + 1;
430}
431
432template<typename ELEMENT>
434Array<ELEMENT>::insert(size_t _index, const ELEMENT& _element)
435{
437 __DCL_ASSERT_PARAM(_index <= size());
438
439 size_t newSize = size() + 1;
440 if (__maxSize() < newSize)
441 {
442 Buffer* buf = (Buffer*) realloc(__buf(), sizeof(Buffer) + newSize * sizeof(ELEMENT));
443 // FIXME!
444 // if (buf == NULL)
445 // throw new BadAllocException();
447
448 buf->__maxSize = newSize;
449 __pData = buf->data();
450
451 if (_index < buf->__size)
452 memmove((void*)(__pData + _index + 1), (const void*)(__pData + _index),
453 sizeof(ELEMENT) * (buf->__size - _index));
454 }
455
456 constructElements(__pData + _index, 1);
457 __size() = newSize;
458 __pData[_index] = (ELEMENT) _element;
459 return *this;
460}
461
462template<typename ELEMENT>
465{
466 if (_src.size() > 0)
467 {
468 size_t newSize = size() + _src.size();
469 if (__maxSize() < newSize)
470 {
471 Buffer* buf = (Buffer*) realloc(__buf(), sizeof(Buffer) + newSize * sizeof(ELEMENT));
472 // FIXME!
473 // if (buf == NULL)
474 // throw new BadAllocException();
476
477 __pData = buf->data();
478 }
479
480 constructElements(__pData + size(), _src.size());
481 for(size_t i = 0; i < _src.size(); i++)
482 __pData[size() + i] = _src.__pData[i];
483
484 __maxSize() = __size() = newSize;
485 }
486 return *this;
487}
488
489template<typename ELEMENT>
490typename Array<ELEMENT>::Iterator
491Array<ELEMENT>::find(const ELEMENT& _element)
492{
493 Iterator it = begin();
494 for ( ; it != end(); it++)
495 {
496 if (*it == _element)
497 break;
498 }
499 return it;
500}
501
502template<typename ELEMENT>
504Array<ELEMENT>::erase(size_t _index, size_t _size)
505{
506 __DCL_ASSERT_PARAM(_index + _size <= size());
507 if (_size > 0)
508 {
509 destructElements(__pData + _index, _size);
510 if (_index + _size < size())
511 memmove((void*)(__pData + _index), (const void*)(__pData + _index + _size),
512 (size() - (_index + _size)) * sizeof(ELEMENT));
513 __size() -= _size;
514 }
515 return *this;
516}
517
518
519#if __DCL_HAVE_THIS_FILE__
520 #undef __THIS_FILE__
521 #define __THIS_FILE__ __T(__FILE__)
522#endif
523
524#if __DCL_HAVE_ALLOC_DEBUG
525 #undef __DCL_ALLOC_LEVEL
526 #define __DCL_ALLOC_LEVEL __DCL_ALLOC_USER
527 #undef new
528 #define new __DCL_DEBUG_NEW
529#endif
530
531__DCL_END_NAMESPACE
532
533#endif // __DCL_ARRAY_T_H__
#define constructElements(_pElements, _size)
#define destructElements(_pElements, _size)
#define NULL
Definition Config.h:340
wchar_t char_t
Definition Config.h:275
#define __DCL_ASSERT_PARAM(expr)
Definition Object.h:384
#define DECLARE_CLASSINFO(class_name)
Definition Object.h:210
#define CLASSINFO(class_name)
Definition Object.h:209
#define __DCL_ASSERT(expr)
Definition Object.h:371
#define __T(str)
Definition Object.h:44
#define __DCL_ASSERT_HANDLE(expr)
Definition Object.h:383
ByteBuffer * buf
void CharsetConvertException *__fields clear()
Definition ArrayT.h:42
virtual ~Array()
Definition ArrayT.h:324
Iterator erase(Iterator _pos)
Definition ArrayT.h:152
ConstIterator end() const
Definition ArrayT.h:120
Array(size_t _size=0)
Definition ArrayT.h:310
Iterator find(const ELEMENT &_element)
Definition ArrayT.h:491
void destructElements(ELEMENT *_pElements, size_t _size)
Definition ArrayT.h:301
void clear()
Definition ArrayT.h:346
Iterator insert(Iterator _pos, const ELEMENT &_element)
Definition ArrayT.h:422
ELEMENT * __pData
Definition ArrayT.h:90
ELEMENT * data() const
Definition ArrayT.h:231
void constructElements(ELEMENT *_pElements, size_t _size)
Definition ArrayT.h:282
void shrink()
Definition ArrayT.h:357
ELEMENT & operator[](size_t _index)
Definition ArrayT.h:213
size_t size() const
Definition ArrayT.h:197
Array< ELEMENT > & add(const ELEMENT &_element)
Definition ArrayT.h:144
void resize(size_t _size)
Definition ArrayT.h:374
const Array< ELEMENT > & operator=(const Array< ELEMENT > &_src)
Definition ArrayT.h:333
Buffer * __buf() const
Definition ArrayT.h:99
size_t & __maxSize() const
Definition ArrayT.h:101
bool isEmpty() const
Definition ArrayT.h:205
ConstIterator begin() const
Definition ArrayT.h:112
size_t & __size() const
Definition ArrayT.h:100
size_t index(Iterator _pos) const
Definition ArrayT.h:188
Object()
Definition Object.cpp:183
virtual const std::type_info & typeInfo() const
Definition Object.cpp:126
ELEMENT * data()
Definition ArrayT.h:96
size_t __maxSize
Definition ArrayT.h:95
size_t __size
Definition ArrayT.h:94