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 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
|
/*****************************************************************************/
/* XDMF */
/* eXtensible Data Model and Format */
/* */
/* Id : XdmfArrayType.cpp */
/* */
/* Author: */
/* Kenneth Leiter */
/* kenneth.leiter@arl.army.mil */
/* US Army Research Laboratory */
/* Aberdeen Proving Ground, MD */
/* */
/* Copyright @ 2011 US Army Research Laboratory */
/* All Rights Reserved */
/* See Copyright.txt 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 <sstream>
#include <utility>
#include <boost/assign.hpp>
#include "string.h"
#include "XdmfArrayType.hpp"
#include "XdmfError.hpp"
std::map<std::string, std::map<unsigned int ,shared_ptr<const XdmfArrayType>(*)()> >
XdmfArrayType::mArrayDefinitions;
// Supported XdmfArrayTypes
shared_ptr<const XdmfArrayType>
XdmfArrayType::Uninitialized()
{
static shared_ptr<const XdmfArrayType> p(new XdmfArrayType("None", 0, XdmfArrayType::Unsigned));
return p;
}
shared_ptr<const XdmfArrayType>
XdmfArrayType::Int8()
{
static shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Char", 1, XdmfArrayType::Signed));
return p;
}
shared_ptr<const XdmfArrayType>
XdmfArrayType::Int16()
{
static shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Short", 2, XdmfArrayType::Signed));
return p;
}
shared_ptr<const XdmfArrayType>
XdmfArrayType::Int32()
{
static shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Int", 4, XdmfArrayType::Signed));
return p;
}
shared_ptr<const XdmfArrayType>
XdmfArrayType::Int64()
{
static shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Int", 8, XdmfArrayType::Signed));
return p;
}
shared_ptr<const XdmfArrayType>
XdmfArrayType::Float32()
{
static shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Float", 4, XdmfArrayType::Float));
return p;
}
shared_ptr<const XdmfArrayType>
XdmfArrayType::Float64()
{
static shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Float", 8, XdmfArrayType::Float));
return p;
}
shared_ptr<const XdmfArrayType>
XdmfArrayType::UInt8()
{
static shared_ptr<const XdmfArrayType> p(new XdmfArrayType("UChar", 1, XdmfArrayType::Unsigned));
return p;
}
shared_ptr<const XdmfArrayType>
XdmfArrayType::UInt16()
{
static shared_ptr<const XdmfArrayType> p(new XdmfArrayType("UShort", 2, XdmfArrayType::Unsigned));
return p;
}
shared_ptr<const XdmfArrayType>
XdmfArrayType::UInt32()
{
static shared_ptr<const XdmfArrayType> p(new XdmfArrayType("UInt", 4, XdmfArrayType::Unsigned));
return p;
}
shared_ptr<const XdmfArrayType>
XdmfArrayType::String()
{
static shared_ptr<const XdmfArrayType> p(new XdmfArrayType("String", 0, XdmfArrayType::Unsigned));
return p;
}
void
XdmfArrayType::InitTypes()
{
mArrayDefinitions["NONE"][0] = Uninitialized;
mArrayDefinitions["CHAR"][1] = Int8;
mArrayDefinitions["SHORT"][2] = Int16;
mArrayDefinitions["INT"][4] = Int32;
mArrayDefinitions["INT"][8] = Int64;
mArrayDefinitions["FLOAT"][4] = Float32;
mArrayDefinitions["FLOAT"][8] = Float64;
mArrayDefinitions["UCHAR"][1] = UInt8;
mArrayDefinitions["USHORT"][2] = UInt16;
mArrayDefinitions["UINT"][4] = UInt32;
mArrayDefinitions["STRING"][0] = String;
}
XdmfArrayType::XdmfArrayType(const std::string & name,
const unsigned int precision,
const Format typeFormat) :
mName(name),
mPrecision(precision),
mTypeFormat(typeFormat)
{
std::stringstream precisionString;
precisionString << precision;
mPrecisionString = precisionString.str();
}
XdmfArrayType::~XdmfArrayType()
{
}
shared_ptr<const XdmfArrayType>
XdmfArrayType::New(const std::map<std::string, std::string> & itemProperties)
{
InitTypes();
std::map<std::string, std::string>::const_iterator type =
itemProperties.find("DataType");
if(type == itemProperties.end()) {
type = itemProperties.find("NumberType");
}
if(type == itemProperties.end()) {
// to support old xdmf defaults, return Float32()
return Float32();
}
const std::string & typeVal = ConvertToUpper(type->second);
std::map<std::string, std::string>::const_iterator precision =
itemProperties.find("Precision");
const unsigned int precisionVal =
(precision == itemProperties.end()) ? 0 : atoi(precision->second.c_str());
std::map<std::string, std::map<unsigned int ,shared_ptr<const XdmfArrayType>(*)()> >::const_iterator returnType = mArrayDefinitions.find(typeVal);
if (returnType == mArrayDefinitions.end()) {
XdmfError::message(XdmfError::FATAL,
"Type not one of accepted values: " + typeVal +
" in XdmfArrayType::New");
}
else {
std::map<unsigned int ,shared_ptr<const XdmfArrayType>(*)()>::const_iterator returnPrecision = returnType->second.find(precisionVal);
// If only one precision is available, assume that if not specified.
if (returnType->second.size() == 1 && precisionVal == 0)
{
return (*((returnType->second.begin())->second))();
}
if (returnPrecision == returnType->second.end()) {
// Default to 32bit types if not specified otherwise
returnPrecision = returnType->second.find(4);
}
if (returnPrecision == returnType->second.end()) {
std::string errorVal = "";
if (precision == itemProperties.end()) {
errorVal = "0";
}
else {
errorVal = precision->second;
}
XdmfError::message(XdmfError::FATAL,
"Type not one of accepted precision: " + errorVal +
" in XdmfArrayType::New");
}
else {
return (*(returnPrecision->second))();
}
}
XdmfError::message(XdmfError::FATAL,
"Type not one of accepted values: " + typeVal +
" in XdmfArrayType::New");
// unreachable
return shared_ptr<const XdmfArrayType>();
}
shared_ptr<const XdmfArrayType>
XdmfArrayType::comparePrecision(shared_ptr<const XdmfArrayType> type1,
shared_ptr<const XdmfArrayType> type2)
{
std::string type1Name = type1->getName();
std::string type2Name = type2->getName();
if (type2Name.compare(type1Name) == 0) {
if (type1->getElementSize() >= type2->getElementSize()) {
return type1;
}
else {
return type2;
}
}
bool firstIsSigned = false;
if (type1Name.compare("UChar") != 0 &&
type1Name.compare("UShort") != 0 &&
type1Name.compare("UInt") != 0) {
firstIsSigned = true;
}
bool secondIsSigned = false;
if (type2Name.compare("UChar") != 0 &&
type2Name.compare("UShort") != 0 &&
type2Name.compare("UInt") != 0) {
secondIsSigned = true;
}
std::map<std::string, int> controlmap;
controlmap["Char"] = 1;
controlmap["UChar"] = 2;
controlmap["Short"] = 3;
controlmap["UShort"] = 4;
controlmap["Int"] = 5;
controlmap["UInt"] = 6;
controlmap["Float"] = 7;
controlmap["String"] = 8;
int control = controlmap[type1Name];
// In this switch the starting location is determined by
// the first type and then the algorithm cascades
// until it finds the second type
switch (control) {
case 1:
// Char
case 2:
// UChar
if (type2Name.compare("Char") == 0 ||
type2Name.compare("UChar") == 0) {
// This statement would be called in the case
// where there is a mixed type of Char and UChar
// The resulting type should be a Short
return Int16();
}
case 3:
// Short
if (type2Name.compare("Char") == 0 ||
type2Name.compare("UChar") == 0 ||
type2Name.compare("Short") == 0) {
// This will be called for any combination of
// Char/UChar and Short
// In all of these cases the result shoule be a Short
return Int16();
}
case 4:
// UShort
if (type2Name.compare("Char") == 0 ||
type2Name.compare("Short") == 0) {
// When mixing UShort with a signed type that has a lower precision
// the resulting type should be an int
return Int32();
}
else if (type2Name.compare("UChar") == 0 ||
type2Name.compare("UShort") == 0) {
// When mixing UShort with an unsigned type that has a lower precision
// a Ushort should be the resulting type
if (!firstIsSigned) {
return UInt16();
}
else {
return Int32();
}
}
case 5:
// Int
if (type2Name.compare("Int") != 0 &&
type2Name.compare("UInt") != 0 &&
type2Name.compare("Float") != 0 &&
type2Name.compare("String") != 0) {
// When mixing an Int with a type of lower precision
// the resulting type should match the Int's precision
if (type1->getElementSize() == 4) {
return Int32();
}
else {
return Int64();
}
}
if (type2Name.compare("Int") == 0) {
if (type2->getElementSize() == 4) {
return Int32();
}
else {
return Int64();
}
}
case 6:
// UInt
if (type2Name.compare("UInt") != 0 &&
type2Name.compare("Int") != 0 &&
type2Name.compare("Float") != 0 &&
type2Name.compare("String") != 0) {
// When mixing UInt with another non-floating-point type
// the result should be either long or unsigned int
// depending on the if the mixed type is signed or not
if (!secondIsSigned) {
return UInt32();
}
else {
return Int64();
}
}
else if (type2Name.compare("UInt") == 0) {
if (firstIsSigned) {
return Int64();
}
else {
return UInt32();
}
}
else if (type2Name.compare("Int") == 0) {
return Int64();
}
case 7:
// Float
if (type2Name.compare("String") != 0 &&
type2Name.compare("Float") != 0 &&
type2Name.compare("UInt") != 0) {
// String is the only type that has priority over a float
// This case occurs when type1 is a float
return type1;
}
else if (type2Name.compare("UInt") == 0) {
return Float64();
}
else if (type2Name.compare("Float") == 0) {
// Since there was a check earlier to see if the type names matched
// This is the case when type2 is a float
if (type1Name.compare("UInt") == 0) {
return Float64();
}
else {
return type2;
}
}
case 8:
// String
// String has priority over everything
return String();
default:
break;
}
// Double is the default value
// Should all of the above manage to fail to return a value
return Float64();
}
unsigned int
XdmfArrayType::getElementSize() const
{
return mPrecision;
}
std::string
XdmfArrayType::getName() const
{
return mName;
}
bool
XdmfArrayType::getIsFloat() const
{
if (mTypeFormat == XdmfArrayType::Float) {
return true;
}
else {
return false;
}
}
bool
XdmfArrayType::getIsSigned() const
{
if (mTypeFormat == XdmfArrayType::Float ||
mTypeFormat == XdmfArrayType::Signed) {
return true;
}
else {
return false;
}
}
void
XdmfArrayType::getProperties(std::map<std::string, std::string> & collectedProperties) const
{
collectedProperties.insert(std::make_pair("DataType", mName));
collectedProperties.insert(std::make_pair("Precision", mPrecisionString));
}
// C Wrappers
shared_ptr<const XdmfArrayType>
intToType(int type)
{
switch (type) {
case XDMF_ARRAY_TYPE_UINT8:
return XdmfArrayType::UInt8();
break;
case XDMF_ARRAY_TYPE_UINT16:
return XdmfArrayType::UInt16();
break;
case XDMF_ARRAY_TYPE_UINT32:
return XdmfArrayType::UInt32();
break;
case XDMF_ARRAY_TYPE_INT8:
return XdmfArrayType::Int8();
break;
case XDMF_ARRAY_TYPE_INT16:
return XdmfArrayType::Int16();
break;
case XDMF_ARRAY_TYPE_INT32:
return XdmfArrayType::Int32();
break;
case XDMF_ARRAY_TYPE_INT64:
return XdmfArrayType::Int64();
break;
case XDMF_ARRAY_TYPE_FLOAT32:
return XdmfArrayType::Float32();
break;
case XDMF_ARRAY_TYPE_FLOAT64:
return XdmfArrayType::Float64();
break;
default:
XdmfError::message(XdmfError::FATAL,
"Error: Invalid ArrayType.");
break;
}
return shared_ptr<const XdmfArrayType>();
}
int
typeToInt(shared_ptr<const XdmfArrayType> type)
{
std::string typeName = type->getName();
unsigned int typePrecision = type->getElementSize();
if (typeName == XdmfArrayType::UInt8()->getName())
{
return XDMF_ARRAY_TYPE_UINT8;
}
else if (typeName == XdmfArrayType::UInt16()->getName())
{
return XDMF_ARRAY_TYPE_UINT16;
}
else if (typeName == XdmfArrayType::UInt32()->getName())
{
return XDMF_ARRAY_TYPE_UINT32;
}
else if (typeName == XdmfArrayType::Int8()->getName())
{
return XDMF_ARRAY_TYPE_INT8;
}
else if (typeName == XdmfArrayType::Int16()->getName())
{
return XDMF_ARRAY_TYPE_INT16;
}
else if (typeName == XdmfArrayType::Int32()->getName() || typeName == XdmfArrayType::Int64()->getName())
{
if (typePrecision == 4)
{
return XDMF_ARRAY_TYPE_INT32;
}
else if (typePrecision == 8)
{
return XDMF_ARRAY_TYPE_INT64;
}
else
{
}
}
else if (typeName == XdmfArrayType::Float32()->getName() || typeName == XdmfArrayType::Float64()->getName())
{
if (typePrecision == 4)
{
return XDMF_ARRAY_TYPE_FLOAT32;
}
else if (typePrecision == 8)
{
return XDMF_ARRAY_TYPE_FLOAT64;
}
else
{
}
}
else if (typeName == XdmfArrayType::String()->getName())
{
//This shouldn't be used from C bindings
XdmfError::message(XdmfError::FATAL,
"Error: String type not usable from C.");
}
else
{
XdmfError::message(XdmfError::FATAL,
"Error: Invalid ArrayType.");
}
return -1;
}
int XdmfArrayTypeInt8()
{
return XDMF_ARRAY_TYPE_INT8;
}
int XdmfArrayTypeInt16()
{
return XDMF_ARRAY_TYPE_INT16;
}
int XdmfArrayTypeInt32()
{
return XDMF_ARRAY_TYPE_INT32;
}
int XdmfArrayTypeInt64()
{
return XDMF_ARRAY_TYPE_INT64;
}
int XdmfArrayTypeFloat32()
{
return XDMF_ARRAY_TYPE_FLOAT32;
}
int XdmfArrayTypeFloat64()
{
return XDMF_ARRAY_TYPE_FLOAT64;
}
int XdmfArrayTypeUInt8()
{
return XDMF_ARRAY_TYPE_UINT8;
}
int XdmfArrayTypeUInt16()
{
return XDMF_ARRAY_TYPE_UINT16;
}
int XdmfArrayTypeUInt32()
{
return XDMF_ARRAY_TYPE_UINT32;
}
int XdmfArrayTypeComparePrecision(int type1, int type2, int * status)
{
XDMF_ERROR_WRAP_START(status)
shared_ptr<const XdmfArrayType> tempType1 = intToType(type1);
shared_ptr<const XdmfArrayType> tempType2 = intToType(type2);
shared_ptr<const XdmfArrayType> returnType = XdmfArrayType::comparePrecision(tempType1, tempType2);
return typeToInt(returnType);
XDMF_ERROR_WRAP_END(status)
return -1;
}
int XdmfArrayTypeGetElementSize(int type, int * status)
{
XDMF_ERROR_WRAP_START(status)
return intToType(type)->getElementSize();
XDMF_ERROR_WRAP_END(status)
return 0;
}
int XdmfArrayTypeGetIsFloat(int type, int * status)
{
XDMF_ERROR_WRAP_START(status)
return intToType(type)->getIsFloat();
XDMF_ERROR_WRAP_END(status)
return false;
}
int XdmfArrayTypeGetIsSigned(int type, int * status)
{
XDMF_ERROR_WRAP_START(status)
return intToType(type)->getIsSigned();
XDMF_ERROR_WRAP_END(status)
return false;
}
char * XdmfArrayTypeGetName(int type, int * status)
{
XDMF_ERROR_WRAP_START(status)
char * returnPointer = strdup(intToType(type)->getName().c_str());
return returnPointer;
XDMF_ERROR_WRAP_END(status)
return NULL;
}
|