28 # ifndef TPL_HASH_CACHE_H 29 # define TPL_HASH_CACHE_H 33 # include <tpl_dnode.H> 34 # include <tpl_lhash.H> 56 template <
typename Key,
typename Data,
class Cmp = Aleph::equal_to<Key>>
78 Dlink* link_lru() {
return &dlink_lru; }
80 bool is_in_hash_table;
85 throw std::runtime_error(
"Cache_Entry is already locked");
93 throw std::runtime_error(
"Cache_Entry is not locked");
100 Dlink * link_inside() {
return &dlink_inside; }
107 : data(), locked(
false), is_in_hash_table(
false) { }
130 Chunk_Descriptor chunk_list;
132 void insert_entry_to_lru_list(
Cache_Entry * cache_entry)
135 lru_list.
insert(cache_entry->link_lru());
137 void remove_entry_from_lru_list(
Cache_Entry * cache_entry)
140 cache_entry->link_lru()->
del();
143 void insert_entry_to_locked_list(
Cache_Entry * cache_entry)
146 locked_list.
insert(cache_entry->link_lru());
149 void remove_entry_from_locked_list(
Cache_Entry * cache_entry)
152 cache_entry->link_lru()->
del();
155 void move_to_inside_front(
Cache_Entry * cache_entry)
157 cache_entry->link_inside()->
del();
158 inside_list.
insert(cache_entry->link_inside());
163 cache_entry->link_lru()->
del();
164 lru_list.
insert(cache_entry->link_lru());
169 cache_entry->link_lru()->
del();
170 lru_list.
append(cache_entry->link_lru());
173 void remove_entry_from_hash_table(
Cache_Entry * cache_entry)
175 cache_entry->link_inside()->
del();
176 hash_table.
remove(cache_entry);
177 cache_entry->is_in_hash_table =
false;
184 throw std::underflow_error(
"All entries are locked");
188 Cache_Entry * cache_entry = dlink_lru_to_Cache_Entry(lru_entry_link);
191 if (cache_entry->is_in_hash_table)
192 remove_entry_from_hash_table(cache_entry);
220 size_t __hash_size,
const size_t & __cache_size)
221 : hash_table(__hash_size, hash_fct),
222 num_lru(0), cache_size(__cache_size), num_locked(0)
229 std::unique_ptr<Chunk_Descriptor>
230 chunk_descriptor (
new Chunk_Descriptor (entries_array));
231 chunk_list.
insert(chunk_descriptor.get());
234 for (
int i = 0; i < cache_size; i++)
235 insert_entry_to_lru_list(&entries_array[i]);
237 chunk_descriptor.release();
242 delete [] entries_array;
252 Chunk_Descriptor * current_chunk = chunk_list.
remove_next();
254 delete [] current_chunk->
get_data();
255 delete current_chunk;
279 cache_entry->get_key() = key;
281 inside_list.
insert(cache_entry->link_inside());
282 hash_table.
insert(cache_entry);
283 cache_entry->is_in_hash_table =
true;
298 if (cache_entry !=
nullptr)
301 move_to_inside_front(cache_entry);
320 if (next_entry !=
nullptr)
323 move_to_inside_front(cache_entry);
335 throw std::runtime_error(
"Cache_Entry is already locked");
338 throw std::domain_error(
"Cache_Entry is not in the cache");
340 remove_entry_from_lru_list(cache_entry);
341 insert_entry_to_locked_list(cache_entry);
350 throw std::runtime_error(
"Cache_Entry is not locked");
352 remove_entry_from_locked_list(cache_entry);
353 insert_entry_to_lru_list(cache_entry);
354 cache_entry->unlock();
363 throw std::runtime_error(
"Cache_Entry is already locked");
365 throw std::domain_error(
"Cache_Entry is not in the cache");
367 remove_entry_from_hash_table(cache_entry);
377 throw std::range_error (
"bad plus_size");
379 const size_t new_cache_size = cache_size + plus_size;
386 std::unique_ptr<Chunk_Descriptor>
387 chunk_descriptor (
new Chunk_Descriptor (entries_array));
390 const float curr_hash_ratio = 1.0*cache_size/hash_table.
capacity();
391 const size_t new_hash_capacity = new_cache_size/curr_hash_ratio;
393 hash_table.
resize(new_hash_capacity);
396 for (
int i = 0; i < plus_size; i++)
397 insert_entry_to_lru_list(&entries_array[i]);
399 chunk_list.
insert(chunk_descriptor.release());
400 cache_size = new_cache_size;
405 delete [] entries_array;
412 const size_t &
capacity()
const {
return cache_size; }
415 const size_t &
size()
const {
return hash_table.
size(); }
454 return Cache_Entry::dlink_inside_to_Cache_Entry(dl);
462 # endif // TPL_HASH_CACHE_H Hash_Cache(size_t(*hash_fct)(const Key &), size_t __hash_size, const size_t &__cache_size)
Definition: tpl_hash_cache.H:219
Bucket * insert(Bucket *bucket)
Definition: tpl_lhash.H:216
Cache_Entry * get_curr()
Retorna el Cache_Entry actual.
Definition: tpl_hash_cache.H:451
void expand(const size_t &plus_size)
Definition: tpl_hash_cache.H:374
Cache_Entry * search(const Key &key)
Definition: tpl_hash_cache.H:295
Definition: tpl_hash_cache.H:439
const size_t & get_num_busy_slots() const noexcept
Retorna la cantidad de entradas del arreglo que están ocupadas.
Definition: tpl_lhash.H:369
#define LINKNAME_TO_TYPE(type_name, link_name)
Definition: dlink.H:937
const bool & is_in_table() const
Retorna true si la entrada está dentro de la tabla.
Definition: tpl_hash_cache.H:116
Bucket * remove(Bucket *bucket) noexcept
Definition: tpl_lhash.H:288
const size_t & get_num_locked() const
Definition: tpl_hash_cache.H:420
const size_t & size() const
Retorna en número de datos que están contenidos en el cache.
Definition: tpl_hash_cache.H:415
void unlock_entry(Cache_Entry *cache_entry)
Definition: tpl_hash_cache.H:347
Cache_Entry * Item_Type
El tipo de elemento que retorna get_curr().
Definition: tpl_hash_cache.H:445
Data & get_data()
Retorna una referencia al dato asociado a la clave.
Definition: tpl_hash_cache.H:110
Definition: tpl_hash_cache.H:57
Iterator(Hash_Cache &cache)
Instancia un iterador sobre cache.
Definition: tpl_hash_cache.H:448
T & get_data() noexcept
Return a modifiable reference to the data contained in the node.
Definition: tpl_dnode.H:178
Cache_Entry * insert(const Key &key, const Data &data)
Definition: tpl_hash_cache.H:276
void insert(Dlink *node) noexcept
Definition: dlink.H:205
Dlink *& get_prev() const noexcept
Return the link that is before this
Definition: dlink.H:248
Cache_Entry * search_next(Cache_Entry *cache_entry)
Definition: tpl_hash_cache.H:316
Dlink * get_curr() const
Definition: dlink.H:681
bool is_empty() const noexcept
Return true if this (as header node) is empty.
Definition: dlink.H:192
Dnode< T > * remove_next() noexcept
Remove the next node to this; return its address.
Definition: tpl_dnode.H:62
const size_t & size() const noexcept
Retorna el número de elementos que contiene la tabla.
Definition: tpl_lhash.H:365
Dlink * del() noexcept
Remove this from the list. this must not be a header node.
Definition: dlink.H:389
size_t resize(size_t new_size)
Definition: tpl_lhash.H:301
void lock_entry(Cache_Entry *cache_entry)
Definition: tpl_hash_cache.H:331
Definition: tpl_hash_cache.H:72
const size_t & get_num_busy_slots() const
Definition: tpl_hash_cache.H:425
const bool & is_locked() const
Retorna true si la entrada está trancada.
Definition: tpl_hash_cache.H:113
Bucket * search_next(Bucket *bucket) const noexcept
Definition: tpl_lhash.H:338
void append(Dlink *node) noexcept
Definition: dlink.H:228
Bucket * search(const Key &key) const noexcept
Definition: tpl_lhash.H:261
Definition: tpl_lhash.H:660
const size_t & get_hash_capacity() const
Retorna el tamaño de la tabla hash.
Definition: tpl_hash_cache.H:431
const size_t & capacity() const
Retorna el tamaño de cache.
Definition: tpl_hash_cache.H:412
const size_t & capacity() const noexcept
Retorna el tamaño de la tabla.
Definition: tpl_lhash.H:362
Hash_Cache Set_Type
El tipo de conjunto sobre el cual se itera.
Definition: tpl_hash_cache.H:443