#include <emcache.h>
Inheritance diagram for EMAN::EMCache< T >:
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 |
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.
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 }
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 }
void EMAN::EMCache< T >::add | ( | const string & | itemname, | |
T * | item | |||
) | [inline] |
Definition at line 104 of file emcache.h.
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 }
T* EMAN::EMCache< T >::get | ( | const string & | itemname | ) | const [inline] |
Definition at line 89 of file emcache.h.
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 }
int EMAN::EMCache< T >::get_size | ( | ) | const [inline] |
void EMAN::EMCache< T >::remove | ( | const string & | itemname | ) | [inline] |
Definition at line 128 of file emcache.h.
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 }
T** EMAN::EMCache< T >::item_cache [private] |
Definition at line 153 of file emcache.h.
Referenced by EMAN::EMCache< EMAN::ImageIO >::add(), EMAN::EMCache< EMAN::ImageIO >::EMCache(), EMAN::EMCache< EMAN::ImageIO >::get(), EMAN::EMCache< EMAN::ImageIO >::remove(), and EMAN::EMCache< EMAN::ImageIO >::~EMCache().
string* EMAN::EMCache< T >::name_cache [private] |
Definition at line 154 of file emcache.h.
Referenced by EMAN::EMCache< EMAN::ImageIO >::add(), EMAN::EMCache< EMAN::ImageIO >::EMCache(), EMAN::EMCache< EMAN::ImageIO >::get(), EMAN::EMCache< EMAN::ImageIO >::remove(), and EMAN::EMCache< EMAN::ImageIO >::~EMCache().
int EMAN::EMCache< T >::nitems [private] |
Definition at line 157 of file emcache.h.
Referenced by EMAN::EMCache< EMAN::ImageIO >::add(), EMAN::EMCache< EMAN::ImageIO >::EMCache(), EMAN::EMCache< EMAN::ImageIO >::get(), EMAN::EMCache< EMAN::ImageIO >::remove(), and EMAN::EMCache< EMAN::ImageIO >::~EMCache().
int EMAN::EMCache< T >::size [private] |
Definition at line 156 of file emcache.h.
Referenced by EMAN::EMCache< EMAN::ImageIO >::add(), EMAN::EMCache< EMAN::ImageIO >::EMCache(), and EMAN::EMCache< EMAN::ImageIO >::get_size().