The DejaVU Framework -- hush 3.1

include: professional-program-23-Hashmap-FinalHashmap-hashmap.h /cygdrive/d/www/media


- [up] [top] - index make include source logic grammar scripts html configure mx slides talks scenes reports projects
<body bgcolor="#FFFFFF" text="#000000">

include 
include 
include 
include  
include 
using std::vector;
using std::list;
using std::invalid_argument;
using std::string;
using std::pair;

// Any Hash Class must provide two methods: hash() and numBuckets().
template 
<h4 align=right text=red> DefaultHash</h4><hr>
  class DefaultHash
{
 public:
  // throws invalid_argument if numBuckets is non-positive
  DefaultHash(int numBuckets = 101) throw (invalid_argument);
  int hash(const T& key) const;
  int numBuckets() const { return mNumBuckets; }

 protected:
  int mNumBuckets;
};
<hr>


// Specialization for strings
template <>
<hr>>

.html> DefaultHash</h4>
  class DefaultHash<string>
  {
   public:
    // throws invalid_argument if numBuckets is non-positive
    DefaultHash(int numBuckets = 101) throw (invalid_argument);
    int hash(const string& key) const;
    int numBuckets() const { return mNumBuckets; }
  
   protected:
    int mNumBuckets;
  };


  
  template <typename Key, typename T, typename Compare, typename Hash>
  class hashmap;
  
  // HashIterator class definition
  template<typename Key, typename T, typename Compare, typename Hash>
  

HashIterator</h4>
  class HashIterator : public std::iterator >
  {
    // The iterator class needs access to protected members of the hashmap
    friend

hashmap</h4>
   class hashmap T, Compare, Hash>;
  
      public:
          HashIterator(); // bi-directional iterators must supply default ctors
          HashIterator(int bucket,
              typename list >::iterator listIt,
              const hashmap* inHashmap);
  
          pair& operator*() const;
  
          // Return type must be something to which -> can be applied.
          // Return a pointer to a pair, to which the compiler will
          // apply -> again
          pair* operator->() const;
          HashIterator& operator++();
          const HashIterator operator++(int);
  
          HashIterator& operator--();
          const HashIterator operator--(int);
  
          // don't need to define a copy constructor or operator= because the
          // default behavior is what we want.
  
          // don't need destructor because the default behavior
          // (not deleting mHashmap) is what we want.
  
          // These are ok as member functions because we don't support
          // comparisons of different types to this one
          bool operator==(const HashIterator& rhs) const;
          bool operator!=(const HashIterator& rhs) const;
  
      protected:
          int mBucket;
          typename list >::iterator mIt;
          const hashmap* mHashmap;
  
          // Helper methods for operator++ and operator--
          void increment();
          void decrement();
  };


  
  template Compare = std::equal_to,
            typename Hash = DefaultHash >
  

hashmap</h4>
  class hashmap
  {
    public:
    typedef Key key_type;
    typedef T mapped_type;
    typedef pair value_type;
    typedef Compare key_compare;
    typedef pair& reference;
    typedef const pair& const_reference;
    typedef size_t size_type;
    typedef ptrdiff_t difference_type;
    typedef HashIterator iterator;
    typedef HashIterator const_iterator;
  
    // Required class definition for associative containers
  

value_compare</h4>
    class value_compare :
    public std::binary_function
    {
      friend

hashmap</h4>
   class hashmap T, Compare, Hash>;
    public:
      bool operator() (const value_type& x, const value_type& y) const
        {
          return comp(x.first, y.first);
        }
    protected:
      Compare comp;
      value_compare(Compare c) : comp(c) {}
    };


  
    // The iterator class needs access to protected members of the hashmap
    friend

HashIterator</h4>
   class HashIterator T, Compare, Hash>;
  
    // Iterator methods
    iterator begin();
    iterator end();
    const_iterator begin() const;
    const_iterator end() const;
  
    // Constructors
    // throws invalid_argument if the hash object specifies a non-positive
    // number of buckets.   
    explicit hashmap(const Compare& comp = Compare(),
                     const Hash& hash = Hash()) throw(invalid_argument);
  
    template 
    hashmap(InputIterator first, InputIterator last,
            const Compare& comp = Compare(), const Hash& hash = Hash())
    throw(invalid_argument);
  
    // destructor, copy constructor, assignment operator
    ~hashmap();
    hashmap(const hashmap& src);
    hashmap& operator=(const hashmap<
                                              Key, T, Compare, Hash>& rhs);
  
    // Size methods
    bool empty() const;
    size_type size() const;
    size_type max_size() const;
  
    // element insert methods
    T& operator[] (const key_type& x);
    pair insert(const value_type& x);
    iterator insert(iterator position, const value_type& x);
          template 
    void insert(InputIterator first, InputIterator last);
  
    // element delete methods
    void erase(iterator position);
    size_type erase(const key_type& x);
    void erase(iterator first, iterator last);
  
    // other modifying utilities
    void swap(hashmap& hashIn);
  
    void clear();
  
    // access methods for STL conformity
    key_compare key_comp() const;
    value_compare value_comp() const;
  
    // Lookup methods
    iterator find(const key_type& x);
    const_iterator find(const key_type& x) const;
    size_type count(const key_type& x) const;
  
  protected:
  typename list >::iterator
  findElement(const key_type& x, int& bucket) const;
  
    typedef list ListType;
  
    // In this first implementation, it would be easier to use a vector
    // instead of a pointer to a vector, which requires dynamic allocation.
    // However, we use a ptr to a vector so that, in the final
    // implementation, swap() can be implemented in constant time.
    vector* mElems;
    int mSize;
    Compare mComp;
    Hash mHash;
  };


  
  include <hashmap.cpp>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author. <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> <script type="text/javascript"> _uacct = "UA-2780434-1"; urchinTracker(); </script>

Hush Online Technology
hush@cs.vu.nl
10/19/08