Changeset 223236 in webkit


Ignore:
Timestamp:
Oct 12, 2017 5:45:57 AM (6 years ago)
Author:
commit-queue@webkit.org
Message:

It should be possible to iterate just the values (and not the counts) of a HashCountedSet
https://bugs.webkit.org/show_bug.cgi?id=178169

Patch by Sam Weinig <sam@webkit.org> on 2017-10-12
Reviewed by Daniel Bates.

Source/WTF:

Cleanup (fix indentation, simplify type names, adopt using), and add a values() range to
HashCountedSet. This will allow getting a Vector of all the values (and not the counts)
using the new copyToVector.

  • wtf/HashCountedSet.h:

Tools:

  • TestWebKitAPI/Tests/WTF/HashCountedSet.cpp:

(TestWebKitAPI::TEST):
Add test for HashCountedSet's new values() range.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r223219 r223236  
     12017-10-12  Sam Weinig  <sam@webkit.org>
     2
     3        It should be possible to iterate just the values (and not the counts) of a HashCountedSet
     4        https://bugs.webkit.org/show_bug.cgi?id=178169
     5
     6        Reviewed by Daniel Bates.
     7
     8        Cleanup (fix indentation, simplify type names, adopt using), and add a values() range to
     9        HashCountedSet. This will allow getting a Vector of all the values (and not the counts)
     10        using the new copyToVector.
     11
     12        * wtf/HashCountedSet.h:
     13
    1142017-10-11  Michael Saboff  <msaboff@apple.com>
    215
  • trunk/Source/WTF/wtf/HashCountedSet.h

    r196802 r223236  
    1919 */
    2020
    21 #ifndef WTF_HashCountedSet_h
    22 #define WTF_HashCountedSet_h
     21#pragma once
    2322
    2423#include <initializer_list>
     
    2928namespace WTF {
    3029
    31     template<typename Value, typename HashFunctions = typename DefaultHash<Value>::Hash, typename Traits = HashTraits<Value>>
    32     class HashCountedSet final {
    33         WTF_MAKE_FAST_ALLOCATED;
    34     private:
    35         typedef HashMap<Value, unsigned, HashFunctions, Traits> ImplType;
    36     public:
    37         typedef Value ValueType;
    38         typedef typename ImplType::iterator iterator;
    39         typedef typename ImplType::const_iterator const_iterator;
    40         typedef typename ImplType::AddResult AddResult;
    41 
    42         HashCountedSet()
    43         {
    44         }
    45 
    46         HashCountedSet(std::initializer_list<typename ImplType::KeyValuePairType> initializerList)
    47         {
    48             for (const auto& keyValuePair : initializerList)
    49                 add(keyValuePair.key, keyValuePair.value);
    50         }
    51 
    52         HashCountedSet(std::initializer_list<typename ImplType::KeyType> initializerList)
    53         {
    54             for (const auto& value : initializerList)
    55                 add(value);
    56         }
    57        
    58         void swap(HashCountedSet&);
    59        
    60         unsigned size() const;
    61         unsigned capacity() const;
    62         bool isEmpty() const;
    63        
    64         // Iterators iterate over pairs of values and counts.
    65         iterator begin();
    66         iterator end();
    67         const_iterator begin() const;
    68         const_iterator end() const;
    69        
    70         iterator find(const ValueType&);
    71         const_iterator find(const ValueType&) const;
    72         bool contains(const ValueType&) const;
    73         unsigned count(const ValueType&) const;
    74 
    75         // Increments the count if an equal value is already present.
    76         // The return value includes both an iterator to the value's location,
    77         // and an isNewEntry bool that indicates whether it is a new or existing entry.
    78         AddResult add(const ValueType&);
    79         AddResult add(ValueType&&);
    80 
    81         // Increments the count of a value by the passed amount.
    82         AddResult add(const ValueType&, unsigned);
    83         AddResult add(ValueType&&, unsigned);
    84 
    85         // Decrements the count of the value, and removes it if count goes down to zero.
    86         // Returns true if the value is removed.
    87         bool remove(const ValueType&);
    88         bool remove(iterator);
    89  
    90         // Removes the value, regardless of its count.
    91         // Returns true if a value was removed.
    92         bool removeAll(iterator);
    93         bool removeAll(const ValueType&);
    94 
    95         // Clears the whole set.
    96         void clear();
    97 
    98         // Overloads for smart pointer keys that take the raw pointer type as the parameter.
    99         template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, iterator>::type find(typename GetPtrHelper<V>::PtrType);
    100         template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, const_iterator>::type find(typename GetPtrHelper<V>::PtrType) const;
    101         template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, bool>::type contains(typename GetPtrHelper<V>::PtrType) const;
    102         template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, unsigned>::type count(typename GetPtrHelper<V>::PtrType) const;
    103         template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, bool>::type remove(typename GetPtrHelper<V>::PtrType);
    104 
    105     private:
    106         ImplType m_impl;
    107     };
    108 
    109 
    110     template<typename Value, typename HashFunctions, typename Traits>
    111     inline void HashCountedSet<Value, HashFunctions, Traits>::swap(HashCountedSet& other)
     30template<typename Value, typename HashFunctions = typename DefaultHash<Value>::Hash, typename Traits = HashTraits<Value>>
     31class HashCountedSet final {
     32    WTF_MAKE_FAST_ALLOCATED;
     33private:
     34    using ImplType = HashMap<Value, unsigned, HashFunctions, Traits>;
     35public:
     36    using ValueType = Value;
     37    using iterator = typename ImplType::iterator;
     38    using const_iterator = typename ImplType::const_iterator;
     39    using ValuesIteratorRange = typename ImplType::KeysIteratorRange;
     40    using ValuesConstIteratorRange = typename ImplType::KeysConstIteratorRange;
     41    using AddResult = typename ImplType::AddResult;
     42
     43    HashCountedSet()
    11244    {
    113         m_impl.swap(other.m_impl);
     45    }
     46
     47    HashCountedSet(std::initializer_list<typename ImplType::KeyValuePairType> initializerList)
     48    {
     49        for (const auto& keyValuePair : initializerList)
     50            add(keyValuePair.key, keyValuePair.value);
     51    }
     52
     53    HashCountedSet(std::initializer_list<typename ImplType::KeyType> initializerList)
     54    {
     55        for (const auto& value : initializerList)
     56            add(value);
    11457    }
    11558   
    116     template<typename Value, typename HashFunctions, typename Traits>
    117     inline unsigned HashCountedSet<Value, HashFunctions, Traits>::size() const
    118     {
    119         return m_impl.size();
     59    void swap(HashCountedSet&);
     60   
     61    unsigned size() const;
     62    unsigned capacity() const;
     63    bool isEmpty() const;
     64   
     65    // Iterators iterate over pairs of values and counts.
     66    iterator begin();
     67    iterator end();
     68    const_iterator begin() const;
     69    const_iterator end() const;
     70
     71    ValuesIteratorRange values();
     72    const ValuesConstIteratorRange values() const;
     73
     74    iterator find(const ValueType&);
     75    const_iterator find(const ValueType&) const;
     76    bool contains(const ValueType&) const;
     77    unsigned count(const ValueType&) const;
     78
     79    // Increments the count if an equal value is already present.
     80    // The return value includes both an iterator to the value's location,
     81    // and an isNewEntry bool that indicates whether it is a new or existing entry.
     82    AddResult add(const ValueType&);
     83    AddResult add(ValueType&&);
     84
     85    // Increments the count of a value by the passed amount.
     86    AddResult add(const ValueType&, unsigned);
     87    AddResult add(ValueType&&, unsigned);
     88
     89    // Decrements the count of the value, and removes it if count goes down to zero.
     90    // Returns true if the value is removed.
     91    bool remove(const ValueType&);
     92    bool remove(iterator);
     93
     94    // Removes the value, regardless of its count.
     95    // Returns true if a value was removed.
     96    bool removeAll(iterator);
     97    bool removeAll(const ValueType&);
     98
     99    // Clears the whole set.
     100    void clear();
     101
     102    // Overloads for smart pointer keys that take the raw pointer type as the parameter.
     103    template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, iterator>::type find(typename GetPtrHelper<V>::PtrType);
     104    template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, const_iterator>::type find(typename GetPtrHelper<V>::PtrType) const;
     105    template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, bool>::type contains(typename GetPtrHelper<V>::PtrType) const;
     106    template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, unsigned>::type count(typename GetPtrHelper<V>::PtrType) const;
     107    template<typename V = ValueType> typename std::enable_if<IsSmartPtr<V>::value, bool>::type remove(typename GetPtrHelper<V>::PtrType);
     108
     109private:
     110    ImplType m_impl;
     111};
     112
     113
     114template<typename Value, typename HashFunctions, typename Traits>
     115inline void HashCountedSet<Value, HashFunctions, Traits>::swap(HashCountedSet& other)
     116{
     117    m_impl.swap(other.m_impl);
     118}
     119
     120template<typename Value, typename HashFunctions, typename Traits>
     121inline unsigned HashCountedSet<Value, HashFunctions, Traits>::size() const
     122{
     123    return m_impl.size();
     124}
     125
     126template<typename Value, typename HashFunctions, typename Traits>
     127inline unsigned HashCountedSet<Value, HashFunctions, Traits>::capacity() const
     128{
     129    return m_impl.capacity();
     130}
     131
     132template<typename Value, typename HashFunctions, typename Traits>
     133inline bool HashCountedSet<Value, HashFunctions, Traits>::isEmpty() const
     134{
     135    return size() == 0;
     136}
     137
     138template<typename Value, typename HashFunctions, typename Traits>
     139inline auto HashCountedSet<Value, HashFunctions, Traits>::begin() -> iterator
     140{
     141    return m_impl.begin();
     142}
     143
     144template<typename Value, typename HashFunctions, typename Traits>
     145inline auto HashCountedSet<Value, HashFunctions, Traits>::end() -> iterator
     146{
     147    return m_impl.end();
     148}
     149
     150template<typename Value, typename HashFunctions, typename Traits>
     151inline auto HashCountedSet<Value, HashFunctions, Traits>::begin() const -> const_iterator
     152{
     153    return m_impl.begin();
     154}
     155
     156template<typename Value, typename HashFunctions, typename Traits>
     157inline auto HashCountedSet<Value, HashFunctions, Traits>::end() const -> const_iterator
     158{
     159    return m_impl.end();
     160}
     161
     162template<typename Value, typename HashFunctions, typename Traits>
     163inline auto HashCountedSet<Value, HashFunctions, Traits>::values() -> ValuesIteratorRange
     164{
     165    return m_impl.keys();
     166}
     167
     168template<typename Value, typename HashFunctions, typename Traits>
     169inline auto HashCountedSet<Value, HashFunctions, Traits>::values() const -> const ValuesConstIteratorRange
     170{
     171    return m_impl.keys();
     172}
     173
     174template<typename Value, typename HashFunctions, typename Traits>
     175inline auto HashCountedSet<Value, HashFunctions, Traits>::find(const ValueType& value) -> iterator
     176{
     177    return m_impl.find(value);
     178}
     179
     180template<typename Value, typename HashFunctions, typename Traits>
     181inline auto HashCountedSet<Value, HashFunctions, Traits>::find(const ValueType& value) const -> const_iterator
     182{
     183    return m_impl.find(value);
     184}
     185
     186template<typename Value, typename HashFunctions, typename Traits>
     187inline bool HashCountedSet<Value, HashFunctions, Traits>::contains(const ValueType& value) const
     188{
     189    return m_impl.contains(value);
     190}
     191
     192template<typename Value, typename HashFunctions, typename Traits>
     193inline unsigned HashCountedSet<Value, HashFunctions, Traits>::count(const ValueType& value) const
     194{
     195    return m_impl.get(value);
     196}
     197
     198template<typename Value, typename HashFunctions, typename Traits>
     199inline auto HashCountedSet<Value, HashFunctions, Traits>::add(const ValueType &value) -> AddResult
     200{
     201    auto result = m_impl.add(value, 0);
     202    ++result.iterator->value;
     203    return result;
     204}
     205
     206template<typename Value, typename HashFunctions, typename Traits>
     207inline auto HashCountedSet<Value, HashFunctions, Traits>::add(ValueType&& value) -> AddResult
     208{
     209    auto result = m_impl.add(std::forward<Value>(value), 0);
     210    ++result.iterator->value;
     211    return result;
     212}
     213
     214template<typename Value, typename HashFunctions, typename Traits>
     215inline auto HashCountedSet<Value, HashFunctions, Traits>::add(const ValueType& value, unsigned count) -> AddResult
     216{
     217    auto result = m_impl.add(value, 0);
     218    result.iterator->value += count;
     219    return result;
     220}
     221
     222template<typename Value, typename HashFunctions, typename Traits>
     223inline auto HashCountedSet<Value, HashFunctions, Traits>::add(ValueType&& value, unsigned count) -> AddResult
     224{
     225    auto result = m_impl.add(std::forward<Value>(value), 0);
     226    result.iterator->value += count;
     227    return result;
     228}
     229
     230template<typename Value, typename HashFunctions, typename Traits>
     231inline bool HashCountedSet<Value, HashFunctions, Traits>::remove(const ValueType& value)
     232{
     233    return remove(find(value));
     234}
     235
     236template<typename Value, typename HashFunctions, typename Traits>
     237inline bool HashCountedSet<Value, HashFunctions, Traits>::remove(iterator it)
     238{
     239    if (it == end())
     240        return false;
     241
     242    unsigned oldVal = it->value;
     243    ASSERT(oldVal);
     244    unsigned newVal = oldVal - 1;
     245    if (newVal) {
     246        it->value = newVal;
     247        return false;
    120248    }
    121    
    122     template<typename Value, typename HashFunctions, typename Traits>
    123     inline unsigned HashCountedSet<Value, HashFunctions, Traits>::capacity() const
    124     {
    125         return m_impl.capacity();
    126     }
    127    
    128     template<typename Value, typename HashFunctions, typename Traits>
    129     inline bool HashCountedSet<Value, HashFunctions, Traits>::isEmpty() const
    130     {
    131         return size() == 0;
    132     }
    133    
    134     template<typename Value, typename HashFunctions, typename Traits>
    135     inline typename HashCountedSet<Value, HashFunctions, Traits>::iterator HashCountedSet<Value, HashFunctions, Traits>::begin()
    136     {
    137         return m_impl.begin();
    138     }
    139 
    140     template<typename Value, typename HashFunctions, typename Traits>
    141     inline typename HashCountedSet<Value, HashFunctions, Traits>::iterator HashCountedSet<Value, HashFunctions, Traits>::end()
    142     {
    143         return m_impl.end();
    144     }
    145    
    146     template<typename Value, typename HashFunctions, typename Traits>
    147     inline typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator HashCountedSet<Value, HashFunctions, Traits>::begin() const
    148     {
    149         return m_impl.begin();
    150     }
    151    
    152     template<typename Value, typename HashFunctions, typename Traits>
    153     inline typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator HashCountedSet<Value, HashFunctions, Traits>::end() const
    154     {
    155         return m_impl.end();
    156     }
    157    
    158     template<typename Value, typename HashFunctions, typename Traits>
    159     inline typename HashCountedSet<Value, HashFunctions, Traits>::iterator HashCountedSet<Value, HashFunctions, Traits>::find(const ValueType& value)
    160     {
    161         return m_impl.find(value);
    162     }
    163    
    164     template<typename Value, typename HashFunctions, typename Traits>
    165     inline typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator HashCountedSet<Value, HashFunctions, Traits>::find(const ValueType& value) const
    166     {
    167         return m_impl.find(value);
    168     }
    169    
    170     template<typename Value, typename HashFunctions, typename Traits>
    171     inline bool HashCountedSet<Value, HashFunctions, Traits>::contains(const ValueType& value) const
    172     {
    173         return m_impl.contains(value);
    174     }
    175 
    176     template<typename Value, typename HashFunctions, typename Traits>
    177     inline unsigned HashCountedSet<Value, HashFunctions, Traits>::count(const ValueType& value) const
    178     {
    179         return m_impl.get(value);
    180     }
    181    
    182     template<typename Value, typename HashFunctions, typename Traits>
    183     inline typename HashCountedSet<Value, HashFunctions, Traits>::AddResult HashCountedSet<Value, HashFunctions, Traits>::add(const ValueType &value)
    184     {
    185         AddResult result = m_impl.add(value, 0);
    186         ++result.iterator->value;
    187         return result;
    188     }
    189 
    190     template<typename Value, typename HashFunctions, typename Traits>
    191     inline typename HashCountedSet<Value, HashFunctions, Traits>::AddResult HashCountedSet<Value, HashFunctions, Traits>::add(ValueType&& value)
    192     {
    193         AddResult result = m_impl.add(std::forward<Value>(value), 0);
    194         ++result.iterator->value;
    195         return result;
    196     }
    197 
    198     template<typename Value, typename HashFunctions, typename Traits>
    199     inline typename HashCountedSet<Value, HashFunctions, Traits>::AddResult HashCountedSet<Value, HashFunctions, Traits>::add(const ValueType& value, unsigned count)
    200     {
    201         AddResult result = m_impl.add(value, 0);
    202         result.iterator->value += count;
    203         return result;
    204     }
    205    
    206     template<typename Value, typename HashFunctions, typename Traits>
    207     inline typename HashCountedSet<Value, HashFunctions, Traits>::AddResult HashCountedSet<Value, HashFunctions, Traits>::add(ValueType&& value, unsigned count)
    208     {
    209         AddResult result = m_impl.add(std::forward<Value>(value), 0);
    210         result.iterator->value += count;
    211         return result;
    212     }
    213    
    214     template<typename Value, typename HashFunctions, typename Traits>
    215     inline bool HashCountedSet<Value, HashFunctions, Traits>::remove(const ValueType& value)
    216     {
    217         return remove(find(value));
    218     }
    219    
    220     template<typename Value, typename HashFunctions, typename Traits>
    221     inline bool HashCountedSet<Value, HashFunctions, Traits>::remove(iterator it)
    222     {
    223         if (it == end())
    224             return false;
    225 
    226         unsigned oldVal = it->value;
    227         ASSERT(oldVal);
    228         unsigned newVal = oldVal - 1;
    229         if (newVal) {
    230             it->value = newVal;
    231             return false;
    232         }
    233 
    234         m_impl.remove(it);
    235         return true;
    236     }
    237    
    238     template<typename Value, typename HashFunctions, typename Traits>
    239     inline bool HashCountedSet<Value, HashFunctions, Traits>::removeAll(const ValueType& value)
    240     {
    241         return removeAll(find(value));
    242     }
    243    
    244     template<typename Value, typename HashFunctions, typename Traits>
    245     inline bool HashCountedSet<Value, HashFunctions, Traits>::removeAll(iterator it)
    246     {
    247         if (it == end())
    248             return false;
    249 
    250         m_impl.remove(it);
    251         return true;
    252     }
    253    
    254     template<typename Value, typename HashFunctions, typename Traits>
    255     inline void HashCountedSet<Value, HashFunctions, Traits>::clear()
    256     {
    257         m_impl.clear();
    258     }
    259    
    260     template<typename Value, typename HashFunctions, typename Traits, typename VectorType>
    261     inline void copyToVector(const HashCountedSet<Value, HashFunctions, Traits>& collection, VectorType& vector)
    262     {
    263         typedef typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator iterator;
    264        
    265         vector.resize(collection.size());
    266        
    267         iterator it = collection.begin();
    268         iterator end = collection.end();
    269         for (unsigned i = 0; it != end; ++it, ++i)
    270             vector[i] = *it;
    271     }
    272 
    273     template<typename Value, typename HashFunctions, typename Traits>
    274     inline void copyToVector(const HashCountedSet<Value, HashFunctions, Traits>& collection, Vector<Value>& vector)
    275     {
    276         typedef typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator iterator;
    277        
    278         vector.resize(collection.size());
    279        
    280         iterator it = collection.begin();
    281         iterator end = collection.end();
    282         for (unsigned i = 0; it != end; ++it, ++i)
    283             vector[i] = (*it).key;
    284     }
    285 
    286     template<typename Value, typename HashFunctions, typename Traits>
    287     template<typename V>
    288     inline auto HashCountedSet<Value, HashFunctions, Traits>::find(typename GetPtrHelper<V>::PtrType value) -> typename std::enable_if<IsSmartPtr<V>::value, iterator>::type
    289     {
    290         return m_impl.find(value);
    291     }
    292    
    293     template<typename Value, typename HashFunctions, typename Traits>
    294     template<typename V>
    295     inline auto HashCountedSet<Value, HashFunctions, Traits>::find(typename GetPtrHelper<V>::PtrType value) const -> typename std::enable_if<IsSmartPtr<V>::value, const_iterator>::type
    296     {
    297         return m_impl.find(value);
    298     }
    299    
    300     template<typename Value, typename HashFunctions, typename Traits>
    301     template<typename V>
    302     inline auto HashCountedSet<Value, HashFunctions, Traits>::contains(typename GetPtrHelper<V>::PtrType value) const -> typename std::enable_if<IsSmartPtr<V>::value, bool>::type
    303     {
    304         return m_impl.contains(value);
    305     }
    306    
    307     template<typename Value, typename HashFunctions, typename Traits>
    308     template<typename V>
    309     inline auto HashCountedSet<Value, HashFunctions, Traits>::count(typename GetPtrHelper<V>::PtrType value) const -> typename std::enable_if<IsSmartPtr<V>::value, unsigned>::type
    310     {
    311         return m_impl.get(value);
    312     }
    313    
    314     template<typename Value, typename HashFunctions, typename Traits>
    315     template<typename V>
    316     inline auto HashCountedSet<Value, HashFunctions, Traits>::remove(typename GetPtrHelper<V>::PtrType value) -> typename std::enable_if<IsSmartPtr<V>::value, bool>::type
    317     {
    318         return remove(find(value));
    319     }
     249
     250    m_impl.remove(it);
     251    return true;
     252}
     253
     254template<typename Value, typename HashFunctions, typename Traits>
     255inline bool HashCountedSet<Value, HashFunctions, Traits>::removeAll(const ValueType& value)
     256{
     257    return removeAll(find(value));
     258}
     259
     260template<typename Value, typename HashFunctions, typename Traits>
     261inline bool HashCountedSet<Value, HashFunctions, Traits>::removeAll(iterator it)
     262{
     263    if (it == end())
     264        return false;
     265
     266    m_impl.remove(it);
     267    return true;
     268}
     269
     270template<typename Value, typename HashFunctions, typename Traits>
     271inline void HashCountedSet<Value, HashFunctions, Traits>::clear()
     272{
     273    m_impl.clear();
     274}
     275
     276template<typename Value, typename HashFunctions, typename Traits>
     277template<typename V>
     278inline auto HashCountedSet<Value, HashFunctions, Traits>::find(typename GetPtrHelper<V>::PtrType value) -> typename std::enable_if<IsSmartPtr<V>::value, iterator>::type
     279{
     280    return m_impl.find(value);
     281}
     282
     283template<typename Value, typename HashFunctions, typename Traits>
     284template<typename V>
     285inline auto HashCountedSet<Value, HashFunctions, Traits>::find(typename GetPtrHelper<V>::PtrType value) const -> typename std::enable_if<IsSmartPtr<V>::value, const_iterator>::type
     286{
     287    return m_impl.find(value);
     288}
     289
     290template<typename Value, typename HashFunctions, typename Traits>
     291template<typename V>
     292inline auto HashCountedSet<Value, HashFunctions, Traits>::contains(typename GetPtrHelper<V>::PtrType value) const -> typename std::enable_if<IsSmartPtr<V>::value, bool>::type
     293{
     294    return m_impl.contains(value);
     295}
     296
     297template<typename Value, typename HashFunctions, typename Traits>
     298template<typename V>
     299inline auto HashCountedSet<Value, HashFunctions, Traits>::count(typename GetPtrHelper<V>::PtrType value) const -> typename std::enable_if<IsSmartPtr<V>::value, unsigned>::type
     300{
     301    return m_impl.get(value);
     302}
     303
     304template<typename Value, typename HashFunctions, typename Traits>
     305template<typename V>
     306inline auto HashCountedSet<Value, HashFunctions, Traits>::remove(typename GetPtrHelper<V>::PtrType value) -> typename std::enable_if<IsSmartPtr<V>::value, bool>::type
     307{
     308    return remove(find(value));
     309}
     310
     311template<typename Value, typename HashFunctions, typename Traits, typename VectorType>
     312inline void copyToVector(const HashCountedSet<Value, HashFunctions, Traits>& collection, VectorType& vector)
     313{
     314    typedef typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator iterator;
     315
     316    vector.resize(collection.size());
     317
     318    iterator it = collection.begin();
     319    iterator end = collection.end();
     320    for (unsigned i = 0; it != end; ++it, ++i)
     321        vector[i] = *it;
     322}
     323
     324template<typename Value, typename HashFunctions, typename Traits>
     325inline void copyToVector(const HashCountedSet<Value, HashFunctions, Traits>& collection, Vector<Value>& vector)
     326{
     327    typedef typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator iterator;
     328
     329    vector.resize(collection.size());
     330
     331    iterator it = collection.begin();
     332    iterator end = collection.end();
     333    for (unsigned i = 0; it != end; ++it, ++i)
     334        vector[i] = (*it).key;
     335}
    320336
    321337} // namespace WTF
     
    323339using WTF::HashCountedSet;
    324340
    325 #endif /* WTF_HashCountedSet_h */
     341
  • trunk/Tools/ChangeLog

    r223234 r223236  
     12017-10-12  Sam Weinig  <sam@webkit.org>
     2
     3        It should be possible to iterate just the values (and not the counts) of a HashCountedSet
     4        https://bugs.webkit.org/show_bug.cgi?id=178169
     5
     6        Reviewed by Daniel Bates.
     7
     8        * TestWebKitAPI/Tests/WTF/HashCountedSet.cpp:
     9        (TestWebKitAPI::TEST):
     10        Add test for HashCountedSet's new values() range.
     11
    1122017-10-11  Frederic Wang  <fwang@igalia.com>
    213
  • trunk/Tools/TestWebKitAPI/Tests/WTF/HashCountedSet.cpp

    r217938 r223236  
    470470}
    471471
     472TEST(WTF_HashCountedSet, Values)
     473{
     474    HashCountedSet<int> set { 1, 1, 2, 3, 3 };
     475   
     476    auto vector = copyToVector(set.values());
     477    EXPECT_EQ(3U, vector.size());
     478
     479    std::sort(vector.begin(), vector.end());
     480    EXPECT_EQ(1, vector[0]);
     481    EXPECT_EQ(2, vector[1]);
     482    EXPECT_EQ(3, vector[2]);
     483}
     484
    472485} // namespace TestWebKitAPI
Note: See TracChangeset for help on using the changeset viewer.