Changeset 2776


Ignore:
Timestamp:
Jun 23, 2013, 5:31:25 PM (8 years ago)
Author:
benlitz
Message:

base: map methods can now take any type of object that is equatable to the key type and can be hashed by the key type specialization of the Hash class (and added string/char const* compatibility)

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/base/hash.cpp

    r2216 r2776  
    168168}
    169169
     170uint32_t Hash<char const *>::operator ()(String const &s) const
     171{
     172    return HashCharString(&s[0]);
     173}
     174
    170175uint32_t Hash<String>::operator ()(String const &s) const
    171176{
     
    173178}
    174179
     180uint32_t Hash<String>::operator ()(char const *s) const
     181{
     182    return HashCharString(s);
     183}
     184
    175185} /* namespace lol */
    176186
  • trunk/src/lol/base/hash.h

    r2261 r2776  
    4141public:
    4242    uint32_t operator()(char const *x) const;
     43    uint32_t operator()(String const &s) const;
    4344};
    4445
     
    4748public:
    4849    uint32_t operator()(String const &s) const;
     50    uint32_t operator()(char const *x) const;
    4951};
    5052
  • trunk/src/lol/base/map.h

    r2479 r2776  
    2525{
    2626public:
     27        /* If E is different from K, Hash<K> must implement operator()(E const&)
     28     * and an equality operator between K and E must exist in order to use this method.
     29      */
     30
    2731    /* I choose to make this inline because passing the key by reference
    2832     * is usually suboptimal. */
    29     inline V const& operator[] (K const &key) const
     33        template <typename E>
     34    inline V const& operator[] (E const &key) const
    3035    {
    3136        /* Look for the hash in our table and return the value. */
     
    4045    }
    4146
    42     inline V & operator[] (K const &key)
     47        template <typename E>
     48    inline V & operator[] (E const &key)
    4349    {
    4450        /* Look for the hash in our table and return the value if found. */
     
    5359    }
    5460
    55     inline void Remove(K const &key)
     61        template <typename E>
     62    inline void Remove(E const &key)
    5663    {
    5764        uint32_t hash = ((Hash<K> const &)*this)(key);
     
    6572    }
    6673
    67     inline bool HasKey(K const &key)
     74        template <typename E>
     75    inline bool HasKey(E const &key)
    6876    {
    6977        uint32_t hash = ((Hash<K> const &)*this)(key);
     
    7583    }
    7684
     85        template <typename E>
     86    inline bool TryGetValue(E const &key, V& value)
     87    {
     88        uint32_t hash = ((Hash<K> const &)*this)(key);
     89        for (int i = 0; i < m_array.Count(); ++i)
     90            if (m_array[i].m1 == hash)
     91                if (m_array[i].m2 == key)
     92                {
     93                    value = m_array[i].m3;
     94                    return true;
     95                }
     96
     97        return false;
     98    }
     99
     100    inline int Count() const { return m_array.Count(); }
     101
    77102private:
    78103    Array<uint32_t, K, V> m_array;
Note: See TracChangeset for help on using the changeset viewer.