GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — pulsar-client-cpp/lib/MapCache.h at main · nodece/pulsar-client-cpp

Expand file tree

/

MapCache.h

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
104 lines (90 loc) · 3.22 KB
/

MapCache.h

Copy path

File metadata and controls

104 lines (90 loc) · 3.22 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#pragma once
#include <deque>
#include <functional>
#include <unordered_map>
#include <vector>
namespace pulsar {
// A map cache that supports removing the first N oldest entries from the map.
// Value must be moveable and have the default constructor.
template <typename Key, typename Value>
class MapCache {
std::unordered_map<Key, Value> map_;
std::deque<Key> keys_;
public:
using const_iterator = typename decltype(map_)::const_iterator;
using iterator = typename decltype(map_)::iterator;
MapCache() = default;
// Here we don't use =default to be compatible with GCC 4.8
MapCache(MapCache&& rhs) noexcept : map_(std::move(rhs.map_)), keys_(std::move(rhs.keys_)) {}
size_t size() const noexcept { return map_.size(); }
const_iterator find(const Key& key) const { return map_.find(key); }
iterator find(const Key& key) { return map_.find(key); }
const_iterator end() const noexcept { return map_.end(); }
iterator end() noexcept { return map_.end(); }
iterator putIfAbsent(const Key& key, Value&& value) {
auto it = map_.find(key);
if (it == map_.end()) {
keys_.push_back(key);
return map_.emplace(key, std::move(value)).first;
} else {
return end();
}
}
void removeOldestValues(size_t numToRemove,
const std::function<void(const Key&, const Value&)>& callback) {
for (size_t i = 0; !keys_.empty() && i < numToRemove; i++) {
const auto key = keys_.front();
auto it = map_.find(key);
if (it != map_.end()) {
if (callback) {
callback(it->first, it->second);
}
map_.erase(it);
}
keys_.pop_front();
}
}
void remove(const Key& key) {
auto it = map_.find(key);
if (it != map_.end()) {
removeKeyFromKeys(key);
map_.erase(it);
}
}
// Following methods are only used for tests
std::vector<Key> getKeys() const {
std::vector<Key> keys;
for (auto key : keys_) {
keys.emplace_back(key);
}
return keys;
}
private:
void removeKeyFromKeys(const Key& key) {
for (auto it = keys_.begin(); it != keys_.end(); ++it) {
if (*it == key) {
keys_.erase(it);
break;
}
}
}
};
} // namespace pulsar
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.