Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

77 lignes
1.9 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2012 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://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. //
  11. // The Map class
  12. // -------------
  13. // A very simple Map class.
  14. //
  15. #if !defined __LOL_CORE_MAP_H__
  16. #define __LOL_CORE_MAP_H__
  17. namespace lol
  18. {
  19. /* A stupidly linear map for now */
  20. template<typename K, typename V> class Map : protected Hash<K>
  21. {
  22. public:
  23. inline V const& operator[] (K const &key) const
  24. {
  25. uint32_t hash = ((Hash<K> const &)*this)(key);
  26. for (int i = 0; i < m_array.Count(); ++i)
  27. if (m_array[i].m1 == hash)
  28. if (m_array[i].m2 == key)
  29. return m_array[i].m3;
  30. return V();
  31. }
  32. inline V & operator[] (K const &key)
  33. {
  34. return const_cast<V &>((const_cast<Map<K,V> const &>(*this))[key]);
  35. }
  36. inline V & Set(K const &key, V const &val)
  37. {
  38. uint32_t hash = ((Hash<K> const &)*this)(key);
  39. for (int i = 0; i < m_array.Count(); ++i)
  40. if (m_array[i].m1 == hash)
  41. if (m_array[i].m2 == key)
  42. {
  43. m_array[i].m3.~V();
  44. return m_array[i].m3 = val;
  45. }
  46. m_array.Push(hash, key, val);
  47. return m_array.Last().m3;
  48. }
  49. inline void Remove(K const &key)
  50. {
  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)
  55. {
  56. m_array.Remove(i);
  57. return;
  58. }
  59. }
  60. private:
  61. Array<uint32_t, K, V> m_array;
  62. };
  63. } /* namespace lol */
  64. #endif // __LOL_CORE_MAP_H__