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"
30 template<typename Value
, typename HashFunctions
= typename DefaultHash
<Value
>::Hash
,
31 typename Traits
= HashTraits
<Value
> > class HashCountedSet
{
33 typedef HashMap
<Value
, unsigned, HashFunctions
, Traits
> ImplType
;
35 typedef Value ValueType
;
36 typedef typename
ImplType::iterator iterator
;
37 typedef typename
ImplType::const_iterator const_iterator
;
45 // iterators iterate over pairs of values and counts
48 const_iterator
begin() const;
49 const_iterator
end() const;
51 iterator
find(const ValueType
& value
);
52 const_iterator
find(const ValueType
& value
) const;
53 bool contains(const ValueType
& value
) const;
54 unsigned count(const ValueType
& value
) const;
56 // increases the count if an equal value is already present
57 // the return value is a pair of an interator to the new value's location,
58 // and a bool that is true if an new entry was added
59 std::pair
<iterator
, bool> add(const ValueType
&value
);
61 // reduces the count of the value, and removes it if count
63 void remove(const ValueType
& value
);
64 void remove(iterator it
);
72 template<typename Value
, typename HashFunctions
, typename Traits
>
73 inline int HashCountedSet
<Value
, HashFunctions
, Traits
>::size() const
78 template<typename Value
, typename HashFunctions
, typename Traits
>
79 inline int HashCountedSet
<Value
, HashFunctions
, Traits
>::capacity() const
81 return m_impl
.capacity();
84 template<typename Value
, typename HashFunctions
, typename Traits
>
85 inline bool HashCountedSet
<Value
, HashFunctions
, Traits
>::isEmpty() const
90 template<typename Value
, typename HashFunctions
, typename Traits
>
91 inline typename HashCountedSet
<Value
, HashFunctions
, Traits
>::iterator HashCountedSet
<Value
, HashFunctions
, Traits
>::begin()
93 return m_impl
.begin();
96 template<typename Value
, typename HashFunctions
, typename Traits
>
97 inline typename HashCountedSet
<Value
, HashFunctions
, Traits
>::iterator HashCountedSet
<Value
, HashFunctions
, Traits
>::end()
102 template<typename Value
, typename HashFunctions
, typename Traits
>
103 inline typename HashCountedSet
<Value
, HashFunctions
, Traits
>::const_iterator HashCountedSet
<Value
, HashFunctions
, Traits
>::begin() const
105 return m_impl
.begin();
108 template<typename Value
, typename HashFunctions
, typename Traits
>
109 inline typename HashCountedSet
<Value
, HashFunctions
, Traits
>::const_iterator HashCountedSet
<Value
, HashFunctions
, Traits
>::end() const
114 template<typename Value
, typename HashFunctions
, typename Traits
>
115 inline typename HashCountedSet
<Value
, HashFunctions
, Traits
>::iterator HashCountedSet
<Value
, HashFunctions
, Traits
>::find(const ValueType
& value
)
117 return m_impl
.find(value
);
120 template<typename Value
, typename HashFunctions
, typename Traits
>
121 inline typename HashCountedSet
<Value
, HashFunctions
, Traits
>::const_iterator HashCountedSet
<Value
, HashFunctions
, Traits
>::find(const ValueType
& value
) const
123 return m_impl
.find(value
);
126 template<typename Value
, typename HashFunctions
, typename Traits
>
127 inline bool HashCountedSet
<Value
, HashFunctions
, Traits
>::contains(const ValueType
& value
) const
129 return m_impl
.contains(value
);
132 template<typename Value
, typename HashFunctions
, typename Traits
>
133 inline unsigned HashCountedSet
<Value
, HashFunctions
, Traits
>::count(const ValueType
& value
) const
135 return m_impl
.get(value
);
138 template<typename Value
, typename HashFunctions
, typename Traits
>
139 inline std::pair
<typename HashCountedSet
<Value
, HashFunctions
, Traits
>::iterator
, bool> HashCountedSet
<Value
, HashFunctions
, Traits
>::add(const ValueType
&value
)
141 pair
<iterator
, bool> result
= m_impl
.add(value
, 0);
142 ++result
.first
->second
;
146 template<typename Value
, typename HashFunctions
, typename Traits
>
147 inline void HashCountedSet
<Value
, HashFunctions
, Traits
>::remove(const ValueType
& value
)
152 template<typename Value
, typename HashFunctions
, typename Traits
>
153 inline void HashCountedSet
<Value
, HashFunctions
, Traits
>::remove(iterator it
)
158 unsigned oldVal
= it
->second
;
160 unsigned newVal
= oldVal
- 1;
167 template<typename Value
, typename HashFunctions
, typename Traits
>
168 inline void HashCountedSet
<Value
, HashFunctions
, Traits
>::clear()
173 template<typename Value
, typename HashFunctions
, typename Traits
, typename VectorType
>
174 inline void copyToVector(const HashCountedSet
<Value
, HashFunctions
, Traits
>& collection
, VectorType
& vector
)
176 typedef typename HashCountedSet
<Value
, HashFunctions
, Traits
>::const_iterator iterator
;
178 vector
.resize(collection
.size());
180 iterator it
= collection
.begin();
181 iterator end
= collection
.end();
182 for (unsigned i
= 0; it
!= end
; ++it
, ++i
)
186 template<typename Value
, typename HashFunctions
, typename Traits
>
187 inline void copyToVector(const HashCountedSet
<Value
, HashFunctions
, Traits
>& collection
, Vector
<Value
>& vector
)
189 typedef typename HashCountedSet
<Value
, HashFunctions
, Traits
>::const_iterator iterator
;
191 vector
.resize(collection
.size());
193 iterator it
= collection
.begin();
194 iterator end
= collection
.end();
195 for (unsigned i
= 0; it
!= end
; ++it
, ++i
)
196 vector
[i
] = (*it
).first
;
202 using WTF::HashCountedSet
;
204 #endif /* WTF_HashCountedSet_h */