30 std::weak_ptr<DMatrix> ref;
32 std::shared_ptr<CacheT> value;
34 CacheT
const&
Value()
const {
return *value; }
35 CacheT&
Value() {
return *value; }
37 Item(std::shared_ptr<DMatrix> m, std::shared_ptr<CacheT> v) : ref{m}, value{std::move(v)} {}
40 static constexpr std::size_t DefaultSize() {
return 32; }
43 mutable std::mutex lock_;
48 std::thread::id
const thread_id;
50 bool operator==(
Key const& that)
const {
51 return ptr == that.ptr && thread_id == that.thread_id;
55 std::size_t operator()(
Key const& key)
const noexcept {
56 std::size_t f = std::hash<DMatrix const*>()(key.ptr);
57 std::size_t s = std::hash<std::thread::id>()(key.thread_id);
65 std::unordered_map<Key, Item, Hash> container_;
66 std::queue<Key> queue_;
67 std::size_t max_size_;
69 void CheckConsistent()
const { CHECK_EQ(queue_.size(), container_.size()); }
73 this->CheckConsistent();
74 std::vector<Key> expired;
75 std::queue<Key> remained;
77 while (!queue_.empty()) {
78 auto p_fmat = queue_.front();
79 auto it = container_.find(p_fmat);
80 CHECK(it != container_.cend());
81 if (it->second.ref.expired()) {
82 expired.push_back(it->first);
84 remained.push(it->first);
88 CHECK(queue_.empty());
89 CHECK_EQ(remained.size() + expired.size(), container_.size());
91 for (
auto const& key : expired) {
92 container_.erase(key);
94 while (!remained.empty()) {
95 auto p_fmat = remained.front();
99 this->CheckConsistent();
103 this->CheckConsistent();
105 std::size_t half_size = max_size_ / 2;
106 while (queue_.size() >= half_size && !queue_.empty()) {
107 auto p_fmat = queue_.front();
109 container_.erase(p_fmat);
111 this->CheckConsistent();
118 explicit DMatrixCache(std::size_t cache_size) : max_size_{cache_size} {}
121 CHECK(lock_.try_lock());
123 CHECK(that.lock_.try_lock());
125 std::swap(this->container_, that.container_);
127 std::swap(this->max_size_, that.max_size_);
144 template <
typename... Args>
145 std::shared_ptr<CacheT>
CacheItem(std::shared_ptr<DMatrix> m, Args
const&... args) {
147 std::lock_guard<std::mutex> guard{lock_};
149 this->ClearExpired();
150 if (container_.size() >= max_size_) {
154 CHECK_LT(container_.size(), max_size_);
155 auto key =
Key{m.get(), std::this_thread::get_id()};
156 auto it = container_.find(key);
157 if (it == container_.cend()) {
159 container_.emplace(key,
Item{m, std::make_shared<CacheT>(args...)});
162 return container_.at(key).value;
173 template <
typename... Args>
174 std::shared_ptr<CacheT>
ResetItem(std::shared_ptr<DMatrix> m, Args
const&... args) {
175 std::lock_guard<std::mutex> guard{lock_};
177 auto key =
Key{m.get(), std::this_thread::get_id()};
178 auto it = container_.find(key);
179 CHECK(it != container_.cend());
180 it->second = {m, std::make_shared<CacheT>(args...)};
182 return it->second.value;
189 std::lock_guard<std::mutex> guard{lock_};
191 this->ClearExpired();
196 std::lock_guard<std::mutex> guard{lock_};
197 auto key = Key{m, std::this_thread::get_id()};
198 CHECK(container_.find(key) != container_.cend());
199 CHECK(!container_.at(key).ref.expired());
200 return container_.at(key).value;
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition json.hpp:24418
Element from a sparse vector.
Definition data.h:216