Move highlighter cache logic to BufferSideCache template

This commit is contained in:
Maxime Coste 2014-01-09 22:51:23 +00:00
parent 85cc740de0
commit d96b5aa04d

View File

@ -146,12 +146,27 @@ void apply_highlighter(const Context& context,
typedef std::unordered_map<size_t, const ColorPair*> ColorSpec; typedef std::unordered_map<size_t, const ColorPair*> ColorSpec;
template<typename T>
struct BufferSideCache
{
BufferSideCache() : m_id{ValueId::get_free_id()} {}
T& get(const Buffer& buffer)
{
Value& cache_val = buffer.values()[m_id];
if (not cache_val)
cache_val = Value(T{});
return cache_val.as<T>();
}
private:
ValueId m_id;
};
class RegexColorizer class RegexColorizer
{ {
public: public:
RegexColorizer(Regex regex, ColorSpec colors) RegexColorizer(Regex regex, ColorSpec colors)
: m_regex{std::move(regex)}, m_colors{std::move(colors)}, : m_regex{std::move(regex)}, m_colors{std::move(colors)}
m_cache_id{ValueId::get_free_id()}
{ {
} }
@ -179,17 +194,14 @@ private:
size_t m_timestamp = 0; size_t m_timestamp = 0;
std::vector<std::vector<std::pair<BufferCoord, BufferCoord>>> m_matches; std::vector<std::vector<std::pair<BufferCoord, BufferCoord>>> m_matches;
}; };
BufferSideCache<MatchesCache> m_cache;
Regex m_regex; Regex m_regex;
ColorSpec m_colors; ColorSpec m_colors;
ValueId m_cache_id;
MatchesCache& update_cache_ifn(const Buffer& buffer, const BufferRange& range) MatchesCache& update_cache_ifn(const Buffer& buffer, const BufferRange& range)
{ {
Value& cache_val = buffer.values()[m_cache_id]; MatchesCache& cache = m_cache.get(buffer);
if (not cache_val)
cache_val = Value(MatchesCache{});
MatchesCache& cache = cache_val.as<MatchesCache>();
if (buffer.timestamp() == cache.m_timestamp and if (buffer.timestamp() == cache.m_timestamp and
range.first >= cache.m_range.first and range.first >= cache.m_range.first and
@ -507,8 +519,7 @@ public:
RegionHighlighter(Regex begin, Regex end, HighlightFunc func) RegionHighlighter(Regex begin, Regex end, HighlightFunc func)
: m_begin(std::move(begin)), : m_begin(std::move(begin)),
m_end(std::move(end)), m_end(std::move(end)),
m_func(std::move(func)), m_func(std::move(func))
m_cache_id{ValueId::get_free_id()}
{} {}
void operator()(const Context& context, DisplayBuffer& display_buffer) void operator()(const Context& context, DisplayBuffer& display_buffer)
@ -521,20 +532,17 @@ private:
Regex m_begin; Regex m_begin;
Regex m_end; Regex m_end;
HighlightFunc m_func; HighlightFunc m_func;
ValueId m_cache_id;
struct RegionCache struct RegionCache
{ {
size_t timestamp = -1; size_t timestamp = -1;
std::vector<std::pair<BufferCoord, BufferCoord>> regions; std::vector<std::pair<BufferCoord, BufferCoord>> regions;
}; };
BufferSideCache<RegionCache> m_cache;
RegionCache& update_cache_ifn(const Buffer& buffer) RegionCache& update_cache_ifn(const Buffer& buffer)
{ {
Value& cache_val = buffer.values()[m_cache_id]; RegionCache& cache = m_cache.get(buffer);
if (not cache_val)
cache_val = Value(RegionCache{});
RegionCache& cache = cache_val.as<RegionCache>();
if (cache.timestamp == buffer.timestamp()) if (cache.timestamp == buffer.timestamp())
return cache; return cache;