返回介绍

QIntCache Class

发布于 2019-10-04 15:00:50 字数 7743 浏览 953 评论 0 收藏 0

The QIntCache class is a template class that provides a cache based on long keys. More...

#include <qintcache.h>

List of all member functions.

Public Members

  • QIntCache ( intmaxCost = 100, intsize = 17 )
  • ~QIntCache ()
  • int maxCost () const
  • int totalCost () const
  • void setMaxCost ( intm )
  • virtual uint count () const
  • uint size () const
  • bool isEmpty () const
  • bool insert ( longk, consttype*d, intc = 1, intp = 0 )
  • bool remove ( longk )
  • type * take ( longk )
  • virtual void clear ()
  • type * find ( longk, boolref = TRUE ) const
  • type * operator[] ( longk ) const
  • void statistics () const

Detailed Description

The QIntCache class is a template class that provides a cache based on long keys.

QIntCache is implemented as a template class. Define a template instance QIntCache<X> to create a cache that operates on pointers to X, or X*.

A cache is a least recently used (LRU) list of cache items, accessed via long keys. Each cache item has a cost. The sum of item costs, totalCost(), will not exceed the maximum cache cost, maxCost(). If inserting a new item would cause the total cost to exceed the maximum cost, the least recently used items in the cache are removed.

Apart from insert(), by far the most important function is find() (which also exists as operator[]). This function looks up an item, returns it, and by default marks it as being the most recently used item.

There are also methods to remove() or take() an object from the cache. Calling setAutoDelete(TRUE) for a cache tells it to delete items that are removed. The default is to not delete items when they are removed (i.e. remove() and take() are equivalent).

When inserting an item into the cache, only the pointer is copied, not the item itself. This is called a shallow copy. It is possible to make the dictionary copy all of the item's data (known as a deep copy) when an item is inserted. insert() calls the virtual function QPtrCollection::newItem() for the item to be inserted. Inherit a dictionary and reimplement it if you want deep copies.

When removing a cache item if auto-deletion is enabled the item will be automatically deleted.

There is a QIntCacheIterator which may be used to traverse the items in the cache in arbitrary order.

See also QIntCacheIterator, QCache, QAsciiCache, Collection Classes and Non-GUI Classes.


Member Function Documentation

QIntCache::QIntCache ( intmaxCost = 100, intsize = 17 )

Constructs a cache whose contents will never have a total cost greater than maxCost and which is expected to contain less than size items.

size is actually the size of an internal hash array; it's usually best to make it prime and at least 50% bigger than the largest expected number of items in the cache.

Each inserted item is associated with a cost. When inserting a new item, if the total cost of all items in the cache will exceed maxCost, the cache will start throwing out the older (least recently used) items until there is enough room for the new item to be inserted.

QIntCache::~QIntCache ()

Removes all items from the cache and then destroys the int cache. If auto-deletion is enabled the cache's items are deleted. All iterators that access this cache will be reset.

void QIntCache::clear () [virtual]

Removes all items from the cache, and deletes them if auto-deletion has been enabled.

All cache iterators that operate this on cache are reset.

See also remove() and take().

uint QIntCache::count () const [virtual]

Returns the number of items in the cache.

See also totalCost().

type * QIntCache::find ( longk, boolref = TRUE ) const

Returns the item associated with k, or null if the key does not exist in the cache. If ref is TRUE (the default), the item is moved to the front of the LRU list.

If there are two or more items with equal keys, the one that was inserted last is returned.

bool QIntCache::insert ( longk, consttype*d, intc = 1, intp = 0 )

Inserts the item d into the cache with key k and cost c (default 1). Returns TRUE if it succeeds and FALSE if it fails.

The cache's size is limited, and if the total cost is too high, QIntCache will remove old, least-used items until there is room for this new item.

The parameter p is internal and should be left at the default value (0).

Warning: If this function returns FALSE, you must delete d yourself. Additionally, be very careful about using d after calling this function. Any other insertions into the cache, from anywhere in the application or within Qt itself, could cause the object to be discarded from the cache and the pointer to become invalid.

bool QIntCache::isEmpty () const

Returns TRUE if the cache is empty; otherwise returns FALSE.

int QIntCache::maxCost () const

Returns the maximum allowed total cost of the cache.

See also setMaxCost() and totalCost().

type * QIntCache::operator[] ( longk ) const

Returns the item associated with k, or null if k does not exist in the cache, and moves the item to the front of the LRU list.

If there are two or more items with equal keys, the one that was inserted last is returned.

This is the same as find( k, TRUE ).

See also find().

bool QIntCache::remove ( longk )

Removes the item associated with k, and returns TRUE if the item was present in the cache or FALSE if it was not.

The item is deleted if auto-deletion has been enabled, i.e. if you have called setAutoDelete(TRUE).

If there are two or more items with equal keys, the one that was inserted last is removed.

All iterators that refer to the removed item are set to point to the next item in the cache's traversal order.

See also take() and clear().

void QIntCache::setMaxCost ( intm )

Sets the maximum allowed total cost of the cache to m. If the current total cost is above m, some items are removed immediately.

See also maxCost() and totalCost().

uint QIntCache::size () const

Returns the size of the hash array used to implement the cache. This should be a bit larger than count() is likely to be.

void QIntCache::statistics () const

A debug-only utility function. Prints out cache usage, hit/miss, and distribution information using qDebug(). This function does nothing in the release library.

type * QIntCache::take ( longk )

Takes the item associated with k out of the cache without deleting it, and returns a pointer to the item taken out or null if the key does not exist in the cache.

If there are two or more items with equal keys, the one that was inserted last is taken.

All iterators that refer to the taken item are set to point to the next item in the cache's traversal order.

See also remove() and clear().

int QIntCache::totalCost () const

Returns the total cost of the items in the cache. This is an integer in the range 0 to maxCost().

See also setMaxCost().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文