maputil.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright 2010-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #ifndef BX_MAPUTIL_H_HEADER_GUARD
  6. #define BX_MAPUTIL_H_HEADER_GUARD
  7. #include "bx.h"
  8. namespace bx
  9. {
  10. template<typename MapType>
  11. typename MapType::iterator mapInsertOrUpdate(MapType& _map, const typename MapType::key_type& _key, const typename MapType::mapped_type& _value)
  12. {
  13. typename MapType::iterator it = _map.lower_bound(_key);
  14. if (it != _map.end()
  15. && !_map.key_comp()(_key, it->first) )
  16. {
  17. it->second = _value;
  18. return it;
  19. }
  20. typename MapType::value_type pair(_key, _value);
  21. return _map.insert(it, pair);
  22. }
  23. template<typename MapType>
  24. bool mapRemove(MapType& _map, const typename MapType::value_type::first_type& _first)
  25. {
  26. typename MapType::const_iterator it = _map.find(_first);
  27. if (it != _map.end() )
  28. {
  29. _map.erase(it);
  30. return true;
  31. }
  32. return false;
  33. }
  34. template<typename MapType>
  35. bool mapRemove(MapType& _map, const typename MapType::value_type::second_type& _second)
  36. {
  37. for (typename MapType::const_iterator it = _map.begin(), itEnd = _map.end(); it != itEnd; ++it)
  38. {
  39. if (it->second == _second)
  40. {
  41. _map.erase(it);
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. } // namespace bx
  48. #endif // BX_MAPUTIL_H_HEADER_GUARD