1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkVariantArray.h
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 /*-------------------------------------------------------------------------
16   Copyright 2008 Sandia Corporation.
17   Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
18   the U.S. Government retains certain rights in this software.
19 -------------------------------------------------------------------------*/
20 // .NAME vtkVariantArray - An array holding vtkVariants.
21 //
22 // .SECTION Description
23 //
24 // .SECTION Thanks
25 // Thanks to Patricia Crossno, Ken Moreland, Andrew Wilson and Brian Wylie from
26 // Sandia National Laboratories for their help in developing this class.
27 
28 
29 #ifndef vtkVariantArray_h
30 #define vtkVariantArray_h
31 
32 #include "vtkCommonCoreModule.h" // For export macro
33 #include "vtkAbstractArray.h"
34 #include "vtkVariant.h" // For variant type
35 
36 class vtkVariantArrayLookup;
37 
38 //BTX
39 /// Forward declaration required for Boost serialization
40 namespace boost { namespace serialization { class access; } }
41 //ETX
42 
43 class VTKCOMMONCORE_EXPORT vtkVariantArray : public vtkAbstractArray
44 {
45 //BTX
46   /// Friendship required for Boost serialization
47   friend class boost::serialization::access;
48 //ETX
49 
50 public:
51   static vtkVariantArray* New();
52   vtkTypeMacro(vtkVariantArray,vtkAbstractArray);
53   void PrintSelf(ostream& os, vtkIndent indent);
54 
55   //
56   // Functions required by vtkAbstractArray
57   //
58 
59   // Description:
60   // Allocate memory for this array. Delete old storage only if necessary.
61   // Note that ext is no longer used.
62   virtual int Allocate(vtkIdType sz, vtkIdType ext=1000);
63 
64   // Description:
65   // Release storage and reset array to initial state.
66   virtual void Initialize();
67 
68   // Description:
69   // Return the underlying data type. An integer indicating data type is
70   // returned as specified in vtkSetGet.h.
71   virtual int GetDataType();
72 
73   // Description:
74   // Return the size of the underlying data type.  For a bit, 1 is
75   // returned.  For string 0 is returned. Arrays with variable length
76   // components return 0.
77   virtual int GetDataTypeSize();
78 
79   // Description:
80   // Return the size, in bytes, of the lowest-level element of an
81   // array.  For vtkDataArray and subclasses this is the size of the
82   // data type.  For vtkStringArray, this is
83   // sizeof(vtkStdString::value_type), which winds up being
84   // sizeof(char).
85   virtual int GetElementComponentSize();
86 
87   // Description:
88   // Set the number of tuples (a component group) in the array. Note that
89   // this may allocate space depending on the number of components.
90   virtual void SetNumberOfTuples(vtkIdType number);
91 
92   // Description:
93   // Set the tuple at the ith location using the jth tuple in the source array.
94   // This method assumes that the two arrays have the same type
95   // and structure. Note that range checking and memory allocation is not
96   // performed; use in conjunction with SetNumberOfTuples() to allocate space.
97   virtual void SetTuple(vtkIdType i, vtkIdType j, vtkAbstractArray* source);
98 
99   // Description:
100   // Insert the jth tuple in the source array, at ith location in this array.
101   // Note that memory allocation is performed as necessary to hold the data.
102   virtual void InsertTuple(vtkIdType i, vtkIdType j, vtkAbstractArray* source);
103 
104   // Description:
105   // Copy the tuples indexed in srcIds from the source array to the tuple
106   // locations indexed by dstIds in this array.
107   // Note that memory allocation is performed as necessary to hold the data.
108   virtual void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds,
109                             vtkAbstractArray *source);
110 
111   // Description:
112   // Copy n consecutive tuples starting at srcStart from the source array to
113   // this array, starting at the dstStart location.
114   // Note that memory allocation is performed as necessary to hold the data.
115   virtual void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart,
116                             vtkAbstractArray* source);
117 
118   // Description:
119   // Insert the jth tuple in the source array, at the end in this array.
120   // Note that memory allocation is performed as necessary to hold the data.
121   // Returns the location at which the data was inserted.
122   virtual vtkIdType InsertNextTuple(vtkIdType j, vtkAbstractArray* source);
123 
124   // Description:
125   // Return a void pointer. For image pipeline interface and other
126   // special pointer manipulation.
127   virtual void *GetVoidPointer(vtkIdType id);
128 
129   // Description:
130   // Deep copy of data. Implementation left to subclasses, which
131   // should support as many type conversions as possible given the
132   // data type.
133   virtual void DeepCopy(vtkAbstractArray *da);
134 
135   // Description:
136   // Set the ith tuple in this array as the interpolated tuple value,
137   // given the ptIndices in the source array and associated
138   // interpolation weights.
139   // This method assumes that the two arrays are of the same type
140   // and strcuture.
141   virtual void InterpolateTuple(vtkIdType i, vtkIdList *ptIndices,
142     vtkAbstractArray* source,  double* weights);
143 
144   // Description
145   // Insert the ith tuple in this array as interpolated from the two values,
146   // p1 and p2, and an interpolation factor, t.
147   // The interpolation factor ranges from (0,1),
148   // with t=0 located at p1. This method assumes that the three arrays are of
149   // the same type. p1 is value at index id1 in source1, while, p2 is
150   // value at index id2 in source2.
151   virtual void InterpolateTuple(vtkIdType i,
152     vtkIdType id1, vtkAbstractArray* source1,
153     vtkIdType id2, vtkAbstractArray* source2, double t);
154 
155   // Description:
156   // Free any unnecessary memory.
157   // Description:
158   // Resize object to just fit data requirement. Reclaims extra memory.
159   virtual void Squeeze();
160 
161   // Description:
162   // Resize the array while conserving the data.  Returns 1 if
163   // resizing succeeded and 0 otherwise.
164   virtual int Resize(vtkIdType numTuples);
165 
166   // Description:
167   // This method lets the user specify data to be held by the array.  The
168   // array argument is a pointer to the data.  size is the size of
169   // the array supplied by the user.  Set save to 1 to keep the class
170   // from deleting the array when it cleans up or reallocates memory.
171   // The class uses the actual array provided; it does not copy the data
172   // from the supplied array.
173   virtual void SetVoidArray(void *arr,
174                             vtkIdType size,
175                             int save);
176 
177   // Description:
178   // Return the memory in kilobytes consumed by this data array. Used to
179   // support streaming and reading/writing data. The value returned is
180   // guaranteed to be greater than or equal to the memory required to
181   // actually represent the data represented by this object. The
182   // information returned is valid only after the pipeline has
183   // been updated.
184   virtual unsigned long GetActualMemorySize();
185 
186   // Description:
187   // Since each item can be of a different type, we say that a variant array is not numeric.
188   virtual int IsNumeric();
189 
190   // Description:
191   // Subclasses must override this method and provide the right
192   // kind of templated vtkArrayIteratorTemplate.
193   virtual vtkArrayIterator* NewIterator();
194 
195   //
196   // Additional functions
197   //
198 
199   // Description:
200   // Get the data at a particular index.
201   vtkVariant & GetValue(vtkIdType id) const;
202 
203   // Description:
204   // Set the data at a particular index. Does not do range checking. Make sure
205   // you use the method SetNumberOfValues() before inserting data.
206   void SetValue(vtkIdType id, vtkVariant value);
207 
208   // Description:
209   // If id < GetNumberOfValues(), overwrite the array at that index.
210   // If id >= GetNumberOfValues(), expand the array size to id+1
211   // and set the final value to the specified value.
212   void InsertValue(vtkIdType id, vtkVariant value);
213 
214   // Description:
215   // Insert a value into the array from a variant.
216   void SetVariantValue(vtkIdType idx, vtkVariant value);
217 
218   // Description:
219   // Expand the array by one and set the value at that location.
220   // Return the array index of the inserted value.
221   vtkIdType InsertNextValue(vtkVariant value);
222 
223   //BTX
224   // Description:
225   // Return a pointer to the location in the internal array at the specified index.
226   vtkVariant* GetPointer(vtkIdType id);
227 
228   // Description:
229   // Set the internal array used by this object.
230   void SetArray(vtkVariant* arr, vtkIdType size, int save);
231   //ETX
232 
233   // Description:
234   // Specify the number of values for this object to hold. Does an
235   // allocation as well as setting the MaxId ivar. Used in conjunction with
236   // SetValue() method for fast insertion.
237   void SetNumberOfValues(vtkIdType number);
238 
239   // Description:
240   // Return the number of values in the array.
GetNumberOfValues()241   vtkIdType GetNumberOfValues() { return this->MaxId + 1; }
242 
243   // Description:
244   // Return the indices where a specific value appears.
245   virtual vtkIdType LookupValue(vtkVariant value);
246   virtual void LookupValue(vtkVariant value, vtkIdList* ids);
247 
248   // Description:
249   // Tell the array explicitly that the data has changed.
250   // This is only necessary to call when you modify the array contents
251   // without using the array's API (i.e. you retrieve a pointer to the
252   // data and modify the array contents).  You need to call this so that
253   // the fast lookup will know to rebuild itself.  Otherwise, the lookup
254   // functions will give incorrect results.
255   virtual void DataChanged();
256 
257   // Description:
258   // Tell the array explicitly that a single data element has
259   // changed. Like DataChanged(), then is only necessary when you
260   // modify the array contents without using the array's API.
261   virtual void DataElementChanged(vtkIdType id);
262 
263   // Description:
264   // Delete the associated fast lookup data structure on this array,
265   // if it exists.  The lookup will be rebuilt on the next call to a lookup
266   // function.
267   virtual void ClearLookup();
268 
269   // Description:
270   // This destructor is public to work around a bug in version 1.36.0 of
271   // the Boost.Serialization library.
272   ~vtkVariantArray();
273 
274 protected:
275   // Construct object with default tuple dimension (number of components) of 1.
276   vtkVariantArray();
277 
278   // Pointer to data
279   //BTX
280   vtkVariant* Array;
281 
282   // Function to resize data
283   vtkVariant* ResizeAndExtend(vtkIdType sz);
284   //ETX
285 
286   int SaveUserArray;
287 
288 private:
289   vtkVariantArray(const vtkVariantArray&);  // Not implemented.
290   void operator=(const vtkVariantArray&);  // Not implemented.
291 
292   vtkVariantArrayLookup* Lookup;
293   void UpdateLookup();
294 };
295 
296 #endif
297