forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkVoidArray.cxx
178 lines (149 loc) · 3.55 KB
/
vtkVoidArray.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkVoidArray.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkVoidArray.h"
#include "vtkObjectFactory.h"
vtkStandardNewMacro(vtkVoidArray);
vtkStandardExtendedNewMacro(vtkVoidArray);
typedef void* voidPtr;
// Instantiate object.
vtkVoidArray::vtkVoidArray()
: NumberOfPointers(0)
, Size(0)
, Array(nullptr)
{
}
vtkVoidArray::~vtkVoidArray()
{
delete[] this->Array;
}
// Allocate memory for this array. Delete old storage only if necessary.
vtkTypeBool vtkVoidArray::Allocate(vtkIdType sz, vtkIdType vtkNotUsed(ext))
{
if (sz > this->Size || this->Array != nullptr)
{
delete[] this->Array;
this->Size = (sz > 0 ? sz : 1);
if ((this->Array = new voidPtr[this->Size]) == nullptr)
{
return 0;
}
}
this->NumberOfPointers = 0;
return 1;
}
// Release storage and reset array to initial state.
void vtkVoidArray::Initialize()
{
delete[] this->Array;
this->Array = nullptr;
this->Size = 0;
this->NumberOfPointers = 0;
}
// Deep copy of another void array.
void vtkVoidArray::DeepCopy(vtkVoidArray* va)
{
// Do nothing on a nullptr input.
if (va == nullptr)
{
return;
}
if (this != va)
{
delete[] this->Array;
this->NumberOfPointers = va->NumberOfPointers;
this->Size = va->Size;
this->Array = new voidPtr[this->Size];
memcpy(this->Array, va->GetVoidPointer(0), this->Size * sizeof(void*));
}
}
void** vtkVoidArray::WritePointer(vtkIdType id, vtkIdType number)
{
vtkIdType newSize = id + number;
if (newSize > this->Size)
{
this->ResizeAndExtend(newSize);
}
if (newSize > this->NumberOfPointers)
{
this->NumberOfPointers = newSize;
}
return this->Array + id;
}
void vtkVoidArray::InsertVoidPointer(vtkIdType id, void* p)
{
if (id >= this->Size)
{
if (!this->ResizeAndExtend(id + 1))
{
return;
}
}
this->Array[id] = p;
if (id >= this->NumberOfPointers)
{
this->NumberOfPointers = id + 1;
}
}
vtkIdType vtkVoidArray::InsertNextVoidPointer(void* p)
{
this->InsertVoidPointer(this->NumberOfPointers, p);
return this->NumberOfPointers - 1;
}
// Protected function does "reallocate"
//
void** vtkVoidArray::ResizeAndExtend(vtkIdType sz)
{
void** newArray;
vtkIdType newSize;
if (sz > this->Size)
{
newSize = this->Size + sz;
}
else if (sz == this->Size)
{
return this->Array;
}
else
{
newSize = sz;
}
if (newSize <= 0)
{
this->Initialize();
return nullptr;
}
if ((newArray = new voidPtr[newSize]) == nullptr)
{
vtkErrorMacro(<< "Cannot allocate memory\n");
return nullptr;
}
memcpy(newArray, this->Array, (sz < this->Size ? sz : this->Size) * sizeof(voidPtr));
if (newSize < this->Size)
{
this->NumberOfPointers = newSize;
}
this->Size = newSize;
delete[] this->Array;
this->Array = newArray;
return this->Array;
}
void vtkVoidArray::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
if (this->Array)
{
os << indent << "Array: " << this->Array << "\n";
}
else
{
os << indent << "Array: (null)\n";
}
}