-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.d
1437 lines (1300 loc) · 32.1 KB
/
buffer.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
Buffer(T) - Versatile appendable D array for buffer reuse, append, reset with preserved capacity.
Has additional template specializations for char, wchar, dchar, which always has an extra space for appending a terminating null character.
Magic property is no memory reallocation or capacity loss if length set to zero.
Resulting speed ups from reduced memory manager work. Perhaps reduction in hardware cache misses.
Almost a drop in replacement for T[]. Some features adopted from std.array and std.range
*/
/**
Authors: Michael Rynn
*/
module xml.util.buffer;
import std.utf;
import std.string;
import std.stdio;
import std.stdint;
import std.traits;
import core.stdc.string;
import std.conv;
import std.variant;
import std.exception;
import std.algorithm;
import std.ascii;
private import core.stdc.stdlib;
private import core.memory;
/// May be thrown by some index checks inside Buffer(T)
class BufferError : Exception
{
this(string s)
{
super(s);
}
static BufferError makeIndexError(intptr_t ix)
{
return new BufferError(format("Out of bounds index %s", ix));
}
static void makeSliceError(uintptr_t p1, uintptr_t p2)
{
throw new BufferError(format("Slice error: %s to %s", p1, p2));
}
}
import core.memory;
private {
enum alignBits = uintptr_t.sizeof;
enum alignMask = alignBits - 1;
enum alignData = ~alignMask;
}
/// Return next integer power of 2
T getNextPower2(T)(T k)
if (isIntegral!T) {
if (k == 0)
return 1;
k--;
for (int i=1; i < (T.sizeof * 8); i<<=1)
k = k | (k >> i);
return k+1;
}
/// starting from index pos, move up items which are T.init
/// return count of T.init found
uintptr_t removeInit(T)(T[] items, uintptr_t pos = 0)
{
auto dp = items.ptr;
if (dp is null || items.length == 0)
return 0;
auto ok = dp + pos;
auto end = dp + items.length;
while ( (ok < end) && (*ok !is T.init) )
ok++;
auto adv = ok + 1;
while (adv < end)
{
if (*adv !is T.init)
{
*ok++ = *adv;
*adv = T.init;
adv++;
}
else
adv++;
}
return items.length - (ok - dp);
}
/// The speed up for using the Buffer class over plain T[] is something like 8/3 or 2.6 times
/// At least this the ratio noted in the XmlParser for a short XML document.
/// Using T[] always triggers a reallocation every time length is set to zero.
/// A T[] cannot be reset and reused as is, which defeats the purpose of having a capacity.
/// Such reallocations also increase possibility of hardware cache misses.
/// std.array Appender is another possibility, but it is not a drop in replacement for T[]
/// Using Buffer can save a lot of memory reallocation, as maximum used capacity is preserved
/// For T = character type, the custom allocation includes 1 extra allocation unit beyond requested capacity for adding a terminating null without reallocation).
/// Null termination (calling nullTerminate()) will therefore never cause a re-allocation.
struct Buffer(T)
{
/// Adapted from std.algorithm, with some assert conditions removed
struct Range
{
// Writeable, and ignores reference count. Writes change without copyOnWrite
private Buffer* _outer;
private uintptr_t _a, _b;
// Does not increment reference count
this(Buffer* data, size_t a, size_t b)
{
assert((data != null) && (a <= b) && (b <= data.length_));
_outer = data;
_a = a;
_b = b;
}
@property Range save()
{
return this;
}
@property bool empty() const
{
return _a >= _b;
}
@property size_t length() const
{
return _b - _a;
}
//??? outer_.length ??
size_t opDollar() const
{
return _b - _a;
}
@property T front()
{
enforce(!empty);
return _outer.ptr_[_a];
}
@property T back()
{
enforce(!empty);
return _outer.ptr_[_b - 1];
}
void popFront()
{
enforce(!empty);
++_a;
}
void popBack()
{
enforce(!empty);
--_b;
}
T moveFront()
{
enforce(!empty);
return move( _outer.ptr_[_a] );
}
T moveBack()
{
enforce(!empty);
return move(_outer.ptr_[_b - 1]);
}
T moveAt(size_t i)
{
i += _a;
enforce(i < _b && !empty);
return move(_outer.ptr_[i]);
}
T opIndex(size_t i)
{
i += _a;
enforce(i < _b);
return _outer.ptr_[i];
}
void opIndexAssign(T value, size_t i)
{
i += _a;
enforce(i < _b);
_outer.ptr_[i] = value;
}
void opIndexOpAssign(string op)(T value, size_t i)
{
i += _a;
enforce(i < _b);
mixin("_outer[i] "~op~"= value;");
}
typeof(this) opSlice()
{
return this;
}
typeof(this) opSlice(size_t a, size_t b)
{
a += _a;
b += _a;
enforce(a <= b && b <= _b);
return typeof(this)(_outer, a, b);
}
void opSliceAssign(T value)
{
_outer.ptr_[_a .. _b] = value;
}
}
alias Buffer!T ThisType;
/** ensure that data is copied */
void copy(ref ThisType po)
{
if (&this == &po)
return;
assign(po.ptr_, po.length);
}
/** Set pointer to null, length and capacity to 0 */
final void forget()
{
ptr_ = null;
length_ = 0;
capacity_ = 0;
}
/** Take ownership of data from argument */
void takeOver(ref ThisType po)
{
if (&this == &po)
return;
if (ptr_ != null)
{
assert(ptr_ != po.ptr_);
forget();
}
ptr_ = po.ptr_;
length_ = po.length_;
capacity_ = po.capacity_;
po.forget();
}
/// Read capacity property
uintptr_t capacity() const @property
{
return (ptr_ is null) ? 0 : capacity_;
}
/// Read pointer property
T* ptr()
{
if (!ptr_)
return null;
return ptr_;
}
/// Read length property
uintptr_t length() const @property
{
return (ptr_ is null) ? 0 : length_;
}
/// mimic of Appender interface, only allow shrink
void shrinkTo(uintptr_t x)
{
if (ptr_ && (x < length_))
{
static if (!isScalarType!(T))// else already marked as having no pointers
{
destroy_data(ptr_+x, length_ - x);
}
length_ = x;
}
}
/// Change length. May call constructor or destructor for non-simple T
void length(uintptr_t x) @property
{
if (ptr_)
{
size_t oldlen = length_;
if (oldlen == x)
return;
if (oldlen > x)
{
static if (!isScalarType!(T))
{
destroy_data(ptr_+x, oldlen - x);
}
length_ = x;
}
else {
auto scap = capacity_;
if (x > scap)
{
reserve(x); // ptr_ expected to change
}
length_ = x;
static if (!isScalarType!(T))
{
init_create(ptr_ + oldlen, x - oldlen);
}
}
return;
}
reserve(x);
length_ = x;
static if (!isScalarType!(T))
{
init_create(ptr_,x);
}
}
/// Const pointer to internal buffer
const(T)* constPtr() const @property
{
return ptr_;
}
private {
/// ensure capacity for an additional number of items.
void roomForExtra(uintptr_t extra)
{
immutable newCap = length_ + extra;
if (newCap > capacity_)
reserve(newCap);
}
static void init_create(T)(T* dest, size_t nlen)
{
T* end = dest + nlen;
while(dest < end)
{
*dest = T.init;
dest++;
}
}
static
void copy_create(T* dest, const(T)* src, size_t nlen)
{
T* end = dest + nlen;
while(dest < end)
{
*dest = * cast(T*) (cast(void*) src);
dest++;
src++;
}
//new (dest) T(*src);
}
static
void destroy_data(T* dest, size_t nlen)
{
T* end = dest + nlen;
while(dest < end)
{
static if (hasElaborateDestructor!(T))
typeid(T).destroy(dest);
else
*dest = T.init;
dest++;
}
}
static
void freeCapacity(T* data,bool doFree = false)
{
if (data is null)
return;
if (doFree)
GC.free(data);
}
static
T* createCapacity(ref uintptr_t kap, bool exactLen = false)
{
// No point in being APPENDABLE if always making a new block.
auto doScan = typeid(T[]).next.flags & 1; // some indication that T contains pointers?
static if (isSomeChar!T)
{
uintptr_t nullSpace = T.sizeof; // zero termination always possible.
}
else {
uintptr_t nullSpace = 0;
}
uintptr_t allocSize = kap * T.sizeof + nullSpace;
/// round it up, maybe fit memory manager better
if (!exactLen)
allocSize = getNextPower2!uintptr_t(allocSize);
auto info = GC.qalloc(allocSize, (doScan) ? 0 : GC.BlkAttr.NO_SCAN);
auto data = cast(T*) info.base;
memset(data, 0, allocSize);
auto newcap = (info.size - nullSpace) / T.sizeof;
assert(newcap >= kap);
kap = newcap;
return data;
}
}
/// Get a writeable range.
Range opSlice()
{
return Range(&this, 0, length);
}
/// Get a writeable range.
Range opSlice(size_t a, size_t b)
{
enforce(a <= b && b <= length_);
return Range(&this, a, b);
}
/// Ensure at least a minimum capacity, greater or equal to current length.
/// Rounds up to a next power of 2, unless exactLen is true.
void reserve (uintptr_t len, bool exactLen = false)
{
if (ptr_ is null)
{
ptr_ = createCapacity(len,exactLen);
if (ptr_ !is null)
capacity_ = len;
debug(TrackCount)
{
ptr_.codeline_ = id_;
addTrack(ptr_);
}
return;
}
auto oldcap = capacity_;
if (len > oldcap)
{
// more than current capacity, current length_ <= oldcap
auto newdata = createCapacity(len);
capacity_ = len;
if (length_ > 0) //must have set length to 0 if doing assign
{
auto len_copy = length_;
memcpy(newdata, ptr_, len_copy*T.sizeof);
length_ = len_copy;
}
freeCapacity(ptr_);
ptr_ = newdata;
}
else {
// can only shrink down to current length, or do nothing
}
}
/// Replace
void assign(const(T)* buf, size_t slen)
{
if (slen == 0)
{
length(0);
return;
}
reserve(slen, true);
copy_create(ptr_, buf, slen);
length_ = slen;
}
void opAssign(immutable(T)[] s)
{
if (s !is null)
assign(s.ptr, s.length);
else
length = 0;
}
void opAssign(const(T)[] s)
{
if (s !is null)
assign(s.ptr, s.length);
else
length = 0;
}
void put(T c)
{
roomForExtra(1);
(ptr_)[length_++] = c;
}
static if (isSomeChar!(T))
{
void putInteger(S)(S value,uint radix = 10)
if (isIntegral!S)
{
static if (isSigned!S)
{
if (value < 0)
put('-');
value = -value;
}
char[value.sizeof * 8] buffer;
uint i = buffer.length;
if (value < radix && value < hexDigits.length)
{
opCatAssign(hexDigits[cast(size_t)value .. cast(size_t)value + 1]);
return;
}
do
{
ubyte c;
c = cast(ubyte)(value % radix);
value = value / radix;
i--;
buffer[i] = cast(char)((c < 10) ? c + '0' : c + 'A' - 10);
}
while (value);
opCatAssign(buffer[i..$]);
}
@property immutable(T)[] freeze()
{
if (ptr_ is null)
return null;
auto result = (cast(immutable(T)*)ptr_)[0..length_];
ptr_ = null;
length_ = 0;
return result;
}
/// Take away internal buffer as if is its single immutable reference. This may change
@property immutable(T)[] idup()
{
if (ptr_ is null)
return null;
return (ptr_)[0..length_].idup;
}
static if(!is(T == dchar))
{
/// encode append dchar to buffer as UTF Ts
void put(dchar c)
{
static if (is(T==char))
{
if (c < 0x80)
{
roomForExtra(1);
(ptr_)[length_++] = cast(char)c;
return;
}
T[4] encoded;
if (c == 163)
{
c = 163;
}
auto len = std.utf.encode(encoded, c);
roomForExtra(len);
auto cptr = ptr_ + length_;
length_ += len;
auto eptr = encoded.ptr;
while(len > 0)
{
len--;
*cptr++ = *eptr++;
}
}
else static if (is(T==wchar))
{
if (c < 0xD800)
{
roomForExtra(1);
(ptr_)[length_++] = cast(wchar)c;
return;
}
else
{
T[2] encoded;
auto len = std.utf.encode(encoded, c);
roomForExtra(len);
auto wptr = ptr_ + length_;
length_ += len;
auto eptr = encoded.ptr;
while(len > 0)
{
len--;
*wptr++ = *eptr++;
}
}
}
}
}
void opCatAssign(dchar c)
{
put(c);
}
int opApply(int delegate(dchar value) dg)
{
// let existing D code do it.
if (ptr_ is null)
return 0;
auto slice = ptr_[0..length_];
uintptr_t ix = 0;
while (ix < slice.length)
{
dchar d = decode(slice,ix);
auto result = dg(d);
if (result)
return result;
}
return 0;
}
/// Always allowed to append 0 at index of length_,
void nullTerminate()
{
if (!ptr_)
{
// At least no one else owns this!
length(0);
}
// always a space at the back
(ptr_)[length_] = 0;
}
const(T)* cstr() @property
{
nullTerminate();
return constPtr();
}
}
static if (is(T==char))
{
this(immutable(char)[] s)
{
reserve(s.length);
opCatAssign(s);
}
this(const(wchar)[] s)
{
reserve(s.length);
opCatAssign(s);
}
this(const(dchar)[] s)
{
reserve(s.length);
opCatAssign(s);
}
/// OpAssigns are opCatAssigns
void opAssign(const(wchar)[] s)
{
if (ptr_ !is null)
length_ = 0;
opCatAssign(s);
}
void opAssign(const(dchar)[] s)
{
if (ptr_ !is null)
length_ = 0;
opCatAssign(s);
}
void put(const(wchar)[] s)
{
opCatAssign(s);
}
void opCatAssign(const(wchar)[] s)
{
immutable slen = s.length;
if (slen==0)
return;
roomForExtra(slen);
for (uintptr_t i = 0; i < slen;)
{
wchar c = s[i];
if (c > 0x7F)
{
dchar d = decode(s, i);
put(d);
}
else
{
i++;
roomForExtra(1);
(ptr_)[length_++] = cast(char) c;
}
}
}
void opCatAssign(const(dchar)[] s)
{
immutable slen = s.length;
if (slen==0)
return;
roomForExtra(slen);
for (uintptr_t i = 0; i < slen; i++)
{
dchar d = s[i];
if (d > 0x7F)
{
put(d);
}
else
{
roomForExtra(1);
(ptr_)[length_++] = cast(char)d;
}
}
}
}
static if (is(T==wchar))
{
this(const(dchar)[] s)
{
reserve(s.length);
opCatAssign(s);
}
void opCatAssign(const(dchar)[] s)
{
immutable slen = s.length;
if (slen==0)
return;
roomForExtra(slen);
for (uintptr_t i = 0; i < slen; i++)
{
dchar d = s[i];
if (d < 0x10000)
{
roomForExtra(1);
ptr_[length_++] = cast(char)d;
}
else
{
roomForExtra(2);
size_t n = d - 0x10000;
ptr_[length_++] = cast(wchar)(0xD800 + (n >> 10));
ptr_[length_++] = cast(wchar)(0xDC00 + (n & 0x3FF));
}
}
}
this(const(char)[] s)
{
reserve(s.length);
opCatAssign(s);
}
void opCatAssign(const(char)[] s)
{
immutable slen = s.length;
if (slen==0)
return;
roomForExtra(slen);
uintptr_t i = 0;
while(i < slen)
{
dchar c = s[i++];
if (c < 0x80)
{
roomForExtra(1);
ptr_[length_++] = cast(char)c;
}
else if (c < 0xF0)
{
if (c < 0xC0)
{
throw new BufferError(format("invalid char {%x}", c));
}
roomForExtra(1);
if (c < 0xE0 && i < slen)
{
c = c & 0x1F;
c = (c << 6) + (s[i++] & 0x3F);
ptr_[length_++] = cast(wchar)c;
}
else if (i < slen-1)
{
c = c & 0x0F;
foreach(k;0..2)
c = (c << 6) + (s[i++] & 0x3F);
ptr_[length_++] = cast(wchar)c;
}
}
else
{ // 2 wchar
roomForExtra(2);
if (c < 0xF8 && i < slen-2)
{
c = c & 0x07;
foreach(k;0..3)
c = (c << 6) + (s[i++] & 0x3F);
}
if (c < 0xFC && i < slen-3)
{
c = c & 0x03;
foreach(k;0..4)
c = (c << 6) + (s[i++] & 0x3F);
}
size_t n = c - 0x10000;
ptr_[length_++] = cast(wchar)(0xD800 + (n >> 10));
ptr_[length_++] = cast(wchar)(0xDC00 + (n & 0x3FF));
}
}
}
}
static if (is(T==dchar))
{
/// OpAssigns are opCatAssigns
void opAssign(const(char)[] s)
{
if (ptr_ !is null)
{
length_ = 0;
}
opCatAssign(s);
}
void opAssign(const(wchar)[] s)
{
if (ptr_ !is null)
{
length_ = 0;
}
opCatAssign(s);
}
void opCatAssign(const(char)[] s)
{
immutable slen = s.length;
if (slen==0)
return;
roomForExtra(slen);
for (uintptr_t i = 0; i < slen; )
{
dchar c = s[i];
if (c > 0x7F)
c = decode(s, i);
else
i++;
roomForExtra(1);
(ptr_)[length_++] = c;
}
}
void opCatAssign(const(wchar)[] s)
{
immutable slen = s.length;
if (slen==0)
return;
roomForExtra(slen);
for (uintptr_t i = 0; i < slen;)
{
dchar c = s[i];
if (c > 0x7F)
c = decode(s,i);
else
i++;
roomForExtra(1);
(ptr_)[length_++] = c;
}
}
}
T[] dup() @property
{
if (ptr_ is null)
return null;
return (ptr_)[0..length_].dup;
}
T[] takeArray()
{
if (ptr_ is null)
return null;
auto result = (ptr_)[0..length_];
forget();
return result;
}
const(T)[] slice(uintptr_t p1, uintptr_t p2)
{
assert((ptr_ !is null) && (p1 <= p2) && (p2 <= length_));
return (ptr_)[p1..p2];
}
/// Get pointer to last value of internal buffer
@property T* last()
{
if ((ptr_ is null) || length_ < 1)
throw new BufferError("last: empty array");
return ( ptr_ + (length_ - 1) );
}
@property bool empty() const {
return (ptr_ is null) ? true : (length_ == 0);
}
/// append T[]
void put(const(T)[] s)
{
if (s.length > 0)
append(s.ptr, s.length);
}
/// Return writeable slice of the buffer.
T[] data() @property
{
if (!ptr_)
return [];
return (ptr_)[0..length_];
}
bool opEquals( const(T)[] rhs) const
{
auto lhs = this.toConstArray();
return typeid(T[]).equals(&rhs, &lhs);
}
bool opEquals(ref const ThisType ro) const
{
auto lhs = this.toConstArray();
auto rhs = ro.toConstArray();
return typeid(T[]).equals(&rhs, &lhs);
}
const(T)[] peek() const @property nothrow
{
if (ptr_ is null)
return [];
return ptr_[0..length_];
}
alias peek toConstArray;
T[] take()
{
auto result = ptr_[0..length_];
forget();
return result;
}
int opCmp(ref const ThisType ro) const
{
auto lhs = this.toConstArray();
auto rhs = ro.toConstArray();
return typeid(T[]).compare(&lhs, &rhs);
}
/// sort in place
/* static if (isSomeChar!T)
{
}
else {
const(T)[] sort() @property
{
if (ptr_ is null)
return null;
if(length_ <= 1)
return (ptr_)[0..length_];
T[] temp = (ptr_)[0..length_];
if (length_ > 1)
{
std.algorithm.sorting.sort!("a < b")(temp);
}
return temp;
}
}
*/
///
T front()
{
if (ptr_ !is null)
{
if (length_ > 0)
return (ptr_)[0];
}
throw new BufferError("front: empty array");
}
///
T back()
{
if (ptr_ !is null)
{
if (length_ > 0)
return (ptr_)[length_-1];
}
throw new BufferError("back: empty array");
}
// create a new item on the end
T* pushNew()
{