source: dev/trunk/abdev/BasicCompiler_Common/include/Hashmap.h@ 238

Last change on this file since 238 was 215, checked in by dai_9181, 17 years ago

BoostSerializationSupportのクラステンプレートインスタンスを明示的に生成するようにした(コンパイル時間の短縮)

File size: 4.1 KB
Line 
1#pragma once
2
3#include <vector>
4
5#include <memory.h>
6
7#include <jenga/include/common/Exception.h>
8
9#include <BoostSerializationSupport.h>
10#include <logger.h>
11
12
13namespace Jenga{
14namespace Common{
15
16
17#define MAX_HASHMAP 65535
18template<class T> class Hashmap
19{
20 T* hashArray[MAX_HASHMAP];
21
22public:
23 virtual int GetHash( const char *keyName ) const
24 {
25 int key;
26 for(key=0;*keyName!='\0';keyName++){
27 key=((key<<8)+ *keyName )%MAX_HASHMAP;
28 }
29 return key;
30 }
31
32 Hashmap()
33 : isIteratorReady( false )
34 {
35 memset( hashArray, 0, MAX_HASHMAP*sizeof(T*) );
36 }
37 ~Hashmap()
38 {
39 Clear();
40 }
41 void Clear()
42 {
43 for( int i=0; i<MAX_HASHMAP; i++ )
44 {
45 T* temp = hashArray[i];
46 if( temp )
47 {
48 delete temp;
49 }
50 }
51 memset( hashArray, 0, MAX_HASHMAP*sizeof(T*) );
52 }
53
54 bool Put( T* value )
55 {
56 int key = GetHash( value->GetKeyName().c_str() );
57
58 if(hashArray[key]){
59 T *temp = hashArray[key];
60 while( true ){
61 if( temp->IsDuplication( value ) )
62 {
63 // 重複している
64 return false;
65 }
66
67 if( temp->GetChainNext() == NULL )
68 {
69 break;
70 }
71 temp = temp->GetChainNext();
72 }
73 temp->SetChainNext( value );
74 }
75 else{
76 hashArray[key] = value;
77 }
78
79 return true;
80 }
81
82 T* GetHashArrayElement( const char *keyName )
83 {
84 return hashArray[GetHash(keyName)];
85 }
86
87 bool IsExist( const std::string &keyName ) const
88 {
89 int key = GetHash( keyName.c_str() );
90
91 if(hashArray[key]){
92 T *temp = hashArray[key];
93 while( true ){
94 if( temp->IsDuplication( keyName ) )
95 {
96 // 重複している
97 return true;
98 }
99
100 if( temp->GetChainNext() == NULL )
101 {
102 break;
103 }
104 temp = temp->GetChainNext();
105 }
106 }
107
108 return false;
109 }
110
111
112 /////////////////////////////////////////////////////////////////
113 // イテレータ
114 /////////////////////////////////////////////////////////////////
115private:
116 mutable std::vector<T*> iterator_Objects;
117 mutable int iterator_CurrentNext;
118 mutable bool isIteratorReady;
119public:
120 void Iterator_Init() const
121 {
122 iterator_Objects.clear();
123 iterator_CurrentNext = 0;
124
125 for( int i=0; i<MAX_HASHMAP; i++ ){
126 if( hashArray[i] ){
127 T* temp = hashArray[i];
128 while( temp )
129 {
130 iterator_Objects.push_back( temp );
131
132 temp = (T*)temp->GetChainNext();
133 }
134 }
135 }
136
137 isIteratorReady = true;
138 }
139 void Iterator_Reset() const
140 {
141 if( !isIteratorReady )
142 {
143 Jenga::Throw( "イテレータの準備ができていない" );
144 }
145 iterator_CurrentNext = 0;
146 }
147 bool Iterator_HasNext() const
148 {
149 return ( iterator_CurrentNext < (int)iterator_Objects.size() );
150 }
151 T *Iterator_GetNext() const
152 {
153 return iterator_Objects[iterator_CurrentNext++];
154 }
155 int Iterator_GetMaxCount() const
156 {
157 return (int)iterator_Objects.size();
158 }
159
160
161 // XMLシリアライズ用
162private:
163 friend class boost::serialization::access;
164 BOOST_SERIALIZATION_SPLIT_MEMBER();
165 template<class Archive> void load(Archive& ar, const unsigned int version)
166 {
167 std::vector<T *> objects;
168 ar & BOOST_SERIALIZATION_NVP( objects );
169
170 // 読み込み後の処理
171 Clear();
172 BOOST_FOREACH( T *object, objects )
173 {
174 Put( object );
175 }
176 Iterator_Init();
177 }
178 template<class Archive> void save(Archive& ar, const unsigned int version) const
179 {
180 // 保存準備
181 std::vector<T *> objects;
182 objects.clear();
183 Iterator_Reset();
184 while( Iterator_HasNext() )
185 {
186 objects.push_back( Iterator_GetNext() );
187 }
188
189 ar & BOOST_SERIALIZATION_NVP( objects );
190 }
191};
192
193template<class T> class ObjectInHashmap
194{
195 T *pNextObject;
196public:
197
198 ObjectInHashmap()
199 : pNextObject( NULL )
200 {
201 }
202 ~ObjectInHashmap()
203 {
204 if( pNextObject )
205 {
206 delete pNextObject;
207 }
208 }
209
210 virtual const std::string &GetKeyName() const = 0;
211 virtual bool IsDuplication( const T *value ) const
212 {
213 return ( GetKeyName() == value->GetName() );
214 }
215 virtual bool IsDuplication( const std::string &keyName ) const
216 {
217 return ( GetKeyName() == keyName );
218 }
219
220 T *GetChainNext()
221 {
222 return pNextObject;
223 }
224 void SetChainNext( T *pNextObject )
225 {
226 this->pNextObject = pNextObject;
227 }
228};
229
230
231}}
Note: See TracBrowser for help on using the repository browser.