1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkVoidArray.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkVoidArray.h"
16 #include "vtkObjectFactory.h"
17 
18 vtkStandardNewMacro(vtkVoidArray);
19 
20 typedef void *voidPtr;
21 
22 // Instantiate object.
vtkVoidArray()23 vtkVoidArray::vtkVoidArray()
24   : NumberOfPointers(0),Size(0),Array(NULL)
25 {
26 }
27 
~vtkVoidArray()28 vtkVoidArray::~vtkVoidArray()
29 {
30   delete [] this->Array;
31 }
32 
33 // Allocate memory for this array. Delete old storage only if necessary.
Allocate(vtkIdType sz,vtkIdType vtkNotUsed (ext))34 int vtkVoidArray::Allocate(vtkIdType sz, vtkIdType vtkNotUsed(ext))
35 {
36   if ( sz > this->Size || this->Array != NULL )
37     {
38     delete [] this->Array;
39 
40     this->Size = ( sz > 0 ? sz : 1);
41     if ( (this->Array = new voidPtr[this->Size]) == NULL )
42       {
43       return 0;
44       }
45     }
46 
47   this->NumberOfPointers = 0;
48 
49   return 1;
50 }
51 
52 // Release storage and reset array to initial state.
Initialize()53 void vtkVoidArray::Initialize()
54 {
55   delete [] this->Array;
56   this->Array = NULL;
57   this->Size = 0;
58   this->NumberOfPointers = 0;
59 }
60 
61 // Deep copy of another void array.
DeepCopy(vtkVoidArray * va)62 void vtkVoidArray::DeepCopy(vtkVoidArray *va)
63 {
64   // Do nothing on a NULL input.
65   if (va == NULL)
66     {
67     return;
68     }
69 
70   if ( this != va )
71     {
72     delete [] this->Array;
73 
74     this->NumberOfPointers = va->NumberOfPointers;
75     this->Size = va->Size;
76 
77     this->Array = new voidPtr[this->Size];
78     memcpy(this->Array, va->GetVoidPointer(0), this->Size*sizeof(void *));
79     }
80 }
81 
WritePointer(vtkIdType id,vtkIdType number)82 void** vtkVoidArray::WritePointer(vtkIdType id,
83                                   vtkIdType number)
84 {
85   vtkIdType newSize=id+number;
86   if ( newSize > this->Size )
87     {
88     this->ResizeAndExtend(newSize);
89     }
90   if ( newSize > this->NumberOfPointers )
91     {
92     this->NumberOfPointers = newSize;
93     }
94   return this->Array + id;
95 }
96 
InsertVoidPointer(vtkIdType id,void * p)97 void vtkVoidArray::InsertVoidPointer(vtkIdType id, void* p)
98 {
99   if ( id >= this->Size )
100     {
101     this->ResizeAndExtend(id+1);
102     }
103   this->Array[id] = p;
104   if ( id >= this->NumberOfPointers )
105     {
106     this->NumberOfPointers = id+1;
107     }
108 }
109 
InsertNextVoidPointer(void * p)110 vtkIdType vtkVoidArray::InsertNextVoidPointer(void* p)
111 {
112   this->InsertVoidPointer(this->NumberOfPointers,p);
113   return this->NumberOfPointers-1;
114 }
115 
116 // Protected function does "reallocate"
117 //
ResizeAndExtend(vtkIdType sz)118 void** vtkVoidArray::ResizeAndExtend(vtkIdType sz)
119 {
120   void** newArray;
121   vtkIdType newSize;
122 
123   if ( sz > this->Size )
124     {
125     newSize = this->Size + sz;
126     }
127   else if (sz == this->Size)
128     {
129     return this->Array;
130     }
131   else
132     {
133     newSize = sz;
134     }
135 
136   if (newSize <= 0)
137     {
138     this->Initialize();
139     return 0;
140     }
141 
142   if ( (newArray = new voidPtr[newSize]) == NULL )
143     {
144     vtkErrorMacro(<< "Cannot allocate memory\n");
145     return 0;
146     }
147 
148   memcpy(newArray, this->Array,
149          (sz < this->Size ? sz : this->Size) * sizeof(voidPtr));
150 
151   if (newSize < this->Size)
152     {
153     this->NumberOfPointers = newSize;
154     }
155   this->Size = newSize;
156   delete [] this->Array;
157   this->Array = newArray;
158 
159   return this->Array;
160 }
161 
PrintSelf(ostream & os,vtkIndent indent)162 void vtkVoidArray::PrintSelf(ostream& os, vtkIndent indent)
163 {
164   this->Superclass::PrintSelf(os,indent);
165 
166   if (this->Array)
167     {
168     os << indent << "Array: " << this->Array << "\n";
169     }
170   else
171     {
172     os << indent << "Array: (null)\n";
173     }
174 }
175