File: fast_cache.hpp

package info (click to toggle)
libsdsl 2.1.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,020 kB
  • sloc: cpp: 42,286; makefile: 1,171; ansic: 318; sh: 201; python: 27
file content (39 lines) | stat: -rw-r--r-- 937 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#ifndef INCLUDED_SDSL_FAST_CACHE
#define INCLUDED_SDSL_FAST_CACHE

#include "int_vector.hpp"

namespace sdsl
{

#define CACHE_SIZE 0x3FFULL

struct fast_cache {
    typedef int_vector<>::size_type size_type;
    size_type m_table[2*(CACHE_SIZE+1)];
    // Constructor
    fast_cache() {
        for (size_type i=0; i < (CACHE_SIZE+1); ++i) {
            m_table[i<<1] = (size_type)-1;
        }
    }
    // Returns true if the request i is cached and
    // x is set to the answer of request i
    bool exists(size_type i, size_type& x) {
        if (m_table[(i&CACHE_SIZE)<<1 ] == i) {
            x = m_table[((i&CACHE_SIZE)<<1) + 1 ];
            return true;
        } else
            return false;
    }
    // Writes the answer for request i to the cache
    void write(size_type i, size_type x) {
        m_table[(i&CACHE_SIZE)<<1 ] = i;
        m_table[((i&CACHE_SIZE)<<1) + 1 ] = x;
    }
};

} // end namespace sdsl

#endif