source: trunk/poppler/mypoppler/poppler/PopplerCache.cc @ 461

Last change on this file since 461 was 461, checked in by Silvan Scherrer, 11 years ago

poppler update to 0.14.2

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1//========================================================================
2//
3// PopplerCache.h
4//
5// This file is licensed under the GPLv2 or later
6//
7// Copyright (C) 2009 Koji Otani <sho@bbr.jp>
8// Copyright (C) 2009, 2010 Albert Astals Cid <aacid@kde.org>
9// Copyright (C) 2010 Carlos Garcia Campos <carlosgc@gnome.org>
10//
11//========================================================================
12
13#include "PopplerCache.h"
14
15#include "XRef.h"
16
17PopplerCacheKey::~PopplerCacheKey()
18{
19}
20
21PopplerCacheItem::~PopplerCacheItem()
22{
23}
24
25PopplerCache::PopplerCache(int cacheSizeA)
26{
27  cacheSize = cacheSizeA;
28  keys = new PopplerCacheKey*[cacheSize];
29  items = new PopplerCacheItem*[cacheSize];
30  lastValidCacheIndex = -1;
31}
32
33PopplerCache::~PopplerCache()
34{
35  for (int i = 0; i <= lastValidCacheIndex; ++i) {
36    delete keys[i];
37    delete items[i];
38  }
39  delete[] keys;
40  delete[] items;
41}
42
43PopplerCacheItem *PopplerCache::lookup(const PopplerCacheKey &key)
44{
45  if (lastValidCacheIndex < 0)
46    return 0;
47
48  if (*keys[0] == key) {
49    return items[0];
50  }
51  for (int i = 1; i <= lastValidCacheIndex; i++) {
52    if (*keys[i] == key) {
53      PopplerCacheKey *keyHit = keys[i];
54      PopplerCacheItem *itemHit = items[i];
55
56      for (int j = i; j > 0; j--) {
57        keys[j] = keys[j - 1];
58        items[j] = items[j - 1];
59      }
60     
61      keys[0] = keyHit;
62      items[0] = itemHit;
63      return itemHit;
64    }
65  }
66  return 0;
67}
68
69void PopplerCache::put(PopplerCacheKey *key, PopplerCacheItem *item)
70{
71  int movingStartIndex = lastValidCacheIndex + 1;
72  if (lastValidCacheIndex == cacheSize - 1) {
73    delete keys[lastValidCacheIndex];
74    delete items[lastValidCacheIndex];
75    movingStartIndex = cacheSize - 1;
76  } else {
77    lastValidCacheIndex++;
78  }
79  for (int i = movingStartIndex; i > 0; i--) {
80    keys[i] = keys[i - 1];
81    items[i] = items[i - 1];
82  }
83  keys[0] = key;
84  items[0] = item;
85}
86
87int PopplerCache::size()
88{
89  return cacheSize;
90}
91
92int PopplerCache::numberOfItems()
93{
94  return lastValidCacheIndex + 1;
95}
96   
97PopplerCacheItem *PopplerCache::item(int index)
98{
99  return items[index];
100}
101   
102PopplerCacheKey *PopplerCache::key(int index)
103{
104  return keys[index];
105}
106
107class ObjectKey : public PopplerCacheKey {
108  public:
109    ObjectKey(int numA, int genA) : num(numA), gen(genA)
110    {
111    }
112
113    bool operator==(const PopplerCacheKey &key) const
114    {
115      const ObjectKey *k = static_cast<const ObjectKey*>(&key);
116      return k->num == num && k->gen == gen;
117    }
118
119    int num, gen;
120};
121
122class ObjectItem : public PopplerCacheItem {
123  public:
124    ObjectItem(Object *obj)
125    {
126      obj->copy(&item);
127    }
128
129    ~ObjectItem()
130    {
131      item.free();
132    }
133
134    Object item;
135};
136
137PopplerObjectCache::PopplerObjectCache(int cacheSize, XRef *xrefA) {
138  cache = new PopplerCache (cacheSize);
139  xref = xrefA;
140}
141
142PopplerObjectCache::~PopplerObjectCache() {
143  delete cache;
144}
145
146Object *PopplerObjectCache::put(const Ref &ref) {
147  Object obj;
148  xref->fetch(ref.num, ref.gen, &obj);
149
150  ObjectKey *key = new ObjectKey(ref.num, ref.gen);
151  ObjectItem *item = new ObjectItem(&obj);
152  cache->put(key, item);
153  obj.free();
154
155  return &item->item;
156}
157
158Object *PopplerObjectCache::lookup(const Ref &ref, Object *obj) {
159  ObjectKey key(ref.num, ref.gen);
160  ObjectItem *item = static_cast<ObjectItem *>(cache->lookup(key));
161
162  return item ? item->item.copy(obj) : obj->initNull();
163}
Note: See TracBrowser for help on using the repository browser.