mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-25 20:33:08 +00:00
style fixups
This commit is contained in:
parent
e52a04e341
commit
973097f025
4 changed files with 18 additions and 26 deletions
|
@ -27,10 +27,7 @@ file_access_attempt_t access_file(const wcstring &path, int mode);
|
|||
|
||||
struct autoload_function_t {
|
||||
explicit autoload_function_t(bool placeholder)
|
||||
: access(),
|
||||
is_loaded(false),
|
||||
is_placeholder(placeholder),
|
||||
is_internalized(false) {}
|
||||
: access(), is_loaded(false), is_placeholder(placeholder), is_internalized(false) {}
|
||||
|
||||
/// The last access attempt recorded
|
||||
file_access_attempt_t access;
|
||||
|
@ -82,7 +79,6 @@ class autoload_t : public lru_cache_t<autoload_t, autoload_function_t> {
|
|||
virtual void command_removed(const wcstring &cmd) { UNUSED(cmd); }
|
||||
|
||||
public:
|
||||
|
||||
// CRTP override
|
||||
void entry_was_evicted(wcstring key, autoload_function_t node);
|
||||
|
||||
|
|
|
@ -1128,9 +1128,7 @@ class test_lru_t : public lru_cache_t<test_lru_t, int> {
|
|||
|
||||
std::vector<std::pair<wcstring, int>> evicted;
|
||||
|
||||
void entry_was_evicted(wcstring key, int val) {
|
||||
evicted.push_back({key, val});
|
||||
}
|
||||
void entry_was_evicted(wcstring key, int val) { evicted.push_back({key, val}); }
|
||||
};
|
||||
|
||||
static void test_lru(void) {
|
||||
|
@ -1144,7 +1142,7 @@ static void test_lru(void) {
|
|||
if (i < 4) expected_evicted.push_back({to_string(i), i});
|
||||
// Adding the node the first time should work, and subsequent times should fail.
|
||||
do_test(cache.insert(to_string(i), i));
|
||||
do_test(!cache.insert(to_string(i), i+1));
|
||||
do_test(!cache.insert(to_string(i), i + 1));
|
||||
}
|
||||
do_test(cache.evicted == expected_evicted);
|
||||
cache.evict_all_nodes();
|
||||
|
|
|
@ -155,8 +155,8 @@ class history_lru_item_t {
|
|||
|
||||
class history_lru_cache_t : public lru_cache_t<history_lru_cache_t, history_lru_item_t> {
|
||||
typedef lru_cache_t<history_lru_cache_t, history_lru_item_t> super;
|
||||
public:
|
||||
|
||||
public:
|
||||
using super::super;
|
||||
|
||||
/// Function to add a history item.
|
||||
|
|
30
src/lru.h
30
src/lru.h
|
@ -8,19 +8,19 @@
|
|||
|
||||
#include "common.h"
|
||||
|
||||
// Least-recently-used cache class
|
||||
// Least-recently-used cache class.
|
||||
//
|
||||
// This a map from wcstring to CONTENTS, that will evict entries when the count exceeds the maximum.
|
||||
// It uses CRTP to inform clients when entries are evicted. This uses the classic LRU cache structure:
|
||||
// a dictionary mapping keys to nodes, where the nodes also form a linked list. Our linked list is
|
||||
// circular and has a sentinel node (the "mouth" - picture a snake swallowing its tail). This simplifies
|
||||
// the logic: no pointer is ever NULL! It also works well with C++'s iterator since the sentinel node
|
||||
// is a natural value for end(). Our nodes also have the unusual property of having a "back pointer":
|
||||
// they store an iterator to the entry in the map containing the node. This allows us, given a node, to
|
||||
// immediately locate the node and its key in the dictionary. This allows us to avoid duplicating the key
|
||||
// in the node.
|
||||
// It uses CRTP to inform clients when entries are evicted. This uses the classic LRU cache
|
||||
// structure: a dictionary mapping keys to nodes, where the nodes also form a linked list. Our
|
||||
// linked list is circular and has a sentinel node (the "mouth" - picture a snake swallowing its
|
||||
// tail). This simplifies the logic: no pointer is ever NULL! It also works well with C++'s iterator
|
||||
// since the sentinel node is a natural value for end(). Our nodes also have the unusual property of
|
||||
// having a "back pointer": they store an iterator to the entry in the map containing the node. This
|
||||
// allows us, given a node, to immediately locate the node and its key in the dictionary. This
|
||||
// allows us to avoid duplicating the key in the node.
|
||||
template <class DERIVED, class CONTENTS>
|
||||
class lru_cache_t {
|
||||
|
||||
struct lru_node_t;
|
||||
typedef typename std::map<wcstring, lru_node_t>::iterator node_iter_t;
|
||||
|
||||
|
@ -44,12 +44,9 @@ class lru_cache_t {
|
|||
// The value from the client
|
||||
CONTENTS value;
|
||||
|
||||
explicit lru_node_t(CONTENTS v) :
|
||||
value(std::move(v))
|
||||
{}
|
||||
explicit lru_node_t(CONTENTS v) : value(std::move(v)) {}
|
||||
};
|
||||
|
||||
|
||||
// Max node count. This may be (transiently) exceeded by add_node_without_eviction, which is
|
||||
// used from background threads.
|
||||
const size_t max_node_count;
|
||||
|
@ -146,7 +143,7 @@ class lru_cache_t {
|
|||
// Adds a node under the given key. Returns true if the node was added, false if the node was
|
||||
// not because a node with that key is already in the set.
|
||||
bool insert(wcstring key, CONTENTS value) {
|
||||
if (! this->insert_no_eviction(std::move(key), std::move(value))) {
|
||||
if (!this->insert_no_eviction(std::move(key), std::move(value))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -161,7 +158,7 @@ class lru_cache_t {
|
|||
bool insert_no_eviction(wcstring key, CONTENTS value) {
|
||||
// Try inserting; return false if it was already in the set.
|
||||
auto iter_inserted = this->node_map.emplace(std::move(key), lru_node_t(std::move(value)));
|
||||
if (! iter_inserted.second) {
|
||||
if (!iter_inserted.second) {
|
||||
// already present
|
||||
return false;
|
||||
}
|
||||
|
@ -190,6 +187,7 @@ class lru_cache_t {
|
|||
// Iterator for walking nodes, from least recently used to most.
|
||||
class iterator {
|
||||
lru_link_t *node;
|
||||
|
||||
public:
|
||||
typedef std::pair<const wcstring &, const CONTENTS &> value_type;
|
||||
|
||||
|
|
Loading…
Reference in a new issue