souffle  2.0.2-371-g6315b36
Public Member Functions | Private Attributes
souffle::LRUCache< T, size > Class Template Reference

An Least-Recently-Used cache for arbitrary element types. More...

#include <CacheUtil.h>

Collaboration diagram for souffle::LRUCache< T, size >:
Collaboration graph

Public Member Functions

void access (const T &val)
 
template<typename Op >
bool any (const Op &op) const
 
void clear (const T &val=T())
 
template<typename Op >
bool forEachInOrder (const Op &op) const
 Iterates over the elements within this cache in LRU order. More...
 
 LRUCache (const T &val=T())
 

Private Attributes

std::array< T, size > entries
 
std::size_t first {0}
 
std::size_t last {size - 1}
 
std::array< std::size_t, size > post
 
std::array< std::size_t, size > priv
 

Detailed Description

template<typename T, unsigned size = 1>
class souffle::LRUCache< T, size >

An Least-Recently-Used cache for arbitrary element types.

Elements can be signaled to be accessed and iterated through in their LRU order.

Definition at line 39 of file CacheUtil.h.

Constructor & Destructor Documentation

◆ LRUCache()

template<typename T , unsigned size = 1>
souffle::LRUCache< T, size >::LRUCache ( const T &  val = T())
inline

Definition at line 58 of file CacheUtil.h.

58  : entries) {
59  cur = val;
60  }
61  }
62 
63  // registers an access to the given element
64  void access(const T& val) {
65  // test whether it is contained
66  for (std::size_t i = 0; i < size; i++) {

Member Function Documentation

◆ access()

template<typename T , unsigned size = 1>
void souffle::LRUCache< T, size >::access ( const T &  val)
inline

Definition at line 76 of file CacheUtil.h.

79  {
80  auto tmp = last;
81  last = priv[last];
82  first = tmp;
83  return;
84  }
85 
86  // otherwise we need to update the linked list
87 
88  // remove from current position
89  post[priv[i]] = post[i];
90  priv[post[i]] = priv[i];
91 
92  // insert in first position
93  post[i] = first;
94  priv[i] = last;
95  priv[first] = i;
96  post[last] = i;
97 
98  // update first pointer
99  first = i;
100  return;
101  }
102  // not present => drop last, make it first
103  entries[last] = val;
104  auto tmp = last;
105  last = priv[last];
106  first = tmp;
107  }
108 
109  /**
110  * Iterates over the elements within this cache in LRU order.
111  * The operator is applied on each element. If the operation
112  * returns false, iteration is continued. If the operator return
113  * true, iteration is stopped -- similar to the any operator.
114  *
115  * @param op the operator to be applied on every element
116  * @return true if op returned true for any entry, false otherwise
117  */
118  template <typename Op>
119  bool forEachInOrder(const Op& op) const {

◆ any()

template<typename T , unsigned size = 1>
template<typename Op >
bool souffle::LRUCache< T, size >::any ( const Op &  op) const
inline

Definition at line 142 of file CacheUtil.h.

151  {

◆ clear()

template<typename T , unsigned size = 1>
void souffle::LRUCache< T, size >::clear ( const T &  val = T())
inline

Definition at line 69 of file CacheUtil.h.

74  {

◆ forEachInOrder()

template<typename T , unsigned size = 1>
template<typename Op >
bool souffle::LRUCache< T, size >::forEachInOrder ( const Op &  op) const
inline

Iterates over the elements within this cache in LRU order.

The operator is applied on each element. If the operation returns false, iteration is continued. If the operator return true, iteration is stopped – similar to the any operator.

Parameters
opthe operator to be applied on every element
Returns
true if op returned true for any entry, false otherwise

Definition at line 131 of file CacheUtil.h.

136  {
137  bool first = true;
138  cache.forEachInOrder([&](const T& val) {

Referenced by souffle::LRUCache< T, 1 >::forEachInOrder(), and souffle::LRUCache< T, 0 >::forEachInOrder().

Field Documentation

◆ entries

template<typename T , unsigned size = 1>
std::array<T, size> souffle::LRUCache< T, size >::entries
private

Definition at line 47 of file CacheUtil.h.

◆ first

template<typename T , unsigned size = 1>
std::size_t souffle::LRUCache< T, size >::first {0}
private

Definition at line 53 of file CacheUtil.h.

◆ last

template<typename T , unsigned size = 1>
std::size_t souffle::LRUCache< T, size >::last {size - 1}
private

Definition at line 54 of file CacheUtil.h.

◆ post

template<typename T , unsigned size = 1>
std::array<std::size_t, size> souffle::LRUCache< T, size >::post
private

Definition at line 51 of file CacheUtil.h.

◆ priv

template<typename T , unsigned size = 1>
std::array<std::size_t, size> souffle::LRUCache< T, size >::priv
private

Definition at line 50 of file CacheUtil.h.


The documentation for this class was generated from the following file:
TCB_SPAN_NAMESPACE_NAME::detail::size
constexpr auto size(const C &c) -> decltype(c.size())
Definition: span.h:198
souffle::LRUCache::post
std::array< std::size_t, size > post
Definition: CacheUtil.h:51
souffle::LRUCache::first
std::size_t first
Definition: CacheUtil.h:53
souffle::LRUCache::last
std::size_t last
Definition: CacheUtil.h:54
souffle::LRUCache::priv
std::array< std::size_t, size > priv
Definition: CacheUtil.h:50
i
size_t i
Definition: json11.h:663
souffle::LRUCache::entries
std::array< T, size > entries
Definition: CacheUtil.h:47
souffle::LRUCache::access
void access(const T &val)
Definition: CacheUtil.h:76
souffle::LRUCache::forEachInOrder
bool forEachInOrder(const Op &op) const
Iterates over the elements within this cache in LRU order.
Definition: CacheUtil.h:131