@@ -199,17 +199,10 @@ public:
199
199
Gets the C data pointer
200
200
*/
201
201
@trusted
202
- T * data () {
202
+ inout (T) * data () inout {
203
203
return memory;
204
204
}
205
-
206
- /**
207
- Gets the C data pointer as an inout pointer
208
- */
209
- @trusted
210
- inout (T)* idata () inout {
211
- return cast (inout (T)* )memory;
212
- }
205
+ alias ptr = data; // /ditto
213
206
214
207
/**
215
208
Gets the C data pointer atomically
@@ -278,6 +271,7 @@ public:
278
271
size_t size () inout {
279
272
return size_;
280
273
}
274
+ alias length = size; // /ditto
281
275
282
276
/**
283
277
Gets the capacity of the vector
@@ -302,8 +296,8 @@ public:
302
296
void clear () {
303
297
304
298
// Delete elements in the array.
305
- static if (ownsMemory) {
306
- foreach (item; 0 .. size_) {
299
+ static if (ownsMemory && ! isBasicType ! T ) {
300
+ foreach_reverse (item; 0 .. size_) {
307
301
nogc_delete(memory[item]);
308
302
}
309
303
}
@@ -318,7 +312,7 @@ public:
318
312
void remove (size_t position) {
319
313
if (position < size_) {
320
314
321
- static if (ownsMemory)
315
+ static if (ownsMemory && ! isBasicType ! T )
322
316
nogc_delete(memory[position]);
323
317
324
318
// Move memory region around so that the deleted element is overwritten.
@@ -329,33 +323,27 @@ public:
329
323
}
330
324
331
325
/**
332
- Erases element at position
326
+ Erases element at position [start, end)
327
+ End is NOT included in that range.
333
328
*/
334
329
@trusted
335
330
void remove (size_t start, size_t end) {
336
331
337
- // Flip inputs if they are reversed, just in case.
338
- if (end > start) {
339
- size_t tmp = start;
340
- start = end;
341
- end = tmp;
342
- }
343
-
344
- if (start < size_ && end < size_) {
345
-
346
- // NOTE: the ".." operator is start inclusive, end exclusive.
347
- static if (ownsMemory) {
348
- foreach (i; start.. end+ 1 )
349
- nogc_delete(memory[i]);
350
- }
332
+ assert (start <= end && end <= size_);
351
333
352
- // Copy over old elements
353
- size_t span = (end + 1 ) - start;
354
- // memory[ start..start+span] = memory[ end..end+span];
355
- memmove(memory + start, memory + end, span * (T * ).sizeof );
334
+ // NOTE: the ".." operator is start inclusive, end exclusive.
335
+ static if (ownsMemory && ! isBasicType ! T) {
336
+ foreach_reverse (i; start.. end)
337
+ nogc_delete(memory[i] );
356
338
357
- size_ -= span;
358
339
}
340
+
341
+ // Copy over old elements
342
+ size_t span = end- start;
343
+ // memory[start..start+span] = memory[end..end+span];
344
+ memmove(memory+ start, memory+ end, span* (T* ).sizeof);
345
+
346
+ size_ -= span;
359
347
}
360
348
361
349
/**
@@ -491,7 +479,7 @@ public:
491
479
Override for $ operator
492
480
*/
493
481
@trusted
494
- size_t opDollar () {
482
+ size_t opDollar () const {
495
483
return size_;
496
484
}
497
485
@@ -525,7 +513,7 @@ public:
525
513
Allows getting an item from the vector.
526
514
*/
527
515
@trusted
528
- ref T opIndex (size_t index) {
516
+ ref inout (T) opIndex (size_t index) inout {
529
517
return memory[index];
530
518
}
531
519
@@ -578,3 +566,12 @@ unittest {
578
566
vector! (shared_ptr! A) v;
579
567
v ~= a; // Used to crash, see Issue #2
580
568
}
569
+
570
+ @(" vector: delete" )
571
+ unittest {
572
+ vector! int v;
573
+ v ~= [1 , 2 , 3 ];
574
+ v.remove(0 , 0 );
575
+ v.remove(1 , 1 );
576
+ v.remove(2 , 2 );
577
+ }
0 commit comments