source: trunk/src/lol/base/map.h @ 2776

Last change on this file since 2776 was 2776, checked in by benlitz, 8 years ago

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)

  • Property svn:keywords set to Id
File size: 3.0 KB
RevLine 
[2106]1//
2// Lol Engine
3//
[2216]4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
[2106]5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
[2183]8//   http://www.wtfpl.net/ for more details.
[2106]9//
10
11//
12// The Map class
13// -------------
14// A very simple Map class.
15//
16
[2261]17#if !defined __LOL_BASE_MAP_H__
18#define __LOL_BASE_MAP_H__
[2106]19
20namespace lol
21{
22
[2109]23/* A stupidly linear map for now. */
[2108]24template<typename K, typename V> class Map : protected Hash<K>
25{
26public:
[2776]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
[2109]31    /* I choose to make this inline because passing the key by reference
32     * is usually suboptimal. */
[2776]33        template <typename E>
34    inline V const& operator[] (E const &key) const
[2108]35    {
[2109]36        /* Look for the hash in our table and return the value. */
[2108]37        uint32_t hash = ((Hash<K> const &)*this)(key);
38        for (int i = 0; i < m_array.Count(); ++i)
39            if (m_array[i].m1 == hash)
40                if (m_array[i].m2 == key)
41                    return m_array[i].m3;
[2109]42        /* XXX: this in an error! */
[2479]43        ASSERT(0, "trying to read a nonexistent key in map");
[2108]44        return V();
45    }
[2106]46
[2776]47        template <typename E>
48    inline V & operator[] (E const &key)
[2108]49    {
[2109]50        /* Look for the hash in our table and return the value if found. */
[2108]51        uint32_t hash = ((Hash<K> const &)*this)(key);
52        for (int i = 0; i < m_array.Count(); ++i)
53            if (m_array[i].m1 == hash)
54                if (m_array[i].m2 == key)
[2109]55                    return m_array[i].m3;
56        /* If not found, insert a new value. */
57        m_array.Push(hash, key, V());
[2108]58        return m_array.Last().m3;
59    }
60
[2776]61        template <typename E>
62    inline void Remove(E const &key)
[2108]63    {
64        uint32_t hash = ((Hash<K> const &)*this)(key);
65        for (int i = 0; i < m_array.Count(); ++i)
66            if (m_array[i].m1 == hash)
67                if (m_array[i].m2 == key)
68                {
69                    m_array.Remove(i);
70                    return;
71                }
72    }
73
[2776]74        template <typename E>
75    inline bool HasKey(E const &key)
[2109]76    {
77        uint32_t hash = ((Hash<K> const &)*this)(key);
78        for (int i = 0; i < m_array.Count(); ++i)
79            if (m_array[i].m1 == hash)
80                if (m_array[i].m2 == key)
81                    return true;
82        return false;
83    }
84
[2776]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
[2108]102private:
103    Array<uint32_t, K, V> m_array;
104};
105
[2106]106} /* namespace lol */
107
[2261]108#endif // __LOL_BASE_MAP_H__
[2106]109
Note: See TracBrowser for help on using the repository browser.