lru_cache.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #pragma once
  32. #ifndef _LRU_CACHE_H_
  33. #define _LRU_CACHE_H_
  34. #include <assert.h>
  35. #include <list>
  36. /* STL map is implemented as a tree, hence all operations are O(logN)
  37. STL unordered_map is implemented as hash, hence all operations are O(1)
  38. http://stackoverflow.com/questions/2196995/is-there-any-advantage-of-using-map-over-unordered-map-in-case-of-trivial-keys
  39. http://stackoverflow.com/questions/3902644/choosing-between-stdmap-and-stdunordered-map
  40. */
  41. #include <unordered_map>
  42. /* this hasher code was needed since strict ansi don't allow 'long long' type
  43. * adding -U__STRICT_ANSI__ to the compilation flags solved the issue
  44. * leaving this code here for future reference
  45. namespace stlpmtx_std
  46. {
  47. template<> struct hash<uint64_t> {
  48. size_t operator()(uint64_t x) const { return (size_t)x; }
  49. };
  50. }
  51. */
  52. typedef struct _list_node
  53. {
  54. uint64_t key;
  55. } list_node_t;
  56. typedef struct _map_node
  57. {
  58. void* data;
  59. std::list<list_node_t*>::iterator list_it;
  60. } map_node_t;
  61. typedef std::unordered_map<uint64_t, map_node_t*>::iterator map_iterator;
  62. typedef std::list<list_node_t*>::iterator list_iterator;
  63. class lru_cache
  64. {
  65. private:
  66. std::list<list_node_t*> list;
  67. std::unordered_map<uint64_t, map_node_t*> map;
  68. list_iterator m_it; // for get_first and get_next sequence
  69. public:
  70. lru_cache();
  71. ~lru_cache();
  72. void rehash(uint32_t size_);
  73. bool add(uint64_t key, void* p);
  74. void* get(uint64_t key);
  75. void* find(uint64_t key); // only returns the object, do not bump it to the head
  76. uint32_t size();
  77. void* get_first();
  78. void* get_next();
  79. void* get_last();
  80. void remove_last();
  81. };
  82. #endif // _LRU_CACHE_H_