MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_hset.h
1 #ifndef SQL_HSET_INCLUDED
2 #define SQL_HSET_INCLUDED
3 /* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
17 
18 #include "my_global.h"
19 #include "hash.h"
20 
21 
26 template <typename T, my_hash_get_key K>
27 class Hash_set
28 {
29 public:
30  typedef T Value_type;
31  enum { START_SIZE= 8 };
37  {
38  my_hash_clear(&m_hash);
39  }
45  {
46  my_hash_free(&m_hash);
47  }
57  bool insert(T *value)
58  {
59  my_hash_init_opt(&m_hash, &my_charset_bin, START_SIZE, 0, 0, K, 0, MYF(0));
60  size_t key_len;
61  const uchar *key= K(reinterpret_cast<uchar*>(value), &key_len, FALSE);
62  if (my_hash_search(&m_hash, key, key_len) == NULL)
63  return my_hash_insert(&m_hash, reinterpret_cast<uchar *>(value));
64  return FALSE;
65  }
67  bool is_empty() const { return m_hash.records == 0; }
69  size_t size() const { return static_cast<size_t>(m_hash.records); }
71  class Iterator
72  {
73  public:
74  Iterator(Hash_set &hash_set)
75  : m_hash(&hash_set.m_hash),
76  m_idx(0)
77  {}
82  inline T *operator++(int)
83  {
84  if (m_idx < m_hash->records)
85  return reinterpret_cast<T*>(my_hash_element(m_hash, m_idx++));
86  return NULL;
87  }
88  void rewind() { m_idx= 0; }
89  private:
90  HASH *m_hash;
91  uint m_idx;
92  };
93 private:
94  HASH m_hash;
95 };
96 
97 #endif // SQL_HSET_INCLUDED