My Project
sql_hset.h
00001 #ifndef SQL_HSET_INCLUDED
00002 #define SQL_HSET_INCLUDED
00003 /* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; version 2 of the License.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program; if not, write to the Free Software
00016    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
00017 
00018 #include "my_global.h"
00019 #include "hash.h"
00020 
00021 
00026 template <typename T, my_hash_get_key K>
00027 class Hash_set
00028 {
00029 public:
00030   typedef T Value_type;
00031   enum { START_SIZE= 8 };
00036   Hash_set()
00037   {
00038     my_hash_clear(&m_hash);
00039   }
00044   ~Hash_set()
00045   {
00046     my_hash_free(&m_hash);
00047   }
00057   bool insert(T *value)
00058   {
00059     my_hash_init_opt(&m_hash, &my_charset_bin, START_SIZE, 0, 0, K, 0, MYF(0));
00060     size_t key_len;
00061     const uchar *key= K(reinterpret_cast<uchar*>(value), &key_len, FALSE);
00062     if (my_hash_search(&m_hash, key, key_len) == NULL)
00063       return my_hash_insert(&m_hash, reinterpret_cast<uchar *>(value));
00064     return FALSE;
00065   }
00067   bool is_empty() const { return m_hash.records == 0; }
00069   size_t size() const { return static_cast<size_t>(m_hash.records); }
00071   class Iterator
00072   {
00073   public:
00074     Iterator(Hash_set &hash_set)
00075       : m_hash(&hash_set.m_hash),
00076         m_idx(0)
00077     {}
00082     inline T *operator++(int)
00083     {
00084       if (m_idx < m_hash->records)
00085         return reinterpret_cast<T*>(my_hash_element(m_hash, m_idx++));
00086       return NULL;
00087     }
00088     void rewind() { m_idx= 0; }
00089   private:
00090     HASH *m_hash;
00091     uint m_idx;
00092   };
00093 private:
00094   HASH m_hash;
00095 };
00096 
00097 #endif // SQL_HSET_INCLUDED
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines