File: HashSet.h

package info (click to toggle)
chromium-browser 37.0.2062.120-1~deb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,707,260 kB
  • sloc: cpp: 8,976,677; ansic: 3,473,199; python: 586,578; asm: 449,013; xml: 184,195; java: 142,924; sh: 118,496; perl: 81,467; makefile: 27,557; yacc: 10,506; objc: 8,886; tcl: 3,186; cs: 2,252; lex: 2,213; sql: 1,198; pascal: 1,170; lisp: 790; awk: 407; ruby: 155; php: 83; sed: 52; exp: 11
file content (279 lines) | stat: -rw-r--r-- 10,050 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * Copyright (C) 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#ifndef WTF_HashSet_h
#define WTF_HashSet_h

#include "wtf/DefaultAllocator.h"
#include "wtf/HashTable.h"

namespace WTF {

    struct IdentityExtractor;

    // Note: empty or deleted values are not allowed, using them may lead to undefined behavior.
    // For pointer valuess this means that null pointers are not allowed unless you supply custom traits.
    template<
        typename ValueArg,
        typename HashArg = typename DefaultHash<ValueArg>::Hash,
        typename TraitsArg = HashTraits<ValueArg>,
        typename Allocator = DefaultAllocator> class HashSet {
        WTF_USE_ALLOCATOR(HashSet, Allocator);
    private:
        typedef HashArg HashFunctions;
        typedef TraitsArg ValueTraits;
        typedef typename ValueTraits::PeekInType ValuePeekInType;
        typedef typename ValueTraits::PassInType ValuePassInType;
        typedef typename ValueTraits::PassOutType ValuePassOutType;

    public:
        typedef typename ValueTraits::TraitType ValueType;

    private:
        typedef HashTable<ValueType, ValueType, IdentityExtractor,
            HashFunctions, ValueTraits, ValueTraits, Allocator> HashTableType;

    public:
        typedef HashTableConstIteratorAdapter<HashTableType, ValueTraits> iterator;
        typedef HashTableConstIteratorAdapter<HashTableType, ValueTraits> const_iterator;
        typedef typename HashTableType::AddResult AddResult;

        void swap(HashSet& ref)
        {
            m_impl.swap(ref.m_impl);
        }

        void swap(typename Allocator::template OtherType<HashSet>::Type other)
        {
            HashSet& ref = Allocator::getOther(other);
            m_impl.swap(ref.m_impl);
        }

        unsigned size() const;
        unsigned capacity() const;
        bool isEmpty() const;

        iterator begin() const;
        iterator end() const;

        iterator find(ValuePeekInType) const;
        bool contains(ValuePeekInType) const;

        // An alternate version of find() that finds the object by hashing and comparing
        // with some other type, to avoid the cost of type conversion. HashTranslator
        // must have the following function members:
        //   static unsigned hash(const T&);
        //   static bool equal(const ValueType&, const T&);
        template<typename HashTranslator, typename T> iterator find(const T&) const;
        template<typename HashTranslator, typename T> bool contains(const T&) const;

        // The return value is a pair of an iterator to the new value's location,
        // and a bool that is true if an new entry was added.
        AddResult add(ValuePassInType);

        // An alternate version of add() that finds the object by hashing and comparing
        // with some other type, to avoid the cost of type conversion if the object is already
        // in the table. HashTranslator must have the following function members:
        //   static unsigned hash(const T&);
        //   static bool equal(const ValueType&, const T&);
        //   static translate(ValueType&, const T&, unsigned hashCode);
        template<typename HashTranslator, typename T> AddResult add(const T&);

        void remove(ValuePeekInType);
        void remove(iterator);
        void clear();
        template<typename Collection>
        void removeAll(const Collection& toBeRemoved) { WTF::removeAll(*this, toBeRemoved); }

        static bool isValidValue(ValuePeekInType);

        ValuePassOutType take(iterator);
        ValuePassOutType take(ValuePeekInType);
        ValuePassOutType takeAny();

        void trace(typename Allocator::Visitor* visitor) { m_impl.trace(visitor); }

    private:
        HashTableType m_impl;
    };

    struct IdentityExtractor {
        template<typename T>
        static const T& extract(const T& t) { return t; }
    };

    template<typename Translator>
    struct HashSetTranslatorAdapter {
        template<typename T> static unsigned hash(const T& key) { return Translator::hash(key); }
        template<typename T, typename U> static bool equal(const T& a, const U& b) { return Translator::equal(a, b); }
        template<typename T, typename U> static void translate(T& location, const U& key, const U&, unsigned hashCode)
        {
            Translator::translate(location, key, hashCode);
        }
    };

    template<typename T, typename U, typename V, typename W>
    inline unsigned HashSet<T, U, V, W>::size() const
    {
        return m_impl.size();
    }

