author | smatz |
Sat, 13 Dec 2008 15:59:25 +0000 | |
changeset 10416 | b35c0a4c73c5 |
parent 9677 | 4767b9937c04 |
permissions | -rw-r--r-- |
8949
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
1 |
/* $Id$ */ |
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
2 |
|
9555
68e7c84b2d19
(svn r13575) -Codechange: Move small vector to core since it fits better in there
skidd13
parents:
9554
diff
changeset
|
3 |
/** @file smallvec_type.hpp Simple vector class that allows allocating an item without the need to copy this->data needlessly. */ |
8949
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
4 |
|
9555
68e7c84b2d19
(svn r13575) -Codechange: Move small vector to core since it fits better in there
skidd13
parents:
9554
diff
changeset
|
5 |
#ifndef SMALLVEC_TYPE_HPP |
68e7c84b2d19
(svn r13575) -Codechange: Move small vector to core since it fits better in there
skidd13
parents:
9554
diff
changeset
|
6 |
#define SMALLVEC_TYPE_HPP |
8949
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
7 |
|
9555
68e7c84b2d19
(svn r13575) -Codechange: Move small vector to core since it fits better in there
skidd13
parents:
9554
diff
changeset
|
8 |
#include "alloc_func.hpp" |
68e7c84b2d19
(svn r13575) -Codechange: Move small vector to core since it fits better in there
skidd13
parents:
9554
diff
changeset
|
9 |
#include "math_func.hpp" |
9427
af652de004a0
(svn r13342) -Fix: smallvec.h/sortlist_type.h didn't include everything they needed.
rubidium
parents:
9348
diff
changeset
|
10 |
|
9554
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
11 |
/** |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
12 |
* Simple vector template class. |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
13 |
* |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
14 |
* @note There are no asserts in the class so you have |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
15 |
* to care about that you grab an item which is |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
16 |
* inside the list. |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
17 |
* |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
18 |
* @param T The type of the items stored |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
19 |
* @param S The steps of allocation |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
20 |
*/ |
9335 | 21 |
template <typename T, uint S> |
9555
68e7c84b2d19
(svn r13575) -Codechange: Move small vector to core since it fits better in there
skidd13
parents:
9554
diff
changeset
|
22 |
class SmallVector { |
68e7c84b2d19
(svn r13575) -Codechange: Move small vector to core since it fits better in there
skidd13
parents:
9554
diff
changeset
|
23 |
protected: |
9554
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
24 |
T *data; ///< The pointer to the first item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
25 |
uint items; ///< The number of items stored |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
26 |
uint capacity; ///< The avalible space for storing items |
8949
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
27 |
|
9555
68e7c84b2d19
(svn r13575) -Codechange: Move small vector to core since it fits better in there
skidd13
parents:
9554
diff
changeset
|
28 |
public: |
8949
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
29 |
SmallVector() : data(NULL), items(0), capacity(0) { } |
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
30 |
|
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
31 |
~SmallVector() |
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
32 |
{ |
9335 | 33 |
free(this->data); |
8949
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
34 |
} |
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
35 |
|
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
36 |
/** |
9348
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
37 |
* Remove all items from the list. |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
38 |
*/ |
9575
58d55b1a70c9
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9555
diff
changeset
|
39 |
FORCEINLINE void Clear() |
9348
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
40 |
{ |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
41 |
/* In fact we just reset the item counter avoiding the need to |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
42 |
* probably reallocate the same amount of memory the list was |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
43 |
* previously using. */ |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
44 |
this->items = 0; |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
45 |
} |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
46 |
|
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
47 |
/** |
10416
b35c0a4c73c5
(svn r14669) -Codechange: use SmallVector instead of std::list at one place
smatz
parents:
9677
diff
changeset
|
48 |
* Remove all items from the list and free allocated memory. |
b35c0a4c73c5
(svn r14669) -Codechange: use SmallVector instead of std::list at one place
smatz
parents:
9677
diff
changeset
|
49 |
*/ |
b35c0a4c73c5
(svn r14669) -Codechange: use SmallVector instead of std::list at one place
smatz
parents:
9677
diff
changeset
|
50 |
void Reset() |
b35c0a4c73c5
(svn r14669) -Codechange: use SmallVector instead of std::list at one place
smatz
parents:
9677
diff
changeset
|
51 |
{ |
b35c0a4c73c5
(svn r14669) -Codechange: use SmallVector instead of std::list at one place
smatz
parents:
9677
diff
changeset
|
52 |
this->items = 0; |
b35c0a4c73c5
(svn r14669) -Codechange: use SmallVector instead of std::list at one place
smatz
parents:
9677
diff
changeset
|
53 |
this->capacity = 0; |
b35c0a4c73c5
(svn r14669) -Codechange: use SmallVector instead of std::list at one place
smatz
parents:
9677
diff
changeset
|
54 |
free(data); |
b35c0a4c73c5
(svn r14669) -Codechange: use SmallVector instead of std::list at one place
smatz
parents:
9677
diff
changeset
|
55 |
data = NULL; |
b35c0a4c73c5
(svn r14669) -Codechange: use SmallVector instead of std::list at one place
smatz
parents:
9677
diff
changeset
|
56 |
} |
b35c0a4c73c5
(svn r14669) -Codechange: use SmallVector instead of std::list at one place
smatz
parents:
9677
diff
changeset
|
57 |
|
b35c0a4c73c5
(svn r14669) -Codechange: use SmallVector instead of std::list at one place
smatz
parents:
9677
diff
changeset
|
58 |
/** |
9348
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
59 |
* Compact the list down to the smallest block size boundary. |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
60 |
*/ |
9575
58d55b1a70c9
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9555
diff
changeset
|
61 |
FORCEINLINE void Compact() |
9348
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
62 |
{ |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
63 |
uint capacity = Align(this->items, S); |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
64 |
if (capacity >= this->capacity) return; |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
65 |
|
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
66 |
this->capacity = capacity; |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
67 |
this->data = ReallocT(this->data, this->capacity); |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
68 |
} |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
69 |
|
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
70 |
/** |
8949
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
71 |
* Append an item and return it. |
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
72 |
*/ |
9575
58d55b1a70c9
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9555
diff
changeset
|
73 |
FORCEINLINE T *Append() |
8949
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
74 |
{ |
9335 | 75 |
if (this->items == this->capacity) { |
76 |
this->capacity += S; |
|
77 |
this->data = ReallocT(this->data, this->capacity); |
|
8949
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
78 |
} |
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
79 |
|
9335 | 80 |
return &this->data[this->items++]; |
8949
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
81 |
} |
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
82 |
|
9348
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
83 |
/** |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
84 |
* Get the number of items in the list. |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
85 |
*/ |
9575
58d55b1a70c9
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9555
diff
changeset
|
86 |
FORCEINLINE uint Length() const |
9348
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
87 |
{ |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
88 |
return this->items; |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
89 |
} |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
90 |
|
9554
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
91 |
/** |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
92 |
* Get the pointer to the first item (const) |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
93 |
* |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
94 |
* @return the pointer to the first item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
95 |
*/ |
9575
58d55b1a70c9
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9555
diff
changeset
|
96 |
FORCEINLINE const T *Begin() const |
8949
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
97 |
{ |
9335 | 98 |
return this->data; |
8949
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
99 |
} |
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
100 |
|
9554
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
101 |
/** |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
102 |
* Get the pointer to the first item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
103 |
* |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
104 |
* @return the pointer to the first item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
105 |
*/ |
9575
58d55b1a70c9
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9555
diff
changeset
|
106 |
FORCEINLINE T *Begin() |
8951
618a0b1a5061
(svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents:
8949
diff
changeset
|
107 |
{ |
9335 | 108 |
return this->data; |
8951
618a0b1a5061
(svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents:
8949
diff
changeset
|
109 |
} |
618a0b1a5061
(svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents:
8949
diff
changeset
|
110 |
|
9554
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
111 |
/** |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
112 |
* Get the pointer behind the last valid item (const) |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
113 |
* |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
114 |
* @return the pointer behind the last valid item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
115 |
*/ |
9575
58d55b1a70c9
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9555
diff
changeset
|
116 |
FORCEINLINE const T *End() const |
8949
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
117 |
{ |
9335 | 118 |
return &this->data[this->items]; |
8949
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
119 |
} |
8951
618a0b1a5061
(svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents:
8949
diff
changeset
|
120 |
|
9554
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
121 |
/** |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
122 |
* Get the pointer behind the last valid item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
123 |
* |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
124 |
* @return the pointer behind the last valid item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
125 |
*/ |
9575
58d55b1a70c9
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9555
diff
changeset
|
126 |
FORCEINLINE T *End() |
8951
618a0b1a5061
(svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents:
8949
diff
changeset
|
127 |
{ |
9335 | 128 |
return &this->data[this->items]; |
8951
618a0b1a5061
(svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents:
8949
diff
changeset
|
129 |
} |
8952
be6bfd75e554
(svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents:
8951
diff
changeset
|
130 |
|
9554
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
131 |
/** |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
132 |
* Get the pointer to item "number" (const) |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
133 |
* |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
134 |
* @param index the position of the item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
135 |
* @return the pointer to the item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
136 |
*/ |
9575
58d55b1a70c9
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9555
diff
changeset
|
137 |
FORCEINLINE const T *Get(uint index) const |
8952
be6bfd75e554
(svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents:
8951
diff
changeset
|
138 |
{ |
9335 | 139 |
return &this->data[index]; |
8952
be6bfd75e554
(svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents:
8951
diff
changeset
|
140 |
} |
be6bfd75e554
(svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents:
8951
diff
changeset
|
141 |
|
9554
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
142 |
/** |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
143 |
* Get the pointer to item "number" |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
144 |
* |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
145 |
* @param index the position of the item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
146 |
* @return the pointer to the item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
147 |
*/ |
9575
58d55b1a70c9
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9555
diff
changeset
|
148 |
FORCEINLINE T *Get(uint index) |
8952
be6bfd75e554
(svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents:
8951
diff
changeset
|
149 |
{ |
9335 | 150 |
return &this->data[index]; |
8952
be6bfd75e554
(svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents:
8951
diff
changeset
|
151 |
} |
9348
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
152 |
|
9554
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
153 |
/** |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
154 |
* Get item "number" (const) |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
155 |
* |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
156 |
* @param index the positon of the item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
157 |
* @return the item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
158 |
*/ |
9575
58d55b1a70c9
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9555
diff
changeset
|
159 |
FORCEINLINE const T &operator[](uint index) const |
9348
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
160 |
{ |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
161 |
return this->data[index]; |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
162 |
} |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
163 |
|
9554
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
164 |
/** |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
165 |
* Get item "number" |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
166 |
* |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
167 |
* @param index the positon of the item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
168 |
* @return the item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
169 |
*/ |
9575
58d55b1a70c9
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents:
9555
diff
changeset
|
170 |
FORCEINLINE T &operator[](uint index) |
9348
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
171 |
{ |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
172 |
return this->data[index]; |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
173 |
} |
8949
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
174 |
}; |
4c9fbf5ec359
(svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff
changeset
|
175 |
|
9677
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
176 |
|
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
177 |
/** |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
178 |
* Simple vector template class, with automatic free. |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
179 |
* |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
180 |
* @note There are no asserts in the class so you have |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
181 |
* to care about that you grab an item which is |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
182 |
* inside the list. |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
183 |
* |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
184 |
* @param T The type of the items stored, must be a pointer |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
185 |
* @param S The steps of allocation |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
186 |
*/ |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
187 |
template <typename T, uint S> |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
188 |
class AutoFreeSmallVector : public SmallVector<T, S> { |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
189 |
public: |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
190 |
~AutoFreeSmallVector() |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
191 |
{ |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
192 |
this->Clear(); |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
193 |
} |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
194 |
|
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
195 |
/** |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
196 |
* Remove all items from the list. |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
197 |
*/ |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
198 |
FORCEINLINE void Clear() |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
199 |
{ |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
200 |
for (uint i = 0; i < this->items; i++) { |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
201 |
free(this->data[i]); |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
202 |
} |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
203 |
|
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
204 |
this->items = 0; |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
205 |
} |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
206 |
}; |
4767b9937c04
(svn r13781) -Feature: NewGRF presets, selected by a drop down list in the NewGRF window. Presets are saved in the config file.
peter1138
parents:
9575
diff
changeset
|
207 |
|
9555
68e7c84b2d19
(svn r13575) -Codechange: Move small vector to core since it fits better in there
skidd13
parents:
9554
diff
changeset
|
208 |
#endif /* SMALLVEC_TYPE_HPP */ |