Commit 588bfe3c authored by Albert Astals Cid's avatar Albert Astals Cid

Add a code to a generic cache based on Koji's code for GfxState cache

parent d09478fc
......@@ -182,6 +182,7 @@ set(poppler_SRCS
poppler/Parser.cc
poppler/PDFDoc.cc
poppler/PDFDocEncoding.cc
poppler/PopplerCache.cc
poppler/ProfileData.cc
poppler/PreScanOutputDev.cc
poppler/PSTokenizer.cc
......
......@@ -255,6 +255,7 @@ libpoppler_la_SOURCES = \
Parser.cc \
PDFDoc.cc \
PDFDocEncoding.cc \
PopplerCache.cc \
ProfileData.cc \
PreScanOutputDev.cc \
PSTokenizer.cc \
......
//========================================================================
//
// PopplerCache.h
//
// This file is licensed under the GPLv2 or later
//
// Copyright (C) 2009 Koji Otani <sho@bbr.jp>
// Copyright (C) 2009 Albert Astals Cid <aacid@kde.org>
//
//========================================================================
#include "PopplerCache.h"
PopplerCacheKey::~PopplerCacheKey()
{
}
PopplerCacheItem::~PopplerCacheItem()
{
}
PopplerCache::PopplerCache(int cacheSizeA)
{
cacheSize = cacheSizeA;
keys = new PopplerCacheKey*[cacheSize];
items = new PopplerCacheItem*[cacheSize];
lastValidCacheIndex = -1;
}
PopplerCache::~PopplerCache()
{
for (int i = 0; i <= lastValidCacheIndex; ++i) {
delete keys[i];
delete items[i];
}
delete[] keys;
delete[] items;
}
PopplerCacheItem *PopplerCache::lookup(const PopplerCacheKey &key)
{
if (lastValidCacheIndex < 0)
return 0;
if (*keys[0] == key) {
return items[0];
}
for (int i = 1; i <= lastValidCacheIndex; i++) {
if (*keys[i] == key) {
PopplerCacheKey *keyHit = keys[i];
PopplerCacheItem *itemHit = items[i];
for (int j = i; j > 0; j--) {
keys[j] = keys[j - 1];
items[j] = items[j - 1];
}
keys[0] = keyHit;
items[0] = itemHit;
return itemHit;
}
}
return 0;
}
void PopplerCache::put(PopplerCacheKey *key, PopplerCacheItem *item)
{
int movingStartIndex = lastValidCacheIndex + 1;
if (lastValidCacheIndex == cacheSize - 1) {
delete keys[lastValidCacheIndex];
delete items[lastValidCacheIndex];
movingStartIndex = cacheSize - 1;
} else {
lastValidCacheIndex++;
}
for (int i = movingStartIndex; i > 0; i--) {
keys[i] = keys[i - 1];
items[i] = items[i - 1];
}
keys[0] = key;
items[0] = item;
}
//========================================================================
//
// PopplerCache.h
//
// This file is licensed under the GPLv2 or later
//
// Copyright (C) 2009 Koji Otani <sho@bbr.jp>
// Copyright (C) 2009 Albert Astals Cid <aacid@kde.org>
//
//========================================================================
#ifndef POPPLER_CACHE_H
#define POPPLER_CACHE_H
class PopplerCacheItem
{
public:
virtual ~PopplerCacheItem();
};
class PopplerCacheKey
{
public:
virtual ~PopplerCacheKey();
virtual bool operator==(const PopplerCacheKey &key) const = 0;
};
class PopplerCache
{
public:
PopplerCache(int cacheSizeA);
~PopplerCache();
/* The item returned is owned by the cache */
PopplerCacheItem *lookup(const PopplerCacheKey &key);
/* The key and item pointers ownership is taken by the cache */
void put(PopplerCacheKey *key, PopplerCacheItem *item);
private:
PopplerCacheKey **keys;
PopplerCacheItem **items;
int lastValidCacheIndex;
int cacheSize;
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment