aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llcommon/lldarray.h
diff options
context:
space:
mode:
authorJacek Antonelli2008-08-15 23:44:46 -0500
committerJacek Antonelli2008-08-15 23:44:46 -0500
commit38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch)
treeadca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llcommon/lldarray.h
parentREADME.txt (diff)
downloadmeta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/llcommon/lldarray.h')
-rw-r--r--linden/indra/llcommon/lldarray.h211
1 files changed, 211 insertions, 0 deletions
diff --git a/linden/indra/llcommon/lldarray.h b/linden/indra/llcommon/lldarray.h
new file mode 100644
index 0000000..24e8543
--- /dev/null
+++ b/linden/indra/llcommon/lldarray.h
@@ -0,0 +1,211 @@
1/**
2 * @file lldarray.h
3 * @brief Wrapped std::vector for backward compatibility.
4 *
5 * Copyright (c) 2001-2007, Linden Research, Inc.
6 *
7 * The source code in this file ("Source Code") is provided by Linden Lab
8 * to you under the terms of the GNU General Public License, version 2.0
9 * ("GPL"), unless you have obtained a separate licensing agreement
10 * ("Other License"), formally executed by you and Linden Lab. Terms of
11 * the GPL can be found in doc/GPL-license.txt in this distribution, or
12 * online at http://secondlife.com/developers/opensource/gplv2
13 *
14 * There are special exceptions to the terms and conditions of the GPL as
15 * it is applied to this Source Code. View the full text of the exception
16 * in the file doc/FLOSS-exception.txt in this software distribution, or
17 * online at http://secondlife.com/developers/opensource/flossexception
18 *
19 * By copying, modifying or distributing this software, you acknowledge
20 * that you have read and understood your obligations described above,
21 * and agree to abide by those obligations.
22 *
23 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
24 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
25 * COMPLETENESS OR PERFORMANCE.
26 */
27
28#ifndef LL_LLDARRAY_H
29#define LL_LLDARRAY_H
30
31#include "llmath.h"
32#include "llerror.h"
33
34#include <vector>
35#include <map>
36
37// class LLDynamicArray<>; // = std::vector + reserves <BlockSize> elements
38// class LLDynamicArrayIndexed<>; // = std::vector + std::map if indices, only supports operator[] and begin(),end()
39
40//--------------------------------------------------------
41// LLDynamicArray declaration
42//--------------------------------------------------------
43// NOTE: BlockSize is used to reserve a minimal initial amount
44template <typename Type, int BlockSize = 32>
45class LLDynamicArray : public std::vector<Type>
46{
47public:
48 enum
49 {
50 OKAY = 0,
51 FAIL = -1
52 };
53
54 LLDynamicArray(S32 size=0) : std::vector<Type>(size) { if (size < BlockSize) std::vector<Type>::reserve(BlockSize); }
55
56 void reset() { std::vector<Type>::resize(0); }
57
58 // ACCESSORS
59 const Type& get(S32 index) const { return std::vector<Type>::operator[](index); }
60 Type& get(S32 index) { return std::vector<Type>::operator[](index); }
61 S32 find(const Type &obj) const;
62
63 S32 count() const { return std::vector<Type>::size(); }
64 S32 getLength() const { return std::vector<Type>::size(); }
65 S32 getMax() const { return std::vector<Type>::capacity(); }
66
67 // MANIPULATE
68 S32 put(const Type &obj); // add to end of array, returns index
69// Type* reserve(S32 num); // reserve a block of indices in advance
70 Type* reserve_block(U32 num); // reserve a block of indices in advance
71
72 S32 remove(S32 index); // remove by index, no bounds checking
73 S32 removeObj(const Type &obj); // remove by object
74 S32 removeLast();
75
76 void operator+=(const LLDynamicArray<Type,BlockSize> &other);
77};
78
79//--------------------------------------------------------
80// LLDynamicArray implementation
81//--------------------------------------------------------
82
83template <typename Type,int BlockSize>
84inline S32 LLDynamicArray<Type,BlockSize>::find(const Type &obj) const
85{
86 typename std::vector<Type>::const_iterator iter = std::find(this->begin(), this->end(), obj);
87 if (iter != this->end())
88 {
89 return iter - this->begin();
90 }
91 return FAIL;
92}
93
94
95template <typename Type,int BlockSize>
96inline S32 LLDynamicArray<Type,BlockSize>::remove(S32 i)
97{
98 // This is a fast removal by swapping with the last element
99 S32 sz = this->size();
100 if (i < 0 || i >= sz)
101 {
102 return FAIL;
103 }
104 if (i < sz-1)
105 {
106 this->operator[](i) = this->back();
107 }
108 this->pop_back();
109 return i;
110}
111
112template <typename Type,int BlockSize>
113inline S32 LLDynamicArray<Type,BlockSize>::removeObj(const Type& obj)
114{
115 typename std::vector<Type>::iterator iter = std::find(this->begin(), this->end(), obj);
116 if (iter != this->end())
117 {
118 typename std::vector<Type>::iterator last = this->end();
119 --last;
120 *iter = *last;
121 this->pop_back();
122 return iter - this->begin();
123 }
124 return FAIL;
125}
126
127template <typename Type,int BlockSize>
128inline S32 LLDynamicArray<Type,BlockSize>::removeLast()
129{
130 if (!this->empty())
131 {
132 this->pop_back();
133 return OKAY;
134 }
135 return FAIL;
136}
137
138template <typename Type,int BlockSize>
139inline Type* LLDynamicArray<Type,BlockSize>::reserve_block(U32 num)
140{
141 U32 sz = this->size();
142 this->resize(sz+num);
143 return &(this->operator[](sz));
144}
145
146template <typename Type,int BlockSize>
147inline S32 LLDynamicArray<Type,BlockSize>::put(const Type &obj)
148{
149 this->push_back(obj);
150 return this->size() - 1;
151}
152
153template <typename Type,int BlockSize>
154inline void LLDynamicArray<Type,BlockSize>::operator+=(const LLDynamicArray<Type,BlockSize> &other)
155{
156 insert(this->end(), other.begin(), other.end());
157}
158
159//--------------------------------------------------------
160// LLDynamicArrayIndexed declaration
161//--------------------------------------------------------
162
163template <typename Type, typename Key, int BlockSize = 32>
164class LLDynamicArrayIndexed
165{
166public:
167 typedef typename std::vector<Type>::iterator iterator;
168 typedef typename std::vector<Type>::const_iterator const_iterator;
169 typedef typename std::vector<Type>::reverse_iterator reverse_iterator;
170 typedef typename std::vector<Type>::const_reverse_iterator const_reverse_iterator;
171 typedef typename std::vector<Type>::size_type size_type;
172protected:
173 std::vector<Type> mVector;
174 std::map<Key, U32> mIndexMap;
175
176public:
177 LLDynamicArrayIndexed() { mVector.reserve(BlockSize); }
178
179 iterator begin() { return mVector.begin(); }
180 const_iterator begin() const { return mVector.begin(); }
181 iterator end() { return mVector.end(); }
182 const_iterator end() const { return mVector.end(); }
183
184 reverse_iterator rbegin() { return mVector.rbegin(); }
185 const_reverse_iterator rbegin() const { return mVector.rbegin(); }
186 reverse_iterator rend() { return mVector.rend(); }
187 const_reverse_iterator rend() const { return mVector.rend(); }
188
189 void reset() { mVector.resize(0); mIndexMap.resize(0); }
190 bool empty() const { return mVector.empty(); }
191 size_type size() const { return mVector.empty(); }
192
193 Type& operator[](const Key& k)
194 {
195 typename std::map<Key, U32>::iterator iter = mIndexMap.find(k);
196 if (iter == mIndexMap.end())
197 {
198 U32 n = mVector.size();
199 mIndexMap[k] = n;
200 mVector.resize(n+1);
201 return mVector[n];
202 }
203 else
204 {
205 return mVector[iter->second];
206 }
207 }
208
209};
210
211#endif