00001 /* 00002 Copyright (C) 2005-2006 Feeling Software Inc. 00003 MIT License: http://www.opensource.org/licenses/mit-license.php 00004 */ 00005 00011 #ifndef _FU_UNIQUE_ID_MAP_H_ 00012 #define _FU_UNIQUE_ID_MAP_H_ 00013 00021 template <typename STRING_BUILDER> 00022 class FUUniqueStringMapT : map<typename STRING_BUILDER::String, void*> 00023 { 00024 private: 00025 typedef map<typename STRING_BUILDER::String, void*> Super; 00026 00027 public: 00032 void AddUniqueString(typename STRING_BUILDER::String& wantedStr); 00033 00036 bool Exists(const typename STRING_BUILDER::String& str) const; 00037 00040 void Erase(const typename STRING_BUILDER::String& str); 00041 }; 00042 00043 typedef FUUniqueStringMapT<FUSStringBuilder> FUSUniqueStringMap; 00044 typedef FUUniqueStringMapT<FUStringBuilder> FUUniqueStringMap; 00046 template <typename Builder> 00047 void FUUniqueStringMapT<Builder>::AddUniqueString(typename Builder::String& wantedStr) 00048 { 00049 if (Exists(wantedStr)) 00050 { 00051 // Attempt to generate a new string by appending an increasing counter. 00052 uint32 counter = 2; 00053 static const maxCounter = 256; 00054 Builder buffer; 00055 buffer.reserve(wantedStr.length() + 5); 00056 do 00057 { 00058 buffer.set(wantedStr); 00059 buffer.append(counter); 00060 } while (counter++ < maxCounter && Exists(buffer.ToString())); 00061 00062 // Hopefully, this is now unique. 00063 wantedStr = buffer.ToString(); 00064 } 00065 insert(Super::value_type(wantedStr, NULL)); 00066 } 00067 00068 template <typename Builder> 00069 bool FUUniqueStringMapT<Builder>::Exists(const typename Builder::String& str) const 00070 { 00071 const_iterator it = find(str); 00072 return it != end(); 00073 } 00074 00075 template <typename Builder> 00076 void FUUniqueStringMapT<Builder>::Erase(const typename Builder::String& str) 00077 { 00078 iterator it = find(str); 00079 if (it != end()) erase(it); 00080 } 00081 00082 #endif // _FU_UNIQUE_ID_MAP_H_ 00083