    template<typename T, typename U, typename V, typename W>
    inline unsigned HashSet<T, U, V, W>::capacity() const
    {
        return m_impl.capacity();
    }

    template<typename T, typename U, typename V, typename W>
    inline bool HashSet<T, U, V, W>::isEmpty() const
    {
        return m_impl.isEmpty();
    }

    template<typename T, typename U, typename V, typename W>
    inline typename HashSet<T, U, V, W>::iterator HashSet<T, U, V, W>::begin() const
    {
        return m_impl.begin();
    }

    template<typename T, typename U, typename V, typename W>
    inline typename HashSet<T, U, V, W>::iterator HashSet<T, U, V, W>::end() const
    {
        return m_impl.end();
    }

    template<typename T, typename U, typename V, typename W>
    inline typename HashSet<T, U, V, W>::iterator HashSet<T, U, V, W>::find(ValuePeekInType value) const
    {
        return m_impl.find(value);
    }

    template<typename Value, typename HashFunctions, typename Traits, typename Allocator>
    inline bool HashSet<Value, HashFunctions, Traits, Allocator>::contains(ValuePeekInType value) const
    {
        return m_impl.contains(value);
    }

    template<typename Value, typename HashFunctions, typename Traits, typename Allocator>
    template<typename HashTranslator, typename T>
    typename HashSet<Value, HashFunctions, Traits, Allocator>::iterator
    inline HashSet<Value, HashFunctions, Traits, Allocator>::find(const T& value) const
    {
        return m_impl.template find<HashSetTranslatorAdapter<HashTranslator> >(value);
    }

    template<typename Value, typename HashFunctions, typename Traits, typename Allocator>
    template<typename HashTranslator, typename T>
    inline bool HashSet<Value, HashFunctions, Traits, Allocator>::contains(const T& value) const
    {
        return m_impl.template contains<HashSetTranslatorAdapter<HashTranslator> >(value);
    }

    template<typename T, typename U, typename V, typename W>
    inline typename HashSet<T, U, V, W>::AddResult HashSet<T, U, V, W>::add(ValuePassInType value)
    {
        return m_impl.add(value);
    }

    template<typename Value, typename HashFunctions, typename Traits, typename Allocator>
    template<typename HashTranslator, typename T>
    inline typename HashSet<Value, HashFunctions, Traits, Allocator>::AddResult
    HashSet<Value, HashFunctions, Traits, Allocator>::add(const T& value)
    {
        return m_impl.template addPassingHashCode<HashSetTranslatorAdapter<HashTranslator> >(value, value);
    }

    template<typename T, typename U, typename V, typename W>
    inline void HashSet<T, U, V, W>::remove(iterator it)
    {
        m_impl.remove(it.m_impl);
    }

    template<typename T, typename U, typename V, typename W>
    inline void HashSet<T, U, V, W>::remove(ValuePeekInType value)
    {
        remove(find(value));
    }

    template<typename T, typename U, typename V, typename W>
    inline void HashSet<T, U, V, W>::clear()
    {
        m_impl.clear();
    }

    template<typename T, typename U, typename V, typename W>
    inline bool HashSet<T, U, V, W>::isValidValue(ValuePeekInType value)
    {
        if (ValueTraits::isDeletedValue(value))
            return false;

        if (HashFunctions::safeToCompareToEmptyOrDeleted) {
            if (value == ValueTraits::emptyValue())
                return false;
        } else {
            if (isHashTraitsEmptyValue<ValueTraits>(value))
                return false;
        }

        return true;
    }

    template<typename T, typename U, typename V, typename W>
    inline typename HashSet<T, U, V, W>::ValuePassOutType HashSet<T, U, V, W>::take(iterator it)
    {
        if (it == end())
            return ValueTraits::emptyValue();

        ValuePassOutType result = ValueTraits::passOut(const_cast<ValueType&>(*it));
        remove(it);

        return result;
    }

    template<typename T, typename U, typename V, typename W>
    inline typename HashSet<T, U, V, W>::ValuePassOutType HashSet<T, U, V, W>::take(ValuePeekInType value)
    {
        return take(find(value));
    }

    template<typename T, typename U, typename V, typename W>
    inline typename HashSet<T, U, V, W>::ValuePassOutType HashSet<T, U, V, W>::takeAny()
    {
        return take(begin());
    }

    template<typename C, typename W>
    inline void copyToVector(const C& collection, W& vector)
    {
        typedef typename C::const_iterator iterator;

        vector.resize(collection.size());

        iterator it = collection.begin();
        iterator end = collection.end();
        for (unsigned i = 0; it != end; ++it, ++i)
            vector[i] = *it;
    }

} // namespace WTF

using WTF::HashSet;

#endif /* WTF_HashSet_h */