- Timestamp:
- Jan 23, 2007, 10:15:07 PM (18 years ago)
- Location:
- trunk/src
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/cee/RpUnitsCInterface.cc
r115 r568 66 66 67 67 return unit->getBasis(); 68 }69 70 int71 rpMakeMetric(const RpUnits* basis) {72 73 return RpUnits::makeMetric(basis);74 68 } 75 69 -
trunk/src/cee/RpUnitsCInterface.h
r554 r568 42 42 const RpUnits* rpFind ( const char* unitSymbol); 43 43 44 int rpMakeMetric ( const RpUnits* basis );45 46 44 // convert functions 47 45 -
trunk/src/core/RpDict.h
r544 r568 14 14 //#include <malloc.h> 15 15 16 #ifndef _RpDICT_H 16 #ifndef _RpDICT_H 17 17 #define _RpDICT_H 18 18 … … 34 34 class _Compare=std::equal_to<KeyType> > 35 35 class RpDictIterator; 36 37 36 38 /* 37 39 * RpDictEntry should not depend on _Compare, 38 * it was originally neede because RpDict is a friend class40 * it was originally needed because RpDict is a friend class 39 41 * then i needed to assign it a default value because RpUnits.cc 40 42 * uses it in its find function. RpUnits::find should really not … … 237 239 public: 238 240 241 typedef bool (*RpDictHint)(ValType); 239 242 240 243 // functionality for the user to access/adjust data members … … 250 253 set( KeyType& key, 251 254 ValType& value, 255 RpDictHint hint=NULL, 252 256 int *newPtr=NULL, 253 257 bool ci=false); … … 255 259 // find an RpUnits object that should exist in RpUnitsTable 256 260 // 257 /*virtual*/ RpDictEntry<KeyType,ValType,_Compare>& 258 find( KeyType& key ); 261 /*virtual*/ RpDictEntry<KeyType,ValType,_Compare>& 262 find( KeyType& key, 263 RpDictHint hint = NULL, 264 bool ci=false ); 259 265 260 266 /*virtual*/ RpDictEntry<KeyType,ValType,_Compare>& operator[]( KeyType& key) 261 267 { 262 return find(key );268 return find(key,NULL); 263 269 } 264 270 … … 356 362 // private member fxns 357 363 364 RpDictEntry<KeyType,ValType,_Compare>* 365 search( KeyType& key, RpDictHint hint = NULL, bool ci = false ); 366 358 367 // static void RpDict::RebuildTable (); 359 368 void RebuildTable (); … … 416 425 RpDict<KeyType,ValType,_Compare>::set( KeyType& key, 417 426 ValType& value, 427 RpDictHint hint, 418 428 int* newPtr, 419 429 bool ci ) … … 427 437 assert(&value); 428 438 429 // take care of the case where we are creating a NULL key entry. 439 hPtr = search(key,hint,ci); 440 if (hPtr != NULL) { 441 // adjust the new flag if it was provided 442 if (newPtr) { 443 *newPtr = 0; 444 } 445 446 // adjust the value if it was provided 447 // memory management is left as an exercise for the caller 448 if (&value) { 449 hPtr->setValue(value); 450 } 451 452 // return a reference to the dictionary object 453 return *this; 454 } 455 456 /* 457 * Entry not found. Add a new one to the bucket. 458 */ 459 430 460 if (&key) { 431 461 if (ci != oldCI) { … … 438 468 } 439 469 else { 470 // we are creating a NULL key entry. 440 471 hash = 0; 441 472 } 442 473 443 474 index = randomIndex(hash); 444 445 /*446 * Search all of the entries in the appropriate bucket.447 */448 for (hPtr = buckets[index]; hPtr != NULL; hPtr = hPtr->nextPtr) {449 if (hash != (unsigned int) hPtr->hash) {450 continue;451 }452 // if element already exists in hash, it should not be re-entered453 // if (key == *(hPtr->getKey())){454 if (_Compare()(key, *(hPtr->getKey()))){455 456 // adjust the new flag if it was provided457 if (newPtr) {458 *newPtr = 0;459 }460 461 // adjust the value if it was provided462 // memory management is left as an exercise for the caller463 if (&value) {464 hPtr->setValue(value);465 }466 467 // return a reference to the dictionary object468 return *this;469 }470 }471 472 /*473 * Entry not found. Add a new one to the bucket.474 */475 476 if (newPtr) {477 *newPtr = 1;478 }479 475 480 476 hPtr = new RpDictEntry<KeyType,ValType,_Compare>(key,value); … … 490 486 numEntries++; 491 487 488 if (newPtr) { 489 *newPtr = 1; 490 } 491 492 492 /* 493 493 * If the table has exceeded a decent size, rebuild it with many … … 503 503 } 504 504 505 506 /* 507 *---------------------------------------------------------------------- 508 * 509 * RpDict::find(const char *key) 505 /* 506 *---------------------------------------------------------------------- 507 * 508 * RpDict::find(KeyType& key, RpDictHint hint) 510 509 * 511 510 * Given a hash table find the entry with a matching key. … … 523 522 template <typename KeyType, typename ValType, class _Compare> 524 523 RpDictEntry<KeyType,ValType,_Compare>& 525 RpDict<KeyType,ValType,_Compare>::find(KeyType& key) 524 RpDict<KeyType,ValType,_Compare>::find( KeyType& key, 525 RpDictHint hint, 526 bool ci ) 526 527 { 527 528 RpDictEntry<KeyType,ValType,_Compare> *hPtr = NULL; 528 unsigned int hash = 0; 529 int index = 0; 530 531 assert(&key); 532 533 // take care of the case where we are creating a NULL key entry. 534 if (&key) { 535 hash = (unsigned int) hashFxn(&key); 536 } 537 else { 538 hash = 0; 539 } 540 541 index = randomIndex(hash); 542 543 /* 544 * Search all of the entries in the appropriate bucket. 545 */ 546 547 for (hPtr = buckets[index]; hPtr != NULL; hPtr = hPtr->nextPtr) { 548 if (hash != (unsigned int) hPtr->hash) { 549 continue; 550 } 551 // if (key == *(hPtr->getKey())) { 552 if (_Compare()(key, *(hPtr->getKey()))){ 553 // return a reference to the found object 554 return *hPtr; 555 } 529 530 hPtr = search(key,hint,ci); 531 532 if (hPtr != NULL) { 533 return *hPtr; 556 534 } 557 535 … … 560 538 // changing this entry's data members... what to do? 561 539 return *nullEntry; 540 } 541 542 /* 543 *---------------------------------------------------------------------- 544 * 545 * RpDict::search(KeyType& key, RpDictHint hint) 546 * 547 * Given a hash table find the entry with a matching key. 548 * 549 * Results: 550 * The return value is a token for the matching entry in the 551 * hash table, or NULL if there was no matching entry. 552 * 553 * Side effects: 554 * None. 555 * 556 *---------------------------------------------------------------------- 557 */ 558 559 template <typename KeyType, typename ValType, class _Compare> 560 RpDictEntry<KeyType,ValType,_Compare>* 561 RpDict<KeyType,ValType,_Compare>::search( KeyType& key, 562 RpDictHint hint, 563 bool ci ) 564 // bool ci, 565 // RpDictEntryList* entryList) 566 { 567 RpDictEntry<KeyType,ValType,_Compare> *hPtr = NULL; 568 unsigned int hash = 0; 569 int index = 0; 570 bool oldCI = caseInsensitive; 571 572 assert(&key); 573 574 // take care of the case where we are creating a NULL key entry. 575 if (&key) { 576 if (ci != oldCI) { 577 // toggle the case insensitivity of the dictionary 578 setCI(ci); 579 } 580 581 hash = (unsigned int) hashFxn(&key); 582 583 if (ci != oldCI) { 584 // reset the case insensitivity of the dictionary 585 setCI(oldCI); 586 } 587 } 588 else { 589 hash = 0; 590 } 591 592 index = randomIndex(hash); 593 594 /* 595 * Search all of the entries in the appropriate bucket. 596 */ 597 598 for (hPtr = buckets[index]; hPtr != NULL; hPtr = hPtr->nextPtr) { 599 if (hash != (unsigned int) hPtr->hash) { 600 continue; 601 } 602 if (_Compare()(key, *(hPtr->getKey()))) { 603 // check to see if the user provided a hint 604 if (hint != NULL ) { 605 // if there is a hint, run the potential return value 606 // throught the hint function. 607 if (hint(*(hPtr->getValue())) == true) { 608 // the hint approves of our choice of return values 609 // return a reference to the found object 610 return hPtr; 611 } 612 } 613 else { 614 // return a reference to the found object 615 return hPtr; 616 } 617 } 618 } 619 620 // return a reference to the null object 621 // find is not supposed to return a const, but i dont want the user 622 // changing this entry's data members... what to do? 623 return hPtr; 562 624 563 625 } … … 601 663 *************************************************************************/ 602 664 template <typename KeyType,typename ValType,class _Compare> 603 RpDictEntry<KeyType,ValType,_Compare>* 665 RpDictEntry<KeyType,ValType,_Compare>* 604 666 RpDictIterator<KeyType,ValType,_Compare>::first() 605 667 { … … 626 688 627 689 template <typename KeyType,typename ValType,class _Compare> 628 RpDictEntry<KeyType,ValType,_Compare>* 629 RpDictIterator<KeyType,ValType,_Compare>::next() 690 RpDictEntry<KeyType,ValType,_Compare>* 691 RpDictIterator<KeyType,ValType,_Compare>::next() 630 692 { 631 693 RpDictEntry<KeyType,ValType,_Compare>* hPtr = NULL; -
trunk/src/core/RpUnits.cc
r562 r568 7 7 * ====================================================================== 8 8 * AUTHOR: Derrick Kearney, Purdue University 9 * Copyright (c) 2004-200 5Purdue Research Foundation9 * Copyright (c) 2004-2007 Purdue Research Foundation 10 10 * 11 11 * See the file "license.terms" for information on usage and … … 35 35 const RpUnits* basis, 36 36 const std::string type, 37 bool metric, 37 38 bool caseInsensitive ) { 38 39 … … 45 46 double exponent = 1; 46 47 48 RpUnitsTypes::RpUnitsTypesHint hint = NULL; 49 47 50 if (units.empty()) { 48 51 // raise error, user sent null units! … … 57 60 58 61 // check to see if the said unit can already be found in the dictionary 59 if (RpUnits::find(units)) { 62 hint = RpUnitsTypes::getTypeHint(type); 63 if (RpUnits::find(units,hint)) { 60 64 return NULL; 61 65 } … … 83 87 } 84 88 85 newRpUnit = new RpUnits(sendStr, exponent, basis, type, caseInsensitive); 89 newRpUnit = new RpUnits( sendStr, 90 exponent, 91 basis, 92 type, 93 metric, 94 caseInsensitive ); 86 95 if (newRpUnit) { 87 96 insert(newRpUnit->getUnitsName(),newRpUnit); … … 171 180 */ 172 181 173 const RpUnits* 174 RpUnits::grabUnits ( std::string inStr, int* offset) { 175 176 const RpUnits* unit = NULL; 182 int 183 RpUnits::grabUnits ( std::string inStr, 184 int* offset, 185 const RpUnits** unit, 186 const RpUnits** prefix ) { 187 177 188 int len = inStr.length(); 189 std::string preStr = ""; 190 191 if ( (unit == NULL) || (prefix == NULL) ) { 192 // incorrect function call, return error 193 return -1; 194 } 195 196 *unit = NULL; 197 *prefix = NULL; 178 198 179 199 while ( ! inStr.empty() ) { 180 unit = RpUnits::find(inStr);181 if ( unit) {200 *unit = RpUnits::find(inStr,&RpUnitsTypes::hintTypeNonPrefix); 201 if (*unit) { 182 202 *offset = len - inStr.length(); 203 204 if ((*unit)->metric) { 205 RpUnits::checkMetricPrefix(preStr,offset,prefix); 206 } 207 183 208 break; 184 209 } 210 preStr = preStr + inStr.substr(0,1); 185 211 inStr.erase(0,1); 186 212 } 187 213 188 return unit; 189 } 190 214 return 0; 215 } 216 217 /**********************************************************************/ 218 // METHOD: checkMetrixPrefix() 219 /// Compare a string with available metric prefixes 220 /** 221 * The metric prefix only has one or two letters before the main unit. 222 * We take in the string of characters before the found unit and search 223 * for the two characters closest to the found unit. If those two 224 * characters are not found in the dictionary as a prefix, then we erase 225 * the 0th character, and search for the 1th character. The 1th character 226 * is the character closest to the found unit. If it is found as a prefix 227 * in the dictionary, it is returned. If no prefix is found, NULL is 228 * returned. 229 */ 230 231 int 232 RpUnits::checkMetricPrefix ( std::string inStr, 233 int* offset, 234 const RpUnits** prefix ) { 235 236 int inStrLen = 0; 237 std::string searchStr = ""; 238 239 inStrLen = inStr.length(); 240 241 if (inStrLen == 0) { 242 // no prefix to search for, exit 243 return 0; 244 } 245 246 if (prefix == NULL) { 247 // incorrect function call, return error 248 return -1; 249 } 250 251 252 if (inStrLen > 2) { 253 searchStr = inStr.substr( inStrLen-2 ); 254 } 255 else { 256 searchStr = inStr; 257 } 258 259 *prefix = NULL; 260 261 *prefix = RpUnits::find(searchStr,&RpUnitsTypes::hintTypePrefix); 262 if ( (*prefix) == NULL ) { 263 // the two letter prefix was not found, 264 // try the one letter prefix 265 searchStr.erase(0,1); 266 *prefix = RpUnits::find(searchStr,&RpUnitsTypes::hintTypePrefix); 267 } 268 269 if (*prefix != NULL) { 270 // if a prefix was found, adjust the offset to reflect 271 // the need to erase the prefix as well as the unit name 272 *offset = *offset - searchStr.length(); 273 } 274 275 return 0; 276 } 191 277 192 278 /**********************************************************************/ … … 563 649 564 650 /**********************************************************************/ 651 // METHOD: setMetric() 652 /// Set the metric flag of the object. 653 /** 654 * Set the metric flag of the object 655 */ 656 657 RpUnits& 658 RpUnits::setMetric(bool newVal) { 659 660 metric = newVal; 661 return *this; 662 } 663 664 /**********************************************************************/ 565 665 // METHOD: makeBasis() 566 666 /// Convert a value into its RpUnits's basis. … … 635 735 */ 636 736 737 /* 637 738 int 638 RpUnits::makeMetric( constRpUnits* basis) {739 RpUnits::makeMetric(RpUnits* basis) { 639 740 640 741 if (!basis) { … … 642 743 } 643 744 644 std::string basisName = basis->getUnitsName(); 645 std::string name; 646 647 name = "d" + basisName; 648 RpUnits * deci = RpUnits::define(name, basis, basis->type); 649 RpUnits::define(deci, basis, deci2base, base2deci); 650 651 name = "c" + basisName; 652 RpUnits * centi = RpUnits::define(name, basis, basis->type); 653 RpUnits::define(centi, basis, centi2base, base2centi); 654 655 name = "m" + basisName; 656 RpUnits * milli = RpUnits::define(name, basis, basis->type, 657 !RPUNITS_CASE_INSENSITIVE); 658 RpUnits::define(milli, basis, milli2base, base2milli); 659 660 name = "u" + basisName; 661 RpUnits * micro = RpUnits::define(name, basis, basis->type); 662 RpUnits::define(micro, basis, micro2base, base2micro); 663 664 name = "n" + basisName; 665 RpUnits * nano = RpUnits::define(name, basis, basis->type); 666 RpUnits::define(nano, basis, nano2base, base2nano); 667 668 name = "p" + basisName; 669 RpUnits * pico = RpUnits::define(name, basis, basis->type, 670 !RPUNITS_CASE_INSENSITIVE); 671 RpUnits::define(pico, basis, pico2base, base2pico); 672 673 name = "f" + basisName; 674 RpUnits * femto = RpUnits::define(name, basis, basis->type); 675 RpUnits::define(femto, basis, femto2base, base2femto); 676 677 name = "a" + basisName; 678 RpUnits * atto = RpUnits::define(name, basis, basis->type); 679 RpUnits::define(atto, basis, atto2base, base2atto); 680 681 name = "da" + basisName; 682 RpUnits * deca = RpUnits::define(name, basis, basis->type); 683 RpUnits::define(deca, basis, deca2base, base2deca); 684 685 name = "h" + basisName; 686 RpUnits * hecto = RpUnits::define(name, basis, basis->type); 687 RpUnits::define(hecto, basis, hecto2base, base2hecto); 688 689 name = "k" + basisName; 690 RpUnits * kilo = RpUnits::define(name, basis, basis->type); 691 RpUnits::define(kilo, basis, kilo2base, base2kilo); 692 693 name = "M" + basisName; 694 RpUnits * mega = RpUnits::define(name, basis, basis->type, 695 !RPUNITS_CASE_INSENSITIVE); 696 RpUnits::define(mega, basis, mega2base, base2mega); 697 698 name = "G" + basisName; 699 RpUnits * giga = RpUnits::define(name, basis, basis->type); 700 RpUnits::define(giga, basis, giga2base, base2giga); 701 702 name = "T" + basisName; 703 RpUnits * tera = RpUnits::define(name, basis, basis->type); 704 RpUnits::define(tera, basis, tera2base, base2tera); 705 706 name = "P" + basisName; 707 RpUnits * peta = RpUnits::define(name, basis, basis->type, 708 !RPUNITS_CASE_INSENSITIVE); 709 RpUnits::define(peta, basis, peta2base, base2peta); 710 711 name = "E" + basisName; 712 RpUnits * exa = RpUnits::define(name, basis, basis->type); 713 RpUnits::define(exa, basis, exa2base, base2exa); 714 715 return (1); 716 } 717 745 basis->setMetric(true); 746 747 return 0; 748 } 749 */ 718 750 719 751 /**********************************************************************/ … … 724 756 725 757 const RpUnits* 726 RpUnits::find(std::string key) { 727 728 RpDictEntry<std::string,RpUnits*,_key_compare>* unitEntry = &(dict->getNullEntry()); 729 RpDictEntry<std::string,RpUnits*,_key_compare>* nullEntry = &(dict->getNullEntry()); 758 RpUnits::find(std::string key, 759 RpDict<std::string,RpUnits*,_key_compare>::RpDictHint hint ) { 760 761 RpDictEntry<std::string,RpUnits*,_key_compare>* 762 unitEntry = &(dict->getNullEntry()); 763 RpDictEntry<std::string,RpUnits*,_key_compare>* 764 nullEntry = &(dict->getNullEntry()); 730 765 double exponent = 1; 731 766 int idx = 0; … … 740 775 } 741 776 742 // pass 1 - look for the unit name as it was stated by the user 743 unitEntry = &(dict->find(key)); 777 if (unitEntry == nullEntry) { 778 // pass 1 - look for the unit name as it was stated by the user 779 // dict->toggleCI(); 780 unitEntry = &(dict->find(key,hint,!RPUNITS_CASE_INSENSITIVE)); 781 // dict->toggleCI(); 782 } 744 783 745 784 if (unitEntry == nullEntry) { 746 785 // pass 2 - use case insensitivity to look for the unit 747 dict->toggleCI(); 748 unitEntry = &(dict->find(key)); 749 dict->toggleCI(); 786 unitEntry = &(dict->find(key,hint,RPUNITS_CASE_INSENSITIVE)); 750 787 } 751 788 … … 776 813 777 814 int 778 // RpUnits::validate ( const std::string& inUnits,779 815 RpUnits::validate ( std::string& inUnits, 780 816 std::string& type, 781 817 std::list<std::string>* compatList ) { 782 818 783 std::string myInUnits = inUnits;784 819 std::string sendUnitStr = ""; 785 820 double exponent = 1; … … 876 911 } 877 912 913 if (prefix != NULL) { 914 name << prefix->getUnits(); 915 } 916 878 917 name << unit->getUnits(); 879 918 … … 927 966 928 967 /**********************************************************************/ 968 // METHOD: getPrefix() 969 /// Return the prefix of an RpUnitsListEntry. 970 /** 971 */ 972 973 const RpUnits* 974 RpUnitsListEntry::getPrefix() const { 975 return prefix; 976 } 977 978 /**********************************************************************/ 929 979 // METHOD: printList() 930 980 /// Traverse a RpUnitsList and print out the name of each element. … … 950 1000 /**********************************************************************/ 951 1001 // METHOD: units2list() 952 /// Split a string of units into a list of units with exponents .1002 /// Split a string of units into a list of units with exponents and prefixes. 953 1003 /** 954 1004 * Splits a string of units like cm2/kVns into a list of units like … … 971 1021 int err = 0; 972 1022 const RpUnits* unit = NULL; 1023 const RpUnits* prefix = NULL; 973 1024 974 1025 … … 1009 1060 offset = RpUnits::grabUnitString(myInUnits); 1010 1061 1011 // if idx> length, then the grabUnitString went through the whole1062 // if offset > length, then the grabUnitString went through the whole 1012 1063 // string and did not find a good string we could use as units. 1013 1064 // this generally means the string was filled with non alphabetical … … 1030 1081 // figure out if we have some defined units in that string 1031 1082 sendUnitStr.str(myInUnits.substr(offset,std::string::npos)); 1032 unit = grabUnits(sendUnitStr.str(),&offset);1083 grabUnits(sendUnitStr.str(),&offset,&unit,&prefix); 1033 1084 if (unit) { 1034 1085 // a unit was found 1035 1086 // add this unit to the list 1036 1087 // erase the found unit's name from our search string 1037 outList.push_front(RpUnitsListEntry(unit,exponent ));1088 outList.push_front(RpUnitsListEntry(unit,exponent,prefix)); 1038 1089 if (type.compare("") == 0) { 1039 1090 type = unit->getType(); … … 1088 1139 /// Join a list of units into a string with proper exponents. 1089 1140 /** 1090 * Join gs a list of units like cm2, kV-1, ns-1, creating a string1141 * Joins a list of units like cm2, kV-1, ns-1, creating a string 1091 1142 * like cm2/kVns. 1092 1143 */ … … 1267 1318 1268 1319 const RpUnits* toUnits = NULL; 1320 const RpUnits* toPrefix = NULL; 1269 1321 const RpUnits* fromUnits = NULL; 1322 const RpUnits* fromPrefix = NULL; 1270 1323 1271 1324 std::string tmpNumVal = ""; … … 1383 1436 while ( (toIter != toUnitsList.end()) && (fromIter != fromUnitsList.end()) && (!convErr) ) { 1384 1437 fromUnits = fromIter->getUnitsObj(); 1438 fromPrefix = fromIter->getPrefix(); 1385 1439 toUnits = toIter->getUnitsObj(); 1440 toPrefix = toIter->getPrefix(); 1386 1441 1387 1442 cList.clear(); 1443 1444 if (fromPrefix != NULL) { 1445 cList.push_back(fromPrefix->convList->conv->convForwFxnPtr); 1446 } 1447 1388 1448 convErr = fromUnits->getConvertFxnList(toUnits, cList); 1449 1450 if (toPrefix != NULL) { 1451 cList.push_back(toPrefix->convList->conv->convBackFxnPtr); 1452 } 1389 1453 1390 1454 if (convErr == 0) { … … 1611 1675 int my_result = 0; 1612 1676 1677 RpUnitsTypes::RpUnitsTypesHint hint = NULL; 1678 1613 1679 // set *result to a default value 1614 1680 if (result) { … … 1642 1708 // original unit. 1643 1709 if ( (toBasis) && (toBasis->getUnitsName() != fromUnit->getUnitsName()) ) { 1644 dictToUnit = find(toBasis->getUnitsName()); 1710 hint = RpUnitsTypes::getTypeHint(toBasis->getType()); 1711 dictToUnit = find(toBasis->getUnitsName(), hint); 1645 1712 } 1646 1713 else { 1647 dictToUnit = find(toUnit->getUnitsName()); 1714 hint = RpUnitsTypes::getTypeHint(toUnit->getType()); 1715 dictToUnit = find(toUnit->getUnitsName(), hint); 1648 1716 } 1649 1717 … … 1807 1875 int my_result = 0; 1808 1876 1877 RpUnitsTypes::RpUnitsTypesHint hint = NULL; 1878 1809 1879 // set *result to a default value 1810 1880 if (result) { … … 1838 1908 // original unit. 1839 1909 if ( (toBasis) && (toBasis->getUnitsName() != fromUnit->getUnitsName()) ) { 1840 dictToUnit = find(toBasis->getUnitsName()); 1910 hint = RpUnitsTypes::getTypeHint(toBasis->getType()); 1911 dictToUnit = find(toBasis->getUnitsName(), hint); 1841 1912 } 1842 1913 else { 1843 dictToUnit = find(toUnit->getUnitsName()); 1914 hint = RpUnitsTypes::getTypeHint(toUnit->getType()); 1915 dictToUnit = find(toUnit->getUnitsName(), hint); 1844 1916 } 1845 1917 … … 2009 2081 // original unit. 2010 2082 if ( (toBasis) && (toBasis->getUnitsName() != fromUnit->getUnitsName()) ) { 2011 dictToUnit = find(toBasis->getUnitsName()); 2083 dictToUnit = find( toBasis->getUnitsName(), 2084 &RpUnitsTypes::hintTypeNonPrefix ); 2012 2085 } 2013 2086 else { 2014 dictToUnit = find(toUnit->getUnitsName()); 2087 dictToUnit = find( toUnit->getUnitsName(), 2088 &RpUnitsTypes::hintTypeNonPrefix ); 2015 2089 } 2016 2090 … … 2182 2256 /** 2183 2257 * 2184 * elements of l2 are pushed onto l1 in the same order in which it2185 * exists in l2. l1 is changed in this function.2186 *2187 2258 * Returns an integer value of zero (0) on success 2188 2259 * Returns non-zero value on failure. … … 2211 2282 2212 2283 int newRecord = 0; 2213 // RpUnits* val = this; 2214 // dict pointer 2215 RpUnits::dict->set(key,val,&newRecord,val->getCI()); 2284 RpUnitsTypes::RpUnitsTypesHint hint = NULL; 2285 2286 if (val == NULL) { 2287 return -1; 2288 } 2289 2290 hint = RpUnitsTypes::getTypeHint(val->getType()); 2291 2292 RpUnits::dict->set(key,val,hint,&newRecord,val->getCI()); 2293 2216 2294 return newRecord; 2217 2295 } … … 2298 2376 retVal = RpUnitsPreset::addPresetMass(); 2299 2377 } 2378 else if (group.compare(RP_TYPE_PREFIX) == 0) { 2379 retVal = RpUnitsPreset::addPresetPrefix(); 2380 } 2300 2381 else if (group.compare(RP_TYPE_PRESSURE) == 0) { 2301 2382 retVal = RpUnitsPreset::addPresetPressure(); … … 2322 2403 int result = 0; 2323 2404 2405 result += addPresetPrefix(); 2324 2406 result += addPresetTime(); 2325 2407 result += addPresetTemp(); … … 2332 2414 result += addPresetConcentration(); 2333 2415 result += addPresetMisc(); 2416 2417 return 0; 2418 } 2419 2420 2421 /**********************************************************************/ 2422 // METHOD: addPresetPrefix() 2423 /// 2424 /** 2425 * Defines the following unit prefixes: 2426 * deci (d) 2427 * centi (c) 2428 * milli (m) 2429 * micro (u) 2430 * nano (n) 2431 * pico (p) 2432 * femto (f) 2433 * atto (a) 2434 * deca (da) 2435 * hecto (h) 2436 * kilo (k) 2437 * mega (M) 2438 * giga (G) 2439 * tera (T) 2440 * peta (P) 2441 * exa (E) 2442 * 2443 * Return codes: 0 success, anything else is error 2444 */ 2445 2446 int 2447 RpUnitsPreset::addPresetPrefix () { 2448 2449 std::string type = RP_TYPE_PREFIX; 2450 RpUnits* basis = NULL; 2451 2452 RpUnits * deci = NULL; 2453 RpUnits * centi = NULL; 2454 RpUnits * milli = NULL; 2455 RpUnits * micro = NULL; 2456 RpUnits * nano = NULL; 2457 RpUnits * pico = NULL; 2458 RpUnits * femto = NULL; 2459 RpUnits * atto = NULL; 2460 RpUnits * deca = NULL; 2461 RpUnits * hecto = NULL; 2462 RpUnits * kilo = NULL; 2463 RpUnits * mega = NULL; 2464 RpUnits * giga = NULL; 2465 RpUnits * tera = NULL; 2466 RpUnits * peta = NULL; 2467 RpUnits * exa = NULL; 2468 2469 deci = RpUnits::define ( "d", basis, type); 2470 centi = RpUnits::define ( "c", basis, type); 2471 milli = RpUnits::define ( "m", basis, type, !RPUNITS_METRIC, 2472 !RPUNITS_CASE_INSENSITIVE); 2473 micro = RpUnits::define ( "u", basis, type); 2474 nano = RpUnits::define ( "n", basis, type); 2475 pico = RpUnits::define ( "p", basis, type, !RPUNITS_METRIC, 2476 !RPUNITS_CASE_INSENSITIVE); 2477 femto = RpUnits::define ( "f", basis, type); 2478 atto = RpUnits::define ( "a", basis, type); 2479 deca = RpUnits::define ( "da", basis, type); 2480 hecto = RpUnits::define ( "h", basis, type); 2481 kilo = RpUnits::define ( "k", basis, type); 2482 mega = RpUnits::define ( "M", basis, type, !RPUNITS_METRIC, 2483 !RPUNITS_CASE_INSENSITIVE); 2484 giga = RpUnits::define ( "G", basis, type); 2485 tera = RpUnits::define ( "T", basis, type); 2486 peta = RpUnits::define ( "P", basis, type, !RPUNITS_METRIC, 2487 !RPUNITS_CASE_INSENSITIVE); 2488 exa = RpUnits::define ( "E", basis, type); 2489 2490 // the use of the unit as the from and the to unit is a hack 2491 // that can be resolved by creating a RpPrefix object 2492 // the define() function cannot handle NULL as to unit. 2493 RpUnits::define ( deci, deci , deci2base, base2deci); 2494 RpUnits::define ( centi, centi, centi2base, base2centi); 2495 RpUnits::define ( milli, milli, milli2base, base2milli); 2496 RpUnits::define ( micro, micro, micro2base, base2micro); 2497 RpUnits::define ( nano, nano , nano2base, base2nano); 2498 RpUnits::define ( pico, pico , pico2base, base2pico); 2499 RpUnits::define ( femto, femto, femto2base, base2femto); 2500 RpUnits::define ( atto, atto , atto2base, base2atto); 2501 RpUnits::define ( deca, deca , deca2base, base2deca); 2502 RpUnits::define ( hecto, hecto, hecto2base, base2hecto); 2503 RpUnits::define ( kilo, kilo , kilo2base, base2kilo); 2504 RpUnits::define ( mega, mega , mega2base, base2mega); 2505 RpUnits::define ( giga, giga , giga2base, base2giga); 2506 RpUnits::define ( tera, tera , tera2base, base2tera); 2507 RpUnits::define ( peta, peta , peta2base, base2peta); 2508 RpUnits::define ( exa, exa , exa2base, base2exa); 2334 2509 2335 2510 return 0; … … 2356 2531 RpUnitsPreset::addPresetTime () { 2357 2532 2358 RpUnits* second = RpUnits::define("s", NULL, RP_TYPE_TIME); 2359 RpUnits* minute = RpUnits::define("min", second, RP_TYPE_TIME); 2360 RpUnits* hour = RpUnits::define("h", second, RP_TYPE_TIME); 2361 RpUnits* day = RpUnits::define("d", second, RP_TYPE_TIME); 2362 2363 RpUnits::makeMetric(second); 2533 RpUnits* second = NULL; 2534 RpUnits* minute = NULL; 2535 RpUnits* hour = NULL; 2536 RpUnits* day = NULL; 2537 2538 second = RpUnits::define("s", NULL, RP_TYPE_TIME, RPUNITS_METRIC); 2539 minute = RpUnits::define("min", second, RP_TYPE_TIME); 2540 hour = RpUnits::define("h", second, RP_TYPE_TIME); 2541 day = RpUnits::define("d", second, RP_TYPE_TIME); 2364 2542 2365 2543 // add time definitions … … 2388 2566 RpUnitsPreset::addPresetTemp () { 2389 2567 2390 RpUnits* fahrenheit = RpUnits::define("F", NULL, RP_TYPE_TEMP); 2391 RpUnits* celcius = RpUnits::define("C", NULL, RP_TYPE_TEMP); 2392 RpUnits* kelvin = RpUnits::define("K", NULL, RP_TYPE_TEMP); 2393 RpUnits* rankine = RpUnits::define("R", NULL, RP_TYPE_TEMP); 2568 RpUnits* fahrenheit = NULL; 2569 RpUnits* celcius = NULL; 2570 RpUnits* kelvin = NULL; 2571 RpUnits* rankine = NULL; 2572 2573 fahrenheit = RpUnits::define("F", NULL, RP_TYPE_TEMP); 2574 celcius = RpUnits::define("C", NULL, RP_TYPE_TEMP); 2575 kelvin = RpUnits::define("K", NULL, RP_TYPE_TEMP); 2576 rankine = RpUnits::define("R", NULL, RP_TYPE_TEMP); 2394 2577 2395 2578 // add temperature definitions … … 2421 2604 RpUnitsPreset::addPresetLength () { 2422 2605 2423 RpUnits* meters = RpUnits::define("m", NULL, RP_TYPE_LENGTH); 2424 RpUnits* angstrom = RpUnits::define("A", NULL, RP_TYPE_LENGTH); 2425 RpUnits* inch = RpUnits::define("in", NULL, RP_TYPE_LENGTH); 2426 RpUnits* feet = RpUnits::define("ft", inch, RP_TYPE_LENGTH); 2427 RpUnits* yard = RpUnits::define("yd", inch, RP_TYPE_LENGTH); 2428 2429 RpUnits::makeMetric(meters); 2606 RpUnits* meters = NULL; 2607 RpUnits* angstrom = NULL; 2608 RpUnits* inch = NULL; 2609 RpUnits* feet = NULL; 2610 RpUnits* yard = NULL; 2611 2612 meters = RpUnits::define("m", NULL, RP_TYPE_LENGTH, RPUNITS_METRIC); 2613 angstrom = RpUnits::define("A", NULL, RP_TYPE_LENGTH); 2614 inch = RpUnits::define("in", NULL, RP_TYPE_LENGTH); 2615 feet = RpUnits::define("ft", inch, RP_TYPE_LENGTH); 2616 yard = RpUnits::define("yd", inch, RP_TYPE_LENGTH); 2617 2618 // RpUnits::makeMetric(meters); 2430 2619 2431 2620 // add length definitions … … 2452 2641 RpUnitsPreset::addPresetEnergy () { 2453 2642 2454 RpUnits* eVolt = RpUnits::define("eV", NULL, RP_TYPE_ENERGY);2455 RpUnits* joule = RpUnits::define("J", NULL, RP_TYPE_ENERGY);2456 2457 RpUnits::makeMetric(eVolt);2458 RpUnits::makeMetric(joule);2643 RpUnits* eVolt = NULL; 2644 RpUnits* joule = NULL; 2645 2646 eVolt = RpUnits::define("eV", NULL, RP_TYPE_ENERGY, RPUNITS_METRIC); 2647 joule = RpUnits::define("J", NULL, RP_TYPE_ENERGY, RPUNITS_METRIC); 2459 2648 2460 2649 // add energy definitions … … 2481 2670 // RpUnits* cubic_meter = RpUnits::define("m3", NULL, RP_TYPE_VOLUME); 2482 2671 // RpUnits* cubic_feet = RpUnits::define("ft3", NULL, RP_TYPE_VOLUME); 2483 RpUnits* us_gallon = RpUnits::define("gal", NULL, RP_TYPE_VOLUME); 2484 RpUnits* liter = RpUnits::define("L", NULL, RP_TYPE_VOLUME); 2672 RpUnits* us_gallon = NULL; 2673 RpUnits* liter = NULL; 2674 2675 us_gallon = RpUnits::define("gal", NULL, RP_TYPE_VOLUME); 2676 liter = RpUnits::define("L", NULL, RP_TYPE_VOLUME, RPUNITS_METRIC); 2485 2677 2486 2678 /* … … 2506 2698 */ 2507 2699 2508 RpUnits::makeMetric(liter);2700 // RpUnits::makeMetric(liter); 2509 2701 2510 2702 … … 2534 2726 RpUnitsPreset::addPresetAngle () { 2535 2727 2536 RpUnits* degree = RpUnits::define("deg", NULL, RP_TYPE_ANGLE); 2537 RpUnits* gradian = RpUnits::define("grad", NULL, RP_TYPE_ANGLE); 2538 RpUnits* radian = RpUnits::define("rad", NULL, RP_TYPE_ANGLE); 2539 2540 RpUnits::makeMetric(radian); 2728 RpUnits* degree = NULL; 2729 RpUnits* gradian = NULL; 2730 RpUnits* radian = NULL; 2731 2732 degree = RpUnits::define("deg", NULL, RP_TYPE_ANGLE); 2733 gradian = RpUnits::define("grad", NULL, RP_TYPE_ANGLE); 2734 radian = RpUnits::define("rad", NULL, RP_TYPE_ANGLE, RPUNITS_METRIC); 2541 2735 2542 2736 // add angle definitions … … 2561 2755 RpUnitsPreset::addPresetMass () { 2562 2756 2563 RpUnits* gram = RpUnits::define("g", NULL, RP_TYPE_MASS); 2564 2565 RpUnits::makeMetric(gram); 2566 2567 // add mass definitions 2757 RpUnits* gram = NULL; 2758 2759 gram = RpUnits::define("g", NULL, RP_TYPE_MASS, RPUNITS_METRIC); 2568 2760 2569 2761 return 0; … … 2593 2785 RpUnitsPreset::addPresetPressure () { 2594 2786 2595 RpUnits* atmosphere = RpUnits::define("atm", NULL, RP_TYPE_PRESSURE); 2596 RpUnits* bar = RpUnits::define("bar", NULL, RP_TYPE_PRESSURE); 2597 RpUnits* pascal = RpUnits::define("Pa", NULL, RP_TYPE_PRESSURE); 2598 RpUnits* psi = RpUnits::define("psi", NULL, RP_TYPE_PRESSURE); 2599 RpUnits* torr = RpUnits::define("torr", NULL, RP_TYPE_PRESSURE); 2600 RpUnits* mmHg = RpUnits::define("mmHg", torr, RP_TYPE_PRESSURE); 2601 2602 RpUnits::makeMetric(pascal); 2603 RpUnits::makeMetric(bar); 2787 RpUnits* atmosphere = NULL; 2788 RpUnits* bar = NULL; 2789 RpUnits* pascal = NULL; 2790 RpUnits* psi = NULL; 2791 RpUnits* torr = NULL; 2792 RpUnits* mmHg = NULL; 2793 2794 atmosphere = RpUnits::define("atm", NULL, RP_TYPE_PRESSURE); 2795 bar = RpUnits::define("bar", NULL, RP_TYPE_PRESSURE, RPUNITS_METRIC); 2796 pascal = RpUnits::define("Pa", NULL, RP_TYPE_PRESSURE, RPUNITS_METRIC); 2797 psi = RpUnits::define("psi", NULL, RP_TYPE_PRESSURE); 2798 torr = RpUnits::define("torr", NULL, RP_TYPE_PRESSURE); 2799 mmHg = RpUnits::define("mmHg", torr, RP_TYPE_PRESSURE); 2604 2800 2605 2801 RpUnits::define(bar,pascal,bar2Pa,Pa2bar); … … 2635 2831 RpUnitsPreset::addPresetConcentration () { 2636 2832 2637 RpUnits* pH = RpUnits::define("pH", NULL, RP_TYPE_CONC); 2638 RpUnits* pOH = RpUnits::define("pOH", NULL, RP_TYPE_CONC); 2833 RpUnits* pH = NULL; 2834 RpUnits* pOH = NULL; 2835 2836 pH = RpUnits::define("pH", NULL, RP_TYPE_CONC); 2837 pOH = RpUnits::define("pOH", NULL, RP_TYPE_CONC); 2639 2838 2640 2839 // add concentration definitions … … 2657 2856 RpUnitsPreset::addPresetMisc () { 2658 2857 2659 RpUnits* volt = RpUnits::define("V", NULL, RP_TYPE_EPOT); 2660 RpUnits* mole = RpUnits::define("mol",NULL, RP_TYPE_MISC); 2661 RpUnits* hertz = RpUnits::define("Hz", NULL, RP_TYPE_MISC); 2662 RpUnits* becquerel = RpUnits::define("Bq", NULL, RP_TYPE_MISC); 2858 RpUnits* volt = NULL; 2859 RpUnits* mole = NULL; 2860 RpUnits* hertz = NULL; 2861 RpUnits* becquerel = NULL; 2862 2863 volt = RpUnits::define("V", NULL, RP_TYPE_EPOT, RPUNITS_METRIC); 2864 mole = RpUnits::define("mol",NULL, RP_TYPE_MISC, RPUNITS_METRIC); 2865 hertz = RpUnits::define("Hz", NULL, RP_TYPE_MISC, RPUNITS_METRIC); 2866 becquerel = RpUnits::define("Bq", NULL, RP_TYPE_MISC, RPUNITS_METRIC); 2867 2663 2868 // RpUnits* percent = RpUnits::define("%", NULL, RP_TYPE_MISC); 2664 2869 2665 RpUnits::makeMetric(volt);2666 RpUnits::makeMetric(mole);2667 RpUnits::makeMetric(hertz);2668 RpUnits::makeMetric(becquerel);2669 2670 // add misc definitions2671 // RpUnits::define(radian,gradian,rad2grad,grad2rad);2672 2673 2870 return 0; 2871 } 2872 2873 RpUnitsTypes::RpUnitsTypesHint 2874 RpUnitsTypes::getTypeHint (std::string type) { 2875 2876 if (type.compare(RP_TYPE_ENERGY) == 0) { 2877 return &RpUnitsTypes::hintTypeEnergy; 2878 } 2879 else if (type.compare(RP_TYPE_EPOT) == 0) { 2880 return &RpUnitsTypes::hintTypeEPot; 2881 } 2882 else if (type.compare(RP_TYPE_LENGTH) == 0) { 2883 return &RpUnitsTypes::hintTypeLength; 2884 } 2885 else if (type.compare(RP_TYPE_TEMP) == 0) { 2886 return &RpUnitsTypes::hintTypeTemp; 2887 } 2888 else if (type.compare(RP_TYPE_TIME) == 0) { 2889 return &RpUnitsTypes::hintTypeTime; 2890 } 2891 else if (type.compare(RP_TYPE_VOLUME) == 0) { 2892 return &RpUnitsTypes::hintTypeVolume; 2893 } 2894 else if (type.compare(RP_TYPE_ANGLE) == 0) { 2895 return &RpUnitsTypes::hintTypeAngle; 2896 } 2897 else if (type.compare(RP_TYPE_MASS) == 0) { 2898 return &RpUnitsTypes::hintTypeMass; 2899 } 2900 else if (type.compare(RP_TYPE_PREFIX) == 0) { 2901 return &RpUnitsTypes::hintTypePrefix; 2902 } 2903 else if (type.compare(RP_TYPE_PRESSURE) == 0) { 2904 return &RpUnitsTypes::hintTypePressure; 2905 } 2906 else if (type.compare(RP_TYPE_CONC) == 0) { 2907 return &RpUnitsTypes::hintTypeConc; 2908 } 2909 else if (type.compare(RP_TYPE_MISC) == 0) { 2910 return &RpUnitsTypes::hintTypeMisc; 2911 } 2912 else { 2913 return NULL; 2914 } 2915 }; 2916 2917 bool 2918 RpUnitsTypes::hintTypePrefix ( RpUnits* unitObj ) { 2919 2920 bool retVal = false; 2921 2922 if ( (unitObj->getType()).compare(RP_TYPE_PREFIX) == 0 ) { 2923 retVal = true; 2924 } 2925 2926 return retVal; 2927 } 2928 2929 bool 2930 RpUnitsTypes::hintTypeNonPrefix ( RpUnits* unitObj ) { 2931 2932 bool retVal = true; 2933 2934 if ( (unitObj->getType()).compare(RP_TYPE_PREFIX) == 0 ) { 2935 retVal = false; 2936 } 2937 2938 return retVal; 2939 } 2940 2941 bool 2942 RpUnitsTypes::hintTypeEnergy ( RpUnits* unitObj ) { 2943 2944 bool retVal = false; 2945 2946 if ( (unitObj->getType()).compare(RP_TYPE_ENERGY) == 0 ) { 2947 retVal = true; 2948 } 2949 2950 return retVal; 2951 } 2952 2953 bool 2954 RpUnitsTypes::hintTypeEPot ( RpUnits* unitObj ) { 2955 2956 bool retVal = false; 2957 2958 if ( (unitObj->getType()).compare(RP_TYPE_EPOT) == 0 ) { 2959 retVal = true; 2960 } 2961 2962 return retVal; 2963 } 2964 2965 bool 2966 RpUnitsTypes::hintTypeLength ( RpUnits* unitObj ) { 2967 2968 bool retVal = false; 2969 2970 if ( (unitObj->getType()).compare(RP_TYPE_LENGTH) == 0 ) { 2971 retVal = true; 2972 } 2973 2974 return retVal; 2975 } 2976 2977 bool 2978 RpUnitsTypes::hintTypeTemp ( RpUnits* unitObj ) { 2979 2980 bool retVal = false; 2981 2982 if ( (unitObj->getType()).compare(RP_TYPE_TEMP) == 0 ) { 2983 retVal = true; 2984 } 2985 2986 return retVal; 2987 } 2988 2989 bool 2990 RpUnitsTypes::hintTypeTime ( RpUnits* unitObj ) { 2991 2992 bool retVal = false; 2993 2994 if ( (unitObj->getType()).compare(RP_TYPE_TIME) == 0 ) { 2995 retVal = true; 2996 } 2997 2998 return retVal; 2999 } 3000 3001 bool 3002 RpUnitsTypes::hintTypeVolume ( RpUnits* unitObj ) { 3003 3004 bool retVal = false; 3005 3006 if ( (unitObj->getType()).compare(RP_TYPE_VOLUME) == 0 ) { 3007 retVal = true; 3008 } 3009 3010 return retVal; 3011 } 3012 3013 bool 3014 RpUnitsTypes::hintTypeAngle ( RpUnits* unitObj ) { 3015 3016 bool retVal = false; 3017 3018 if ( (unitObj->getType()).compare(RP_TYPE_ANGLE) == 0 ) { 3019 retVal = true; 3020 } 3021 3022 return retVal; 3023 } 3024 3025 bool 3026 RpUnitsTypes::hintTypeMass ( RpUnits* unitObj ) { 3027 3028 bool retVal = false; 3029 3030 if ( (unitObj->getType()).compare(RP_TYPE_MASS) == 0 ) { 3031 retVal = true; 3032 } 3033 3034 return retVal; 3035 } 3036 3037 bool 3038 RpUnitsTypes::hintTypePressure ( RpUnits* unitObj ) { 3039 3040 bool retVal = false; 3041 3042 if ( (unitObj->getType()).compare(RP_TYPE_PRESSURE) == 0 ) { 3043 retVal = true; 3044 } 3045 3046 return retVal; 3047 } 3048 3049 bool 3050 RpUnitsTypes::hintTypeConc ( RpUnits* unitObj ) { 3051 3052 bool retVal = false; 3053 3054 if ( (unitObj->getType()).compare(RP_TYPE_CONC) == 0 ) { 3055 retVal = true; 3056 } 3057 3058 return retVal; 3059 } 3060 3061 bool 3062 RpUnitsTypes::hintTypeMisc ( RpUnits* unitObj ) { 3063 3064 bool retVal = false; 3065 3066 if ( (unitObj->getType()).compare(RP_TYPE_MISC) == 0 ) { 3067 retVal = true; 3068 } 3069 3070 return retVal; 2674 3071 } 2675 3072 -
trunk/src/core/RpUnits.h
r562 r568 7 7 * ====================================================================== 8 8 * AUTHOR: Derrick Kearney, Purdue University 9 * Copyright (c) 2004-200 5Purdue Research Foundation9 * Copyright (c) 2004-2007 Purdue Research Foundation 10 10 * 11 11 * See the file "license.terms" for information on usage and … … 30 30 // RpUnits Case Insensitivity define 31 31 #define RPUNITS_CASE_INSENSITIVE true 32 // 32 33 #define RPUNITS_METRIC true 34 33 35 //define our different types of units 34 36 #define RP_TYPE_ENERGY "energy" … … 40 42 #define RP_TYPE_ANGLE "angle" 41 43 #define RP_TYPE_MASS "mass" 44 #define RP_TYPE_PREFIX "prefix" 42 45 #define RP_TYPE_PRESSURE "pressure" 43 46 #define RP_TYPE_CONC "concentration" … … 68 71 }; 69 72 70 static int addPresetAll(); 71 static int addPresetEnergy(); 72 static int addPresetLength(); 73 static int addPresetTemp(); 74 static int addPresetTime(); 75 static int addPresetVolume(); 76 static int addPresetAngle(); 77 static int addPresetMass(); 78 static int addPresetPressure(); 79 static int addPresetConcentration(); 80 static int addPresetMisc(); 73 static int addPresetAll(); 74 static int addPresetEnergy(); 75 static int addPresetLength(); 76 static int addPresetTemp(); 77 static int addPresetTime(); 78 static int addPresetVolume(); 79 static int addPresetAngle(); 80 static int addPresetMass(); 81 static int addPresetPrefix(); 82 static int addPresetPressure(); 83 static int addPresetConcentration(); 84 static int addPresetMisc(); 85 }; 86 87 class RpUnitsTypes { 88 public: 89 90 typedef bool (*RpUnitsTypesHint)(RpUnits*); 91 static RpUnitsTypesHint getTypeHint (std::string type); 92 93 static bool hintTypePrefix ( RpUnits* unitObj ); 94 static bool hintTypeNonPrefix ( RpUnits* unitObj ); 95 static bool hintTypeEnergy ( RpUnits* unitObj ); 96 static bool hintTypeEPot ( RpUnits* unitObj ); 97 static bool hintTypeLength ( RpUnits* unitObj ); 98 static bool hintTypeTemp ( RpUnits* unitObj ); 99 static bool hintTypeTime ( RpUnits* unitObj ); 100 static bool hintTypeVolume ( RpUnits* unitObj ); 101 static bool hintTypeAngle ( RpUnits* unitObj ); 102 static bool hintTypeMass ( RpUnits* unitObj ); 103 static bool hintTypePressure ( RpUnits* unitObj ); 104 static bool hintTypeConc ( RpUnits* unitObj ); 105 static bool hintTypeMisc ( RpUnits* unitObj ); 106 107 private: 108 109 RpUnitsTypes () {}; 81 110 }; 82 111 … … 281 310 282 311 // constructor 283 RpUnitsListEntry (const RpUnits* unit, double exponent )312 RpUnitsListEntry (const RpUnits* unit, double exponent, const RpUnits* prefix=NULL) 284 313 : unit (unit), 285 exponent (exponent) 314 exponent (exponent), 315 prefix (prefix) 286 316 {}; 287 317 … … 289 319 RpUnitsListEntry (const RpUnitsListEntry& other) 290 320 : unit (other.unit), 291 exponent (other.exponent) 321 exponent (other.exponent), 322 prefix (other.prefix) 292 323 {}; 293 324 … … 296 327 unit = other.unit; 297 328 exponent = other.exponent; 329 prefix = other.prefix; 298 330 return *this; 299 331 } … … 313 345 // return the exponent 314 346 double getExponent() const; 347 348 // return the metric prefix associated with this object 349 const RpUnits* getPrefix() const; 315 350 316 351 // destructor … … 322 357 const RpUnits* unit; 323 358 mutable double exponent; 359 const RpUnits* prefix; 324 360 }; 325 361 … … 336 372 public: 337 373 374 static bool cmpFxn (char c1, char c2) 375 { 376 int lc1 = toupper(static_cast<unsigned char>(c1)); 377 int lc2 = toupper(static_cast<unsigned char>(c2)); 378 379 if ( (lc1 < lc2) || (lc1 > lc2) ) { 380 return false; 381 } 382 383 return true; 384 385 } 386 struct _key_compare: 387 public 388 std::binary_function<std::string,std::string,bool> { 389 390 bool operator() ( const std::string& lhs, 391 const std::string& rhs ) const 392 { 393 return std::lexicographical_compare( lhs.begin(),lhs.end(), 394 rhs.begin(),rhs.end(), 395 RpUnits::cmpFxn ); 396 } 397 }; 398 399 // why are these functions friends... 400 // probably need to find a better way to let RpUnits 401 // use the RpDict and RpDictEntry fxns 402 friend class RpDict<std::string,RpUnits*,_key_compare>; 403 friend class RpDictEntry<std::string,RpUnits*,_key_compare>; 338 404 // users member fxns 339 405 std::string getUnits() const; … … 342 408 double getExponent() const; 343 409 const RpUnits* getBasis() const; 410 RpUnits& setMetric(bool newVal); 344 411 345 412 // retrieve a units type. … … 358 425 int* result=NULL ) const; 359 426 // convert from one RpUnits to another if the conversion is defined 360 void* convert( const RpUnits* toUnits, 361 void* val, 427 void* convert( const RpUnits* toUnits, 428 void* val, 362 429 int* result=NULL) const; 363 430 // convert from one RpUnits to another if the conversion is defined … … 380 447 const RpUnits & makeBasis(double* value, int* result = NULL) const; 381 448 382 static int makeMetric( constRpUnits * basis);449 static int makeMetric(RpUnits * basis); 383 450 384 451 // find a RpUnits object that should exist in RpUnitsTable 385 452 // returns 0 on success (object was found) 386 453 // returns !0 on failure (object not found) 387 static const RpUnits* find(std::string key); 388 389 // validate is very similar to find, but it works better 390 // for seeing complex units can be interpreted. 454 static const RpUnits* find( std::string key, 455 RpDict<std::string,RpUnits*,_key_compare>::RpDictHint hint = NULL); 456 457 // validate is very similar to find, but it works better 458 // for seeing complex units can be interpreted. 391 459 // it validates that if the a certain units string is 392 460 // provided as a unit type, then all of the base components … … 404 472 const RpUnits* basis=NULL, 405 473 const std::string type="", 474 bool metric=false, 406 475 bool caseInsensitive=RPUNITS_CASE_INSENSITIVE); 407 476 … … 439 508 // RP_TYPE_ANGLE "angle" load units related to angles 440 509 // RP_TYPE_MASS "mass" load units related to mass 510 // RP_TYPE_PREFIX "prefix" load unit prefixes 441 511 // RP_TYPE_PRESSURE "pressure" load units related to pressure 442 512 // RP_TYPE_CONC "concentration" load units related to pressure … … 449 519 // int undefine(); // delete a relation 450 520 451 static bool cmpFxn (char c1, char c2)452 {453 int lc1 = toupper(static_cast<unsigned char>(c1));454 int lc2 = toupper(static_cast<unsigned char>(c2));455 456 if ( (lc1 < lc2) || (lc1 > lc2) ) {457 return false;458 }459 460 return true;461 462 }463 struct _key_compare:464 public465 std::binary_function<std::string,std::string,bool> {466 467 bool operator() ( const std::string& lhs,468 const std::string& rhs ) const469 {470 return std::lexicographical_compare( lhs.begin(),lhs.end(),471 rhs.begin(),rhs.end(),472 RpUnits::cmpFxn );473 }474 };475 // why are these functions friends...476 // probably need to find a better way to let RpUnits477 // use the RpDict and RpDictEntry fxns478 friend class RpDict<std::string,RpUnits*,_key_compare>;479 friend class RpDictEntry<std::string,RpUnits*,_key_compare>;480 521 481 522 friend int insert(std::string key,RpUnits* val); … … 488 529 basis (other.basis), 489 530 type (other.type), 531 metric (other.metric), 490 532 ci (other.ci), 491 533 convList (NULL) … … 541 583 basis = other.basis; 542 584 type = other.type; 585 metric = other.metric; 543 586 ci = other.ci; 544 587 … … 617 660 std::string type; 618 661 662 // tell if the unit can accept metric prefixes 663 bool metric; 664 619 665 // should this unit be inserted as a case insensitive unit? 620 666 bool ci; … … 648 694 const RpUnits* basis, 649 695 const std::string type, 696 bool metric, 650 697 bool caseInsensitive 651 698 ) 652 : units (units), 653 exponent (exponent), 654 basis (basis), 655 type (type), 656 ci (caseInsensitive), 657 convList (NULL), 699 : units (units), 700 exponent (exponent), 701 basis (basis), 702 type (type), 703 metric (metric), 704 ci (caseInsensitive), 705 convList (NULL), 658 706 incarnationList (NULL) 659 707 {}; … … 669 717 typedef RpUnitsList::iterator RpUnitsListIter; 670 718 671 void newExponent(double newExponent) {exponent = newExponent;}; 672 673 static int units2list( const std::string& inUnits, 674 RpUnitsList& outList, 675 std::string& type); 676 static int list2units( RpUnitsList& inList, 677 std::string& outUnitsStr); 678 static int grabExponent(const std::string& inStr, double* exp); 679 static int grabUnitString( const std::string& inStr); 680 static const RpUnits* grabUnits (std::string inStr, int* offset); 681 static int negateListExponents(RpUnitsList& unitsList); 682 static int printList(RpUnitsList& unitsList); 683 684 static int compareListEntryBasis( RpUnitsList& fromList, 685 RpUnitsListIter& fromIter, 686 RpUnitsListIter& toIter); 687 688 static int compareListEntrySearch( RpUnitsList& fromList, 689 RpUnitsListIter& fromIter, 690 RpUnitsListIter& toIter); 691 692 void connectConversion(conversion* conv) const; 693 void connectIncarnation(const RpUnits* unit) const; 719 // void newExponent (double newExponent) {exponent = newExponent;}; 720 721 static int units2list ( const std::string& inUnits, 722 RpUnitsList& outList, 723 std::string& type ); 724 static int list2units ( RpUnitsList& inList, 725 std::string& outUnitsStr ); 726 static int grabExponent ( const std::string& inStr, 727 double* exp ); 728 static int grabUnitString ( const std::string& inStr ); 729 static int grabUnits ( std::string inStr, 730 int* offset, 731 const RpUnits** unit, 732 const RpUnits** prefix ); 733 static int checkMetricPrefix ( std::string inStr, 734 int* offset, 735 const RpUnits** prefix ); 736 static int negateListExponents ( RpUnitsList& unitsList ); 737 static int printList ( RpUnitsList& unitsList ); 738 739 static int compareListEntryBasis ( RpUnitsList& fromList, 740 RpUnitsListIter& fromIter, 741 RpUnitsListIter& toIter ); 742 743 static int compareListEntrySearch ( RpUnitsList& fromList, 744 RpUnitsListIter& fromIter, 745 RpUnitsListIter& toIter ); 746 747 void connectConversion ( conversion* conv ) const; 748 void connectIncarnation ( const RpUnits* unit ) const; 694 749 695 750 // return the conversion object that will convert 696 751 // from this RpUnits to the proovided toUnits object 697 752 // if the conversion is defined 698 int getConvertFxnList (const RpUnits* toUnits, convertList& cList) const; 699 static int applyConversion (double* val, convertList& cList); 700 static int combineLists (convertList& l1, convertList& l2); 701 static int printList (convertList& l1); 753 int getConvertFxnList ( const RpUnits* toUnits, 754 convertList& cList ) const; 755 static int applyConversion ( double* val, 756 convertList& cList ); 757 static int combineLists ( convertList& l1, 758 convertList& l2 ); 759 static int printList ( convertList& l1 ); 702 760 703 761 }; -
trunk/src/fortran/RpUnitsFInterface.cc
r165 r568 77 77 78 78 int 79 rp_make_metric(int* basis)80 {81 int result = -1;82 const RpUnits* newBasis = NULL;83 84 if (basis && *basis) {85 newBasis = getObject_UnitsStr(*basis);86 87 if (newBasis) {88 // make the metric extensions and retrieve the error code89 result = RpUnits::makeMetric(newBasis);90 }91 }92 93 // return the error code94 return result;95 }96 97 int98 79 rp_get_units(int* unitRefVal, char* retText, int retText_len) 99 80 { -
trunk/src/fortran/RpUnitsFInterface.h
r554 r568 26 26 int rp_find(char* searchName, int searchName_len); 27 27 28 int rp_make_metric(int* basis);29 30 28 int rp_get_units(int* unitRefVal, char* retText, int retText_len); 31 29 -
trunk/src/fortran/RpUnitsFStubs.c
r119 r568 47 47 RP_FIND (char* searchName, int searchName_len) { 48 48 return rp_find(searchName,searchName_len); 49 }50 51 int52 rp_make_metric_ (int* basis) {53 return rp_make_metric(basis);54 }55 56 int57 rp_make_metric__ (int* basis) {58 return rp_make_metric(basis);59 }60 61 int62 RP_MAKE_METRIC (int* basis) {63 return rp_make_metric(basis);64 49 } 65 50 -
trunk/src/fortran/RpUnitsFStubs.h
r511 r568 26 26 int rp_find__ (char* searchName, int searchName_len); 27 27 int RP_FIND (char* searchName, int searchName_len); 28 29 int rp_make_metric_ (int* basis);30 int rp_make_metric__ (int* basis);31 int RP_MAKE_METRIC (int* basis);32 28 33 29 int rp_get_units_ (int* unitRefVal, char* retText, int retText_len); -
trunk/src/tcl/tests/units.test
r562 r568 54 54 test convert-1.1.2 {Rappture::Units::convert, invalid value, valid context} { 55 55 list [catch {Rappture::Units::convert re -context mm} msg] $msg 56 } {1 {bad value "re": should be a real number with units of (A,in,m ,mm)}}56 } {1 {bad value "re": should be a real number with units of (A,in,m)}} 57 57 58 58 test convert-1.1.3 {Rappture::Units::convert, 1 valid argument} { … … 169 169 list [catch {Rappture::Units::convert 33Q -to nm } msg] $msg 170 170 } {1 {Unrecognized units: "Q". 171 Should be units of type length (A,in,m ,nm)}}171 Should be units of type length (A,in,m)}} 172 172 173 173 #---------------------------------------------------------- … … 195 195 test desc-2.2.1 {Rappture::Units::descption, *cm2*K/Vs} { 196 196 list [catch {Rappture::Units::description *cm2*K/Vs} msg] $msg 197 } {0 {length*temperature/electric_potential*time (/V,/s,A2,C,F,K,R, cm2,in2,m2)}}197 } {0 {length*temperature/electric_potential*time (/V,/s,A2,C,F,K,R,in2,m2)}} 198 198 199 199 test desc-2.2.2 {Rappture::Units::descption, cm2 areas} { 200 200 list [catch {Rappture::Units::description cm2} msg] $msg 201 } {0 {area (A2, cm2,in2,m2)}}201 } {0 {area (A2,in2,m2)}} 202 202 203 203 test desc-2.2.3 {Rappture::Units::descption, cm3 volumes} { 204 204 list [catch {Rappture::Units::description cm3} msg] $msg 205 } {0 {volume (A3, cm3,in3,m3)}}205 } {0 {volume (A3,in3,m3)}} 206 206 207 207 test desc-2.2.4 {Rappture::Units::descption, K temperature} { … … 217 217 } {1 {wrong # args: should be "Rappture::Units::description units"}} 218 218 219 test desc-2.3. 0{Rappture::Units::descption, incorrect input type} {219 test desc-2.3.1 {Rappture::Units::descption, incorrect input type} { 220 220 list [catch {Rappture::Units::description 3cm} msg] $msg 221 221 } {0 {}} 222 223 test desc-2.4.0 {Rappture::Units::descption, "mm" correct units retrieval} { 224 list [catch {Rappture::Units::description mm} msg] $msg 225 } {0 {length (A,in,m)}} 226 227 test desc-2.4.1 {Rappture::Units::descption, "MM" correct units retrieval} { 228 list [catch {Rappture::Units::description MM} msg] $msg 229 } {0 {length (A,in,m)}} 230 231 test desc-2.4.2 {Rappture::Units::descption, "mM" correct units retrieval} { 232 list [catch {Rappture::Units::description mM} msg] $msg 233 } {0 {length (A,in,m)}} 234 235 test desc-2.4.3 {Rappture::Units::descption, "Mm" correct units retrieval} { 236 list [catch {Rappture::Units::description Mm} msg] $msg 237 } {0 {length (A,in,m)}} 238 239 test desc-2.4.4 {Rappture::Units::descption, "mv" correct units retrieval} { 240 list [catch {Rappture::Units::description mv} msg] $msg 241 } {0 {electric_potential (V)}} 242 243 test desc-2.4.5 {Rappture::Units::descption, "mV" correct units retrieval} { 244 list [catch {Rappture::Units::description mV} msg] $msg 245 } {0 {electric_potential (V)}} 246 247 test desc-2.4.6 {Rappture::Units::descption, "MV" correct units retrieval} { 248 list [catch {Rappture::Units::description MV} msg] $msg 249 } {0 {electric_potential (V)}} 250 251 test desc-2.4.7 {Rappture::Units::descption, "Mv" correct units retrieval} { 252 list [catch {Rappture::Units::description Mv} msg] $msg 253 } {0 {electric_potential (V)}} 254 255 test desc-2.4.8 {Rappture::Units::descption, "ms" correct units retrieval} { 256 list [catch {Rappture::Units::description ms} msg] $msg 257 } {0 {time (s)}} 258 259 test desc-2.4.9 {Rappture::Units::descption, "mS" correct units retrieval} { 260 list [catch {Rappture::Units::description mS} msg] $msg 261 } {0 {time (s)}} 262 263 test desc-2.4.10 {Rappture::Units::descption, "MS" correct units retrieval} { 264 list [catch {Rappture::Units::description MS} msg] $msg 265 } {0 {time (s)}} 266 267 test desc-2.4.11 {Rappture::Units::descption, "Ms" correct units retrieval} { 268 list [catch {Rappture::Units::description Ms} msg] $msg 269 } {0 {time (s)}} 222 270 223 271 #---------------------------------------------------------- … … 484 532 list [catch {Rappture::Units::convert 2cm2nZ/Vs -to m2/us} msg] $msg 485 533 } {1 {Unrecognized units: "cm2nZ/Vs". 486 Should be units of type length/time (/s, /us,A2,in2,m2)}}534 Should be units of type length/time (/s,A2,in2,m2)}} 487 535 488 536 test convert_units-5.4.4.3 {Rappture::Units::convert, cm2/s->m2/kVus} { 489 537 list [catch {Rappture::Units::convert 2cm2/s -to m2/kVus} msg] $msg 490 538 } {1 {unmatched units in conversion: (kV-1) 491 Please enter units of type length/electric_potential*time (/V,/ kV,/s,/us,A2,in2,m2)}}539 Please enter units of type length/electric_potential*time (/V,/s,A2,in2,m2)}} 492 540 493 541 test convert_units-5.4.4.4 {Rappture::Units::convert, cm2/s->m2/uskV} { 494 542 list [catch {Rappture::Units::convert 2cm2/s -to m2/uskV} msg] $msg 495 543 } {1 {unmatched units in conversion: (kV-1) 496 Please enter units of type length/time*electric_potential (/V,/ kV,/s,/us,A2,in2,m2)}}544 Please enter units of type length/time*electric_potential (/V,/s,A2,in2,m2)}} 497 545 498 546 test convert_units-5.4.4.5 {Rappture::Units::convert, cm2V/s->m2/us} { 499 547 list [catch {Rappture::Units::convert 2cm2V/s -to m2/us} msg] $msg 500 548 } {1 {unmatched units in conversion: (V) 501 Please enter units of type length/time (/s, /us,A2,in2,m2)}}549 Please enter units of type length/time (/s,A2,in2,m2)}} 502 550 503 551 test convert_units-5.4.4.6 {Rappture::Units::convert, cm2V/s->m2/us} { 504 552 list [catch {Rappture::Units::convert 2cm2V/s -to m2/us} msg] $msg 505 553 } {1 {unmatched units in conversion: (V) 506 Please enter units of type length/time (/s, /us,A2,in2,m2)}}554 Please enter units of type length/time (/s,A2,in2,m2)}} 507 555 508 556 test convert_units-5.4.4.7 {Rappture::Units::convert, cm2V->m2} { … … 871 919 } {0 {}} 872 920 921 test search-for-6.0.2.0 {Rappture::Units::Search::for, case searches - m} { 922 set unit "m" 923 set status1 [catch {Rappture::Units::Search::for $unit} runit] 924 set status2 [catch {Rappture::Units::System::for $unit} type] 925 list $status1 $runit $status2 $type 926 } {0 m 0 length} 927 928 test search-for-6.0.2.1 {Rappture::Units::Search::for, case searches - M} { 929 set unit "M" 930 set status1 [catch {Rappture::Units::Search::for $unit} runit] 931 set status2 [catch {Rappture::Units::System::for $unit} type] 932 list $status1 $runit $status2 $type 933 } {0 m 0 length} 934 935 test search-for-6.0.2.2 {Rappture::Units::Search::for, case searches - mm} { 936 set unit "mm" 937 set status1 [catch {Rappture::Units::Search::for $unit} runit] 938 set status2 [catch {Rappture::Units::System::for $unit} type] 939 list $status1 $runit $status2 $type 940 } {0 mm 0 length} 941 942 test search-for-6.0.2.3 {Rappture::Units::Search::for, case searches - Mm} { 943 set unit "Mm" 944 set status1 [catch {Rappture::Units::Search::for $unit} runit] 945 set status2 [catch {Rappture::Units::System::for $unit} type] 946 list $status1 $runit $status2 $type 947 } {0 Mm 0 length} 948 949 test search-for-6.0.2.4 {Rappture::Units::Search::for, case searches - MM} { 950 set unit "MM" 951 set status1 [catch {Rappture::Units::Search::for $unit} runit] 952 set status2 [catch {Rappture::Units::System::for $unit} type] 953 list $status1 $runit $status2 $type 954 } {0 Mm 0 length} 955 956 test search-for-6.0.2.5 {Rappture::Units::Search::for, case searches - mM} { 957 set unit "mM" 958 set status1 [catch {Rappture::Units::Search::for $unit} runit] 959 set status2 [catch {Rappture::Units::System::for $unit} type] 960 list $status1 $runit $status2 $type 961 } {0 mm 0 length} 962 963 test search-for-6.0.2.6 {Rappture::Units::Search::for, case searches - ks} { 964 set unit "ks" 965 set status1 [catch {Rappture::Units::Search::for $unit} runit] 966 set status2 [catch {Rappture::Units::System::for $unit} type] 967 list $status1 $runit $status2 $type 968 } {0 ks 0 time} 969 970 test search-for-6.0.2.7 {Rappture::Units::Search::for, case searches - Ks} { 971 set unit "Ks" 972 set status1 [catch {Rappture::Units::Search::for $unit} runit] 973 set status2 [catch {Rappture::Units::System::for $unit} type] 974 list $status1 $runit $status2 $type 975 } {0 ks 0 time} 976 977 test search-for-6.0.2.8 {Rappture::Units::Search::for, case searches - kS} { 978 set unit "kS" 979 set status1 [catch {Rappture::Units::Search::for $unit} runit] 980 set status2 [catch {Rappture::Units::System::for $unit} type] 981 list $status1 $runit $status2 $type 982 } {0 ks 0 time} 983 984 test search-for-6.0.2.9 {Rappture::Units::Search::for, case searches - KS} { 985 set unit "KS" 986 set status1 [catch {Rappture::Units::Search::for $unit} runit] 987 set status2 [catch {Rappture::Units::System::for $unit} type] 988 list $status1 $runit $status2 $type 989 } {0 ks 0 time} 990 991 test search-for-6.0.2.10 {Rappture::Units::Search::for, case searches - sk} { 992 set unit "sk" 993 set status1 [catch {Rappture::Units::Search::for $unit} runit] 994 set status2 [catch {Rappture::Units::System::for $unit} type] 995 list $status1 $runit $status2 $type 996 } {0 sK 0 time*temperature} 997 998 test search-for-6.0.2.11 {Rappture::Units::Search::for, case searches - sK} { 999 set unit "sK" 1000 set status1 [catch {Rappture::Units::Search::for $unit} runit] 1001 set status2 [catch {Rappture::Units::System::for $unit} type] 1002 list $status1 $runit $status2 $type 1003 } {0 sK 0 time*temperature} 1004 873 1005 ::tcltest::cleanupTests 874 1006 return
Note: See TracChangeset
for help on using the changeset viewer.