/[enigma]/enigma/src/px/dict.hh
ViewVC logotype

Diff of /enigma/src/px/dict.hh

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by dheck, Sun Mar 9 22:18:02 2003 UTC revision 1.3 by dheck, Sun Mar 23 17:17:21 2003 UTC
# Line 121  namespace px Line 121  namespace px
121  /*  /*
122  ** Construction / destruction  ** Construction / destruction
123  */      */    
124          Dict(size_type table_size = 257)          Dict(size_type table_size = 257);
125          : nentries(0), nbuckets (table_size), hashtab (new Entry*[nbuckets])  
         {  
             for (size_type i=0; i<nbuckets; ++i) hashtab[i] = 0;  
         }  
126      private:      private:
127          Dict (const Dict<T> &d)          Dict (const Dict<T> &d)
128          : nentries(d.nentries),          : nentries(d.nentries),
# Line 150  namespace px Line 147  namespace px
147  /*        /*      
148  ** Lookup of keys  ** Lookup of keys
149  */  */
150          iterator find (const std::string &key) {          iterator find (const std::string &key);
151              unsigned h = hash(key) % nbuckets;          const_iterator find (const std::string &key) const;
             for (Entry *e = hashtab[h]; e != 0; e=e->next)  
                 if (e->pair.first == key)  
                     return iterator(this, h, e);  
             return end();  
         }  
   
         const_iterator find (const std::string &key) const {  
             unsigned h = hash(key) % nbuckets;  
             for (Entry *e = hashtab[h]; e != 0; e=e->next)  
                 if (e->pair.first == key)  
                     return const_iterator(this, h, e);  
             return end();  
         }  
152    
153          T &lookup(const std::string &key) {          T &lookup(const std::string &key) {
154              Entry *e = find_entry(key);              Entry *e = find_entry(key);
# Line 182  namespace px Line 166  namespace px
166          const T& operator[] (const std::string &key) const          const T& operator[] (const std::string &key) const
167          { return lookup(key); }          { return lookup(key); }
168    
169          bool has_key(const std::string &key) const {          bool has_key(const std::string &key) const;
             return find_entry(key) != 0;  
         }  
170    
171  /*  /*
172  ** Insertion of new elements.  ** Insertion of new elements.
# Line 202  namespace px Line 184  namespace px
184  */  */
185    
186          /*! Remove all entries from the hash table. */          /*! Remove all entries from the hash table. */
187          void clear() {          void clear();
             for (int i=0; i<nbuckets; ++i) {  
                 Entry *cur, *next;  
                 for (cur = hashtab[i]; cur != 0; cur=next) {  
                     next = cur->next;  
                     delete cur;  
                 }  
                 hashtab[i] = 0;  
             }  
             nentries = 0;  
         }  
188    
189          /*! Remove the entry with key \var key from the table. */          /*! Remove the entry with key \var key from the table. */
190          void remove(const std::string &key) {          void remove(const std::string &key);
             unsigned h = hash(key) % nbuckets;  
             Entry *e = hashtab[h];  
             Entry **eptr = &hashtab[h];  
   
             while (e != 0) {  
                 if (e->pair.first == key) {  
                     *eptr = e->next;            // Modify pointer referring to e  
                     delete e;  
                     nentries -= 1;  
                     break;  
                 }  
                 eptr = &e->next;  
                 e = e->next;  
             }  
         }  
191    
192      private:      private:
193          Entry *find_entry(const std::string &key) const {          Entry *find_entry(const std::string &key) const {
# Line 245  namespace px Line 202  namespace px
202          size_type nbuckets;     // Number of buckets in `hashtab'          size_type nbuckets;     // Number of buckets in `hashtab'
203          Entry **hashtab;          Entry **hashtab;
204      };      };
205    
206    
207        template <class T>
208        Dict<T>::Dict(size_type table_size)
209        : nentries(0), nbuckets (table_size), hashtab (new Entry*[nbuckets])
210        {
211            for (size_type i=0; i<nbuckets; ++i)
212                hashtab[i] = 0;
213        }
214    
215    
216        template <class T>
217        typename Dict<T>::iterator
218        Dict<T>::find (const std::string &key)
219        {
220            unsigned h = hash(key) % nbuckets;
221            for (Entry *e = hashtab[h]; e != 0; e=e->next)
222                if (e->pair.first == key)
223                    return iterator(this, h, e);
224            return end();
225        }
226    
227        template <class T>
228        typename Dict<T>::const_iterator
229        Dict<T>::find (const std::string &key) const
230        {
231            unsigned h = hash(key) % nbuckets;
232            for (Entry *e = hashtab[h]; e != 0; e=e->next)
233                if (e->pair.first == key)
234                    return const_iterator(this, h, e);
235            return end();
236        }
237    
238        template <class T>
239        void
240        Dict<T>::clear()
241        {
242            for (int i=0; i<nbuckets; ++i) {
243                Entry *cur, *next;
244                for (cur = hashtab[i]; cur != 0; cur=next) {
245                    next = cur->next;
246                    delete cur;
247                }
248                hashtab[i] = 0;
249            }
250            nentries = 0;
251        }
252    
253        template <class T>
254        void
255        Dict<T>::remove(const std::string &key)
256        {
257            unsigned h = hash(key) % nbuckets;
258            Entry *e = hashtab[h];
259            Entry **eptr = &hashtab[h];
260    
261            while (e != 0) {
262                if (e->pair.first == key) {
263                    *eptr = e->next;                // Modify pointer referring to e
264                    delete e;
265                    nentries -= 1;
266                    break;
267                }
268                eptr = &e->next;
269                e = e->next;
270            }
271        }
272    
273        template <class T>
274        bool
275        Dict<T>::has_key(const std::string &key) const
276        {
277            return find_entry(key) != 0;
278        }
279    
280  }  }
281  #endif  #endif

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26