author | peter1138 |
Sun, 03 Aug 2008 17:35:08 +0000 | |
changeset 9834 | 7474149531c1 |
parent 9677 | 4767b9937c04 |
child 10416 | b35c0a4c73c5 |
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 |
/** |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
48 |
* 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
|
49 |
*/ |
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
|
50 |
FORCEINLINE void Compact() |
9348
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
51 |
{ |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
52 |
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
|
53 |
if (capacity >= this->capacity) return; |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
54 |
|
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
55 |
this->capacity = capacity; |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
56 |
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
|
57 |
} |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
58 |
|
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
59 |
/** |
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
|
60 |
* 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
|
61 |
*/ |
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
|
62 |
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
|
63 |
{ |
9335 | 64 |
if (this->items == this->capacity) { |
65 |
this->capacity += S; |
|
66 |
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
|
67 |
} |
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
|
68 |
|
9335 | 69 |
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
|
70 |
} |
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 |
|
9348
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
72 |
/** |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
73 |
* 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
|
74 |
*/ |
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
|
75 |
FORCEINLINE uint Length() const |
9348
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
76 |
{ |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
77 |
return this->items; |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
78 |
} |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
79 |
|
9554
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
80 |
/** |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
81 |
* Get the pointer to the first item (const) |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
82 |
* |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
83 |
* @return the pointer to the first item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
84 |
*/ |
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
|
85 |
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
|
86 |
{ |
9335 | 87 |
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
|
88 |
} |
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
|
89 |
|
9554
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
90 |
/** |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
91 |
* Get the pointer to the first item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
92 |
* |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
93 |
* @return the pointer to the first item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
94 |
*/ |
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
|
95 |
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
|
96 |
{ |
9335 | 97 |
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
|
98 |
} |
618a0b1a5061
(svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents:
8949
diff
changeset
|
99 |
|
9554
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
100 |
/** |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
101 |
* Get the pointer behind the last valid item (const) |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
102 |
* |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
103 |
* @return the pointer behind the last valid item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
104 |
*/ |
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
|
105 |
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
|
106 |
{ |
9335 | 107 |
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
|
108 |
} |
8951
618a0b1a5061
(svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents:
8949
diff
changeset
|
109 |
|
9554
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
110 |
/** |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
111 |
* Get the pointer behind the last valid item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
112 |
* |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
113 |
* @return the pointer behind the last valid item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
114 |
*/ |
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
|
115 |
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
|
116 |
{ |
9335 | 117 |
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
|
118 |
} |
8952
be6bfd75e554
(svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents:
8951
diff
changeset
|
119 |
|
9554
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
120 |
/** |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
121 |
* Get the pointer to item "number" (const) |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
122 |
* |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
123 |
* @param index the position of the item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
124 |
* @return the pointer to the 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 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
|
127 |
{ |
9335 | 128 |
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
|
129 |
} |
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" |
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 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
|
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 |
} |
9348
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
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 item "number" (const) |
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 positon of the item |
d677e63894db
(svn r13574) -Doc: Document the small vector template class
skidd13
parents:
9427
diff
changeset
|
146 |
* @return 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 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
|
149 |
{ |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
150 |
return this->data[index]; |
dc680f675138
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents:
9335
diff
changeset
|
151 |
} |
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" |
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 T &operator[](uint index) |
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 |
} |
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
|
163 |
}; |
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
|
164 |
|
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
|
165 |
|
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
|
166 |
/** |
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
|
167 |
* 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
|
168 |
* |
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
|
169 |
* @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
|
170 |
* 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
|
171 |
* 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
|
172 |
* |
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
|
173 |
* @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
|
174 |
* @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
|
175 |
*/ |
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 |
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
|
177 |
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
|
178 |
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
|
179 |
~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
|
180 |
{ |
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 |
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
|
182 |
} |
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 |
/** |
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 |
* 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
|
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 |
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
|
188 |
{ |
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 |
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
|
190 |
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
|
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 |
|
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 |
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
|
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 |
|
9555
68e7c84b2d19
(svn r13575) -Codechange: Move small vector to core since it fits better in there
skidd13
parents:
9554
diff
changeset
|
197 |
#endif /* SMALLVEC_TYPE_HPP */ |