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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkUnicodeStringArray.cxx
Language: C++
Copyright 2004 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
license for use of this work by or on behalf of the
U.S. Government. Redistribution and use in source and binary forms, with
or without modification, are permitted provided that this Notice and any
statement of authorship are reproduced on all copies.
=========================================================================*/
#include "vtkUnicodeString.h"
#include "vtkArrayIteratorTemplate.h"
#include "vtkIdList.h"
#include "vtkObjectFactory.h"
#include "vtkUnicodeStringArray.h"
#include <vector>
#include <algorithm>
class vtkUnicodeStringArray::Implementation
{
public:
typedef std::vector<vtkUnicodeString> StorageT;
StorageT Storage;
};
vtkStandardNewMacro(vtkUnicodeStringArray);
vtkUnicodeStringArray::vtkUnicodeStringArray()
{
this->Internal = new Implementation;
}
vtkUnicodeStringArray::~vtkUnicodeStringArray()
{
delete this->Internal;
}
void vtkUnicodeStringArray::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
int vtkUnicodeStringArray::Allocate(vtkIdType sz, vtkIdType)
{
this->Internal->Storage.reserve(sz);
this->DataChanged();
return 1;
}
void vtkUnicodeStringArray::Initialize()
{
this->Internal->Storage.clear();
this->DataChanged();
}
int vtkUnicodeStringArray::GetDataType()
{
return VTK_UNICODE_STRING;
}
int vtkUnicodeStringArray::GetDataTypeSize()
{
return 0;
}
int vtkUnicodeStringArray::GetElementComponentSize()
{
return sizeof(vtkUnicodeString::value_type);
}
void vtkUnicodeStringArray::SetNumberOfTuples(vtkIdType number)
{
this->Internal->Storage.resize(number);
this->DataChanged();
}
void vtkUnicodeStringArray::SetTuple(vtkIdType i, vtkIdType j, vtkAbstractArray* source)
{
vtkUnicodeStringArray* const array = vtkArrayDownCast<vtkUnicodeStringArray>(source);
if(!array)
{
vtkWarningMacro("Input and output array data types do not match.");
return;
}
this->Internal->Storage[i] = array->Internal->Storage[j];
this->DataChanged();
}
void vtkUnicodeStringArray::InsertTuple(vtkIdType i, vtkIdType j, vtkAbstractArray* source)
{
vtkUnicodeStringArray* const array = vtkArrayDownCast<vtkUnicodeStringArray>(source);
if(!array)
{
vtkWarningMacro("Input and output array data types do not match.");
return;
}
if(static_cast<vtkIdType>(this->Internal->Storage.size()) <= i)
this->Internal->Storage.resize(i + 1);
this->Internal->Storage[i] = array->Internal->Storage[j];
this->DataChanged();
}
void vtkUnicodeStringArray::InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds,
vtkAbstractArray *source)
{
vtkUnicodeStringArray* const array =
vtkArrayDownCast<vtkUnicodeStringArray>(source);
if(!array)
{
vtkWarningMacro("Input and output array data types do not match.");
return;
}
vtkIdType numIds = dstIds->GetNumberOfIds();
if (srcIds->GetNumberOfIds() != numIds)
{
vtkWarningMacro("Input and output id array sizes do not match.");
return;
}
// Find maximum destination id and resize if needed
vtkIdType maxDstId = 0;
for (vtkIdType idIndex = 0; idIndex < numIds; ++idIndex)
{
maxDstId = std::max(maxDstId, dstIds->GetId(idIndex));
}
if (static_cast<vtkIdType>(this->Internal->Storage.size()) <= maxDstId)
{
this->Internal->Storage.resize(maxDstId + 1);
}
// Copy data
for (vtkIdType idIndex = 0; idIndex < numIds; ++idIndex)
{
this->Internal->Storage[dstIds->GetId(idIndex)] =
array->Internal->Storage[srcIds->GetId(idIndex)];
}
this->DataChanged();
}
//------------------------------------------------------------------------------
void vtkUnicodeStringArray::InsertTuples(vtkIdType dstStart, vtkIdType n,
vtkIdType srcStart,
vtkAbstractArray *source)
{
vtkUnicodeStringArray* sa = vtkArrayDownCast<vtkUnicodeStringArray>(source);
if (!sa)
{
vtkWarningMacro("Input and outputs array data types do not match.");
return ;
}
if (this->NumberOfComponents != source->GetNumberOfComponents())
{
vtkWarningMacro("Input and output component sizes do not match.");
return;
}
vtkIdType srcEnd = srcStart + n;
if (srcEnd > source->GetNumberOfTuples())
{
vtkWarningMacro("Source range exceeds array size (srcStart=" << srcStart
<< ", n=" << n << ", numTuples="
<< source->GetNumberOfTuples() << ").");
return;
}
for (vtkIdType i = 0; i < n; ++i)
{
vtkIdType numComp = this->NumberOfComponents;
vtkIdType srcLoc = (srcStart + i) * this->NumberOfComponents;
vtkIdType dstLoc = (dstStart + i) * this->NumberOfComponents;
while (numComp-- > 0)
{
this->InsertValue(dstLoc++, sa->GetValue(srcLoc++));
}
}
this->DataChanged();
}
vtkIdType vtkUnicodeStringArray::InsertNextTuple(vtkIdType j, vtkAbstractArray* source)
{
vtkUnicodeStringArray* const array = vtkArrayDownCast<vtkUnicodeStringArray>(source);
if(!array)
{
vtkWarningMacro("Input and output array data types do not match.");
return 0;
}
this->Internal->Storage.push_back(array->Internal->Storage[j]);
this->DataChanged();
return static_cast<vtkIdType>(this->Internal->Storage.size()) - 1;
}
void* vtkUnicodeStringArray::GetVoidPointer(vtkIdType id)
{
// Err.. not totally sure what to do here
if (this->Internal->Storage.empty())
return 0;
else
return &this->Internal->Storage[id];
}
void vtkUnicodeStringArray::DeepCopy(vtkAbstractArray* da)
{
if(!da)
return;
if(this == da)
return;
vtkUnicodeStringArray* const array = vtkArrayDownCast<vtkUnicodeStringArray>(da);
if(!array)
{
vtkWarningMacro("Input and output array data types do not match.");
return;
}
this->Internal->Storage = array->Internal->Storage;
this->DataChanged();
}
void vtkUnicodeStringArray::InterpolateTuple(vtkIdType i, vtkIdList *ptIndices,
vtkAbstractArray* source, double* weights)
{
if(this->GetDataType() != source->GetDataType())
{
vtkErrorMacro("Cannot CopyValue from array of type "
<< source->GetDataTypeAsString());
return;
}
if (ptIndices->GetNumberOfIds() == 0)
{
// nothing to do.
return;
}
// We use nearest neighbour for interpolating strings.
// First determine which is the nearest neighbour using the weights-
// it's the index with maximum weight.
vtkIdType nearest = ptIndices->GetId(0);
double max_weight = weights[0];
for (int k=1; k < ptIndices->GetNumberOfIds(); k++)
{
if (weights[k] > max_weight)
{
nearest = ptIndices->GetId(k);
max_weight = weights[k];
}
}
this->InsertTuple(i, nearest, source);
}
void vtkUnicodeStringArray::InterpolateTuple(vtkIdType i,
vtkIdType id1, vtkAbstractArray* source1,
vtkIdType id2, vtkAbstractArray* source2, double t)
{
if (source1->GetDataType() != this->GetDataType() ||
source2->GetDataType() != this->GetDataType())
{
vtkErrorMacro("All arrays to InterpolateValue() must be of same type.");
return;
}
if (t >= 0.5)
{
// Use p2
this->InsertTuple(i, id2, source2);
}
else
{
// Use p1.
this->InsertTuple(i, id1, source1);
}
}
void vtkUnicodeStringArray::Squeeze()
{
Implementation::StorageT(this->Internal->Storage).swap(this->Internal->Storage);
this->DataChanged();
}
int vtkUnicodeStringArray::Resize(vtkIdType numTuples)
{
this->Internal->Storage.resize(numTuples);
this->DataChanged();
return 1;
}
void vtkUnicodeStringArray::SetVoidArray(void*, vtkIdType, int)
{
vtkErrorMacro("Not implemented.");
}
void vtkUnicodeStringArray::SetVoidArray(void*, vtkIdType, int, int)
{
vtkErrorMacro("Not implemented.");
}
unsigned long vtkUnicodeStringArray::GetActualMemorySize()
{
unsigned long count = 0;
for(Implementation::StorageT::size_type i = 0; i != this->Internal->Storage.size(); ++i)
{
count += static_cast<unsigned long>(this->Internal->Storage[i].byte_count());
count += static_cast<unsigned long>(sizeof(vtkUnicodeString));
}
return count;
}
int vtkUnicodeStringArray::IsNumeric()
{
return 0;
}
vtkArrayIterator* vtkUnicodeStringArray::NewIterator()
{
vtkErrorMacro("Not implemented.");
return 0;
}
vtkVariant vtkUnicodeStringArray::GetVariantValue(vtkIdType idx)
{
return this->GetValue(idx);
}
vtkIdType vtkUnicodeStringArray::LookupValue(vtkVariant value)
{
const vtkUnicodeString search_value = value.ToUnicodeString();
for(Implementation::StorageT::size_type i = 0; i != this->Internal->Storage.size(); ++i)
{
if(this->Internal->Storage[i] == search_value)
return i;
}
return -1;
}
void vtkUnicodeStringArray::LookupValue(vtkVariant value, vtkIdList* ids)
{
const vtkUnicodeString search_value = value.ToUnicodeString();
ids->Reset();
for(Implementation::StorageT::size_type i = 0; i != this->Internal->Storage.size(); ++i)
{
if(this->Internal->Storage[i] == search_value)
ids->InsertNextId(i);
}
}
void vtkUnicodeStringArray::SetVariantValue(vtkIdType id, vtkVariant value)
{
this->SetValue( id, value.ToUnicodeString() );
}
void vtkUnicodeStringArray::InsertVariantValue(vtkIdType id, vtkVariant value)
{
this->InsertValue( id, value.ToUnicodeString() );
}
void vtkUnicodeStringArray::DataChanged()
{
this->MaxId = static_cast<vtkIdType>(this->Internal->Storage.size()) - 1;
}
void vtkUnicodeStringArray::ClearLookup()
{
}
vtkIdType vtkUnicodeStringArray::InsertNextValue(const vtkUnicodeString& value)
{
this->Internal->Storage.push_back(value);
this->DataChanged();
return static_cast<vtkIdType>(this->Internal->Storage.size()) - 1;
}
void vtkUnicodeStringArray::InsertValue(vtkIdType i, const vtkUnicodeString& value)
{
// Range check
if(static_cast<vtkIdType>(this->Internal->Storage.size()) <= i)
this->Internal->Storage.resize(i + 1);
this->SetValue(i, value);
}
void vtkUnicodeStringArray::SetValue(vtkIdType i, const vtkUnicodeString& value)
{
this->Internal->Storage[i] = value;
this->DataChanged();
}
vtkUnicodeString& vtkUnicodeStringArray::GetValue(vtkIdType i)
{
return this->Internal->Storage[i];
}
void vtkUnicodeStringArray::InsertNextUTF8Value(const char* value)
{
this->InsertNextValue(vtkUnicodeString::from_utf8(value));
}
void vtkUnicodeStringArray::SetUTF8Value(vtkIdType i, const char* value)
{
this->SetValue(i, vtkUnicodeString::from_utf8(value));
}
const char* vtkUnicodeStringArray::GetUTF8Value(vtkIdType i)
{
return this->Internal->Storage[i].utf8_str();
}
|