• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

WTF

  • sources
  • kde-4.12
  • kdelibs
  • kjs
  • wtf
HashTraits.h
Go to the documentation of this file.
1 // -*- mode: c++; c-basic-offset: 4 -*-
2 /*
3  * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21 
22 #ifndef WTF_HashTraits_h
23 #define WTF_HashTraits_h
24 
25 #include "Assertions.h"
26 #include "HashFunctions.h"
27 #include <utility>
28 #include <limits>
29 
30 namespace WTF {
31 
32  using std::pair;
33  using std::make_pair;
34 
35  template<typename T> struct IsInteger { static const bool value = false; };
36  template<> struct IsInteger<bool> { static const bool value = true; };
37  template<> struct IsInteger<char> { static const bool value = true; };
38  template<> struct IsInteger<signed char> { static const bool value = true; };
39  template<> struct IsInteger<unsigned char> { static const bool value = true; };
40  template<> struct IsInteger<short> { static const bool value = true; };
41  template<> struct IsInteger<unsigned short> { static const bool value = true; };
42  template<> struct IsInteger<int> { static const bool value = true; };
43  template<> struct IsInteger<unsigned int> { static const bool value = true; };
44  template<> struct IsInteger<long> { static const bool value = true; };
45  template<> struct IsInteger<unsigned long> { static const bool value = true; };
46  template<> struct IsInteger<long long> { static const bool value = true; };
47  template<> struct IsInteger<unsigned long long> { static const bool value = true; };
48 
49 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
50  template<> struct IsInteger<wchar_t> { static const bool value = true; };
51 #endif
52 
53  COMPILE_ASSERT(IsInteger<bool>::value, WTF_IsInteger_bool_true)
54  COMPILE_ASSERT(IsInteger<char>::value, WTF_IsInteger_char_true)
55  COMPILE_ASSERT(IsInteger<signed char>::value, WTF_IsInteger_signed_char_true)
56  COMPILE_ASSERT(IsInteger<unsigned char>::value, WTF_IsInteger_unsigned_char_true)
57  COMPILE_ASSERT(IsInteger<short>::value, WTF_IsInteger_short_true)
58  COMPILE_ASSERT(IsInteger<unsigned short>::value, WTF_IsInteger_unsigned_short_true)
59  COMPILE_ASSERT(IsInteger<int>::value, WTF_IsInteger_int_true)
60  COMPILE_ASSERT(IsInteger<unsigned int>::value, WTF_IsInteger_unsigned_int_true)
61  COMPILE_ASSERT(IsInteger<long>::value, WTF_IsInteger_long_true)
62  COMPILE_ASSERT(IsInteger<unsigned long>::value, WTF_IsInteger_unsigned_long_true)
63  COMPILE_ASSERT(IsInteger<long long>::value, WTF_IsInteger_long_long_true)
64  COMPILE_ASSERT(IsInteger<unsigned long long>::value, WTF_IsInteger_unsigned_long_long_true)
65 
66 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
67  COMPILE_ASSERT(IsInteger<wchar_t>::value, WTF_IsInteger_wchar_t_true)
68 #endif
69 
70  COMPILE_ASSERT(!IsInteger<char*>::value, WTF_IsInteger_char_pointer_false)
71  COMPILE_ASSERT(!IsInteger<const char* >::value, WTF_IsInteger_const_char_pointer_false)
72  COMPILE_ASSERT(!IsInteger<volatile char* >::value, WTF_IsInteger_volatile_char_pointer__false)
73  COMPILE_ASSERT(!IsInteger<double>::value, WTF_IsInteger_double_false)
74  COMPILE_ASSERT(!IsInteger<float>::value, WTF_IsInteger_float_false)
75 
76  template<typename T> struct HashTraits;
77 
78  template<bool isInteger, typename T> struct GenericHashTraitsBase;
79 
80  template<typename T> struct GenericHashTraitsBase<false, T> {
81  static const bool emptyValueIsZero = false;
82  static const bool needsDestruction = true;
83  };
84 
85  // default integer traits disallow both 0 and -1 as keys (max value instead of -1 for unsigned)
86  template<typename T> struct GenericHashTraitsBase<true, T> {
87  static const bool emptyValueIsZero = true;
88  static const bool needsDestruction = false;
89  static void constructDeletedValue(T* slot) { *slot = static_cast<T>(-1); }
90  static bool isDeletedValue(T value) { return value == static_cast<T>(-1); }
91  };
92 
93  template<typename T> struct GenericHashTraits : GenericHashTraitsBase<IsInteger<T>::value, T> {
94  typedef T TraitType;
95  static T emptyValue() { return T(); }
96  };
97 
98  template<typename T> struct HashTraits : GenericHashTraits<T> { };
99 
100  template<typename T> struct FloatHashTraits : GenericHashTraits<T> {
101  static const bool needsDestruction = false;
102  static T emptyValue() { return std::numeric_limits<T>::infinity(); }
103  static void constructDeletedValue(T* slot) { *slot = -std::numeric_limits<T>::infinity(); }
104  static bool isDeletedValue(T value) { return value == -std::numeric_limits<T>::infinity(); }
105  };
106 
107  template<> struct HashTraits<float> : FloatHashTraits<float> { };
108  template<> struct HashTraits<double> : FloatHashTraits<double> { };
109 
110  template<typename P> struct HashTraits<P*> : GenericHashTraits<P*> {
111  static const bool emptyValueIsZero = true;
112  static const bool needsDestruction = false;
113  static void constructDeletedValue(P** slot) { *slot = reinterpret_cast<P*>(-1); }
114  static bool isDeletedValue(P* value) { return value == reinterpret_cast<P*>(-1); }
115  };
116 
117  template<typename P> struct HashTraits<RefPtr<P> > : GenericHashTraits<RefPtr<P> > {
118  static const bool emptyValueIsZero = true;
119  static void constructDeletedValue(RefPtr<P>* slot) { new (slot) RefPtr<P>(HashTableDeletedValue); }
120  static bool isDeletedValue(const RefPtr<P>& value) { return value.isHashTableDeletedValue(); }
121  };
122 
123  // special traits for pairs, helpful for their use in HashMap implementation
124 
125  template<typename FirstTraitsArg, typename SecondTraitsArg>
126  struct PairHashTraits : GenericHashTraits<pair<typename FirstTraitsArg::TraitType, typename SecondTraitsArg::TraitType> > {
127  typedef FirstTraitsArg FirstTraits;
128  typedef SecondTraitsArg SecondTraits;
129  typedef pair<typename FirstTraits::TraitType, typename SecondTraits::TraitType> TraitType;
130 
131  static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero;
132  static TraitType emptyValue() { return make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); }
133 
134  static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction;
135 
136  static void constructDeletedValue(TraitType* slot) { FirstTraits::constructDeletedValue(&slot->first); }
137  static bool isDeletedValue(const TraitType& value) { return FirstTraits::isDeletedValue(value.first); }
138  };
139 
140  template<typename First, typename Second>
141  struct HashTraits<pair<First, Second> > : public PairHashTraits<HashTraits<First>, HashTraits<Second> > { };
142 
143 } // namespace WTF
144 
145 using WTF::HashTraits;
146 using WTF::PairHashTraits;
147 
148 #endif // WTF_HashTraits_h
WTF::PairHashTraits::emptyValueIsZero
static const bool emptyValueIsZero
Definition: HashTraits.h:131
WTF::IsInteger
Definition: HashTraits.h:35
WTF::GenericHashTraits::TraitType
T TraitType
Definition: HashTraits.h:94
WTF::IsInteger::value
static const bool value
Definition: HashTraits.h:35
WTF::FloatHashTraits::constructDeletedValue
static void constructDeletedValue(T *slot)
Definition: HashTraits.h:103
WTF::GenericHashTraits::emptyValue
static T emptyValue()
Definition: HashTraits.h:95
WTF::RefPtr
Definition: Forward.h:31
WTF::RefPtr::isHashTableDeletedValue
bool isHashTableDeletedValue() const
Definition: RefPtr.h:50
WTF::FloatHashTraits
Definition: HashTraits.h:100
WTF::PairHashTraits::TraitType
pair< typename FirstTraits::TraitType, typename SecondTraits::TraitType > TraitType
Definition: HashTraits.h:129
HashFunctions.h
WTF::PairHashTraits::isDeletedValue
static bool isDeletedValue(const TraitType &value)
Definition: HashTraits.h:137
Assertions.h
WTF::GenericHashTraits
Definition: HashTraits.h:93
WTF::HashTraits< P * >::isDeletedValue
static bool isDeletedValue(P *value)
Definition: HashTraits.h:114
WTF::PairHashTraits::FirstTraits
FirstTraitsArg FirstTraits
Definition: HashTraits.h:127
WTF::GenericHashTraitsBase< true, T >::constructDeletedValue
static void constructDeletedValue(T *slot)
Definition: HashTraits.h:89
WTF::FloatHashTraits::emptyValue
static T emptyValue()
Definition: HashTraits.h:102
WTF::GenericHashTraitsBase< true, T >::isDeletedValue
static bool isDeletedValue(T value)
Definition: HashTraits.h:90
WTF::HashTableDeletedValue
Definition: RefPtr.h:35
WTF::FloatHashTraits::needsDestruction
static const bool needsDestruction
Definition: HashTraits.h:101
WTF::HashTraits< P * >::constructDeletedValue
static void constructDeletedValue(P **slot)
Definition: HashTraits.h:113
COMPILE_ASSERT
#define COMPILE_ASSERT(exp, name)
Definition: Assertions.h:38
WTF::HashTraits< RefPtr< P > >::isDeletedValue
static bool isDeletedValue(const RefPtr< P > &value)
Definition: HashTraits.h:120
WTF::PairHashTraits::constructDeletedValue
static void constructDeletedValue(TraitType *slot)
Definition: HashTraits.h:136
WTF::PairHashTraits::SecondTraits
SecondTraitsArg SecondTraits
Definition: HashTraits.h:128
WTF::PairHashTraits
Definition: HashTraits.h:126
WTF::PairHashTraits::emptyValue
static TraitType emptyValue()
Definition: HashTraits.h:132
WTF::GenericHashTraitsBase
Definition: HashTraits.h:78
WTF::FloatHashTraits::isDeletedValue
static bool isDeletedValue(T value)
Definition: HashTraits.h:104
WTF::HashTraits
Definition: HashTraits.h:76
WTF::PairHashTraits::needsDestruction
static const bool needsDestruction
Definition: HashTraits.h:134
WTF::HashTraits< RefPtr< P > >::constructDeletedValue
static void constructDeletedValue(RefPtr< P > *slot)
Definition: HashTraits.h:119
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:00 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

WTF

Skip menu "WTF"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal