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