Changeset 828 in dev for trunk/ab5.0/jenga
- Timestamp:
- Mar 19, 2012, 1:59:48 AM (13 years ago)
- Location:
- trunk
- Files:
-
- 1 deleted
- 18 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to
/branches/egtra merged eligible
-
Property svn:mergeinfo
set to
-
trunk/ab5.0/jenga/include/MyAssert.h
r738 r828 1 #include <string> 2 1 3 #pragma once 2 4 -
trunk/ab5.0/jenga/include/common/Binary.h
r582 r828 1 #include <boost/serialization/serialization.hpp> 2 #include <boost/serialization/split_member.hpp> 3 1 4 #pragma once 2 5 -
trunk/ab5.0/jenga/include/common/BoostSerializationSupport.h
r523 r828 1 #include <iosfwd> 2 #include <string> 3 1 4 #pragma once 2 5 -
trunk/ab5.0/jenga/include/common/CmdLine.h
r518 r828 1 #include <string> 2 1 3 #pragma once 2 4 -
trunk/ab5.0/jenga/include/common/Directory.h
r680 r828 1 #include <string> 2 1 3 #pragma once 2 4 -
trunk/ab5.0/jenga/include/common/EasyToken.h
r622 r828 1 #include <string> 2 1 3 #pragma once 2 4 -
trunk/ab5.0/jenga/include/common/Environment.h
r718 r828 1 #include <string> 2 #include <windows.h> 3 #include <shlobj.h> 4 1 5 #pragma once 2 6 … … 4 8 namespace Common{ 5 9 6 #pragma warning(disable : 4996)7 10 8 11 class Environment -
trunk/ab5.0/jenga/include/common/Exception.h
r524 r828 1 #include <string> 2 1 3 #pragma once 2 4 -
trunk/ab5.0/jenga/include/common/File.h
r769 r828 1 #include <string> 2 #include <Windows.h> 3 1 4 #pragma once 2 #pragma warning(disable : 4996)3 5 4 6 -
trunk/ab5.0/jenga/include/common/FileSystem.h
r694 r828 1 #include <string> 2 1 3 #pragma once 2 4 -
trunk/ab5.0/jenga/include/common/Hashmap.h
r639 r828 1 #include <stdexcept> 2 #include <unordered_set> 3 #include <boost/range/algorithm.hpp> 4 #include <boost/checked_delete.hpp> 5 #include <boost/iterator/transform_iterator.hpp> 6 #include <boost/cast.hpp> 7 #include <boost/serialization/serialization.hpp> 8 #include <boost/serialization/split_member.hpp> 9 1 10 #pragma once 2 11 … … 5 14 namespace Common{ 6 15 16 template<class T> 17 class ObjectInHashmap; 18 template<class T> 19 struct ObjectInHashmapHash; 20 template<class T> 21 struct ObjectInHashmapEqualTo; 7 22 8 23 #define MAX_HASHMAP 65535 9 template<class T> class Hashmap 10 { 11 T* hashArray[MAX_HASHMAP]; 24 template<class T> class Hashmap : boost::noncopyable 25 { 26 typedef std::unordered_set<ObjectInHashmap<T>*, ObjectInHashmapHash<T>, ObjectInHashmapEqualTo<T>> MapType; 27 MapType map; 28 29 struct downcast 30 { 31 typedef T* result_type; 32 T* operator ()(ObjectInHashmap<T>* p) const 33 { 34 return boost::polymorphic_cast<T*>(p); 35 } 36 }; 37 struct const_downcast 38 { 39 typedef T const* result_type; 40 T const* operator ()(ObjectInHashmap<T> const* p) const 41 { 42 return boost::polymorphic_cast<T const*>(p); 43 } 44 }; 12 45 13 46 public: 14 virtual int GetHash( const char *keyName ) const15 {16 int key;17 for(key=0;*keyName!='\0';keyName++){18 key=((key<<8)+ *keyName )%MAX_HASHMAP;19 }20 return key;21 }22 47 23 48 Hashmap() 24 : isIteratorReady( false ) 25 { 26 memset( hashArray, 0, MAX_HASHMAP*sizeof(T*) ); 49 { 27 50 } 28 51 ~Hashmap() … … 32 55 void Clear() 33 56 { 34 for( int i=0; i<MAX_HASHMAP; i++ ) 35 { 36 T* temp = hashArray[i]; 37 if( temp ) 38 { 39 delete temp; 40 } 41 } 42 memset( hashArray, 0, MAX_HASHMAP*sizeof(T*) ); 57 boost::for_each(*this, boost::checked_deleter<T const>()); 58 map.clear(); 43 59 } 44 60 … … 46 62 void PullOutAll() 47 63 { 48 memset( hashArray, 0, MAX_HASHMAP*sizeof(T*) ); 49 } 50 51 bool Put( T* value ) 52 { 53 int key = GetHash( value->GetKeyName().c_str() ); 54 55 if(hashArray[key]){ 56 T *temp = hashArray[key]; 57 while( true ){ 58 if( temp->IsDuplication( value ) ) 59 { 60 // 重複している 61 return false; 62 } 63 64 if( temp->GetChainNext() == NULL ) 65 { 66 break; 67 } 68 temp = temp->GetChainNext(); 69 } 70 temp->SetChainNext( value ); 71 } 72 else{ 73 hashArray[key] = value; 74 } 75 76 return true; 77 } 78 79 T* GetHashArrayElement( const char *keyName ) 80 { 81 return hashArray[GetHash(keyName)]; 82 } 83 const T* GetHashArrayElement( const char *keyName ) const 84 { 85 return hashArray[GetHash(keyName)]; 86 } 87 88 bool IsExistDuplicationKeyName( const std::string &keyName ) const 89 { 90 int key = GetHash( keyName.c_str() ); 91 92 if(hashArray[key]){ 93 const T *temp = hashArray[key]; 94 while( true ){ 95 if( temp->IsDuplication( keyName ) ) 96 { 97 // 重複している 98 return true; 99 } 100 101 if( temp->GetChainNext() == NULL ) 102 { 103 break; 104 } 105 temp = temp->GetChainNext(); 106 } 107 } 108 109 return false; 64 map.clear(); 65 } 66 67 bool Put(T* value) 68 { 69 if (value == nullptr) 70 { 71 throw std::invalid_argument("Hashmap::Put"); 72 } 73 return map.insert(value).second; 74 } 75 76 typedef boost::transform_iterator<downcast, typename MapType::local_iterator> local_iterator; 77 typedef boost::transform_iterator<const_downcast, typename MapType::const_local_iterator> const_local_iterator; 78 79 boost::iterator_range<local_iterator> GetHashArrayElement(std::string const& keyName) 80 { 81 ObjectInHashmapDummy<T> t(keyName); 82 return boost::iterator_range<local_iterator>( 83 local_iterator(map.begin(map.bucket(&t)), downcast()), 84 local_iterator(map.end(map.bucket(&t)), downcast())); 85 } 86 87 boost::iterator_range<const_local_iterator> GetHashArrayElement(std::string const& keyName) const 88 { 89 ObjectInHashmapDummy<T> t(keyName); 90 return boost::iterator_range<const_local_iterator>( 91 const_local_iterator(map.begin(map.bucket(&t)), const_downcast()), 92 const_local_iterator(map.end(map.bucket(&t)), const_downcast())); 93 } 94 95 bool IsExistDuplicationKeyName(const std::string &keyName) const 96 { 97 ObjectInHashmapDummy<T> t(keyName); 98 return map.find(&t) != map.end(); 110 99 } 111 100 112 101 bool IsExist( const T* value ) const 113 102 { 114 int key = GetHash( value->GetKeyName().c_str() ); 115 116 if(hashArray[key]){ 117 const T *temp = hashArray[key]; 118 while( true ){ 119 if( temp->IsDuplication( value ) ) 120 { 121 // 重複している 122 return true; 123 } 124 125 if( temp->GetChainNext() == NULL ) 126 { 127 break; 128 } 129 temp = temp->GetChainNext(); 130 } 131 } 132 133 return false; 134 } 135 136 const T *FindLike( const T* value ) const 137 { 138 int key = GetHash( value->GetKeyName().c_str() ); 139 140 if(hashArray[key]){ 141 const T *temp = hashArray[key]; 142 while( true ){ 143 if( temp->IsDuplication( value ) ) 144 { 145 // 重複している 146 return temp; 147 } 148 149 if( temp->GetChainNext() == NULL ) 150 { 151 break; 152 } 153 temp = temp->GetChainNext(); 154 } 155 } 156 157 return NULL; 103 return map.find(const_cast<T*>(value)) != map.end(); 104 } 105 106 const T *FindLike( const ObjectInHashmap<T>* value ) const 107 { 108 auto it = map.find(const_cast<ObjectInHashmap<T>*>(value)); 109 return it != map.end() 110 ? boost::polymorphic_downcast<T const*>(*it) 111 : nullptr; 158 112 } 159 113 … … 162 116 // イテレータ 163 117 ///////////////////////////////////////////////////////////////// 164 private: 165 mutable std::vector<T*> iterator_Objects; 166 mutable int iterator_CurrentNext; 167 mutable bool isIteratorReady; 168 public: 169 void Iterator_Init() const 170 { 171 iterator_Objects.clear(); 172 iterator_CurrentNext = 0; 173 174 for( int i=0; i<MAX_HASHMAP; i++ ){ 175 if( hashArray[i] ){ 176 T* temp = hashArray[i]; 177 while( temp ) 178 { 179 iterator_Objects.push_back( temp ); 180 181 temp = (T*)temp->GetChainNext(); 182 } 183 } 184 } 185 186 isIteratorReady = true; 187 } 188 void Iterator_Reset() const 189 { 190 if( !isIteratorReady ) 191 { 192 Jenga::Throw( "イテレータの準備ができていない" ); 193 } 194 iterator_CurrentNext = 0; 195 } 196 bool Iterator_HasNext() const 197 { 198 return ( iterator_CurrentNext < (int)iterator_Objects.size() ); 199 } 200 T *Iterator_GetNext() const 201 { 202 return iterator_Objects[iterator_CurrentNext++]; 203 } 204 int Iterator_GetMaxCount() const 205 { 206 return (int)iterator_Objects.size(); 207 } 208 118 //typedef boost::transform_iterator<downcast, typename MapType::iterator> iterator; 119 typedef boost::transform_iterator<downcast, typename MapType::const_iterator> const_iterator; 120 typedef const_iterator iterator; 121 typedef typename MapType::size_type size_type; 122 typedef typename MapType::difference_type difference_type; 123 //iterator begin() 124 //{ 125 // return iterator(map.begin(), downcast()); 126 //} 127 //iterator end() 128 //{ 129 // return iterator(map.end(), downcast()); 130 //} 131 const_iterator begin() const 132 { 133 return const_iterator(map.begin(), downcast()); 134 } 135 const_iterator end() const 136 { 137 return const_iterator(map.end(), downcast()); 138 } 209 139 210 140 // XMLシリアライズ用 … … 215 145 { 216 146 std::vector<T *> objects; 217 ar & BOOST_SERIALIZATION_NVP( objects ); 218 219 // 読み込み後の処理 147 ar & BOOST_SERIALIZATION_NVP(objects); 220 148 Clear(); 221 BOOST_FOREACH( T *object, objects ) 222 { 223 Put( object ); 224 } 225 Iterator_Init(); 149 map = boost::copy_range<MapType>(objects); 226 150 } 227 151 template<class Archive> void save(Archive& ar, const unsigned int version) const 228 152 { 229 // 保存準備230 std::vector<T *> objects;231 objects.clear();232 Iterator_Reset();233 while( Iterator_HasNext() ) 234 { 235 objects.push_back( Iterator_GetNext() ); 236 } 237 238 ar & BOOST_SERIALIZATION_NVP( objects );239 }240 }; 241 242 template<class T> class ObjectInHashmap 243 { 244 T *pNextObject; 153 std::vector<T *> objects(begin(), end()); 154 ar & BOOST_SERIALIZATION_NVP(objects); 155 } 156 }; 157 158 template<class T> 159 class ObjectInHashmap 160 { 161 protected: 162 ObjectInHashmap() 163 { 164 } 165 ~ObjectInHashmap() 166 { 167 } 168 245 169 public: 246 247 ObjectInHashmap()248 : pNextObject( NULL )249 {250 }251 ~ObjectInHashmap()252 {253 if( pNextObject )254 {255 delete pNextObject;256 }257 }258 259 170 virtual const std::string &GetKeyName() const = 0; 260 171 virtual bool IsDuplication( const T *value ) const … … 266 177 return ( GetKeyName() == keyName ); 267 178 } 268 269 T *GetChainNext() 270 { 271 return pNextObject; 272 } 273 const T *GetChainNext() const 274 { 275 return pNextObject; 276 } 277 void SetChainNext( T *pNextObject ) 278 { 279 this->pNextObject = pNextObject; 179 }; 180 181 template<class T> 182 struct ObjectInHashmapHash 183 { 184 typedef std::size_t result_type; 185 std::size_t operator ()(ObjectInHashmap<T> const* x) const 186 { 187 return std::hash<std::string>()(x->GetKeyName()); 188 } 189 }; 190 191 // GetHashArrayElementなどで文字列から検索するために用いる。 192 template<class T> 193 class ObjectInHashmapDummy : public ObjectInHashmap<T>, boost::noncopyable 194 { 195 public: 196 explicit ObjectInHashmapDummy(std::string const& s) : str(s) {} 197 198 virtual std::string const& GetKeyName() const override 199 { 200 return str; 201 } 202 203 virtual bool IsDuplication(T const* value) const override 204 { 205 return value != nullptr 206 && value->ObjectInHashmap<T>::IsDuplication(str); 207 } 208 209 private: 210 std::string const& str; 211 }; 212 213 template<class T> 214 struct ObjectInHashmapEqualTo 215 { 216 typedef bool result_type; 217 bool operator ()(_In_ ObjectInHashmap<T> const* lhs, _In_ ObjectInHashmap<T> const* rhs) const 218 { 219 assert(lhs != nullptr); 220 assert(rhs != nullptr); 221 if (auto pl = dynamic_cast<T const*>(rhs)) 222 { 223 return lhs->IsDuplication(pl); 224 } 225 else 226 { 227 return rhs->IsDuplication(dynamic_cast<T const*>(lhs)); 228 } 280 229 } 281 230 }; -
trunk/ab5.0/jenga/include/common/Path.h
r749 r828 1 #include <string> 2 1 3 #pragma once 2 4 -
trunk/ab5.0/jenga/include/common/SourceTemplate.h
r625 r828 1 #include <map> 2 #include <string> 3 1 4 #pragma once 2 5 -
trunk/ab5.0/jenga/include/common/String.h
r763 r828 1 #include <string> 2 #include <vector> 3 1 4 #pragma once 2 5 -
trunk/ab5.0/jenga/include/common/VectorSupporter.h
r624 r828 7 7 template<class T> void EraseVectorItem( T &v, int index ) 8 8 { 9 T::iterator it = v.begin(); 10 int i = 0; 11 while( i < index ) 12 { 13 i ++; 14 it ++; 15 } 16 v.erase( it ); 9 v.erase(v.begin() + index); 17 10 } 18 11 -
trunk/ab5.0/jenga/projects/jenga/stdafx.h
r763 r828 1 1 #pragma once 2 2 3 #include <algorithm> 3 4 #include <map> 4 5 #include <string> … … 21 22 #include <assert.h> 22 23 23 //boost libraries24 24 #include <boost/foreach.hpp> 25 #include <boost/implicit_cast.hpp> 26 #include <boost/range/algorithm.hpp> 25 27 #include <boost/serialization/serialization.hpp> 26 28 #include <boost/serialization/nvp.hpp> -
trunk/ab5.0/jenga/src/common/String.cpp
r763 r828 1 1 #include "stdafx.h" 2 #include <algorithm> 2 #include <boost/numeric/conversion/cast.hpp> 3 #include <boost/algorithm/string/replace.hpp> 3 4 4 bool Jenga::Common::IsExistString( const Jenga::Common::Strings &strings, const std::string &findStr ) 5 using boost::numeric_cast; 6 using boost::implicit_cast; 7 8 bool Jenga::Common::IsExistString(const Jenga::Common::Strings &strings, const std::string &findStr) 5 9 { 6 return std::find( strings.begin(), strings.end(), findStr) != strings.end();10 return boost::find(strings, findStr) != strings.end(); 7 11 } 8 12 9 std::string& Jenga::Common::StringReplace( std::string& str, const std::string &sb, const std::string &sa)13 std::string& Jenga::Common::StringReplace(std::string& str, const std::string &sb, const std::string &sa) 10 14 { 11 std::string::size_type n, nb = 0; 12 13 while ((n = str.find(sb,nb)) != std::string::npos) 14 { 15 str.replace(n,sb.size(),sa); 16 nb = n + sa.size(); 17 } 18 15 boost::algorithm::replace_all(str, sb, sa); 19 16 return str; 20 17 } 21 18 22 std::string Jenga::Common::ToString( int n)19 std::string Jenga::Common::ToString(int n) 23 20 { 24 char temp[255]; 25 wsprintf( temp, "%d", n ); 26 return temp; 21 // VC++ 2010だとこのキャストが必要。 22 return std::to_string(implicit_cast<long long>(n)); 27 23 } 28 24 29 25 std::string Jenga::Common::ToString( const std::wstring &wstr ) 30 26 { 31 int needSize = WideCharToMultiByte( 32 CP_THREAD_ACP, 27 if (wstr.empty()) 28 { 29 return std::string(); 30 } 31 int srcSize = numeric_cast<int>(wstr.size()); 32 int needSize = WideCharToMultiByte( 33 CP_ACP, 33 34 0, 34 wstr.data(), static_cast<int>(wstr.size()), 35 NULL, NULL, 36 NULL, NULL ); 35 wstr.data(), srcSize, 36 nullptr, 0, 37 nullptr, nullptr); 38 if (needSize <= 0) 39 { 40 throw std::runtime_error("WideCharToMultiByte error"); 41 } 37 42 38 char *pstr = (char *)calloc( needSize, 1);39 WideCharToMultiByte(40 CP_ THREAD_ACP,43 std::string ret(needSize, '\0'); 44 int res = WideCharToMultiByte( 45 CP_ACP, 41 46 0, 42 wstr.data(), static_cast<int>(wstr.size()), 43 pstr, needSize, 44 NULL, NULL ); 47 wstr.data(), srcSize, 48 &ret[0], needSize, 49 nullptr, nullptr); 50 if (res <= 0) 51 { 52 throw std::runtime_error("WideCharToMultiByte error"); 53 } 45 54 46 std::string result(pstr, needSize); 47 48 free( pstr ); 49 50 return result; 55 return ret; 51 56 } 52 57 53 std::wstring Jenga::Common::ToWString( const std::string &str)58 std::wstring Jenga::Common::ToWString(const std::string &str) 54 59 { 55 int size = MultiByteToWideChar( 60 if (str.empty()) 61 { 62 return std::wstring(); 63 } 64 int srcSize = numeric_cast<int>(str.size()); 65 int needSize = MultiByteToWideChar( 56 66 CP_ACP, 57 67 0, 58 str.data(), static_cast<int>(str.size()), 59 NULL, 0 ); 68 str.data(), srcSize, 69 nullptr, 0); 70 if (needSize <= 0) 71 { 72 throw std::runtime_error("WideCharToMultiByte error"); 73 } 60 74 61 LPWSTR pwstr = (LPWSTR)calloc( size, sizeof (wchar_t) ); 62 63 MultiByteToWideChar( 75 std::wstring ret(needSize, L'\0'); 76 int res = MultiByteToWideChar( 64 77 CP_ACP, 65 78 0, 66 str.data(), static_cast<int>(str.size()), 67 pwstr, size ); 79 str.data(), srcSize, 80 &ret[0], needSize); 81 if (res <= 0) 82 { 83 throw std::runtime_error("WideCharToMultiByte error"); 84 } 68 85 69 std::wstring wstr( pwstr, size ); 70 71 free( pwstr ); 72 73 return wstr; 86 return ret; 74 87 } 75 88 76 89 bool Jenga::Common::IsIdentifierTopChar( char c ) 77 90 { 78 return ( isalpha( c ) || c == '_' ); 91 return ('A' <= c && c <= 'Z') 92 || ('a' <= c && c <= 'z') 93 || c == '_' ; 79 94 } 80 95 81 96 bool Jenga::Common::IsIdentifierChar( char c ) 82 97 { 83 return ( IsIdentifierTopChar( c ) || isdigit( c ) ); 98 return IsIdentifierTopChar(c) 99 || ('0' <= c && c <= '9'); 84 100 }
Note:
See TracChangeset
for help on using the changeset viewer.