diff options
author | Albert Astals Cid <aacid@kde.org> | 2009-06-07 13:36:39 +0200 |
---|---|---|
committer | Albert Astals Cid <aacid@kde.org> | 2009-06-07 13:36:39 +0200 |
commit | 588bfe3c14f42be492066c2a98e30482475a6926 (patch) | |
tree | fd5407dfad40392b6ae173aced8e695818e84b28 | |
parent | d09478fcc44b5c594f1803fc24654af5e10fa129 (diff) |
Add a code to a generic cache based on Koji's code for GfxState cache
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | poppler/Makefile.am | 1 | ||||
-rw-r--r-- | poppler/PopplerCache.cc | 82 | ||||
-rw-r--r-- | poppler/PopplerCache.h | 47 |
4 files changed, 131 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 0fbc49ae..194d46b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/poppler/Makefile.am b/poppler/Makefile.am index 4f0f4102..f43cf4bf 100644 --- a/poppler/Makefile.am +++ b/poppler/Makefile.am @@ -255,6 +255,7 @@ libpoppler_la_SOURCES = \ Parser.cc \ PDFDoc.cc \ PDFDocEncoding.cc \ + PopplerCache.cc \ ProfileData.cc \ PreScanOutputDev.cc \ PSTokenizer.cc \ diff --git a/poppler/PopplerCache.cc b/poppler/PopplerCache.cc new file mode 100644 index 00000000..10c5f99d --- /dev/null +++ b/poppler/PopplerCache.cc @@ -0,0 +1,82 @@ +//======================================================================== +// +// 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; +} diff --git a/poppler/PopplerCache.h b/poppler/PopplerCache.h new file mode 100644 index 00000000..7d72d760 --- /dev/null +++ b/poppler/PopplerCache.h @@ -0,0 +1,47 @@ +//======================================================================== +// +// 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 |