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, version 2.0, 7 as published by the Free Software Foundation. 8 9 This program is also distributed with certain software (including 10 but not limited to OpenSSL) that is licensed under separate terms, 11 as designated in a particular file or component or in included license 12 documentation. The authors of MySQL hereby grant you an additional 13 permission to link the program and your derivative works with the 14 separately licensed software that they have included with MySQL. 15 16 This program is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 GNU General Public License, version 2.0, for more details. 20 21 You should have received a copy of the GNU General Public License 22 along with this program; if not, write to the Free Software 23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ 24 25 #include "my_global.h" 26 #include "hash.h" 27 28 29 /** 30 A type-safe wrapper around mysys HASH. 31 */ 32 33 template <typename T, my_hash_get_key K> 34 class Hash_set 35 { 36 public: 37 typedef T Value_type; 38 enum { START_SIZE= 8 }; 39 /** 40 Constructs an empty hash. Does not allocate memory, it is done upon 41 the first insert. Thus does not cause or return errors. 42 */ Hash_set()43 Hash_set() 44 { 45 my_hash_clear(&m_hash); 46 } 47 /** 48 Destroy the hash by freeing the buckets table. Does 49 not call destructors for the elements. 50 */ ~Hash_set()51 ~Hash_set() 52 { 53 my_hash_free(&m_hash); 54 } 55 /** 56 Insert a single value into a hash. Does not tell whether 57 the value was inserted -- if an identical value existed, 58 it is not replaced. 59 60 @retval TRUE Out of memory. 61 @retval FALSE OK. The value either was inserted or existed 62 in the hash. 63 */ insert(T * value)64 bool insert(T *value) 65 { 66 my_hash_init_opt(&m_hash, &my_charset_bin, START_SIZE, 0, 0, K, 0, MYF(0)); 67 size_t key_len; 68 const uchar *key= K(reinterpret_cast<uchar*>(value), &key_len, FALSE); 69 if (my_hash_search(&m_hash, key, key_len) == NULL) 70 return my_hash_insert(&m_hash, reinterpret_cast<uchar *>(value)); 71 return FALSE; 72 } 73 /** Is this hash set empty? */ is_empty()74 bool is_empty() const { return m_hash.records == 0; } 75 /** Returns the number of unique elements. */ size()76 size_t size() const { return static_cast<size_t>(m_hash.records); } 77 /** An iterator over hash elements. Is not insert-stable. */ 78 class Iterator 79 { 80 public: Iterator(Hash_set & hash_set)81 Iterator(Hash_set &hash_set) 82 : m_hash(&hash_set.m_hash), 83 m_idx(0) 84 {} 85 /** 86 Return the current element and reposition the iterator to the next 87 element. 88 */ 89 inline T *operator++(int) 90 { 91 if (m_idx < m_hash->records) 92 return reinterpret_cast<T*>(my_hash_element(m_hash, m_idx++)); 93 return NULL; 94 } rewind()95 void rewind() { m_idx= 0; } 96 private: 97 HASH *m_hash; 98 uint m_idx; 99 }; 100 private: 101 HASH m_hash; 102 }; 103 104 #endif // SQL_HSET_INCLUDED 105