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

WTF

HashCountedSet.h

Go to the documentation of this file.
00001 // -*- mode: c++; c-basic-offset: 4 -*-
00002 /*
00003  * This file is part of the KDE libraries
00004  * Copyright (C) 2005 Apple Computer, Inc.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public License
00017  * along with this library; see the file COPYING.LIB.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
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         // iterators iterate over pairs of values and counts
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         // increases the count if an equal value is already present
00058         // the return value is a pair of an interator to the new value's location, 
00059         // and a bool that is true if an new entry was added
00060         std::pair<iterator, bool> add(const ValueType &value);
00061         
00062         // reduces the count of the value, and removes it if count
00063         // goes down to zero
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 } // namespace khtml
00175 
00176 using WTF::HashCountedSet;
00177 
00178 #endif /* WTF_HashCountedSet_h */

WTF

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   WTF
  • KJSEmbed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  •   core
  • Phonon
  •   Backend
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal