TrixelCache

Search for usage in LXR

TrixelCache< content > Class Template Reference

#include <trixelcache.h>

Inheritance diagram for TrixelCache< content >:

Classes

class  element
 

Public Member Functions

 TrixelCache (const size_t data_size, const size_t cache_size)
 
void clear () noexcept
 
size_t current_usage ()
 
bool noop () const
 
elementoperator[] (const size_t index) noexcept
 
std::list< size_t > primed_indices ()
 
void prune (size_t keep=0) noexcept
 
void set_size (const size_t size)
 
size_t size () const
 

Detailed Description

template<typename content>
class TrixelCache< content >

A simple integer indexed elastically cached wrapper around std::vector to hold container types content which are cheap to construct empty and provide a default constructor, as well as [], size and swap members (think std::vector and std::list).

The caching mechanism will be short-circuited if the cache size equals the data size.

The container is resized to a given data_size by default initializing its elements. The elements are stored as TrixelCache::element objects and retrieved by index and LRU cached on the fly. To test if a given element is cached (or not empty) the TrixelCache::element::is_set() can be queried. Setting an element works by just assigning it with the new data. The data from an element can be retrieved through the TrixelCache::element::data() member. Modifying the data through that reference is allowed.

Setting an element is done by assigning it the new data.

When it is convenient TrixelCache::prune() may be called, which clears the least recently used elements (by default initializing them) until the number of elements does not exceed the cache size. This is expensive relative to setting an elment which has almost no cost.

Template Parameters
contentThe content type to use. Most likely a QList, std::vector or std::list.
Todo
Use std::optional when C++17 is adopted in kstars.

Example

auto& element = chache[0];
if(!elmenent.is_set())
elment = std::vector<int> {1, 2, 3, 4};
std::cout << cache[0].data(); // {1, 2, 3, 4}
A container to hold cache elements.
Definition trixelcache.h:70
A simple integer indexed elastically cached wrapper around std::vector to hold container types conten...
Definition trixelcache.h:60

Definition at line 59 of file trixelcache.h.

Constructor & Destructor Documentation

◆ TrixelCache()

template<typename content >
TrixelCache< content >::TrixelCache ( const size_t data_size,
const size_t cache_size )
inline

Constructs a cache with data_size default constructed elements with an elastic ceiling capacity of cache_size.

Definition at line 108 of file trixelcache.h.

Member Function Documentation

◆ clear()

template<typename content >
void TrixelCache< content >::clear ( )
inlinenoexcept

Clear the cache without changing it's size.

Definition at line 187 of file trixelcache.h.

◆ current_usage()

template<typename content >
size_t TrixelCache< content >::current_usage ( )
inline
Returns
the number of set elements in the cache, slow

Definition at line 170 of file trixelcache.h.

◆ noop()

template<typename content >
bool TrixelCache< content >::noop ( ) const
inline
Returns
wether the cache is just a wrapped vector

Definition at line 184 of file trixelcache.h.

◆ operator[]()

template<typename content >
element & TrixelCache< content >::operator[] ( const size_t index)
inlinenoexcept

Retrieve an element at index.

Definition at line 118 of file trixelcache.h.

◆ primed_indices()

template<typename content >
std::list< size_t > TrixelCache< content >::primed_indices ( )
inline
Returns
a list of currently primed indices, slow

Definition at line 177 of file trixelcache.h.

◆ prune()

template<typename content >
void TrixelCache< content >::prune ( size_t keep = 0)
inlinenoexcept

Remove excess elements from the cache The capacity can be temporarily readjusted to keep.

keep must be greater than the cache size to be of effect.

Definition at line 131 of file trixelcache.h.

◆ set_size()

template<typename content >
void TrixelCache< content >::set_size ( const size_t size)
inline

Reset the cache size to size.

This does clear the cache.

Definition at line 155 of file trixelcache.h.

◆ size()

template<typename content >
size_t TrixelCache< content >::size ( ) const
inline
Returns
the size of the cache

Definition at line 167 of file trixelcache.h.


The documentation for this class was generated from the following file:
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 13 2024 11:47:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.