- Timestamp:
- Nov 19, 2006 6:09:47 PM (17 years ago)
- Location:
- trunk/src/core
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/RpDict.h
r536 r544 20 20 /**************************************************************************/ 21 21 22 template <typename KeyType, typename ValType> class RpDict; 23 template <typename KeyType, typename ValType> class RpDictEntry; 22 template < typename KeyType, 23 typename ValType, 24 class _Compare=std::equal_to<KeyType> > 25 class RpDict; 26 27 template < typename KeyType, 28 typename ValType, 29 class _Compare=std::equal_to<KeyType> > 30 class RpDictEntry; 31 32 template < typename KeyType, 33 typename ValType, 34 class _Compare=std::equal_to<KeyType> > 35 class RpDictIterator; 36 /* 37 * RpDictEntry should not depend on _Compare, 38 * it was originally neede because RpDict is a friend class 39 * then i needed to assign it a default value because RpUnits.cc 40 * uses it in its find function. RpUnits::find should really not 41 * have to use it. 42 * 43 * RpDictIterator has the same thing going on. It has a default value 44 * because RpBindingsDict uses it. 45 */ 46 24 47 25 48 /**************************************************************************/ 26 49 27 template <typename KeyType, typename ValType> class RpDictIterator 50 template <typename KeyType, typename ValType, class _Compare> 51 class RpDictIterator 28 52 { 29 53 … … 35 59 36 60 // retrieve the table the iterator is iterating 37 // virtual RpDict<KeyType,ValType >& getTable();61 // virtual RpDict<KeyType,ValType,_Compare>& getTable(); 38 62 39 63 // send the search iterator to the beginning of the hash table 40 /*virtual*/ RpDictEntry<KeyType,ValType >* first();64 /*virtual*/ RpDictEntry<KeyType,ValType,_Compare>* first(); 41 65 42 66 // send the search iterator to the next element of the hash table 43 /*virtual*/ RpDictEntry<KeyType,ValType >* next();67 /*virtual*/ RpDictEntry<KeyType,ValType,_Compare>* next(); 44 68 /* 45 69 RpDictIterator(RpDict* table_Ptr) … … 50 74 } 51 75 */ 52 RpDictIterator(RpDict<KeyType,ValType >& table_Ptr)76 RpDictIterator(RpDict<KeyType,ValType,_Compare>& table_Ptr) 53 77 : tablePtr(table_Ptr), 54 78 srchNextIndex(0), … … 58 82 59 83 // copy constructor 60 RpDictIterator(RpDictIterator<KeyType,ValType >& iterRef)84 RpDictIterator(RpDictIterator<KeyType,ValType,_Compare>& iterRef) 61 85 : tablePtr(iterRef.tablePtr), 62 86 srchNextIndex(iterRef.srchNextIndex), … … 69 93 private: 70 94 71 RpDict<KeyType,ValType >&95 RpDict<KeyType,ValType,_Compare>& 72 96 tablePtr; /* pointer to the table we want to 73 97 * iterate */ 74 98 int srchNextIndex; /* Index of next bucket to be 75 99 * enumerated after present one. */ 76 RpDictEntry<KeyType,ValType >*100 RpDictEntry<KeyType,ValType,_Compare>* 77 101 srchNextEntryPtr; /* Next entry to be enumerated in the 78 102 * the current bucket. */ … … 81 105 82 106 83 template <typename KeyType, typename ValType> class RpDictEntry 107 template <typename KeyType, typename ValType, class _Compare> 108 class RpDictEntry 84 109 { 85 110 public: … … 106 131 // template <KeyType,ValType> friend class RpDictIterator; 107 132 108 friend class RpDict<KeyType,ValType >;109 friend class RpDictIterator<KeyType,ValType >;133 friend class RpDict<KeyType,ValType,_Compare>; 134 friend class RpDictIterator<KeyType,ValType,_Compare>; 110 135 111 136 // one-arg constructor … … 154 179 */ 155 180 // copy constructor 156 RpDictEntry (const RpDictEntry<KeyType,ValType >& entry)181 RpDictEntry (const RpDictEntry<KeyType,ValType,_Compare>& entry) 157 182 { 158 183 nextPtr = entry.nextPtr; … … 174 199 175 200 // private data members 176 RpDictEntry<KeyType,ValType >*201 RpDictEntry<KeyType,ValType,_Compare>* 177 202 nextPtr; /* Pointer to next entry in this 178 203 * hash bucket, or NULL for end of 179 204 * chain. */ 180 205 181 RpDict<KeyType,ValType >*206 RpDict<KeyType,ValType,_Compare>* 182 207 tablePtr; /* Pointer to table containing entry. */ 183 208 … … 207 232 208 233 209 template <typename KeyType, typename ValType> class RpDict 234 template <typename KeyType, typename ValType, class _Compare> 235 class RpDict 210 236 { 211 237 public: … … 221 247 // returns !0 on failure (object cannot be inserted or dne) 222 248 // 223 /*virtual*/ RpDict<KeyType,ValType >&249 /*virtual*/ RpDict<KeyType,ValType,_Compare>& 224 250 set( KeyType& key, 225 251 ValType& value, 226 int *newPtr=NULL ); 252 int *newPtr=NULL, 253 bool ci=false); 227 254 228 255 // find an RpUnits object that should exist in RpUnitsTable 229 256 // 230 /*virtual*/ RpDictEntry<KeyType,ValType >&257 /*virtual*/ RpDictEntry<KeyType,ValType,_Compare>& 231 258 find( KeyType& key ); 232 259 233 /*virtual*/ RpDictEntry<KeyType,ValType >& operator[]( KeyType& key)260 /*virtual*/ RpDictEntry<KeyType,ValType,_Compare>& operator[]( KeyType& key) 234 261 { 235 262 return find(key); 236 263 } 237 264 265 RpDict<KeyType,ValType,_Compare>& setCI( bool val ); 266 bool getCI(); 267 RpDict<KeyType,ValType,_Compare>& toggleCI(); 268 238 269 // clear the entire table 239 270 // iterate through the table and call erase on each element 240 /*virtual*/ RpDict<KeyType,ValType >& clear();271 /*virtual*/ RpDict<KeyType,ValType,_Compare>& clear(); 241 272 242 273 // get the nullEntry hash entry for initialization of references 243 /*virtual*/ RpDictEntry<KeyType,ValType >& getNullEntry();274 /*virtual*/ RpDictEntry<KeyType,ValType,_Compare>& getNullEntry(); 244 275 245 276 // template <KeyType, ValType> friend class RpDictEntry; 246 277 // template <KeyType, ValType> friend class RpDictIterator; 247 278 248 friend class RpDictEntry<KeyType, ValType>;249 friend class RpDictIterator<KeyType, ValType>;279 friend class RpDictEntry<KeyType,ValType,_Compare>; 280 friend class RpDictIterator<KeyType,ValType,_Compare>; 250 281 251 282 // default constructor 252 RpDict ( )283 RpDict (bool ci=false) 253 284 : SMALL_RP_DICT_SIZE(4), 254 285 REBUILD_MULTIPLIER(3), … … 258 289 rebuildSize(SMALL_RP_DICT_SIZE*REBUILD_MULTIPLIER), 259 290 downShift(28), 260 mask(3) 291 mask(3), 292 caseInsensitive(ci) 261 293 { 262 294 … … 265 297 266 298 // setup a dummy entry of NULL 267 nullEntry = new RpDictEntry<KeyType,ValType >();299 nullEntry = new RpDictEntry<KeyType,ValType,_Compare>(); 268 300 269 301 assert(nullEntry != NULL); … … 293 325 const int REBUILD_MULTIPLIER; 294 326 295 RpDictEntry<KeyType,ValType >327 RpDictEntry<KeyType,ValType,_Compare> 296 328 **buckets; /* Pointer to bucket array. Each 297 329 * element points to first entry in 298 330 * bucket's hash chain, or NULL. */ 299 331 // RpDictEntry *staticBuckets[SMALL_RP_DICT_SIZE]; 300 RpDictEntry<KeyType,ValType >332 RpDictEntry<KeyType,ValType,_Compare> 301 333 *staticBuckets[4]; 302 334 /* Bucket array used for small tables … … 313 345 int mask; /* Mask value used in hashing 314 346 * function. */ 315 RpDictEntry<KeyType,ValType> 347 bool caseInsensitive; /* When set to true, dictionary uses 348 * case insensitive functions, 349 * translating chars to uppercase */ 350 351 RpDictEntry<KeyType,ValType,_Compare> 316 352 *nullEntry; /* if not const, compiler complains*/ 317 353 … … 352 388 *************************************************************************/ 353 389 354 template <typename KeyType, typename ValType >390 template <typename KeyType, typename ValType, class _Compare> 355 391 const int 356 RpDict<KeyType,ValType >::size() const392 RpDict<KeyType,ValType,_Compare>::size() const 357 393 { 358 394 return numEntries; … … 373 409 * Side Effects: 374 410 * if successful, the hash table will have a new entry 375 * 411 * 376 412 * 377 413 *************************************************************************/ 378 template <typename KeyType, typename ValType> 379 RpDict<KeyType,ValType>& 380 RpDict<KeyType,ValType>::set( KeyType& key, 381 ValType& value, 382 int* newPtr) 383 { 384 RpDictEntry<KeyType,ValType> *hPtr = NULL; 414 template <typename KeyType, typename ValType, class _Compare> 415 RpDict<KeyType,ValType,_Compare>& 416 RpDict<KeyType,ValType,_Compare>::set( KeyType& key, 417 ValType& value, 418 int* newPtr, 419 bool ci ) 420 { 421 RpDictEntry<KeyType,ValType,_Compare> *hPtr = NULL; 385 422 unsigned int hash = 0; 386 423 int index = 0; 424 bool oldCI = caseInsensitive; 387 425 388 426 assert(&key); … … 391 429 // take care of the case where we are creating a NULL key entry. 392 430 if (&key) { 431 if (ci != oldCI) { 432 setCI(ci); 433 } 393 434 hash = (unsigned int) hashFxn(&key); 435 if (ci != oldCI) { 436 setCI(oldCI); 437 } 394 438 } 395 439 else { … … 407 451 } 408 452 // if element already exists in hash, it should not be re-entered 409 if (key == *(hPtr->getKey())){ 453 // if (key == *(hPtr->getKey())){ 454 if (_Compare()(key, *(hPtr->getKey()))){ 410 455 411 456 // adjust the new flag if it was provided … … 433 478 } 434 479 435 hPtr = new RpDictEntry<KeyType,ValType >(key,value);480 hPtr = new RpDictEntry<KeyType,ValType,_Compare>(key,value); 436 481 // hPtr->setValue(value); 437 482 // … … 476 521 */ 477 522 478 template <typename KeyType, typename ValType >479 RpDictEntry<KeyType,ValType >&480 RpDict<KeyType,ValType >::find(KeyType& key)481 { 482 RpDictEntry<KeyType,ValType > *hPtr = NULL;523 template <typename KeyType, typename ValType, class _Compare> 524 RpDictEntry<KeyType,ValType,_Compare>& 525 RpDict<KeyType,ValType,_Compare>::find(KeyType& key) 526 { 527 RpDictEntry<KeyType,ValType,_Compare> *hPtr = NULL; 483 528 unsigned int hash = 0; 484 529 int index = 0; … … 504 549 continue; 505 550 } 506 if (key == *(hPtr->getKey())) { 551 // if (key == *(hPtr->getKey())) { 552 if (_Compare()(key, *(hPtr->getKey()))){ 507 553 // return a reference to the found object 508 554 return *hPtr; … … 533 579 *************************************************************************/ 534 580 /* 535 template <typename KeyType,typename ValType >536 RpDict<KeyType,ValType >&537 RpDictIterator<KeyType,ValType >::getTable()581 template <typename KeyType,typename ValType,class _Compare> 582 RpDict<KeyType,ValType,_Compare>& 583 RpDictIterator<KeyType,ValType,_Compare>::getTable() 538 584 { 539 585 return tablePtr; … … 554 600 * 555 601 *************************************************************************/ 556 template <typename KeyType,typename ValType >557 RpDictEntry<KeyType,ValType >*558 RpDictIterator<KeyType,ValType >::first()602 template <typename KeyType,typename ValType,class _Compare> 603 RpDictEntry<KeyType,ValType,_Compare>* 604 RpDictIterator<KeyType,ValType,_Compare>::first() 559 605 { 560 606 srchNextIndex = 0; … … 579 625 *************************************************************************/ 580 626 581 template <typename KeyType,typename ValType >582 RpDictEntry<KeyType,ValType >*583 RpDictIterator<KeyType,ValType >::next()584 { 585 RpDictEntry<KeyType,ValType >* hPtr = NULL;627 template <typename KeyType,typename ValType,class _Compare> 628 RpDictEntry<KeyType,ValType,_Compare>* 629 RpDictIterator<KeyType,ValType,_Compare>::next() 630 { 631 RpDictEntry<KeyType,ValType,_Compare>* hPtr = NULL; 586 632 587 633 while (srchNextEntryPtr == NULL) { … … 600 646 /************************************************************************** 601 647 * 648 * RpDict & setCI(bool val) 649 * Use case insensitive functions where applicable within the dictionary 650 * 651 * Results: 652 * sets the dictionary objects caseInsensitive variable to the boolean val 653 * 654 * Side Effects: 655 * mostly find and set functions will begin to execute their functions 656 * with case insensitivity in mind. 657 * 658 * 659 *************************************************************************/ 660 template <typename KeyType, typename ValType, class _Compare> 661 RpDict<KeyType,ValType,_Compare>& 662 RpDict<KeyType,ValType,_Compare>::setCI( bool val ) 663 { 664 caseInsensitive = val; 665 return *this; 666 } 667 668 /************************************************************************** 669 * 670 * bool getCI() 671 * Retrieve the case insensitivity of this dictionary object 672 * 673 * Results: 674 * returns the dictionary object's caseInsensitive variable to the user 675 * 676 * Side Effects: 677 * None 678 * 679 *************************************************************************/ 680 template <typename KeyType, typename ValType, class _Compare> 681 bool 682 RpDict<KeyType,ValType,_Compare>::getCI() 683 { 684 return caseInsensitive; 685 } 686 687 /************************************************************************** 688 * 689 * RpDict & toggleCI() 690 * Toggle the case insensitivity of this dictionary object 691 * 692 * Results: 693 * returns the dictionary object's caseInsensitive variable to the user 694 * 695 * Side Effects: 696 * None 697 * 698 *************************************************************************/ 699 template <typename KeyType, typename ValType, class _Compare> 700 RpDict<KeyType,ValType,_Compare>& 701 RpDict<KeyType,ValType,_Compare>::toggleCI() 702 { 703 caseInsensitive = !caseInsensitive; 704 return *this; 705 } 706 707 /************************************************************************** 708 * 602 709 * RpDict & clear() 603 710 * iterate through the table and call erase on each element … … 611 718 * 612 719 *************************************************************************/ 613 template <typename KeyType, typename ValType >614 RpDict<KeyType,ValType >&615 RpDict<KeyType,ValType >::clear()616 { 617 RpDictEntry<KeyType,ValType > *hPtr = NULL;618 RpDictIterator<KeyType,ValType > iter((RpDict&)*this);720 template <typename KeyType, typename ValType, class _Compare> 721 RpDict<KeyType,ValType,_Compare>& 722 RpDict<KeyType,ValType,_Compare>::clear() 723 { 724 RpDictEntry<KeyType,ValType,_Compare> *hPtr = NULL; 725 RpDictIterator<KeyType,ValType,_Compare> iter((RpDict&)*this); 619 726 620 727 hPtr = iter.first(); … … 642 749 * 643 750 *************************************************************************/ 644 template <typename KeyType, typename ValType >645 RpDictEntry<KeyType,ValType >&646 RpDict<KeyType,ValType >::getNullEntry()751 template <typename KeyType, typename ValType, class _Compare> 752 RpDictEntry<KeyType,ValType,_Compare>& 753 RpDict<KeyType,ValType,_Compare>::getNullEntry() 647 754 { 648 755 return *nullEntry; … … 669 776 */ 670 777 671 template <typename KeyType, typename ValType >778 template <typename KeyType, typename ValType, class _Compare> 672 779 void 673 RpDictEntry<KeyType,ValType >::erase()674 { 675 RpDictEntry<KeyType,ValType > *prevPtr = NULL;676 RpDictEntry<KeyType,ValType > **bucketPtr = NULL;780 RpDictEntry<KeyType,ValType,_Compare>::erase() 781 { 782 RpDictEntry<KeyType,ValType,_Compare> *prevPtr = NULL; 783 RpDictEntry<KeyType,ValType,_Compare> **bucketPtr = NULL; 677 784 int index = 0; 678 785 … … 744 851 */ 745 852 746 template <typename KeyType, typename ValType >853 template <typename KeyType, typename ValType, class _Compare> 747 854 const KeyType* 748 RpDictEntry<KeyType,ValType >::getKey() const855 RpDictEntry<KeyType,ValType,_Compare>::getKey() const 749 856 { 750 857 return (const KeyType*) &key; … … 769 876 */ 770 877 771 template <typename KeyType, typename ValType >878 template <typename KeyType, typename ValType, class _Compare> 772 879 const ValType* 773 RpDictEntry<KeyType,ValType >::getValue() const880 RpDictEntry<KeyType,ValType,_Compare>::getValue() const 774 881 { 775 882 return (const ValType*) &clientData; … … 792 899 */ 793 900 794 template <typename KeyType, typename ValType >901 template <typename KeyType, typename ValType, class _Compare> 795 902 const ValType* 796 RpDictEntry<KeyType,ValType >::setValue(const ValType& value)903 RpDictEntry<KeyType,ValType,_Compare>::setValue(const ValType& value) 797 904 { 798 905 clientData = value; … … 801 908 } 802 909 803 template <typename KeyType, typename ValType >804 RpDictEntry<KeyType,ValType >::operator int() const910 template <typename KeyType, typename ValType, class _Compare> 911 RpDictEntry<KeyType,ValType,_Compare>::operator int() const 805 912 { 806 913 … … 829 936 */ 830 937 831 template <typename KeyType, typename ValType >938 template <typename KeyType, typename ValType, class _Compare> 832 939 bool 833 RpDictEntry<KeyType,ValType >::isValid() const940 RpDictEntry<KeyType,ValType,_Compare>::isValid() const 834 941 { 835 942 if (valid) { … … 865 972 */ 866 973 867 template <typename KeyType, typename ValType >974 template <typename KeyType, typename ValType, class _Compare> 868 975 void 869 RpDict<KeyType,ValType >::RebuildTable()976 RpDict<KeyType,ValType,_Compare>::RebuildTable() 870 977 { 871 978 int oldSize=0, count=0, index=0; 872 RpDictEntry<KeyType,ValType > **oldBuckets = NULL;873 RpDictEntry<KeyType,ValType > **oldChainPtr = NULL, **newChainPtr = NULL;874 RpDictEntry<KeyType,ValType > *hPtr = NULL;979 RpDictEntry<KeyType,ValType,_Compare> **oldBuckets = NULL; 980 RpDictEntry<KeyType,ValType,_Compare> **oldChainPtr = NULL, **newChainPtr = NULL; 981 RpDictEntry<KeyType,ValType,_Compare> *hPtr = NULL; 875 982 876 983 // void *key = NULL; … … 887 994 numBuckets *= 4; 888 995 889 buckets = (RpDictEntry<KeyType,ValType > **) malloc((unsigned)890 (numBuckets * sizeof(RpDictEntry<KeyType,ValType > *)));996 buckets = (RpDictEntry<KeyType,ValType,_Compare> **) malloc((unsigned) 997 (numBuckets * sizeof(RpDictEntry<KeyType,ValType,_Compare> *))); 891 998 892 999 for (count = numBuckets, newChainPtr = buckets; … … 945 1052 */ 946 1053 947 template <typename KeyType, typename ValType >1054 template <typename KeyType, typename ValType, class _Compare> 948 1055 unsigned int 949 RpDict<KeyType,ValType >::hashFxn(const void *keyPtr) const1056 RpDict<KeyType,ValType,_Compare>::hashFxn(const void *keyPtr) const 950 1057 { 951 1058 const char *stopAddr = (const char *) keyPtr + sizeof(&keyPtr) - 1 ; … … 983 1090 984 1091 985 template <typename KeyType, typename ValType >1092 template <typename KeyType, typename ValType, class _Compare> 986 1093 unsigned int 987 RpDict<KeyType,ValType >::hashFxn(std::string* keyPtr) const1094 RpDict<KeyType,ValType,_Compare>::hashFxn(std::string* keyPtr) const 988 1095 { 989 1096 const char *str = (const char *) (keyPtr->c_str()); … … 994 1101 995 1102 while (1) { 996 c = *str; 1103 // c = *str; 1104 1105 if (caseInsensitive == true) { 1106 c = toupper(static_cast<unsigned char>(*str)); 1107 } 1108 else { 1109 c = *str; 1110 } 1111 997 1112 if (c == 0) { 998 1113 break; … … 1005 1120 } 1006 1121 1007 template <typename KeyType, typename ValType >1122 template <typename KeyType, typename ValType, class _Compare> 1008 1123 unsigned int 1009 RpDict<KeyType,ValType >::hashFxn(char* keyPtr) const1124 RpDict<KeyType,ValType,_Compare>::hashFxn(char* keyPtr) const 1010 1125 { 1011 1126 const char *str = (const char *) (keyPtr); … … 1041 1156 */ 1042 1157 1043 template <typename KeyType, typename ValType >1158 template <typename KeyType, typename ValType, class _Compare> 1044 1159 int 1045 RpDict<KeyType,ValType >::randomIndex(unsigned int hash)1160 RpDict<KeyType,ValType,_Compare>::randomIndex(unsigned int hash) 1046 1161 { 1047 1162 return (((((long) (hash))*1103515245) >> downShift) & mask); -
trunk/src/core/RpUnits.cc
r538 r544 17 17 18 18 // dict pointer 19 RpDict<std::string,RpUnits*>* RpUnits::dict = new RpDict<std::string,RpUnits*>(); 19 // set the dictionary to be case insensitive for seaches and storage 20 RpDict<std::string,RpUnits*,RpUnits::_key_compare>* RpUnits::dict = 21 new RpDict<std::string,RpUnits*,RpUnits::_key_compare>(true); 20 22 21 23 // install predefined units … … 33 35 34 36 RpUnits * 35 RpUnits::define( const std::string units, 36 const RpUnits* basis, 37 const std::string type ) { 37 RpUnits::define( const std::string units, 38 const RpUnits* basis, 39 const std::string type, 40 bool caseInsensitive ) { 38 41 39 42 RpUnits* newRpUnit = NULL; … … 83 86 } 84 87 85 newRpUnit = new RpUnits(sendStr, exponent, basis, type );88 newRpUnit = new RpUnits(sendStr, exponent, basis, type, caseInsensitive); 86 89 if (newRpUnit) { 87 90 insert(newRpUnit->getUnitsName(),newRpUnit); … … 146 149 /// Return units name from a units string containing a unit name and exponent 147 150 /** 151 * this function will be the cause of problems related to adding symbols like % 148 152 */ 149 153 … … 197 201 RpUnits::getType() const { 198 202 return this->type; 203 } 204 205 /**********************************************************************/ 206 // METHOD: getCI() 207 /// Return the case insensitivity of an RpUnits object. 208 /** 209 */ 210 bool 211 RpUnits::getCI() const { 212 return this->ci; 199 213 } 200 214 … … 491 505 492 506 /**********************************************************************/ 507 // METHOD: getSearchName() 508 /// Report the units name used when searching through the units dictionary. 509 /** 510 * Reports the search name used to store and retrieve this object from 511 * the units dictionary. 512 */ 513 514 std::string 515 RpUnits::getSearchName() const { 516 517 std::string searchName = getUnitsName(); 518 519 std::transform( searchName.begin(), 520 searchName.end(), 521 searchName.begin(), 522 tolower ); 523 524 return (searchName); 525 } 526 527 /**********************************************************************/ 493 528 // METHOD: getExponent() 494 529 /// Report the exponent of the units of this object back to caller. … … 611 646 612 647 name = "m" + basisName; 613 RpUnits * milli = RpUnits::define(name, basis, basis->type );648 RpUnits * milli = RpUnits::define(name, basis, basis->type,!RPUNITS_CI); 614 649 RpUnits::define(milli, basis, milli2base, base2milli); 615 650 … … 623 658 624 659 name = "p" + basisName; 625 RpUnits * pico = RpUnits::define(name, basis, basis->type );660 RpUnits * pico = RpUnits::define(name, basis, basis->type,!RPUNITS_CI); 626 661 RpUnits::define(pico, basis, pico2base, base2pico); 627 662 … … 647 682 648 683 name = "M" + basisName; 649 RpUnits * mega = RpUnits::define(name, basis, basis->type );684 RpUnits * mega = RpUnits::define(name, basis, basis->type,!RPUNITS_CI); 650 685 RpUnits::define(mega, basis, mega2base, base2mega); 651 686 … … 659 694 660 695 name = "P" + basisName; 661 RpUnits * peta = RpUnits::define(name, basis, basis->type );696 RpUnits * peta = RpUnits::define(name, basis, basis->type,!RPUNITS_CI); 662 697 RpUnits::define(peta, basis, peta2base, base2peta); 663 698 … … 672 707 /**********************************************************************/ 673 708 // METHOD: find() 674 /// Find a nRpUnits Object from the provided string.709 /// Find a simple RpUnits Object from the provided string. 675 710 /** 676 711 */ … … 679 714 RpUnits::find(std::string key) { 680 715 681 // dict pointer 682 /* 683 const RpUnits* unitEntry = NULL; 684 const RpUnits* nullEntry = NULL; 685 */ 686 RpDictEntry<std::string,RpUnits*>* unitEntry = &(dict->getNullEntry()); 687 RpDictEntry<std::string,RpUnits*>* nullEntry = &(dict->getNullEntry()); 716 RpDictEntry<std::string,RpUnits*,_key_compare>* unitEntry = &(dict->getNullEntry()); 717 RpDictEntry<std::string,RpUnits*,_key_compare>* nullEntry = &(dict->getNullEntry()); 688 718 double exponent = 1; 689 719 int idx = 0; … … 698 728 } 699 729 700 /* 701 unitEntry = *(dict->find(key).getValue()); 702 nullEntry = *(dict->getNullEntry().getValue()); 703 */ 704 730 // pass 1 - look for the unit name as it was stated by the user 705 731 unitEntry = &(dict->find(key)); 706 732 707 // dict pointer 733 if (unitEntry == nullEntry) { 734 // pass 2 - use case insensitivity to look for the unit 735 dict->toggleCI(); 736 unitEntry = &(dict->find(key)); 737 dict->toggleCI(); 738 } 739 708 740 if ( (!unitEntry->isValid()) || (unitEntry == nullEntry) ) { 709 741 // unitEntry = NULL; … … 822 854 823 855 /**********************************************************************/ 824 // METHOD: define()856 // METHOD: getBasis() 825 857 /// Provide the caller with the basis of the RpUnits object being stored 826 858 /** … … 2041 2073 // RpUnits* val = this; 2042 2074 // dict pointer 2043 RpUnits::dict->set(key,val,&newRecord );2075 RpUnits::dict->set(key,val,&newRecord,val->getCI()); 2044 2076 return newRecord; 2045 2077 } … … 2485 2517 RpUnitsPreset::addPresetMisc () { 2486 2518 2487 RpUnits* volt = RpUnits::define("V", NULL, RP_TYPE_ENERGY); 2488 RpUnits* mole = RpUnits::define("mol", NULL, RP_TYPE_MISC); 2489 RpUnits* hertz = RpUnits::define("Hz", NULL, RP_TYPE_MISC); 2490 RpUnits* becquerel = RpUnits::define("Bq", NULL, RP_TYPE_MISC); 2519 RpUnits* volt = RpUnits::define("V", NULL, RP_TYPE_ENERGY); 2520 RpUnits* mole = RpUnits::define("mol",NULL, RP_TYPE_MISC); 2521 RpUnits* hertz = RpUnits::define("Hz", NULL, RP_TYPE_MISC); 2522 RpUnits* becquerel = RpUnits::define("Bq", NULL, RP_TYPE_MISC); 2523 // RpUnits* percent = RpUnits::define("%", NULL, RP_TYPE_MISC); 2491 2524 2492 2525 RpUnits::makeMetric(volt); -
trunk/src/core/RpUnits.h
r536 r544 29 29 #define LIST_TEMPLATE RpUnitsListEntry 30 30 31 // RpUnits Case Insensitivity define 32 #define RPUNITS_CI true 33 31 34 //define our different types of units 32 35 #define RP_TYPE_ENERGY "energy" … … 334 337 std::string getUnits() const; 335 338 std::string getUnitsName() const; 339 std::string getSearchName() const; 336 340 double getExponent() const; 337 341 const RpUnits* getBasis() const; … … 339 343 // retrieve a units type. 340 344 std::string getType() const; 345 346 // retrieve the case insensitivity of this unit object 347 bool getCI() const; 341 348 342 349 // retrieve a list of compatible units. … … 406 413 static RpUnits * define(const std::string units, 407 414 const RpUnits* basis=NULL, 408 const std::string type="" ); 415 const std::string type="", 416 bool caseInsensitive=RPUNITS_CI); 409 417 410 418 // add relation rule … … 450 458 // int undefine(); // delete a relation 451 459 460 static bool cmpFxn (char c1, char c2) 461 { 462 int lc1 = toupper(static_cast<unsigned char>(c1)); 463 int lc2 = toupper(static_cast<unsigned char>(c2)); 464 465 if ( (lc1 < lc2) || (lc1 > lc2) ) { 466 return false; 467 } 468 469 return true; 470 471 } 472 struct _key_compare: 473 public 474 std::binary_function<std::string,std::string,bool> { 475 476 bool operator() ( const std::string& lhs, 477 const std::string& rhs ) const 478 { 479 return std::lexicographical_compare( lhs.begin(),lhs.end(), 480 rhs.begin(),rhs.end(), 481 RpUnits::cmpFxn ); 482 } 483 }; 452 484 // why are these functions friends... 453 485 // probably need to find a better way to let RpUnits 454 486 // use the RpDict and RpDictEntry fxns 455 friend class RpDict<std::string,RpUnits* >;456 friend class RpDictEntry<std::string,RpUnits* >;487 friend class RpDict<std::string,RpUnits*,_key_compare>; 488 friend class RpDictEntry<std::string,RpUnits*,_key_compare>; 457 489 458 490 friend int insert(std::string key,RpUnits* val); … … 465 497 basis (other.basis), 466 498 type (other.type), 499 ci (other.ci), 467 500 convList (NULL) 468 501 { … … 517 550 basis = other.basis; 518 551 type = other.type; 552 ci = other.ci; 519 553 520 554 if (other.convList) { … … 592 626 std::string type; 593 627 628 // should this unit be inserted as a case insensitive unit? 629 bool ci; 630 594 631 // linked list of units this RpUnit can convert to 595 632 // its mutable because the connectConversion function takes in a … … 604 641 mutable incarnationEntry* incarnationList; 605 642 606 607 643 // dictionary to store the units. 608 static RpDict<std::string,RpUnits* >* dict;644 static RpDict<std::string,RpUnits*,_key_compare>* dict; 609 645 610 646 // create a units element … … 620 656 double& exponent, 621 657 const RpUnits* basis, 622 const std::string type 658 const std::string type, 659 bool caseInsensitive 623 660 ) 624 661 : units (units), … … 626 663 basis (basis), 627 664 type (type), 665 ci (caseInsensitive), 628 666 convList (NULL), 629 667 incarnationList (NULL)
Note: See TracChangeset
for help on using the changeset viewer.