00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef WTF_HashCountedSet_h
00024 #define WTF_HashCountedSet_h
00025
00026 #include "Assertions.h"
00027 #include "HashMap.h"
00028
00029 namespace WTF {
00030
00031 template<typename Value, typename HashFunctions = typename DefaultHash<Value>::Hash,
00032 typename Traits = HashTraits<Value> > class HashCountedSet {
00033 private:
00034 typedef HashMap<Value, unsigned, HashFunctions, Traits> ImplType;
00035 public:
00036 typedef Value ValueType;
00037 typedef typename ImplType::iterator iterator;
00038 typedef typename ImplType::const_iterator const_iterator;
00039
00040 HashCountedSet() {}
00041
00042 int size() const;
00043 int capacity() const;
00044 bool isEmpty() const;
00045
00046
00047 iterator begin();
00048 iterator end();
00049 const_iterator begin() const;
00050 const_iterator end() const;
00051
00052 iterator find(const ValueType& value);
00053 const_iterator find(const ValueType& value) const;
00054 bool contains(const ValueType& value) const;
00055 unsigned count(const ValueType& value) const;
00056
00057
00058
00059
00060 std::pair<iterator, bool> add(const ValueType &value);
00061
00062
00063
00064 void remove(const ValueType& value);
00065 void remove(iterator it);
00066
00067 void clear();
00068
00069 private:
00070 ImplType m_impl;
00071 };
00072
00073 template<typename Value, typename HashFunctions, typename Traits>
00074 inline int HashCountedSet<Value, HashFunctions, Traits>::size() const
00075 {
00076 return m_impl.size();
00077 }
00078
00079 template<typename Value, typename HashFunctions, typename Traits>
00080 inline int HashCountedSet<Value, HashFunctions, Traits>::capacity() const
00081 {
00082 return m_impl.capacity();
00083 }
00084
00085 template<typename Value, typename HashFunctions, typename Traits>
00086 inline bool HashCountedSet<Value, HashFunctions, Traits>::isEmpty() const
00087 {
00088 return size() == 0;
00089 }
00090
00091 template<typename Value, typename HashFunctions, typename Traits>
00092 inline typename HashCountedSet<Value, HashFunctions, Traits>::iterator HashCountedSet<Value, HashFunctions, Traits>::begin()
00093 {
00094 return m_impl.begin();
00095 }
00096
00097 template<typename Value, typename HashFunctions, typename Traits>
00098 inline typename HashCountedSet<Value, HashFunctions, Traits>::iterator HashCountedSet<Value, HashFunctions, Traits>::end()
00099 {
00100 return m_impl.end();
00101 }
00102
00103 template<typename Value, typename HashFunctions, typename Traits>
00104 inline typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator HashCountedSet<Value, HashFunctions, Traits>::begin() const
00105 {
00106 return m_impl.begin();
00107 }
00108
00109 template<typename Value, typename HashFunctions, typename Traits>
00110 inline typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator HashCountedSet<Value, HashFunctions, Traits>::end() const
00111 {
00112 return m_impl.end();
00113 }
00114
00115 template<typename Value, typename HashFunctions, typename Traits>
00116 inline typename HashCountedSet<Value, HashFunctions, Traits>::iterator HashCountedSet<Value, HashFunctions, Traits>::find(const ValueType& value)
00117 {
00118 return m_impl.find(value);
00119 }
00120
00121 template<typename Value, typename HashFunctions, typename Traits>
00122 inline typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator HashCountedSet<Value, HashFunctions, Traits>::find(const ValueType& value) const
00123 {
00124 return m_impl.find(value);
00125 }
00126
00127 template<typename Value, typename HashFunctions, typename Traits>
00128 inline bool HashCountedSet<Value, HashFunctions, Traits>::contains(const ValueType& value) const
00129 {
00130 return m_impl.contains(value);
00131 }
00132
00133 template<typename Value, typename HashFunctions, typename Traits>
00134 inline unsigned HashCountedSet<Value, HashFunctions, Traits>::count(const ValueType& value) const
00135 {
00136 return m_impl.get(value);
00137 }
00138
00139 template<typename Value, typename HashFunctions, typename Traits>
00140 inline std::pair<typename HashCountedSet<Value, HashFunctions, Traits>::iterator, bool> HashCountedSet<Value, HashFunctions, Traits>::add(const ValueType &value)
00141 {
00142 pair<iterator, bool> result = m_impl.add(value, 0);
00143 ++result.first->second;
00144 return result;
00145 }
00146
00147 template<typename Value, typename HashFunctions, typename Traits>
00148 inline void HashCountedSet<Value, HashFunctions, Traits>::remove(const ValueType& value)
00149 {
00150 remove(find(value));
00151 }
00152
00153 template<typename Value, typename HashFunctions, typename Traits>
00154 inline void HashCountedSet<Value, HashFunctions, Traits>::remove(iterator it)
00155 {
00156 if (it == end())
00157 return;
00158
00159 unsigned oldVal = it->second;
00160 ASSERT(oldVal != 0);
00161 unsigned newVal = oldVal - 1;
00162 if (newVal == 0)
00163 m_impl.remove(it);
00164 else
00165 it->second = newVal;
00166 }
00167
00168 template<typename Value, typename HashFunctions, typename Traits>
00169 inline void HashCountedSet<Value, HashFunctions, Traits>::clear()
00170 {
00171 m_impl.clear();
00172 }
00173
00174 }
00175
00176 using WTF::HashCountedSet;
00177
00178 #endif