• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

WTF

  • sources
  • kde-4.14
  • kdelibs
  • kjs
  • wtf
HashCountedSet.h
Go to the documentation of this file.
1 // -*- mode: c++; c-basic-offset: 4 -*-
2 /*
3  * This file is part of the KDE libraries
4  * Copyright (C) 2005 Apple Computer, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef WTF_HashCountedSet_h
24 #define WTF_HashCountedSet_h
25 
26 #include "Assertions.h"
27 #include "HashMap.h"
28 #include "Vector.h"
29 
30 namespace WTF {
31 
32  template<typename Value, typename HashFunctions = typename DefaultHash<Value>::Hash,
33  typename Traits = HashTraits<Value> > class HashCountedSet {
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 
41  HashCountedSet() {}
42 
43  int size() const;
44  int capacity() const;
45  bool isEmpty() const;
46 
47  // iterators iterate over pairs of values and counts
48  iterator begin();
49  iterator end();
50  const_iterator begin() const;
51  const_iterator end() const;
52 
53  iterator find(const ValueType& value);
54  const_iterator find(const ValueType& value) const;
55  bool contains(const ValueType& value) const;
56  unsigned count(const ValueType& value) const;
57 
58  // increases the count if an equal value is already present
59  // the return value is a pair of an interator to the new value's location,
60  // and a bool that is true if an new entry was added
61  std::pair<iterator, bool> add(const ValueType &value);
62 
63  // reduces the count of the value, and removes it if count
64  // goes down to zero
65  void remove(const ValueType& value);
66  void remove(iterator it);
67 
68  void clear();
69 
70  private:
71  ImplType m_impl;
72  };
73 
74  template<typename Value, typename HashFunctions, typename Traits>
75  inline int HashCountedSet<Value, HashFunctions, Traits>::size() const
76  {
77  return m_impl.size();
78  }
79 
80  template<typename Value, typename HashFunctions, typename Traits>
81  inline int HashCountedSet<Value, HashFunctions, Traits>::capacity() const
82  {
83  return m_impl.capacity();
84  }
85 
86  template<typename Value, typename HashFunctions, typename Traits>
87  inline bool HashCountedSet<Value, HashFunctions, Traits>::isEmpty() const
88  {
89  return size() == 0;
90  }
91 
92  template<typename Value, typename HashFunctions, typename Traits>
93  inline typename HashCountedSet<Value, HashFunctions, Traits>::iterator HashCountedSet<Value, HashFunctions, Traits>::begin()
94  {
95  return m_impl.begin();
96  }
97 
98  template<typename Value, typename HashFunctions, typename Traits>
99  inline typename HashCountedSet<Value, HashFunctions, Traits>::iterator HashCountedSet<Value, HashFunctions, Traits>::end()
100  {
101  return m_impl.end();
102  }
103 
104  template<typename Value, typename HashFunctions, typename Traits>
105  inline typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator HashCountedSet<Value, HashFunctions, Traits>::begin() const
106  {
107  return m_impl.begin();
108  }
109 
110  template<typename Value, typename HashFunctions, typename Traits>
111  inline typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator HashCountedSet<Value, HashFunctions, Traits>::end() const
112  {
113  return m_impl.end();
114  }
115 
116  template<typename Value, typename HashFunctions, typename Traits>
117  inline typename HashCountedSet<Value, HashFunctions, Traits>::iterator HashCountedSet<Value, HashFunctions, Traits>::find(const ValueType& value)
118  {
119  return m_impl.find(value);
120  }
121 
122  template<typename Value, typename HashFunctions, typename Traits>
123  inline typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator HashCountedSet<Value, HashFunctions, Traits>::find(const ValueType& value) const
124  {
125  return m_impl.find(value);
126  }
127 
128  template<typename Value, typename HashFunctions, typename Traits>
129  inline bool HashCountedSet<Value, HashFunctions, Traits>::contains(const ValueType& value) const
130  {
131  return m_impl.contains(value);
132  }
133 
134  template<typename Value, typename HashFunctions, typename Traits>
135  inline unsigned HashCountedSet<Value, HashFunctions, Traits>::count(const ValueType& value) const
136  {
137  return m_impl.get(value);
138  }
139 
140  template<typename Value, typename HashFunctions, typename Traits>
141  inline std::pair<typename HashCountedSet<Value, HashFunctions, Traits>::iterator, bool> HashCountedSet<Value, HashFunctions, Traits>::add(const ValueType &value)
142  {
143  pair<iterator, bool> result = m_impl.add(value, 0);
144  ++result.first->second;
145  return result;
146  }
147 
148  template<typename Value, typename HashFunctions, typename Traits>
149  inline void HashCountedSet<Value, HashFunctions, Traits>::remove(const ValueType& value)
150  {
151  remove(find(value));
152  }
153 
154  template<typename Value, typename HashFunctions, typename Traits>
155  inline void HashCountedSet<Value, HashFunctions, Traits>::remove(iterator it)
156  {
157  if (it == end())
158  return;
159 
160  unsigned oldVal = it->second;
161  ASSERT(oldVal != 0);
162  unsigned newVal = oldVal - 1;
163  if (newVal == 0)
164  m_impl.remove(it);
165  else
166  it->second = newVal;
167  }
168 
169  template<typename Value, typename HashFunctions, typename Traits>
170  inline void HashCountedSet<Value, HashFunctions, Traits>::clear()
171  {
172  m_impl.clear();
173  }
174 
175  template<typename Value, typename HashFunctions, typename Traits, typename VectorType>
176  inline void copyToVector(const HashCountedSet<Value, HashFunctions, Traits>& collection, VectorType& vector)
177  {
178  typedef typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator iterator;
179 
180  vector.resize(collection.size());
181 
182  iterator it = collection.begin();
183  iterator end = collection.end();
184  for (unsigned i = 0; it != end; ++it, ++i)
185  vector[i] = *it;
186  }
187 
188  template<typename Value, typename HashFunctions, typename Traits>
189  inline void copyToVector(const HashCountedSet<Value, HashFunctions, Traits>& collection, Vector<Value>& vector)
190  {
191  typedef typename HashCountedSet<Value, HashFunctions, Traits>::const_iterator iterator;
192 
193  vector.resize(collection.size());
194 
195  iterator it = collection.begin();
196  iterator end = collection.end();
197  for (unsigned i = 0; it != end; ++it, ++i)
198  vector[i] = (*it).first;
199  }
200 
201 
202 } // namespace khtml
203 
204 using WTF::HashCountedSet;
205 
206 #endif /* WTF_HashCountedSet_h */
WTF::HashCountedSet::ValueType
Value ValueType
Definition: HashCountedSet.h:37
WTF::Vector
Definition: Forward.h:32
WTF::HashCountedSet::begin
iterator begin()
Definition: HashCountedSet.h:93
WTF::HashCountedSet::contains
bool contains(const ValueType &value) const
Definition: HashCountedSet.h:129
WTF::HashCountedSet::capacity
int capacity() const
Definition: HashCountedSet.h:81
Assertions.h
HashMap.h
WTF::HashCountedSet::isEmpty
bool isEmpty() const
Definition: HashCountedSet.h:87
WTF::Vector::resize
void resize(size_t size)
Definition: Vector.h:629
WTF::HashCountedSet::find
iterator find(const ValueType &value)
Definition: HashCountedSet.h:117
WTF::Vector::first
T & first()
Definition: Vector.h:458
WTF::HashCountedSet::count
unsigned count(const ValueType &value) const
Definition: HashCountedSet.h:135
WTF::copyToVector
void copyToVector(const HashCountedSet< Value, HashFunctions, Traits > &collection, VectorType &vector)
Definition: HashCountedSet.h:176
WTF::HashCountedSet::add
std::pair< iterator, bool > add(const ValueType &value)
Definition: HashCountedSet.h:141
Vector.h
WTF::HashCountedSet::clear
void clear()
Definition: HashCountedSet.h:170
WTF::HashMap< Value, unsigned, HashFunctions, Traits >
WTF::HashCountedSet::remove
void remove(const ValueType &value)
Definition: HashCountedSet.h:149
WTF::HashCountedSet
Definition: HashCountedSet.h:33
ASSERT
#define ASSERT(x)
Definition: Assertions.h:33
WTF::HashTraits
Definition: HashTraits.h:76
WTF::HashCountedSet::HashCountedSet
HashCountedSet()
Definition: HashCountedSet.h:41
WTF::HashCountedSet::size
int size() const
Definition: HashCountedSet.h:75
WTF::HashCountedSet::end
iterator end()
Definition: HashCountedSet.h:99
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:23:46 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

WTF

Skip menu "WTF"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal