8
\$\begingroup\$

I'm making a simple container class for fun and education, but my rebuilding/resizing method seems rather inefficient. Is there an easier way to do this?

// If FromFront is true, cells should be added or
// subtracted from the front of the array rather than the back.
void Rebuild(std::size_t NewSize, bool FromFront = false)
{
    const std::size_t OldSize = Size;
    Size = NewSize; // Size is used in other methods.

    Datatype* TemporaryStorage = new Datatype[OldSize]; // Allocate space for the values to be preserved while a new main array is made.

    for (std::size_t Index = 0; Index < OldSize; Index++) TemporaryStorage[Index] = BasePointer[Index]; // Copy values over to the temporary array.

    delete[] BasePointer; // Delete the main array...
    BasePointer = new Datatype[NewSize]; // ...And rebuild it with the appropriate size.

    for (std::size_t Iteration = 0; (Iteration < OldSize) && (Iteration < NewSize); Iteration++)
    {
        std::size_t BasePointerIndex = Iteration;
        std::size_t TemporaryStorageIndex = Iteration;

        if (FromFront) // We need to take special measures to give the indices offsets.
        {
            if (NewSize > OldSize) BasePointerIndex += (NewSize - OldSize);
            else TemporaryStorageIndex += (OldSize - NewSize);
        }

        BasePointer[BasePointerIndex] = TemporaryStorage[TemporaryStorageIndex]; // Copy values from the temporary array to the new main array.
    }

    delete[] TemporaryStorage; // Finally, delete the temporary storage array.
}
\$\endgroup\$
2
  • \$\begingroup\$ I must be missing something...why are you even using a temporary array? \$\endgroup\$ Commented Mar 11, 2011 at 18:41
  • 1
    \$\begingroup\$ @Mark Probably because I'm stupid. \$\endgroup\$
    – Maxpm
    Commented Mar 11, 2011 at 20:00

1 Answer 1

9
\$\begingroup\$
void Rebuild(std::size_t NewSize, bool FromFront = false)
{
    const std::size_t OldSize = Size;

    Datatype* NewStorage = new Datatype[NewSize]; 

Rather then creating a temporary array and copying the existing data into it, just create the new array and copy into that.

    int CopyLength = std::min(OldSize, NewSize);

We start by determining how many elements we will actually copy

    if( FromFront )
    {
        std::copy(BasePointer + OldSize - CopyLength, BasePointer + OldSize,
                  NewStorage + NewSize - CopyLength);
    }
    else
    {
        std::copy(BasePointer, BasePointer + CopyLength, NewStorage);
    }

I use std::copy to avoid having to write a copying for loop myself.

    delete[] BasePointer; // Delete the main array...
    BasePointer = NewStorage;
    Size = NewSize; // Size is used in other methods.

delete the current array and replace it with my new array

}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ std::copy further can actually be extra smart and copy in a more efficient way if the data type supports it (something like memcpy for primitive types). \$\endgroup\$ Commented Mar 13, 2011 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.