1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
45 #include <QtCore/qhash.h>
52 template <class Key, class T>
56 inline Node() : keyPtr(0) {}
57 inline Node(T *data, int cost)
58 : keyPtr(0), t(data), c(cost), p(0), n(0) {}
59 const Key *keyPtr; T *t; int c; Node *p,*n;
62 QHash<Key, Node> hash;
65 inline void unlink(Node &n) {
66 if (n.p) n.p->n = n.n;
67 if (n.n) n.n->p = n.p;
72 hash.remove(*n.keyPtr);
75 inline T *relink(const Key &key) {
76 typename QHash<Key, Node>::iterator i = hash.find(key);
77 if (typename QHash<Key, Node>::const_iterator(i) == hash.constEnd())
82 if (n.p) n.p->n = n.n;
83 if (n.n) n.n->p = n.p;
93 Q_DISABLE_COPY(QCache)
96 inline explicit QCache(int maxCost = 100);
97 inline ~QCache() { clear(); }
99 inline int maxCost() const { return mx; }
100 void setMaxCost(int m);
101 inline int totalCost() const { return total; }
103 inline int size() const { return hash.size(); }
104 inline int count() const { return hash.size(); }
105 inline bool isEmpty() const { return hash.isEmpty(); }
106 inline QList<Key> keys() const { return hash.keys(); }
110 bool insert(const Key &key, T *object, int cost = 1);
111 T *object(const Key &key) const;
112 inline bool contains(const Key &key) const { return hash.contains(key); }
113 T *operator[](const Key &key) const;
115 bool remove(const Key &key);
116 T *take(const Key &key);
122 template <class Key, class T>
123 inline QCache<Key, T>::QCache(int amaxCost)
124 : f(0), l(0), mx(amaxCost), total(0) {}
126 template <class Key, class T>
127 inline void QCache<Key,T>::clear()
128 { while (f) { delete f->t; f = f->n; }
129 hash.clear(); l = 0; total = 0; }
131 template <class Key, class T>
132 inline void QCache<Key,T>::setMaxCost(int m)
133 { mx = m; trim(mx); }
135 template <class Key, class T>
136 inline T *QCache<Key,T>::object(const Key &key) const
137 { return const_cast<QCache<Key,T>*>(this)->relink(key); }
139 template <class Key, class T>
140 inline T *QCache<Key,T>::operator[](const Key &key) const
141 { return object(key); }
143 template <class Key, class T>
144 inline bool QCache<Key,T>::remove(const Key &key)
146 typename QHash<Key, Node>::iterator i = hash.find(key);
147 if (typename QHash<Key, Node>::const_iterator(i) == hash.constEnd()) {
155 template <class Key, class T>
156 inline T *QCache<Key,T>::take(const Key &key)
158 typename QHash<Key, Node>::iterator i = hash.find(key);
169 template <class Key, class T>
170 bool QCache<Key,T>::insert(const Key &akey, T *aobject, int acost)
178 Node sn(aobject, acost);
179 typename QHash<Key, Node>::iterator i = hash.insert(akey, sn);
181 Node *n = &i.value();
182 n->keyPtr = &i.key();
190 template <class Key, class T>
191 void QCache<Key,T>::trim(int m)
194 while (n && total > m) {