author | KUDr |
Sun, 31 Dec 2006 21:13:40 +0000 | |
branch | custombridgeheads |
changeset 5616 | 0570ae953222 |
parent 5129 | 15e48dea98a5 |
permissions | -rw-r--r-- |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
1 |
/* $Id$ */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
2 |
|
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
3 |
#ifndef FIXEDSIZEARRAY_HPP |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
4 |
#define FIXEDSIZEARRAY_HPP |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
5 |
|
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
6 |
|
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
7 |
/** fixed size array |
4549
60410aa1aa88
(svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents:
3900
diff
changeset
|
8 |
* Upon construction it preallocates fixed size block of memory |
60410aa1aa88
(svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents:
3900
diff
changeset
|
9 |
* for all items, but doesn't construct them. Item's construction |
60410aa1aa88
(svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents:
3900
diff
changeset
|
10 |
* is delayed. */ |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
11 |
template <class Titem_, int Tcapacity_> |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
12 |
struct CFixedSizeArrayT { |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
13 |
/** the only member of fixed size array is pointer to the block |
4549
60410aa1aa88
(svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents:
3900
diff
changeset
|
14 |
* of C array of items. Header can be found on the offset -sizeof(CHdr). */ |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
15 |
Titem_ *m_items; |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
16 |
|
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
17 |
/** header for fixed size array */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
18 |
struct CHdr |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
19 |
{ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
20 |
int m_num_items; ///< number of items in the array |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
21 |
int m_ref_cnt; ///< block reference counter (used by copy constructor and by destructor) |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
22 |
}; |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
23 |
|
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
24 |
// make types and constants visible from outside |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
25 |
typedef Titem_ Titem; // type of array item |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
26 |
|
5082
f5c258d60562
(svn r7146) -CodeChange: ST_CONST macro removed as it is no longer needed (Tron)
KUDr
parents:
5081
diff
changeset
|
27 |
static const int Tcapacity = Tcapacity_; // the array capacity (maximum size) |
f5c258d60562
(svn r7146) -CodeChange: ST_CONST macro removed as it is no longer needed (Tron)
KUDr
parents:
5081
diff
changeset
|
28 |
static const int TitemSize = sizeof(Titem_); // size of item |
f5c258d60562
(svn r7146) -CodeChange: ST_CONST macro removed as it is no longer needed (Tron)
KUDr
parents:
5081
diff
changeset
|
29 |
static const int ThdrSize = sizeof(CHdr); // size of header |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
30 |
|
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
31 |
/** Default constructor. Preallocate space for items and header, then initialize header. */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
32 |
CFixedSizeArrayT() |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
33 |
{ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
34 |
// allocate block for header + items (don't construct items) |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
35 |
m_items = (Titem*)(((int8*)malloc(ThdrSize + Tcapacity * sizeof(Titem))) + ThdrSize); |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
36 |
SizeRef() = 0; // initial number of items |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
37 |
RefCnt() = 1; // initial reference counter |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
38 |
} |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
39 |
|
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
40 |
/** Copy constructor. Preallocate space for items and header, then initialize header. */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
41 |
CFixedSizeArrayT(const CFixedSizeArrayT<Titem_, Tcapacity_>& src) |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
42 |
{ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
43 |
// share block (header + items) with the source array |
4988
cd54974faf2e
(svn r6991) Remove an unnecessary const_cast<> and incorrect comment (There is a difference between const FOO* and FOO* const)
tron
parents:
4549
diff
changeset
|
44 |
m_items = src.m_items; |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
45 |
RefCnt()++; // now we share block with the source |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
46 |
} |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
47 |
|
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
48 |
/** destroy remaining items and free the memory block */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
49 |
~CFixedSizeArrayT() |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
50 |
{ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
51 |
// release one reference to the shared block |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
52 |
if ((--RefCnt()) > 0) return; // and return if there is still some owner |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
53 |
|
5129
15e48dea98a5
(svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents:
5082
diff
changeset
|
54 |
Clear(); |
15e48dea98a5
(svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents:
5082
diff
changeset
|
55 |
// free the memory block occupied by items |
15e48dea98a5
(svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents:
5082
diff
changeset
|
56 |
free(((int8*)m_items) - ThdrSize); |
15e48dea98a5
(svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents:
5082
diff
changeset
|
57 |
m_items = NULL; |
15e48dea98a5
(svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents:
5082
diff
changeset
|
58 |
} |
15e48dea98a5
(svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents:
5082
diff
changeset
|
59 |
|
15e48dea98a5
(svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents:
5082
diff
changeset
|
60 |
/** Clear (destroy) all items */ |
15e48dea98a5
(svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents:
5082
diff
changeset
|
61 |
FORCEINLINE void Clear() |
15e48dea98a5
(svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents:
5082
diff
changeset
|
62 |
{ |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
63 |
// walk through all allocated items backward and destroy them |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
64 |
for (Titem* pItem = &m_items[Size() - 1]; pItem >= m_items; pItem--) { |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
65 |
pItem->~Titem_(); |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
66 |
} |
5129
15e48dea98a5
(svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents:
5082
diff
changeset
|
67 |
// number of items become zero |
15e48dea98a5
(svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
KUDr
parents:
5082
diff
changeset
|
68 |
SizeRef() = 0; |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
69 |
} |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
70 |
|
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
71 |
protected: |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
72 |
/** return reference to the array header (non-const) */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
73 |
FORCEINLINE CHdr& Hdr() { return *(CHdr*)(((int8*)m_items) - ThdrSize); } |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
74 |
/** return reference to the array header (const) */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
75 |
FORCEINLINE const CHdr& Hdr() const { return *(CHdr*)(((int8*)m_items) - ThdrSize); } |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
76 |
/** return reference to the block reference counter */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
77 |
FORCEINLINE int& RefCnt() { return Hdr().m_ref_cnt; } |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
78 |
/** return reference to number of used items */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
79 |
FORCEINLINE int& SizeRef() { return Hdr().m_num_items; } |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
80 |
public: |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
81 |
/** return number of used items */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
82 |
FORCEINLINE int Size() const { return Hdr().m_num_items; } |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
83 |
/** return true if array is full */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
84 |
FORCEINLINE bool IsFull() const { return Size() >= Tcapacity; }; |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
85 |
/** return true if array is empty */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
86 |
FORCEINLINE bool IsEmpty() const { return Size() <= 0; }; |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
87 |
/** index validation */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
88 |
FORCEINLINE void CheckIdx(int idx) const { assert(idx >= 0); assert(idx < Size()); } |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
89 |
/** add (allocate), but don't construct item */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
90 |
FORCEINLINE Titem& AddNC() { assert(!IsFull()); return m_items[SizeRef()++]; } |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
91 |
/** add and construct item using default constructor */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
92 |
FORCEINLINE Titem& Add() { Titem& item = AddNC(); new(&item)Titem; return item; } |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
93 |
/** return item by index (non-const version) */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
94 |
FORCEINLINE Titem& operator [] (int idx) { CheckIdx(idx); return m_items[idx]; } |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
95 |
/** return item by index (const version) */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
96 |
FORCEINLINE const Titem& operator [] (int idx) const { CheckIdx(idx); return m_items[idx]; } |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
97 |
}; |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
98 |
|
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff
changeset
|
99 |
#endif /* FIXEDSIZEARRAY_HPP */ |