|
| 1 | +// Copyright 2016-2017 Diffblue Limited. All Rights Reserved. |
| 2 | + |
| 3 | +/// \file |
| 4 | +/// A map-like data structure that holds all entries in memory and implements |
| 5 | +/// the same interface as cached_mapt. |
| 6 | + |
| 7 | +#ifndef CPROVER_UTIL_IN_MEMORY_MAP_H |
| 8 | +#define CPROVER_UTIL_IN_MEMORY_MAP_H |
| 9 | + |
| 10 | +#include "virtual_map.h" |
| 11 | +#include <boost/range/adaptor/map.hpp> |
| 12 | +#include <map> |
| 13 | +#include <memory> |
| 14 | + |
| 15 | +/// A map that holds all entries in memory and implements the same interface as |
| 16 | +/// cached_mapt |
| 17 | +/// \tparam keyt: The type of keys of the map. |
| 18 | +/// \tparam valuet: The type of values of the map. |
| 19 | +template <typename keyt, typename valuet> |
| 20 | +class in_memory_mapt final |
| 21 | +{ |
| 22 | +public: |
| 23 | + // NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL |
| 24 | + typedef keyt key_type; |
| 25 | + // NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL |
| 26 | + typedef valuet mapped_type; |
| 27 | + // NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL |
| 28 | + typedef std::pair<const key_type, std::shared_ptr<mapped_type>> value_type; |
| 29 | + // NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL |
| 30 | + typedef value_type &reference; |
| 31 | + // NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL |
| 32 | + typedef const value_type &const_reference; |
| 33 | + // NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL |
| 34 | + typedef value_type *pointer; |
| 35 | + // NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL |
| 36 | + typedef const value_type *const_pointer; |
| 37 | + typedef std::unordered_map<key_type, std::shared_ptr<mapped_type>> storet; |
| 38 | + // NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL |
| 39 | + typedef typename storet::size_type size_type; |
| 40 | + |
| 41 | +private: |
| 42 | + mutable storet store; |
| 43 | + |
| 44 | +private: |
| 45 | + in_memory_mapt(const in_memory_mapt &) = delete; |
| 46 | + in_memory_mapt &operator=(const in_memory_mapt &) = delete; |
| 47 | + |
| 48 | +public: |
| 49 | + /// Creates a in_memory_mapt. |
| 50 | + in_memory_mapt() |
| 51 | + { |
| 52 | + } |
| 53 | + |
| 54 | + /// Returns the size of the cache. |
| 55 | + /// \returns The size of the cache. |
| 56 | + size_type cache_size() const |
| 57 | + { |
| 58 | + return store.size(); |
| 59 | + } |
| 60 | + |
| 61 | + /// Returns the number of locked elements, equal to the size in this class. |
| 62 | + /// \returns The number of locked elements. |
| 63 | + size_type locked_count() const |
| 64 | + { |
| 65 | + return store.size(); |
| 66 | + } |
| 67 | + |
| 68 | + /// Returns the size of the map. |
| 69 | + /// \returns The size of the map. |
| 70 | + size_type size() const |
| 71 | + { |
| 72 | + return store.size(); |
| 73 | + } |
| 74 | + |
| 75 | + /// Returns all the keys in the map. |
| 76 | + /// \returns The set of all keys in the map. |
| 77 | + typename virtual_mapt<key_type, mapped_type>::keys_ranget keys() const |
| 78 | + { |
| 79 | + return store.keys(); |
| 80 | + } |
| 81 | + |
| 82 | + // NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL |
| 83 | + typedef typename storet::iterator iterator; |
| 84 | + // NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL |
| 85 | + typedef typename storet::const_iterator const_iterator; |
| 86 | + |
| 87 | + iterator begin() |
| 88 | + { |
| 89 | + return store.begin(); |
| 90 | + } |
| 91 | + |
| 92 | + iterator end() |
| 93 | + { |
| 94 | + return store.end(); |
| 95 | + } |
| 96 | + |
| 97 | + const_iterator begin() const |
| 98 | + { |
| 99 | + return store.begin(); |
| 100 | + } |
| 101 | + |
| 102 | + const_iterator end() const |
| 103 | + { |
| 104 | + return store.end(); |
| 105 | + } |
| 106 | + |
| 107 | + /// Checks whether a given key exists in the map. |
| 108 | + /// \param key: The key to search for. |
| 109 | + /// \returns True if the map contains the given key. |
| 110 | + bool contains(const key_type &key) const |
| 111 | + { |
| 112 | + return store.count(key) != 0; |
| 113 | + } |
| 114 | + |
| 115 | + /// Adds a key-value pair to the map. |
| 116 | + /// \param key: The key of the key-value pair to add. |
| 117 | + /// \param value: The value of the key-value pair to add. |
| 118 | + /// \returns |
| 119 | + /// A pair, the first element of which is a shared_pointer to the value |
| 120 | + /// corresponding to key now in the map and the second element of which |
| 121 | + /// is true if and only if the key did not already exist in the map and the |
| 122 | + /// new element has been added. |
| 123 | + /// \remarks |
| 124 | + /// If the value argument is an r-value at the call site it will be moved |
| 125 | + /// into the cache. Otherwise you need a copy constructor on mapped_type. |
| 126 | + std::pair<std::shared_ptr<mapped_type>, bool> insert( |
| 127 | + const key_type &key, mapped_type value) |
| 128 | + { |
| 129 | + auto it = store.find(key); |
| 130 | + if(it != store.end()) |
| 131 | + return { it->second, false }; |
| 132 | + // Create and return shared pointer |
| 133 | + std::shared_ptr<mapped_type> elt_shared_ptr = |
| 134 | + std::make_shared(std::move(value)); |
| 135 | + store.insert({ key, elt_shared_ptr }); |
| 136 | + return { elt_shared_ptr, true }; |
| 137 | + } |
| 138 | + |
| 139 | + /// Gets the value corresponding to a given key, inserting a new key-value |
| 140 | + /// pair if one does not already exist. |
| 141 | + /// \param key: The key to search for. |
| 142 | + /// \returns A shared_pointer to the corresponding to the given key. |
| 143 | + /// \remarks Requires a default constructor on mapped_type. |
| 144 | + std::shared_ptr<mapped_type> operator[](const key_type &key) |
| 145 | + { |
| 146 | + auto it = store.find(key); |
| 147 | + if(it != store.end()) |
| 148 | + return it->second; |
| 149 | + // Create and return shared pointer |
| 150 | + auto elt_shared_ptr = std::make_shared<mapped_type>(); |
| 151 | + store.insert({ key, elt_shared_ptr }); |
| 152 | + return elt_shared_ptr; |
| 153 | + } |
| 154 | + |
| 155 | + /// Gets the value corresponding to a given key. |
| 156 | + /// \param key: The key to search for. |
| 157 | + /// \returns A shared_pointer to the value corresponding to the given key. |
| 158 | + std::shared_ptr<mapped_type> at(const key_type &key) const |
| 159 | + { |
| 160 | + return store.at(key); |
| 161 | + } |
| 162 | + |
| 163 | + /// Removes a key-value pair from the map. |
| 164 | + /// \param key: The key of the key-value pair to remove. |
| 165 | + /// \returns Whether the key-value pair could be deleted. |
| 166 | + /// \remarks: If this function returns false, no changes are made to the map. |
| 167 | + bool erase(const key_type &key) |
| 168 | + { |
| 169 | + store.erase(key); |
| 170 | + return true; |
| 171 | + } |
| 172 | + |
| 173 | + /// Removes all key-value pairs from the map. |
| 174 | + /// \returns Whether the map could be cleared. |
| 175 | + /// \remarks: If this function returns false, no changes are made to the map. |
| 176 | + bool clear() |
| 177 | + { |
| 178 | + store.clear(); |
| 179 | + return true; |
| 180 | + } |
| 181 | + |
| 182 | + /// No effect in this class. |
| 183 | + void flush() |
| 184 | + { |
| 185 | + } |
| 186 | + |
| 187 | + /// Totally removes an element from the map. |
| 188 | + /// \param key: A key whose corresponding value should be unloaded. |
| 189 | + /// \returns True, if the referenced element was unloaded, and false otherwise. |
| 190 | + bool unload(const key_type &key) |
| 191 | + { |
| 192 | + return erase(key); |
| 193 | + } |
| 194 | +}; |
| 195 | + |
| 196 | +#endif |
0 commit comments