src/Hash.h
author Jens Alfke <jens@mooseyard.com>
Sun Sep 20 20:39:24 2009 -0700 (2009-09-20)
changeset 1 6cbad782d16a
parent 0 include/Hash.h@31a43d94cc26
child 3 8e3ae153e2c9
permissions -rw-r--r--
Moved non-public headers out of include/ directory.
     1 /*
     2  *  Hash.h
     3  *  Ottoman
     4  *
     5  *  Created by Jens Alfke on 8/20/09.
     6  *  Copyright 2009 Jens Alfke. All rights reserved.
     7  *  BSD-Licensed: See the file "LICENSE.txt" for details.
     8  */
     9 
    10 #ifndef _MOOSEYARD_HASH_
    11 #define _MOOSEYARD_HASH_
    12 #include "Base.h"
    13 #include "Dictionary.h"
    14 
    15 namespace Mooseyard {
    16         
    17     /** A fairly low-level hash-table class whose keys and values are arbitrary data blobs.
    18         This class does no memory management of the keys and values: the memory ranges
    19         pointed to by the key and value parameters to put() must remain valid as long as
    20         that key or value remains in the hash-table. */
    21     class Hash {
    22         
    23     public:
    24         typedef void* Value;
    25         
    26         /** Creates a new, empty Hash. */
    27         explicit Hash (int capacity = 50);
    28         
    29         ~Hash();
    30         
    31         /** Gets the value associated with the given key. */
    32         Value get(Key) const;
    33         Value operator[] (Key key) const                        {return get(key);}
    34         
    35         /** Returns the number of values in the Hash. */
    36         int count() const                                       {return _count;}
    37         
    38         /** Sets the given value for the given key, replacing any existing value. */
    39         void put(Key, Value);
    40         
    41         /** Removes the value with the given key. */
    42         bool remove(Key);
    43         
    44         /** Removes all values. */
    45         void removeAll();
    46         
    47         void dump() const;
    48         
    49         class IndexEntry;
    50         
    51         class Iterator {
    52         public:
    53             Iterator (const Hash *h);
    54             Iterator (const Hash &h);
    55             operator bool() const                           {return hasValue();}
    56             Value operator*() const                         {return value();}
    57             Iterator& operator++()                          {next(); return *this;}
    58             
    59             Key key() const;
    60             Value value() const;
    61             bool hasValue() const                           {return _entry < _end;}
    62             bool next();
    63         private:
    64             Iterator (IndexEntry *start, IndexEntry*end);
    65             IndexEntry* _entry;
    66             IndexEntry* const _end;
    67             UNCOPYABLE(Iterator);
    68         };
    69         
    70     private:
    71         IndexEntry& _find (Key key) const;
    72         IndexEntry& _findForPut (const IndexEntry &newEntry);
    73         void _resize(int newSize);
    74         void _read();
    75         UNCOPYABLE(Hash);
    76         
    77         int _count;
    78         int _tableSize;
    79         IndexEntry *_table;
    80         
    81         friend class Iterator;
    82     };
    83     
    84 
    85 
    86     /** A concrete Dictionary implemented with a Hash. */
    87     class HashDictionary :public MutableDictionary {
    88     public:
    89         explicit HashDictionary (int capacity =50)  :_hash(capacity) { }
    90         virtual ~HashDictionary();
    91         virtual int count() const                   {return _hash.count();}
    92         virtual bool contains (Key) const;
    93         virtual Blob get (Key key) const            {return _convertValue(_hash.get(key));}
    94         virtual void put (Key, Blob value);
    95         virtual bool remove (Key);
    96         virtual void removeAll();
    97         virtual Iterator* iterate() const           {return new Iterator(*this);}
    98         
    99         class Iterator :public Dictionary::Iterator {
   100         public:
   101             explicit Iterator (const HashDictionary &d)      :_it(d._hash) { }
   102             virtual Key key() const                 {return _it.key();}
   103             virtual Blob value() const              {return _convertValue(_it.value());}
   104             virtual bool hasValue() const           {return _it.hasValue();}
   105             virtual bool next()                     {return _it.next();}
   106         private:
   107             Hash::Iterator _it;
   108         };
   109         
   110     private:
   111         static Blob _convertValue (void*);
   112         Hash _hash;
   113         friend class Iterator;
   114     };
   115     
   116     
   117 }
   118 
   119 #endif _MOOSEYARD_HASH_