1
1

fifoMap.hpp 694 B

12345678910111213141516171819202122232425262728293031323334
  1. #pragma once
  2. #include <stdexcept>
  3. #include <utility>
  4. #include <deque>
  5. template <class K, class V>
  6. class fifoMap: public std::deque<std::pair<K, V>>
  7. {
  8. public:
  9. bool contains(const K &) const;
  10. V &operator[](const K&);
  11. };
  12. template<class K, class V>
  13. bool fifoMap<K, V>::contains(const K &k) const
  14. {
  15. for (std::pair<K, V> i: *this)
  16. if (i.first == k)
  17. return true;
  18. return false;
  19. }
  20. template<class K, class V>
  21. V &fifoMap<K, V>::operator[](const K &k)
  22. {
  23. for (std::pair<K, V> &i: *this)
  24. if (i.first == k)
  25. return i.second;
  26. this->push_back(std::pair<K, V>(k, V()));
  27. std::pair<K, V> &i = this->back();
  28. return i.second;
  29. }