2  * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved. 
   4  * This library is free software; you can redistribute it and/or 
   5  * modify it under the terms of the GNU Library General Public 
   6  * License as published by the Free Software Foundation; either 
   7  * version 2 of the License, or (at your option) any later version. 
   9  * This library 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 GNU 
  12  * Library General Public License for more details. 
  14  * You should have received a copy of the GNU Library General Public License 
  15  * along with this library; see the file COPYING.LIB.  If not, write to 
  16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
  17  * Boston, MA 02110-1301, USA. 
  21 #ifndef WTF_HashCountedSet_h 
  22 #define WTF_HashCountedSet_h 
  24 #include "Assertions.h" 
  25 #include "FastAllocBase.h" 
  31     template<typename Value
, typename HashFunctions 
= typename DefaultHash
<Value
>::Hash
, 
  32         typename Traits 
= HashTraits
<Value
> > class HashCountedSet 
: public FastAllocBase 
{ 
  34         typedef HashMap
<Value
, unsigned, HashFunctions
, Traits
> ImplType
; 
  36         typedef Value ValueType
; 
  37         typedef typename 
ImplType::iterator iterator
; 
  38         typedef typename 
ImplType::const_iterator const_iterator
; 
  46         // iterators iterate over pairs of values and counts 
  49         const_iterator 
begin() const; 
  50         const_iterator 
end() const; 
  52         iterator 
find(const ValueType
&); 
  53         const_iterator 
find(const ValueType
&) const; 
  54         bool contains(const ValueType
&) const; 
  55         unsigned count(const ValueType
&) const; 
  57         // increases the count if an equal value is already present 
  58         // the return value is a pair of an interator to the new value's location,  
  59         // and a bool that is true if an new entry was added 
  60         std::pair
<iterator
, bool> add(const ValueType
&); 
  62         // reduces the count of the value, and removes it if count 
  64         void remove(const ValueType
&); 
  65         void remove(iterator
); 
  67         // removes the value, regardless of its count 
  68         void removeAll(iterator
); 
  69         void removeAll(const ValueType
&); 
  71         // clears the whole set 
  78     template<typename Value
, typename HashFunctions
, typename Traits
> 
  79     inline int HashCountedSet
<Value
, HashFunctions
, Traits
>::size() const 
  84     template<typename Value
, typename HashFunctions
, typename Traits
> 
  85     inline int HashCountedSet
<Value
, HashFunctions
, Traits
>::capacity() const 
  87         return m_impl
.capacity();  
  90     template<typename Value
, typename HashFunctions
, typename Traits
> 
  91     inline bool HashCountedSet
<Value
, HashFunctions
, Traits
>::isEmpty() const 
  96     template<typename Value
, typename HashFunctions
, typename Traits
> 
  97     inline typename HashCountedSet
<Value
, HashFunctions
, Traits
>::iterator HashCountedSet
<Value
, HashFunctions
, Traits
>::begin() 
  99         return m_impl
.begin();  
 102     template<typename Value
, typename HashFunctions
, typename Traits
> 
 103     inline typename HashCountedSet
<Value
, HashFunctions
, Traits
>::iterator HashCountedSet
<Value
, HashFunctions
, Traits
>::end() 
 108     template<typename Value
, typename HashFunctions
, typename Traits
> 
 109     inline typename HashCountedSet
<Value
, HashFunctions
, Traits
>::const_iterator HashCountedSet
<Value
, HashFunctions
, Traits
>::begin() const 
 111         return m_impl
.begin();  
 114     template<typename Value
, typename HashFunctions
, typename Traits
> 
 115     inline typename HashCountedSet
<Value
, HashFunctions
, Traits
>::const_iterator HashCountedSet
<Value
, HashFunctions
, Traits
>::end() const 
 120     template<typename Value
, typename HashFunctions
, typename Traits
> 
 121     inline typename HashCountedSet
<Value
, HashFunctions
, Traits
>::iterator HashCountedSet
<Value
, HashFunctions
, Traits
>::find(const ValueType
& value
) 
 123         return m_impl
.find(value
);  
 126     template<typename Value
, typename HashFunctions
, typename Traits
> 
 127     inline typename HashCountedSet
<Value
, HashFunctions
, Traits
>::const_iterator HashCountedSet
<Value
, HashFunctions
, Traits
>::find(const ValueType
& value
) const 
 129         return m_impl
.find(value
);  
 132     template<typename Value
, typename HashFunctions
, typename Traits
> 
 133     inline bool HashCountedSet
<Value
, HashFunctions
, Traits
>::contains(const ValueType
& value
) const 
 135         return m_impl
.contains(value
);  
 138     template<typename Value
, typename HashFunctions
, typename Traits
> 
 139     inline unsigned HashCountedSet
<Value
, HashFunctions
, Traits
>::count(const ValueType
& value
) const 
 141         return m_impl
.get(value
); 
 144     template<typename Value
, typename HashFunctions
, typename Traits
> 
 145     inline std::pair
<typename HashCountedSet
<Value
, HashFunctions
, Traits
>::iterator
, bool> HashCountedSet
<Value
, HashFunctions
, Traits
>::add(const ValueType 
&value
) 
 147         pair
<iterator
, bool> result 
= m_impl
.add(value
, 0);  
 148         ++result
.first
->second
; 
 152     template<typename Value
, typename HashFunctions
, typename Traits
> 
 153     inline void HashCountedSet
<Value
, HashFunctions
, Traits
>::remove(const ValueType
& value
) 
 158     template<typename Value
, typename HashFunctions
, typename Traits
> 
 159     inline void HashCountedSet
<Value
, HashFunctions
, Traits
>::remove(iterator it
) 
 164         unsigned oldVal 
= it
->second
; 
 166         unsigned newVal 
= oldVal 
- 1; 
 173     template<typename Value
, typename HashFunctions
, typename Traits
> 
 174     inline void HashCountedSet
<Value
, HashFunctions
, Traits
>::removeAll(const ValueType
& value
) 
 176         removeAll(find(value
)); 
 179     template<typename Value
, typename HashFunctions
, typename Traits
> 
 180     inline void HashCountedSet
<Value
, HashFunctions
, Traits
>::removeAll(iterator it
) 
 188     template<typename Value
, typename HashFunctions
, typename Traits
> 
 189     inline void HashCountedSet
<Value
, HashFunctions
, Traits
>::clear() 
 194     template<typename Value
, typename HashFunctions
, typename Traits
, typename VectorType
> 
 195     inline void copyToVector(const HashCountedSet
<Value
, HashFunctions
, Traits
>& collection
, VectorType
& vector
) 
 197         typedef typename HashCountedSet
<Value
, HashFunctions
, Traits
>::const_iterator iterator
; 
 199         vector
.resize(collection
.size()); 
 201         iterator it 
= collection
.begin(); 
 202         iterator end 
= collection
.end(); 
 203         for (unsigned i 
= 0; it 
!= end
; ++it
, ++i
) 
 207     template<typename Value
, typename HashFunctions
, typename Traits
> 
 208     inline void copyToVector(const HashCountedSet
<Value
, HashFunctions
, Traits
>& collection
, Vector
<Value
>& vector
) 
 210         typedef typename HashCountedSet
<Value
, HashFunctions
, Traits
>::const_iterator iterator
; 
 212         vector
.resize(collection
.size()); 
 214         iterator it 
= collection
.begin(); 
 215         iterator end 
= collection
.end(); 
 216         for (unsigned i 
= 0; it 
!= end
; ++it
, ++i
) 
 217             vector
[i
] = (*it
).first
; 
 223 using WTF::HashCountedSet
; 
 225 #endif /* WTF_HashCountedSet_h */