Home | History | Annotate | Line # | Download | only in gcc
hash-set.h revision 1.1.1.4
      1      1.1  mrg /* A type-safe hash set.
      2  1.1.1.4  mrg    Copyright (C) 2014-2018 Free Software Foundation, Inc.
      3      1.1  mrg 
      4      1.1  mrg This file is part of GCC.
      5      1.1  mrg 
      6      1.1  mrg GCC is free software; you can redistribute it and/or modify it under
      7      1.1  mrg the terms of the GNU General Public License as published by the Free
      8      1.1  mrg Software Foundation; either version 3, or (at your option) any later
      9      1.1  mrg version.
     10      1.1  mrg 
     11      1.1  mrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
     12      1.1  mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
     13      1.1  mrg FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     14      1.1  mrg for more details.
     15      1.1  mrg 
     16      1.1  mrg You should have received a copy of the GNU General Public License
     17      1.1  mrg along with GCC; see the file COPYING3.  If not see
     18      1.1  mrg <http://www.gnu.org/licenses/>.  */
     19      1.1  mrg 
     20      1.1  mrg 
     21      1.1  mrg #ifndef hash_set_h
     22      1.1  mrg #define hash_set_h
     23      1.1  mrg 
     24  1.1.1.2  mrg template<typename KeyId, typename Traits = default_hash_traits<KeyId> >
     25      1.1  mrg class hash_set
     26      1.1  mrg {
     27      1.1  mrg public:
     28  1.1.1.2  mrg   typedef typename Traits::value_type Key;
     29  1.1.1.2  mrg   explicit hash_set (size_t n = 13, bool ggc = false CXX_MEM_STAT_INFO)
     30  1.1.1.2  mrg     : m_table (n, ggc, GATHER_STATISTICS, HASH_SET_ORIGIN PASS_MEM_STAT) {}
     31      1.1  mrg 
     32      1.1  mrg   /* Create a hash_set in gc memory with space for at least n elements.  */
     33      1.1  mrg 
     34      1.1  mrg   static hash_set *
     35      1.1  mrg     create_ggc (size_t n)
     36      1.1  mrg       {
     37      1.1  mrg 	hash_set *set = ggc_alloc<hash_set> ();
     38      1.1  mrg 	new (set) hash_set (n, true);
     39      1.1  mrg 	return set;
     40      1.1  mrg       }
     41      1.1  mrg 
     42      1.1  mrg   /* If key k isn't already in the map add it to the map, and
     43      1.1  mrg      return false.  Otherwise return true.  */
     44      1.1  mrg 
     45      1.1  mrg   bool add (const Key &k)
     46      1.1  mrg     {
     47  1.1.1.2  mrg       Key *e = m_table.find_slot_with_hash (k, Traits::hash (k), INSERT);
     48  1.1.1.2  mrg       bool existed = !Traits::is_empty (*e);
     49      1.1  mrg       if (!existed)
     50  1.1.1.2  mrg 	*e = k;
     51      1.1  mrg 
     52      1.1  mrg       return existed;
     53      1.1  mrg     }
     54      1.1  mrg 
     55      1.1  mrg   /* if the passed in key is in the map return its value otherwise NULL.  */
     56      1.1  mrg 
     57      1.1  mrg   bool contains (const Key &k)
     58      1.1  mrg     {
     59  1.1.1.2  mrg       Key &e = m_table.find_with_hash (k, Traits::hash (k));
     60  1.1.1.2  mrg       return !Traits::is_empty (e);
     61  1.1.1.2  mrg     }
     62  1.1.1.2  mrg 
     63  1.1.1.2  mrg   void remove (const Key &k)
     64  1.1.1.2  mrg     {
     65  1.1.1.2  mrg       m_table.remove_elt_with_hash (k, Traits::hash (k));
     66      1.1  mrg     }
     67      1.1  mrg 
     68      1.1  mrg   /* Call the call back on each pair of key and value with the passed in
     69      1.1  mrg      arg.  */
     70      1.1  mrg 
     71  1.1.1.2  mrg   template<typename Arg, bool (*f)(const typename Traits::value_type &, Arg)>
     72      1.1  mrg   void traverse (Arg a) const
     73      1.1  mrg     {
     74  1.1.1.2  mrg       for (typename hash_table<Traits>::iterator iter = m_table.begin ();
     75      1.1  mrg 	   iter != m_table.end (); ++iter)
     76  1.1.1.2  mrg 	f (*iter, a);
     77      1.1  mrg     }
     78      1.1  mrg 
     79      1.1  mrg   /* Return the number of elements in the set.  */
     80      1.1  mrg 
     81      1.1  mrg   size_t elements () const { return m_table.elements (); }
     82      1.1  mrg 
     83  1.1.1.4  mrg   /* Clear the hash table.  */
     84  1.1.1.4  mrg 
     85  1.1.1.4  mrg   void empty () { m_table.empty (); }
     86  1.1.1.4  mrg 
     87  1.1.1.2  mrg   class iterator
     88  1.1.1.2  mrg   {
     89  1.1.1.2  mrg   public:
     90  1.1.1.2  mrg     explicit iterator (const typename hash_table<Traits>::iterator &iter) :
     91  1.1.1.2  mrg       m_iter (iter) {}
     92  1.1.1.2  mrg 
     93  1.1.1.2  mrg     iterator &operator++ ()
     94  1.1.1.2  mrg       {
     95  1.1.1.2  mrg 	++m_iter;
     96  1.1.1.2  mrg 	return *this;
     97  1.1.1.2  mrg       }
     98  1.1.1.2  mrg 
     99  1.1.1.2  mrg     Key
    100  1.1.1.2  mrg     operator* ()
    101  1.1.1.2  mrg       {
    102  1.1.1.2  mrg 	return *m_iter;
    103  1.1.1.2  mrg       }
    104  1.1.1.2  mrg 
    105  1.1.1.2  mrg     bool
    106  1.1.1.2  mrg     operator != (const iterator &other) const
    107  1.1.1.2  mrg       {
    108  1.1.1.2  mrg 	return m_iter != other.m_iter;
    109  1.1.1.2  mrg       }
    110  1.1.1.2  mrg 
    111  1.1.1.2  mrg   private:
    112  1.1.1.2  mrg     typename hash_table<Traits>::iterator m_iter;
    113  1.1.1.2  mrg   };
    114  1.1.1.2  mrg 
    115  1.1.1.2  mrg   /* Standard iterator retrieval methods.  */
    116  1.1.1.2  mrg 
    117  1.1.1.2  mrg   iterator begin () const { return iterator (m_table.begin ()); }
    118  1.1.1.2  mrg   iterator end () const { return iterator (m_table.end ()); }
    119  1.1.1.2  mrg 
    120  1.1.1.2  mrg 
    121      1.1  mrg private:
    122      1.1  mrg 
    123      1.1  mrg   template<typename T, typename U> friend void gt_ggc_mx (hash_set<T, U> *);
    124      1.1  mrg   template<typename T, typename U> friend void gt_pch_nx (hash_set<T, U> *);
    125      1.1  mrg       template<typename T, typename U> friend void gt_pch_nx (hash_set<T, U> *, gt_pointer_operator, void *);
    126      1.1  mrg 
    127  1.1.1.2  mrg   hash_table<Traits> m_table;
    128      1.1  mrg };
    129      1.1  mrg 
    130  1.1.1.4  mrg /* Generic hash_set<TYPE> debug helper.
    131  1.1.1.4  mrg 
    132  1.1.1.4  mrg    This needs to be instantiated for each hash_set<TYPE> used throughout
    133  1.1.1.4  mrg    the compiler like this:
    134  1.1.1.4  mrg 
    135  1.1.1.4  mrg     DEFINE_DEBUG_HASH_SET (TYPE)
    136  1.1.1.4  mrg 
    137  1.1.1.4  mrg    The reason we have a debug_helper() is because GDB can't
    138  1.1.1.4  mrg    disambiguate a plain call to debug(some_hash), and it must be called
    139  1.1.1.4  mrg    like debug<TYPE>(some_hash).  */
    140  1.1.1.4  mrg template<typename T>
    141  1.1.1.4  mrg void
    142  1.1.1.4  mrg debug_helper (hash_set<T> &ref)
    143  1.1.1.4  mrg {
    144  1.1.1.4  mrg   for (typename hash_set<T>::iterator it = ref.begin ();
    145  1.1.1.4  mrg        it != ref.end (); ++it)
    146  1.1.1.4  mrg     {
    147  1.1.1.4  mrg       debug_slim (*it);
    148  1.1.1.4  mrg       fputc ('\n', stderr);
    149  1.1.1.4  mrg     }
    150  1.1.1.4  mrg }
    151  1.1.1.4  mrg 
    152  1.1.1.4  mrg #define DEFINE_DEBUG_HASH_SET(T) \
    153  1.1.1.4  mrg   template void debug_helper (hash_set<T> &);		\
    154  1.1.1.4  mrg   DEBUG_FUNCTION void					\
    155  1.1.1.4  mrg   debug (hash_set<T> &ref)				\
    156  1.1.1.4  mrg   {							\
    157  1.1.1.4  mrg     debug_helper <T> (ref);				\
    158  1.1.1.4  mrg   }							\
    159  1.1.1.4  mrg   DEBUG_FUNCTION void					\
    160  1.1.1.4  mrg   debug (hash_set<T> *ptr)				\
    161  1.1.1.4  mrg   {							\
    162  1.1.1.4  mrg     if (ptr)						\
    163  1.1.1.4  mrg       debug (*ptr);					\
    164  1.1.1.4  mrg     else						\
    165  1.1.1.4  mrg       fprintf (stderr, "<nil>\n");			\
    166  1.1.1.4  mrg   }
    167  1.1.1.4  mrg 
    168      1.1  mrg /* ggc marking routines.  */
    169      1.1  mrg 
    170      1.1  mrg template<typename K, typename H>
    171      1.1  mrg static inline void
    172      1.1  mrg gt_ggc_mx (hash_set<K, H> *h)
    173      1.1  mrg {
    174      1.1  mrg   gt_ggc_mx (&h->m_table);
    175      1.1  mrg }
    176      1.1  mrg 
    177      1.1  mrg template<typename K, typename H>
    178      1.1  mrg static inline void
    179      1.1  mrg gt_pch_nx (hash_set<K, H> *h)
    180      1.1  mrg {
    181      1.1  mrg   gt_pch_nx (&h->m_table);
    182      1.1  mrg }
    183      1.1  mrg 
    184      1.1  mrg template<typename K, typename H>
    185      1.1  mrg static inline void
    186      1.1  mrg gt_pch_nx (hash_set<K, H> *h, gt_pointer_operator op, void *cookie)
    187      1.1  mrg {
    188      1.1  mrg   op (&h->m_table.m_entries, cookie);
    189      1.1  mrg }
    190      1.1  mrg 
    191      1.1  mrg #endif
    192