Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

Q3Cache Class

TheQ3Cache class is a template class that provides a cache based onQString keys.More...

Header:#include <Q3Cache>
Inherits:Q3PtrCollection

Public Functions

Q3Cache(int maxCost = 100, int size = 17, bool caseSensitive = true)
~Q3Cache()
type *find(const QString & k, bool ref = true) const
boolinsert(const QString & k, const type * d, int c = 1, int p = 0)
boolisEmpty() const
intmaxCost() const
boolremove(const QString & k)
voidsetMaxCost(int m)
uintsize() const
voidstatistics() const
type *take(const QString & k)
inttotalCost() const
type *operator[](const QString & k) const

Reimplemented Public Functions

virtual voidclear()
virtual uintcount() const

Additional Inherited Members

Detailed Description

TheQ3Cache class is a template class that provides a cache based onQString keys.

A cache is a least recently used (LRU) list of cache items. Each cache item has a key and a certain cost. The sum of item costs,totalCost(), never exceeds 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.

Q3Cache is a template class.Q3Cache<X> defines a cache that operates on pointers to X, or X*.

Apart frominsert(), by far the most important function isfind() (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 toremove() ortake() an object from the cache. CallingsetAutoDelete(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() andtake() 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 cache copy all of the item's data (known as a deep copy) when an item is inserted.insert() calls the virtual functionQ3PtrCollection::newItem() for the item to be inserted. Inherit a cache and reimplementnewItem() if you want deep copies.

When removing a cache item, the virtual functionQ3PtrCollection::deleteItem() is called. The default implementation deletes the item if auto-deletion is enabled, and does nothing otherwise.

There is aQ3CacheIterator that can be used to traverse the items in the cache in arbitrary order.

InQ3Cache, the cache items are accessed viaQString keys, which are Unicode strings. If you want to use non-Unicode, plain 8-bitchar* keys, use theQ3AsciiCache template. AQ3Cache has the same performance as aQ3AsciiCache.

See alsoQ3CacheIterator,Q3AsciiCache, andQ3IntCache.

Member Function Documentation

Q3Cache::Q3Cache(int maxCost = 100,int size = 17,bool caseSensitive = true)

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

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

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

IfcaseSensitive is TRUE (the default), the cache keys are case sensitive; if it is FALSE, they are case-insensitive. Case-insensitive comparison considers all Unicode letters.

Q3Cache::~Q3Cache()

Removes all items from the cache and destroys it. All iterators that access this cache will be reset.

[virtual]void Q3Cache::clear()

Reimplemented fromQ3PtrCollection::clear().

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 alsoremove() andtake().

[virtual]uint Q3Cache::count() const

Reimplemented fromQ3PtrCollection::count().

Returns the number of items in the cache.

See alsototalCost().

type * Q3Cache::find(constQString & k,bool ref = true) const

Returns the item associated with keyk, or 0 if the key does not exist in the cache. Ifref is TRUE (the default), the item is moved to the front of the least recently used list.

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

bool Q3Cache::insert(constQString & k, consttype * d,int c = 1,int p = 0)

Inserts the itemd into the cache with keyk and associated cost,c. Returns TRUE if it is successfully inserted; otherwise returns FALSE.

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

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

Warning: If this function returns FALSE (which could happen, e.g. if the cost of this item alone exceedsmaxCost()) you must deleted yourself. Additionally, be very careful about usingd after calling this function because 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 Q3Cache::isEmpty() const

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

int Q3Cache::maxCost() const

Returns the maximum allowed total cost of the cache.

See alsosetMaxCost() andtotalCost().

bool Q3Cache::remove(constQString & k)

Removes the item associated withk, and returns TRUE if the item was present in the cache; otherwise returns FALSE.

The item is deleted if auto-deletion has been enabled, i.e., if you have calledsetAutoDelete(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 alsotake() andclear().

void Q3Cache::setMaxCost(int m)

Sets the maximum allowed total cost of the cache tom. If the current total cost is greater thanm, some items are deleted immediately.

See alsomaxCost() andtotalCost().

uint Q3Cache::size() const

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

void Q3Cache::statistics() const

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

type * Q3Cache::take(constQString & k)

Takes the item associated withk out of the cache without deleting it, and returns a pointer to the item taken out, or 0 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 alsoremove() andclear().

int Q3Cache::totalCost() const

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

See alsosetMaxCost().

type * Q3Cache::operator[](constQString & k) const

Returns the item associated with keyk, or 0 ifk does not exist in the cache, and moves the item to the front of the least recently used 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 alsofind().

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp