Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

Q3Cache Class Reference

The Q3Cache class is a template class that provides a cache based on QString keys. More...

 #include <Q3Cache>

This class is part of the Qt 3 support library. It is provided to keep old source code working. We strongly advise against using it in new code. See Porting to Qt 4 for more information.

Inherits: Q3PtrCollection.

Public Functions

Q3Cache ( int maxCost = 100, int size = 17, bool caseSensitive = true )
~Q3Cache ()
type * find ( const QString & k, bool ref = true ) const
bool insert ( const QString & k, const type * d, int c = 1, int p = 0 )
bool isEmpty () const
int maxCost () const
bool remove ( const QString & k )
void setMaxCost ( int m )
uint size () const
void statistics () const
type * take ( const QString & k )
int totalCost () const
type * operator[] ( const QString & k ) const

Reimplemented Public Functions

virtual void clear ()
virtual uint count () const

Additional Inherited Members

Detailed Description

The Q3Cache class is a template class that provides a cache based on QString 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 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 cache copy all of the item's data (known as a deep copy) when an item is inserted. insert() calls the virtual function Q3PtrCollection::newItem() for the item to be inserted. Inherit a cache and reimplement newItem() if you want deep copies.

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

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

In Q3Cache, the cache items are accessed via QString keys, which are Unicode strings. If you want to use non-Unicode, plain 8-bit char* keys, use the Q3AsciiCache template. A Q3Cache has the same performance as a Q3AsciiCache.

See also Q3CacheIterator, Q3AsciiCache, and Q3IntCache.

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 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 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 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.

If caseSensitive 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.

void Q3Cache::clear () [virtual]

Reimplemented from Q3PtrCollection::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 also remove() and take().

uint Q3Cache::count () const [virtual]

Reimplemented from Q3PtrCollection::count().

Returns the number of items in the cache.

See also totalCost().

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

Returns the item associated with key k, or 0 if the key does not exist in the cache. If ref 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 ( const QString & k, const type * d, int c = 1, int p = 0 )

Inserts the item d into the cache with key k 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 parameter p 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 exceeds maxCost()) you must delete d yourself. Additionally, be very careful about using d 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 also setMaxCost() and totalCost().

bool Q3Cache::remove ( const QString & k )

Removes the item associated with k, 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 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 Q3Cache::setMaxCost ( int m )

Sets the maximum allowed total cost of the cache to m. If the current total cost is greater than m, some items are deleted immediately.

See also maxCost() and totalCost().

uint Q3Cache::size () const

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

void Q3Cache::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 * Q3Cache::take ( const QString & k )

Takes the item associated with k 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 also remove() and clear().

int Q3Cache::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().

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

Returns the item associated with key k, or 0 if k 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 also find().

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 53
  2. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  3. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  4. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le blog Digia au hasard

Logo

Déploiement d'applications Qt Commercial sur les tablettes Windows 8

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.7-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web