Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

EMAN::EMCache< T > Class Template Reference

EMCache is a generic cache that can cache anything defined by 'T'. More...

#include <emcache.h>

Inheritance diagram for EMAN::EMCache< T >:

Inheritance graph
[legend]
Collaboration diagram for EMAN::EMCache< T >:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 EMCache (int cache_size)
 ~EMCache ()
T * get (const string &itemname) const
void add (const string &itemname, T *item)
void remove (const string &itemname)
int get_size () const

Private Attributes

T ** item_cache
string * name_cache
int size
int nitems

Detailed Description

template<class T>
class EMAN::EMCache< T >

EMCache is a generic cache that can cache anything defined by 'T'.

Item in the cache is identified by its name. Use add() to add an item to the cache, and get() to return the item. If the item is not in the cache, a 0 pointer will be returned.

Definition at line 53 of file emcache.h.


Constructor & Destructor Documentation

template<class T>
EMAN::EMCache< T >::EMCache int  cache_size  )  [inline, explicit]
 

Definition at line 56 of file emcache.h.

00057                 {
00058                         item_cache = new T *[cache_size];
00059                         name_cache = new string[cache_size];
00060 
00061                         size = cache_size;
00062                         nitems = 0;
00063                 }

template<class T>
EMAN::EMCache< T >::~EMCache  )  [inline]
 

Definition at line 65 of file emcache.h.

00066                 {
00067                         for (int i = 0; i < nitems; i++) {
00068                                 if( item_cache[i] )
00069                                 {
00070                                         delete item_cache[i];
00071                                         item_cache[i] = 0;
00072                                 }
00073                         }
00074 
00075                         if( item_cache )
00076                         {
00077                                 delete[]item_cache;
00078                                 item_cache = 0;
00079                         }
00080 
00081                         if( name_cache )
00082                         {
00083                                 delete[]name_cache;
00084                                 name_cache = 0;
00085                         }
00086                 }


Member Function Documentation

template<class T>
void EMAN::EMCache< T >::add const string &  itemname,
T *  item
[inline]
 

Definition at line 104 of file emcache.h.

Referenced by EMAN::GlobalCache::add_imageio().

00105                 {
00106                         if (!item) {
00107                                 return;
00108                         }
00109 
00110                         if (nitems < size) {
00111                                 item_cache[nitems] = item;
00112                                 name_cache[nitems] = itemname;
00113                                 nitems++;
00114                         }
00115                         else {
00116                                 int r = (int) (1.0 * size * rand() / (RAND_MAX + 1.0));
00117                                 if( item_cache[r] )
00118                                 {
00119                                         delete item_cache[r];
00120                                         item_cache[r] = 0;
00121                                 }
00122 
00123                                 item_cache[r] = item;
00124                                 name_cache[r] = itemname;
00125                         }
00126                 }

template<class T>
T* EMAN::EMCache< T >::get const string &  itemname  )  const [inline]
 

Definition at line 89 of file emcache.h.

Referenced by EMAN::GlobalCache::get_imageio().

00090                 {
00091                         T *result = 0;
00092 
00093                         for (int i = 0; i < nitems; i++)
00094                         {
00095                                 if (name_cache[i] == itemname) {
00096                                         result = item_cache[i];
00097                                         break;
00098                                 }
00099                         }
00100 
00101                         return result;
00102                 }

template<class T>
int EMAN::EMCache< T >::get_size  )  const [inline]
 

Definition at line 147 of file emcache.h.

00148                 {
00149                         return size;
00150                 }

template<class T>
void EMAN::EMCache< T >::remove const string &  itemname  )  [inline]
 

Definition at line 128 of file emcache.h.

Referenced by EMAN::GlobalCache::get_imageio().

00129                 {
00130                         int r = -1;
00131                         for (int i = 0; i < nitems; i++) {
00132                                 if (name_cache[i] == itemname) {
00133                                         r = i;
00134                                         break;
00135                                 }
00136                         }
00137                         if (r >= 0) {
00138                                 if( item_cache[r] )
00139                                 {
00140                                         delete item_cache[r];
00141                                         item_cache[r] = 0;
00142                                 }
00143                                 name_cache[r] = "";
00144                         }
00145                 }


Member Data Documentation

template<class T>
T** EMAN::EMCache< T >::item_cache [private]
 

Definition at line 153 of file emcache.h.

template<class T>
string* EMAN::EMCache< T >::name_cache [private]
 

Definition at line 154 of file emcache.h.

template<class T>
int EMAN::EMCache< T >::nitems [private]
 

Definition at line 157 of file emcache.h.

template<class T>
int EMAN::EMCache< T >::size [private]
 

Definition at line 156 of file emcache.h.


The documentation for this class was generated from the following file:
Generated on Tue Jun 11 13:48:00 2013 for EMAN2 by  doxygen 1.3.9.1