------------------------------------------------------------------------------
| 817 | |
| 818 | //------------------------------------------------------------------------------ |
| 819 | vtkVariantArray::ValueType* vtkVariantArray::ResizeAndExtend(vtkIdType sz) |
| 820 | { |
| 821 | ValueType* newArray; |
| 822 | vtkIdType newSize; |
| 823 | |
| 824 | if (sz > this->Size) |
| 825 | { |
| 826 | // Requested size is bigger than current size. Allocate enough |
| 827 | // memory to fit the requested size and be more than double the |
| 828 | // currently allocated memory. |
| 829 | newSize = this->Size + sz; |
| 830 | } |
| 831 | else if (sz == this->Size) |
| 832 | { |
| 833 | // Requested size is equal to current size. Do nothing. |
| 834 | return this->Array; |
| 835 | } |
| 836 | else |
| 837 | { |
| 838 | // Requested size is smaller than current size. Squeeze the |
| 839 | // memory. |
| 840 | newSize = sz; |
| 841 | } |
| 842 | |
| 843 | if (newSize <= 0) |
| 844 | { |
| 845 | this->Initialize(); |
| 846 | return nullptr; |
| 847 | } |
| 848 | |
| 849 | newArray = new ValueType[newSize]; |
| 850 | if (!newArray) |
| 851 | { |
| 852 | vtkErrorMacro("Cannot allocate memory\n"); |
| 853 | return nullptr; |
| 854 | } |
| 855 | |
| 856 | if (this->Array) |
| 857 | { |
| 858 | // can't use memcpy here |
| 859 | vtkIdType numCopy = (newSize < this->Size ? newSize : this->Size); |
| 860 | for (vtkIdType i = 0; i < numCopy; ++i) |
| 861 | { |
| 862 | newArray[i] = this->Array[i]; |
| 863 | } |
| 864 | if (this->DeleteFunction) |
| 865 | { |
| 866 | this->DeleteFunction(this->Array); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | if (newSize < this->Size) |
| 871 | { |
| 872 | this->MaxId = newSize - 1; |
| 873 | } |
| 874 | this->Size = newSize; |
| 875 | this->Array = newArray; |
| 876 | this->DeleteFunction = DefaultDeleteFunction; |
no test coverage detected