358 useNewCapacity = BUF_SIZE;
361 useNewCapacity = Foundation::Containers::Support::ReserveTweaks::GetScaledUpCapacity (useNewCapacity,
sizeof (T),
362 Math::AtLeast<size_t> (BUF_SIZE, 1));
367 Assert (useNewCapacity >= newCapacity);
368 if (useNewCapacity != oldCapacity) [[unlikely]] {
369 bool oldInPlaceBuffer = oldCapacity <= BUF_SIZE;
370 bool newInPlaceBuffer = useNewCapacity <= BUF_SIZE;
371 if constexpr (is_trivially_copyable_v<T>) {
373 if (not oldInPlaceBuffer and not newInPlaceBuffer) {
375 fLiveData_ =
reinterpret_cast<T*
> (Reallocate_ (LiveDataAsAllocatedBytes_ (), SizeInBytes_ (useNewCapacity)));
376 fCapacityOfFreeStoreAllocation_ = useNewCapacity;
378 else if (oldInPlaceBuffer and newInPlaceBuffer) {
381 else if (oldInPlaceBuffer and not newInPlaceBuffer) {
383 byte* newPtr = Allocate_ (SizeInBytes_ (useNewCapacity));
385 Assert (this->begin () !=
reinterpret_cast<T*
> (newPtr));
386 Assert (
static_cast<size_t> (this->end () - this->begin ()) <= useNewCapacity);
387 uninitialized_copy (this->begin (), this->end (),
reinterpret_cast<T*
> (newPtr));
388 fLiveData_ =
reinterpret_cast<T*
> (newPtr);
389 if (not newInPlaceBuffer) {
390 fCapacityOfFreeStoreAllocation_ = useNewCapacity;
393 else if (not oldInPlaceBuffer and newInPlaceBuffer) {
395 byte* newPtr = std::begin (fInlinePreallocatedBuffer_);
397 Assert (this->begin () !=
reinterpret_cast<T*
> (newPtr));
398 Assert (
static_cast<size_t> (this->end () - this->begin ()) <= useNewCapacity);
399 uninitialized_copy (this->begin (), this->end (),
reinterpret_cast<T*
> (newPtr));
400 Deallocate_ (LiveDataAsAllocatedBytes_ ());
401 fLiveData_ =
reinterpret_cast<T*
> (newPtr);
406 if (oldInPlaceBuffer != newInPlaceBuffer or (not newInPlaceBuffer)) {
407 byte* newPtr = newInPlaceBuffer ? std::begin (fInlinePreallocatedBuffer_) : Allocate_ (SizeInBytes_ (useNewCapacity));
410 Assert (this->begin () !=
reinterpret_cast<T*
> (newPtr));
411 Assert (
static_cast<size_t> (this->end () - this->begin ()) <= useNewCapacity);
412 uninitialized_copy (this->begin (), this->end (),
reinterpret_cast<T*
> (newPtr));
415 DestroyElts_ (this->begin (), this->end ());
418 if (not oldInPlaceBuffer) {
419 Assert (not UsingInlinePreallocatedBuffer_ ());
420 Deallocate_ (LiveDataAsAllocatedBytes_ ());
423 fLiveData_ =
reinterpret_cast<T*
> (newPtr);
424 if (not newInPlaceBuffer) {
425 fCapacityOfFreeStoreAllocation_ = useNewCapacity;
430 Ensure ((useNewCapacity <= BUF_SIZE and capacity () == BUF_SIZE) or (useNewCapacity > BUF_SIZE and useNewCapacity == capacity ()));
433 template <
typename T,
size_t BUF_SIZE>
436 Ensure (fSize_ <= capacity ());
439 template <
typename T,
size_t BUF_SIZE>
442 Ensure (fSize_ <= capacity ());
445 template <
typename T,
size_t BUF_SIZE>
450 template <
typename T,
size_t BUF_SIZE>
453 Require (i < fSize_);
454 return *(fLiveData_ + i);
456 template <
typename T,
size_t BUF_SIZE>
459 Require (i < fSize_);
460 return *(fLiveData_ + i);
462 template <
typename T,
size_t BUF_SIZE>
467 template <
typename T,
size_t BUF_SIZE>
472 template <
typename T,
size_t BUF_SIZE>
473 template <ISpanOfT<T> SPAN_T>
474 inline void InlineBuffer<T, BUF_SIZE>::Insert (
size_t at,
const SPAN_T& copyFrom)
477 size_t n2Add = copyFrom.size ();
478 size_t newS = s + n2Add;
479 if (not this->HasEnoughCapacity_ (newS)) [[unlikely]] {
482 Assert (this->HasEnoughCapacity_ (newS));
483 this->fSize_ = Memory::Insert (span{this->begin (), size ()}, span{this->begin (), capacity ()}, at, copyFrom).size ();
484 Assert (this->fSize_ == newS);
486 template <
typename T,
size_t BUF_SIZE>
487 inline void InlineBuffer<T, BUF_SIZE>::Insert (
size_t at,
const T& item)
489 Insert (at, span{&item, 1});
491 template <
typename T,
size_t BUF_SIZE>
494 Insert (i - begin (), span{from, to});
496 template <
typename T,
size_t BUF_SIZE>
501 if (not this->HasEnoughCapacity_ (newS)) [[unlikely]] {
504 if constexpr (is_trivially_copyable_v<T>) {
508 uninitialized_copy (&e, &e + 1, this->begin () + s);
513 template <
typename T,
size_t BUF_SIZE>
514 template <ISpanOfT<T> SPAN_T>
518 size_t newS = s + copyFrom.size ();
519 if (not this->HasEnoughCapacity_ (newS)) [[unlikely]] {
522 Assert (this->HasEnoughCapacity_ (newS));
523 if constexpr (is_trivially_copyable_v<T>) {
524 CopySpanData (copyFrom, span{this->begin () + s, copyFrom.size ()});
527 uninitialized_copy (copyFrom.begin (), copyFrom.end (), this->begin () + s);
531 template <
typename T,
size_t BUF_SIZE>
532 template <ISpan SPAN_T>
536 size_t newS = s + copyFrom.size ();
537 if (not this->HasEnoughCapacity_ (newS)) [[unlikely]] {
540 Assert (this->HasEnoughCapacity_ (newS));
541 auto outPtr = this->begin () + s;
542 if constexpr (is_trivially_copyable_v<T>) {
543 CopySpanData (copyFrom, span{outPtr, copyFrom.size ()});
546 uninitialized_copy (copyFrom.begin (), copyFrom.end (), outPtr);
550 template <
typename T,
size_t BUF_SIZE>