1 mike 1.5 //%/////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001 The Open group, BMC Software, Tivoli Systems, IBM
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to
7 // deal in the Software without restriction, including without limitation the
8 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 // sell copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
13 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
14 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 //
21 //==============================================================================
22 mike 1.5 //
23 // Author: Mike Brasher (mbrasher@bmc.com)
24 //
25 // Modified By:
26 //
27 //%/////////////////////////////////////////////////////////////////////////////
28
29 #if defined(PEGASUS_EXPLICIT_INSTANTIATION) || !defined(PEGASUS_ARRAY_T)
30
31 #ifndef PEGASUS_ARRAY_T
32 template<class PEGASUS_ARRAY_T>
33 #else
34 PEGASUS_TEMPLATE_SPECIALIZATION
35 #endif
36 Array<PEGASUS_ARRAY_T>::Array()
37 {
38 _rep = Rep::getNullRep();
39 }
40
41 #ifndef PEGASUS_ARRAY_T
42 template<class PEGASUS_ARRAY_T>
43 mike 1.5 #else
44 PEGASUS_TEMPLATE_SPECIALIZATION
45 #endif
46 Array<PEGASUS_ARRAY_T>::Array(const Array<PEGASUS_ARRAY_T>& x)
47 {
48 Rep::inc(_rep = x._rep);
49 }
50
51 #ifndef PEGASUS_ARRAY_T
52 template<class PEGASUS_ARRAY_T>
53 #else
54 PEGASUS_TEMPLATE_SPECIALIZATION
55 #endif
56 Array<PEGASUS_ARRAY_T>::Array(Uint32 size)
57 {
58 _rep = Rep::create(size);
59 InitializeRaw(_rep->data(), size);
60 }
61
62 #ifndef PEGASUS_ARRAY_T
63 template<class PEGASUS_ARRAY_T>
64 mike 1.5 #else
65 PEGASUS_TEMPLATE_SPECIALIZATION
66 #endif
67 Array<PEGASUS_ARRAY_T>::Array(Uint32 size, const PEGASUS_ARRAY_T& x)
68 {
69 _rep = Rep::create(size);
70
71 PEGASUS_ARRAY_T* data = _rep->data();
72
73 while (size--)
74 new(data++) PEGASUS_ARRAY_T(x);
75 }
76
77 #ifndef PEGASUS_ARRAY_T
78 template<class PEGASUS_ARRAY_T>
79 #else
80 PEGASUS_TEMPLATE_SPECIALIZATION
81 #endif
82 Array<PEGASUS_ARRAY_T>::Array(const PEGASUS_ARRAY_T* items, Uint32 size)
83 {
84 _rep = Rep::create(size);
85 mike 1.5 CopyToRaw(_rep->data(), items, size);
86 }
87
88 #ifndef PEGASUS_ARRAY_T
89 template<class PEGASUS_ARRAY_T>
90 #else
91 PEGASUS_TEMPLATE_SPECIALIZATION
92 #endif
93 Array<PEGASUS_ARRAY_T>::~Array()
94 {
95 Rep::dec(_rep);
96 }
97
98 #ifndef PEGASUS_ARRAY_T
99 template<class PEGASUS_ARRAY_T>
100 #else
101 PEGASUS_TEMPLATE_SPECIALIZATION
102 #endif
103 Array<PEGASUS_ARRAY_T>& Array<PEGASUS_ARRAY_T>::operator=(
104 const Array<PEGASUS_ARRAY_T>& x)
105 {
106 mike 1.5 if (x._rep != _rep)
107 {
108 Rep::dec(_rep);
109 Rep::inc(_rep = x._rep);
110 }
111 return *this;
112 }
113
114 #ifndef PEGASUS_ARRAY_T
115 template<class PEGASUS_ARRAY_T>
116 #else
117 PEGASUS_TEMPLATE_SPECIALIZATION
118 #endif
119 void Array<PEGASUS_ARRAY_T>::clear()
120 {
121 Rep::dec(_rep);
122 _rep = Rep::getNullRep();
123 }
124
125 #ifndef PEGASUS_ARRAY_T
126 template<class PEGASUS_ARRAY_T>
127 mike 1.5 #else
128 PEGASUS_TEMPLATE_SPECIALIZATION
129 #endif
130 void Array<PEGASUS_ARRAY_T>::_reserveAux(Uint32 capacity)
131 {
132 Uint32 size = this->size();
133 Rep* rep = Rep::create(capacity);
134 rep->size = size;
135 CopyToRaw(rep->data(), _rep->data(), size);
136 Rep::dec(_rep);
137 _rep = rep;
138 }
139
140 #ifndef PEGASUS_ARRAY_T
141 template<class PEGASUS_ARRAY_T>
142 #else
143 PEGASUS_TEMPLATE_SPECIALIZATION
144 #endif
145 void Array<PEGASUS_ARRAY_T>::_copyOnWriteAux()
146 {
147 if (_rep->ref != 1)
148 mike 1.5 {
149 Rep* rep = _rep->clone();
150 Rep::dec(_rep);
151 _rep = rep;
152 }
153 }
154
155 #ifndef PEGASUS_ARRAY_T
156 template<class PEGASUS_ARRAY_T>
157 #else
158 PEGASUS_TEMPLATE_SPECIALIZATION
159 #endif
160 void Array<PEGASUS_ARRAY_T>::grow(Uint32 size, const PEGASUS_ARRAY_T& x)
161 {
162 Uint32 oldSize = _rep->size;
163 reserve(oldSize + size);
164 _copyOnWrite();
165
166 PEGASUS_ARRAY_T* p = _rep->data() + oldSize;
167 Uint32 n = size;
168
169 mike 1.5 while (n--)
170 new(p++) PEGASUS_ARRAY_T(x);
171
172 _rep->size += size;
173 }
174
175 #ifndef PEGASUS_ARRAY_T
176 template<class PEGASUS_ARRAY_T>
177 #else
178 PEGASUS_TEMPLATE_SPECIALIZATION
179 #endif
180 void Array<PEGASUS_ARRAY_T>::swap(Array<PEGASUS_ARRAY_T>& x)
181 {
182 Rep* tmp = _rep;
183 _rep = x._rep;
184 x._rep = tmp;
185 }
186
187 #ifndef PEGASUS_ARRAY_T
188 template<class PEGASUS_ARRAY_T>
189 #else
190 mike 1.5 PEGASUS_TEMPLATE_SPECIALIZATION
191 #endif
192 void Array<PEGASUS_ARRAY_T>::append(const PEGASUS_ARRAY_T& x)
193 {
194 reserve(size() + 1);
195 _copyOnWrite();
196 new (_data() + size()) PEGASUS_ARRAY_T(x);
197 _rep->size++;
198 }
199
200 #ifndef PEGASUS_ARRAY_T
201 template<class PEGASUS_ARRAY_T>
202 #else
203 PEGASUS_TEMPLATE_SPECIALIZATION
204 #endif
205 void Array<PEGASUS_ARRAY_T>::append(const PEGASUS_ARRAY_T* x, Uint32 size)
206 {
207 reserve(this->size() + size);
208 _copyOnWrite();
209 CopyToRaw(_data() + this->size(), x, size);
210 _rep->size += size;
211 mike 1.5 }
212
213 #ifndef PEGASUS_ARRAY_T
214 template<class PEGASUS_ARRAY_T>
215 #else
216 PEGASUS_TEMPLATE_SPECIALIZATION
217 #endif
218 void Array<PEGASUS_ARRAY_T>::prepend(const PEGASUS_ARRAY_T& x)
219 {
220 reserve(size() + 1);
221 _copyOnWrite();
222 memmove(_data() + 1, _data(), sizeof(PEGASUS_ARRAY_T) * size());
223 new(_data()) PEGASUS_ARRAY_T(x);
224 _rep->size++;
225 }
226
227 #ifndef PEGASUS_ARRAY_T
228 template<class PEGASUS_ARRAY_T>
229 #else
230 PEGASUS_TEMPLATE_SPECIALIZATION
231 #endif
232 mike 1.5 void Array<PEGASUS_ARRAY_T>::prepend(const PEGASUS_ARRAY_T* x, Uint32 size)
233 {
234 reserve(this->size() + size);
235 _copyOnWrite();
236 memmove(_data() + size, _data(), sizeof(PEGASUS_ARRAY_T) * this->size());
237 CopyToRaw(_data(), x, size);
238 _rep->size += size;
239 }
240
241 #ifndef PEGASUS_ARRAY_T
242 template<class PEGASUS_ARRAY_T>
243 #else
244 PEGASUS_TEMPLATE_SPECIALIZATION
245 #endif
246 void Array<PEGASUS_ARRAY_T>::insert(Uint32 pos, const PEGASUS_ARRAY_T& x)
247 {
248 if (pos > size())
249 ThrowOutOfBounds();
250
251 reserve(size() + 1);
252 _copyOnWrite();
253 mike 1.5
254 Uint32 n = size() - pos;
255
256 if (n)
257 memmove(_data() + pos + 1, _data() + pos, sizeof(PEGASUS_ARRAY_T) * n);
258
259 new(_data() + pos) PEGASUS_ARRAY_T(x);
260 _rep->size++;
261 }
262
263 #ifndef PEGASUS_ARRAY_T
264 template<class PEGASUS_ARRAY_T>
265 #else
266 PEGASUS_TEMPLATE_SPECIALIZATION
267 #endif
268 void Array<PEGASUS_ARRAY_T>::insert(Uint32 pos, const PEGASUS_ARRAY_T* x, Uint32 size)
269 {
270 if (pos + size > this->size())
271 ThrowOutOfBounds();
272
273 reserve(this->size() + size);
274 mike 1.5 _copyOnWrite();
275
276 Uint32 n = this->size() - pos;
277
278 if (n)
279 memmove(
280 _data() + pos + size, _data() + pos, sizeof(PEGASUS_ARRAY_T) * n);
281
282 CopyToRaw(_data() + pos, x, size);
283 _rep->size += size;
284 }
285
286 #ifndef PEGASUS_ARRAY_T
287 template<class PEGASUS_ARRAY_T>
288 #else
289 PEGASUS_TEMPLATE_SPECIALIZATION
290 #endif
291 void Array<PEGASUS_ARRAY_T>::remove(Uint32 pos)
292 {
293 if (pos >= this->size())
294 ThrowOutOfBounds();
295 mike 1.5
296 _copyOnWrite();
297
298 Destroy(_data() + pos);
299
300 Uint32 rem = this->size() - pos - 1;
301
302 if (rem)
303 memmove(_data() + pos, _data() + pos + 1, sizeof(PEGASUS_ARRAY_T) * rem);
304
305 _rep->size--;
306 }
307
308 #ifndef PEGASUS_ARRAY_T
309 template<class PEGASUS_ARRAY_T>
310 #else
311 PEGASUS_TEMPLATE_SPECIALIZATION
312 #endif
313 void Array<PEGASUS_ARRAY_T>::remove(Uint32 pos, Uint32 size)
314 {
315 if (pos + size > this->size())
316 mike 1.5 ThrowOutOfBounds();
317
318 _copyOnWrite();
319
320 Destroy(_data() + pos, size);
321
322 Uint32 rem = this->size() - (pos + size);
323
324 if (rem)
325 memmove(
326 _data() + pos, _data() + pos + size, sizeof(PEGASUS_ARRAY_T) * rem);
327
328 _rep->size -= size;
329 }
330
331 #endif /*defined(PEGASUS_EXPLICIT_INSTANTIATION) || !defined(PEGASUS_ARRAY_T)*/
|