Merge branch 'master' into jwi-bcc64xsingletonwarning
[ACE_TAO.git] / ACE / apps / JAWS2 / JAWS / Assoc_Array.cpp
blob9db941b9496883caa36cb4cde0dcdcf73687b2ce
1 #ifndef JAWS_ASSOC_ARRAY_CPP
2 #define JAWS_ASSOC_ARRAY_CPP
4 #include "ace/config-all.h"
5 #include "JAWS/Assoc_Array.h"
7 template <class KEY, class DATA>
8 JAWS_Assoc_Array<KEY,DATA>::JAWS_Assoc_Array (int maxsize)
9 : k_array_ (0),
10 d_array_ (0),
11 maxsize_ (maxsize)
13 typedef void * ptr_type;
15 this->k_array_ = reinterpret_cast<KEY **> (new ptr_type[this->maxsize_]);
16 if (this->k_array_ == 0)
18 this->maxsize_ = 0;
19 return;
22 this->d_array_ = reinterpret_cast<DATA **> (new ptr_type[this->maxsize_]);
23 if (this->d_array_ == 0)
25 delete[] this->k_array_;
26 this->maxsize_ = 0;
27 return;
30 for (int i = 0; i < this->maxsize_; i++)
32 this->k_array_[i] = 0;
33 this->d_array_[i] = 0;
37 template <class KEY, class DATA>
38 JAWS_Assoc_Array<KEY,DATA>::~JAWS_Assoc_Array ()
40 this->clear ();
42 delete[] reinterpret_cast<void **> (this->k_array_);
43 delete[] reinterpret_cast<void **> (this->d_array_);
45 this->k_array_ = 0;
46 this->d_array_ = 0;
49 template <class KEY, class DATA> int
50 JAWS_Assoc_Array<KEY,DATA>::index (const KEY &k)
52 return this->find_i (k);
55 template <class KEY, class DATA> DATA *
56 JAWS_Assoc_Array<KEY,DATA>::find (const KEY &k)
58 int i = this->find_i (k);
60 return (i < this->maxsize_) ? this->d_array_[i] : 0;
63 template <class KEY, class DATA> DATA *
64 JAWS_Assoc_Array<KEY,DATA>::find_by_index (int i)
66 return ((0 <= i) && (i < this->maxsize_)) ? this->d_array_[i] : 0;
69 template <class KEY, class DATA> DATA *
70 JAWS_Assoc_Array<KEY,DATA>::insert (const KEY &k, const DATA &d)
72 int i = this->find_i (k);
74 if (i == this->maxsize_)
75 return 0;
77 KEY *&kk = this->k_array_[i];
78 DATA *&dd = this->d_array_[i];
80 if (kk == 0)
82 dd = new DATA (d);
83 if (dd == 0)
84 return 0;
86 KEY *kkk = new KEY (k);
87 if (kkk == 0)
89 delete dd;
90 return 0;
92 kk = kkk;
95 return dd;
98 template <class KEY, class DATA> int
99 JAWS_Assoc_Array<KEY,DATA>::remove (const KEY &k)
101 int i = this->find_i (k);
103 if (i == this->maxsize_)
104 return 0;
106 KEY *&kk = this->k_array_[i];
107 DATA *&dd = this->d_array_[i];
109 if (kk != 0)
111 KEY *kkk = kk;
112 kk = 0;
113 delete kkk;
114 delete dd;
115 dd = 0;
116 return 1;
119 return 0;
122 template <class KEY, class DATA> void
123 JAWS_Assoc_Array<KEY,DATA>::clear ()
125 for (int i = 0; i < this->maxsize_; i++)
127 if (this->k_array_[i] != 0)
129 delete this->k_array_[i];
130 delete this->d_array_[i];
132 this->k_array_[i] = 0;
133 this->d_array_[i] = 0;
138 template <class KEY, class DATA> int
139 JAWS_Assoc_Array<KEY,DATA>::find_i (const KEY &k)
141 int j = this->maxsize_;
143 for (int i = 0; i < this->maxsize_; i++)
145 KEY *kk = this->k_array_[i];
146 if (kk)
148 if (*kk == k)
149 return i;
151 else if (j == this->maxsize_)
152 j = i;
154 return j;
157 template <class KEY, class DATA>
158 JAWS_Assoc_Array_Iterator<KEY,DATA>::
159 JAWS_Assoc_Array_Iterator (const JAWS_Assoc_Array<KEY, DATA> &aa)
160 : aa_ (aa),
161 i_ (0),
162 j_ (0)
166 template <class KEY, class DATA>
167 JAWS_Assoc_Array_Iterator<KEY,DATA>::~JAWS_Assoc_Array_Iterator ()
171 template <class KEY, class DATA> KEY *
172 JAWS_Assoc_Array_Iterator<KEY,DATA>::key ()
174 return this->aa_.k_array_[this->i_];
177 template <class KEY, class DATA> DATA *
178 JAWS_Assoc_Array_Iterator<KEY,DATA>::data ()
180 return this->aa_.d_array_[this->i_];
183 template <class KEY, class DATA> int
184 JAWS_Assoc_Array_Iterator<KEY,DATA>::first ()
186 this->i_ = 0;
187 for (this->j_ = 0; this->j_ < this->aa_.maxsize_; this->j_++)
189 if (this->aa_.k_array_[this->j_] != 0)
190 break;
192 return this->next ();
195 template <class KEY, class DATA> int
196 JAWS_Assoc_Array_Iterator<KEY,DATA>::last ()
198 this->j_ = this->aa_.maxsize_;
199 for (this->i_ = this->aa_.maxsize_; this->i_ > 0; this->i_--)
201 if (this->aa_.k_array_[this->i_-1] != 0)
202 break;
205 return (this->i_-- > 0);
208 template <class KEY, class DATA> int
209 JAWS_Assoc_Array_Iterator<KEY,DATA>::next ()
211 if (this->j_ < this->aa_.maxsize_)
213 this->i_ = this->j_;
214 for (this->j_++; this->j_ < this->aa_.maxsize_; this->j_++)
216 if (this->aa_.k_array_[this->j_] != 0)
217 break;
221 return (this->i_ < this->aa_.maxsize_);
224 template <class KEY, class DATA> int
225 JAWS_Assoc_Array_Iterator<KEY,DATA>::previous ()
227 if (this->i_ > 0)
229 for (this->j_ = this->i_; this->i_ > 0; this->i_--)
231 if (this->aa_.k_array_[this->i_-1] != 0)
232 break;
236 if (this->i_ == 0)
237 this->first ();
238 else
239 this->i_--;
241 return 1;
244 template <class KEY, class DATA> int
245 JAWS_Assoc_Array_Iterator<KEY,DATA>::is_done ()
247 return (this->j_ == this->aa_.maxsize_);
250 template <class KEY, class DATA>
251 JAWS_Assoc_Array_Iterator<KEY,DATA>::
252 JAWS_Assoc_Array_Iterator (const JAWS_Assoc_Array_Iterator<KEY, DATA> &aai)
253 : aa_ (aai.aa_),
254 i_ (aai.i_),
255 j_ (aai.j_)
259 template <class KEY, class DATA> void
260 JAWS_Assoc_Array_Iterator<KEY,DATA>::
261 operator= (const JAWS_Assoc_Array_Iterator<KEY, DATA> &)
265 #endif /* !defined (JAWS_ASSOC_ARRAY_CPP) */