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

Last change on this file since 2808 was 2808, checked in by sam, 8 years ago

base: fix map.h build.

  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
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
8//   http://www.wtfpl.net/ for more details.
9//
10
11//
12// The Map class
13// -------------
14// A very simple Map class.
15//
16
17#if !defined __LOL_BASE_MAP_H__
18#define __LOL_BASE_MAP_H__
19
20namespace lol
21{
22
23/* A stupidly linear map for now. */
24template<typename K, typename V> class Map : protected Hash<K>
25{
26public:
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
29     * this method. */
30
31    /* I choose to make this inline because passing the key by reference
32     * is usually suboptimal. */
33    template <typename E>
34    inline V const& operator[] (E const &key) const
35    {
36        /* Look for the hash in our table and return the value. */
37        int i = FindIndex(key);
38        ASSERT(i >= 0, "trying to read a nonexistent key in map");
39        return m_array[i].m3;
40    }
41
42    template <typename E>
43    inline V & operator[] (E const &key)
44    {
45        /* Look for the hash in our table and return the value if found. */
46        uint32_t hash = ((Hash<K> const &)*this)(key);
47        int i = FindIndex(key, hash);
48        if (i >= 0)
49            return m_array[i].m3;
50
51        /* If not found, insert a new value. */
52        m_array.Push(hash, key, V());
53        return m_array.Last().m3;
54    }
55
56    template <typename E>
57    inline void Remove(E const &key)
58    {
59        int i = FindIndex(key);
60        if (i >= 0)
61            m_array.Remove(i);
62    }
63
64    template <typename E>
65    inline bool HasKey(E const &key)
66    {
67        return FindIndex(key) >= 0;
68    }
69
70    template <typename E>
71    inline bool TryGetValue(E const &key, V& value)
72    {
73        int i = FindIndex(key);
74        if (i >= 0)
75        {
76            value = m_array[i].m3;
77            return true;
78        }
79
80        return false;
81    }
82
83    inline int Count() const
84    {
85        return m_array.Count();
86    }
87
88private:
89    template <typename E>
90    inline int FindIndex(E const &key, uint32_t hash)
91    {
92        for (int i = 0; i < m_array.Count(); ++i)
93            if (m_array[i].m1 == hash)
94                if (m_array[i].m2 == key)
95                    return i;
96        return -1;
97    }
98
99    template <typename E>
100    inline int FindIndex(E const &key)
101    {
102        uint32_t hash = ((Hash<K> const &)*this)(key);
103        return FindIndex(key, hash);
104    }
105
106    Array<uint32_t, K, V> m_array;
107};
108
109} /* namespace lol */
110
111#endif // __LOL_BASE_MAP_H__
112
Note: See TracBrowser for help on using the repository browser.