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
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestSortDataArray.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.
=========================================================================*/
/*
* 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.
*/
// -*- c++ -*- *******************************************************
#include "vtkSortDataArray.h"
#include "vtkIntArray.h"
#include "vtkFloatArray.h"
#include "vtkStringArray.h"
#include "vtkIdList.h"
#include "vtkMath.h"
#include "vtkTimerLog.h"
#include <sstream>
#include <locale> // C++ locale
#define ARRAY_SIZE (2*1024*1024)
// #define ARRAY_SIZE 128
int TestSortDataArray(int, char *[])
{
vtkIdType i;
vtkTimerLog *timer = vtkTimerLog::New();
int retVal = 0;
//---------------------------------------------------------------------------
// Sort data array
cout << "Building array----------" << endl;
vtkIntArray *keys = vtkIntArray::New();
keys->SetNumberOfComponents(1);
keys->SetNumberOfTuples(ARRAY_SIZE);
for (i = 0; i < ARRAY_SIZE; i++)
{
keys->SetComponent(i,0,static_cast<int>(vtkMath::Random(0, ARRAY_SIZE*4)));
}
cout << "Sorting array" << endl;
timer->StartTimer();
vtkSortDataArray::Sort(keys);
timer->StopTimer();
cout << "Time to sort array: " << timer->GetElapsedTime() << " sec" << endl;
for (i = 0; i < ARRAY_SIZE-1; i++)
{
if (keys->GetComponent(i, 0) > keys->GetComponent(i+1, 0))
{
cout << "Array not properly sorted!" << endl;
retVal = 1;
break;
}
}
cout << "Array consistency check finished\n" << endl;
cout << "Sorting sorted array" << endl;
timer->StartTimer();
vtkSortDataArray::Sort(keys);
timer->StopTimer();
cout << "Time to sort array: " << timer->GetElapsedTime() << " sec" << endl;
for (i = 0; i < ARRAY_SIZE-1; i++)
{
if (keys->GetComponent(i, 0) > keys->GetComponent(i+1, 0))
{
cout << "Array not properly sorted!" << endl;
retVal = 1;
break;
}
}
cout << "Array consistency check finished\n" << endl;
//---------------------------------------------------------------------------
// Sort id list (ascending)
cout << "Building id list (ascending order)----------" << endl;
vtkIdList *ids = vtkIdList::New();
ids->SetNumberOfIds(ARRAY_SIZE);
for (i = 0; i < ARRAY_SIZE; i++)
{
ids->SetId(i,static_cast<vtkIdType>(vtkMath::Random(0, ARRAY_SIZE*4)));
}
cout << "Sorting ids" << endl;
timer->StartTimer();
vtkSortDataArray::Sort(ids);
timer->StopTimer();
cout << "Time to sort ids: " << timer->GetElapsedTime() << " sec" << endl;
for (i = 0; i < ARRAY_SIZE-1; i++)
{
if (ids->GetId(i) > ids->GetId(i+1))
{
cout << "Id list not properly sorted!" << endl;
retVal = 1;
break;
}
}
cout << "Id list consistency check finished\n" << endl;
//---------------------------------------------------------------------------
// Sort id list (descending)
cout << "Building id list (descending order)----------" << endl;
ids->SetNumberOfIds(ARRAY_SIZE);
for (i = 0; i < ARRAY_SIZE; i++)
{
ids->SetId(i,static_cast<vtkIdType>(vtkMath::Random(0, ARRAY_SIZE*4)));
}
cout << "Sorting ids" << endl;
timer->StartTimer();
vtkSortDataArray::Sort(ids,1);
timer->StopTimer();
cout << "Time to sort ids: " << timer->GetElapsedTime() << " sec" << endl;
for (i = 0; i < ARRAY_SIZE-1; i++)
{
if (ids->GetId(i) < ids->GetId(i+1))
{
cout << "Id list not properly sorted!" << endl;
retVal = 1;
break;
}
}
cout << "Id list consistency check finished\n" << endl;
//---------------------------------------------------------------------------
// Sort key/value pairs
cout << "Building key/value arrays----------\n" << endl;
vtkIntArray *values = vtkIntArray::New();
values->SetNumberOfComponents(2);
values->SetNumberOfTuples(ARRAY_SIZE);
for (i = 0; i < ARRAY_SIZE; i++)
{
keys->SetComponent(i,0,static_cast<int>(vtkMath::Random(0, ARRAY_SIZE*4)));
values->SetComponent(i, 0, i);
values->SetComponent(i,1,
static_cast<int>(vtkMath::Random(0, ARRAY_SIZE*4)));
}
vtkIntArray *saveKeys = vtkIntArray::New();
saveKeys->DeepCopy(keys);
vtkIntArray *saveValues = vtkIntArray::New();
saveValues->DeepCopy(values);
cout << "Sorting arrays" << endl;
timer->StartTimer();
vtkSortDataArray::Sort(keys, values);
timer->StopTimer();
cout << "Time to sort array: " << timer->GetElapsedTime() << " sec" << endl;
for (i = 0; i < ARRAY_SIZE-1; i++)
{
int lookup = static_cast<int>(values->GetComponent(i, 0));
if (keys->GetComponent(i, 0) > keys->GetComponent(i+1, 0))
{
cout << "Array not properly sorted!" << endl;
retVal = 1;
break;
}
if (keys->GetComponent(i, 0) != saveKeys->GetComponent(lookup, 0))
{
cout << "Values array not consistent with keys array!" << endl;
retVal = 1;
break;
}
if (values->GetComponent(i, 1) != saveValues->GetComponent(lookup, 1))
{
cout << "Values array not consistent with keys array!" << endl;
retVal = 1;
break;
}
}
cout << "Array consistency check finished\n" << endl;
cout << "Sorting sorted arrays" << endl;
timer->StartTimer();
vtkSortDataArray::Sort(keys, values);
timer->StopTimer();
cout << "Time to sort array: " << timer->GetElapsedTime() << " sec" << endl;
for (i = 0; i < ARRAY_SIZE-1; i++)
{
int lookup = static_cast<int>(values->GetComponent(i, 0));
if (keys->GetComponent(i, 0) > keys->GetComponent(i+1, 0))
{
cout << "Array not properly sorted!" << endl;
retVal = 1;
break;
}
if (keys->GetComponent(i, 0) != saveKeys->GetComponent(lookup, 0))
{
cout << "Values array not consistent with keys array!" << endl;
retVal = 1;
break;
}
if (values->GetComponent(i, 1) != saveValues->GetComponent(lookup, 1))
{
cout << "Values array not consistent with keys array!" << endl;
retVal = 1;
break;
}
}
cout << "Array consistency check finished\n" << endl;
//---------------------------------------------------------------------------
// Sort data array on component value pairs
cout << "Building data array----------\n" << endl;
vtkFloatArray *fvalues = vtkFloatArray::New();
fvalues->SetNumberOfComponents(3);
fvalues->SetNumberOfTuples(ARRAY_SIZE);
for (i = 0; i < ARRAY_SIZE; i++)
{
fvalues->SetComponent(i,0, i);
fvalues->SetComponent(i,1, static_cast<float>(vtkMath::Random(0, ARRAY_SIZE*4)));
fvalues->SetComponent(i,2, i);
}
vtkFloatArray *saveFValues = vtkFloatArray::New();
saveFValues->DeepCopy(fvalues);
cout << "Sorting data array with component #1" << endl;
timer->StartTimer();
vtkSortDataArray::SortArrayByComponent(fvalues,1);
timer->StopTimer();
cout << "Time to sort data array: " << timer->GetElapsedTime() << " sec" << endl;
for (i = 0; i < ARRAY_SIZE-1; i++)
{
if (fvalues->GetComponent(i, 1) > fvalues->GetComponent(i+1, 1))
{
cout << "Data array sorted incorrectly!" << endl;
retVal = 1;
break;
}
if (fvalues->GetComponent(i, 0) != fvalues->GetComponent(i, 2))
{
cout << "Data array tuples inconsistent!" << endl;
retVal = 1;
break;
}
}
cout << "Data array consistency check finished\n" << endl;
//---------------------------------------------------------------------------
// Sort string array
std::ostringstream ostr;
ostr.imbue(std::locale::classic());
cout << "Building string array----------\n" << endl;
vtkStringArray *sarray = vtkStringArray::New();
sarray->SetNumberOfTuples(ARRAY_SIZE);
for (i = 0; i < ARRAY_SIZE; ++i)
{
ostr.str(""); //clear it out
ostr << static_cast<int>(vtkMath::Random(0,ARRAY_SIZE*4));
sarray->SetValue(i,ostr.str());
}
cout << "Sorting string array" << endl;
timer->StartTimer();
vtkSortDataArray::Sort(sarray,1);
timer->StopTimer();
cout << "Time to sort strings: " << timer->GetElapsedTime() << " sec" << endl;
vtkStdString s1, s2;
for (i = 0; i < ARRAY_SIZE-1; ++i)
{
// s1 = std::stoi(sarray->GetValue(i));
// s2 = std::stoi(sarray->GetValue(i+1));
s1 = sarray->GetValue(i);
s2 = sarray->GetValue(i+1);
if ( s1 < s2 )
{
cout << "String array sorted incorrectly!" << endl;
retVal = 1;
break;
}
}
cout << "String array consistency check finished\n" << endl;
timer->Delete();
keys->Delete();
ids->Delete();
values->Delete();
fvalues->Delete();
saveKeys->Delete();
saveValues->Delete();
saveFValues->Delete();
sarray->Delete();
return retVal;
}
|