diff options
author | Łukasz Niemier <lukasz@niemier.pl> | 2012-11-18 11:23:22 +0100 |
---|---|---|
committer | Łukasz Niemier <lukasz@niemier.pl> | 2012-11-18 11:23:22 +0100 |
commit | 47df1ae40adecd0a02fc7dd06ab0745cb18c3fe0 (patch) | |
tree | 13bf3e8fdcae60fdfb5fa5e26c95818dc7a49790 /lru.h | |
parent | b79854ad1aa814d9d35d76a1929b4726fa4bffa5 (diff) |
Remove trailing whitespaces and change tabs to spaces
Diffstat (limited to 'lru.h')
-rw-r--r-- | lru.h | 72 |
1 files changed, 36 insertions, 36 deletions
@@ -20,17 +20,17 @@ struct dereference_less_t { class lru_node_t { template<class T> friend class lru_cache_t; - + /** Our linked list pointer */ lru_node_t *prev, *next; - + public: /** The key used to look up in the cache */ const wcstring key; - + /** Constructor */ lru_node_t(const wcstring &pkey) : prev(NULL), next(NULL), key(pkey) { } - + /** operator< for std::set */ bool operator<(const lru_node_t &other) const { return key < other.key; } }; @@ -41,29 +41,29 @@ class lru_cache_t { /** Max node count */ const size_t max_node_count; - + /** Count of nodes */ size_t node_count; - + /** The set of nodes */ typedef std::set<lru_node_t *, dereference_less_t> node_set_t; node_set_t node_set; - + void promote_node(node_type_t *node) { /* We should never promote the mouth */ assert(node != &mouth); - + /* First unhook us */ node->prev->next = node->next; node->next->prev = node->prev; - + /* Put us after the mouth */ node->next = mouth.next; node->next->prev = node; node->prev = &mouth; mouth.next = node; } - + void evict_node(node_type_t *condemned_node) { /* We should never evict the mouth */ assert(condemned_node != NULL && condemned_node != &mouth); @@ -79,42 +79,42 @@ class lru_cache_t { /* Tell ourselves */ this->node_was_evicted(condemned_node); } - + void evict_last_node(void) { /* Simple */ evict_node((node_type_t *)mouth.prev); } - + protected: - + /** Head of the linked list */ lru_node_t mouth; /** Overridable callback for when a node is evicted */ virtual void node_was_evicted(node_type_t *node) { } - + public: - + /** Constructor */ lru_cache_t(size_t max_size = 1024 ) : max_node_count(max_size), node_count(0), mouth(wcstring()) { /* Hook up the mouth to itself: a one node circularly linked list! */ mouth.prev = mouth.next = &mouth; } - + /** Note that we do not evict nodes in our destructor (even though they typically need to be deleted by their creator). */ virtual ~lru_cache_t() { } - + /** Returns the node for a given key, or NULL */ node_type_t *get_node(const wcstring &key) { node_type_t *result = NULL; - + /* Construct a fake node as our key */ lru_node_t node_key(key); - + /* Look for it in the set */ node_set_t::iterator iter = node_set.find(&node_key); - + /* If we found a node, promote and return it */ if (iter != node_set.end()) { result = static_cast<node_type_t*>(*iter); @@ -122,73 +122,73 @@ class lru_cache_t { } return result; } - + /** Evicts the node for a given key, returning true if a node was evicted. */ bool evict_node(const wcstring &key) { /* Construct a fake node as our key */ lru_node_t node_key(key); - + /* Look for it in the set */ node_set_t::iterator iter = node_set.find(&node_key); if (iter == node_set.end()) return false; - + /* Evict the given node */ evict_node(static_cast<node_type_t*>(*iter)); return true; } - + /** 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 add_node(node_type_t *node) { /* Add our node without eviction */ if (! this->add_node_without_eviction(node)) return false; - + /* Evict */ while (node_count > max_node_count) evict_last_node(); - + /* Success */ return true; } - + /** Adds a node under the given key without triggering eviction. 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 add_node_without_eviction(node_type_t *node) { assert(node != NULL && node != &mouth); - + /* Try inserting; return false if it was already in the set */ if (! node_set.insert(node).second) return false; - + /* Add the node after the mouth */ node->next = mouth.next; node->next->prev = node; node->prev = &mouth; mouth.next = node; - + /* Update the count */ node_count++; - + /* Evict */ while (node_count > max_node_count) evict_last_node(); - + /* Success */ return true; } - + /** Counts nodes */ size_t size(void) { return node_count; } - + /** Evicts all nodes */ void evict_all_nodes(void) { while (node_count > 0) { evict_last_node(); } } - + /** Iterator for walking nodes, from least recently used to most */ class iterator { lru_node_t *node; @@ -200,7 +200,7 @@ class lru_cache_t { bool operator!=(const iterator &other) { return !(*this == other); } node_type_t *operator*() { return static_cast<node_type_t *>(node); } }; - + iterator begin() { return iterator(mouth.prev); } iterator end() { return iterator(&mouth); } }; |