www.digitalmars.com         C & C++   DMDScript  

D - Fastest way to clear an Associative Array?

reply Gerd Janssen <Gerd_member pathlink.com> writes:
Hi,

I use an associative array as cache: 

void clearCache() {

char[][] keys = cache.keys;	
for (int i = 0; i < keys.length; i++)
delete cache(keys[i]);
}

Is there a faster way to clear the cache?


Thanks 
Gerd
Oct 09 2003
next sibling parent Justin Henzie <jhenzie mac.com> writes:
Does dup not work on associative arrays.

If iy does (I am on my mac), one could create a static associative array 
(empty) and then assuming cache is an ivar.

cache = BasicArray.dup();

or

class
{
	char[char[]] cache;

	void clearCache () {
		char[char[]] dummyCache;
		cache = dummyCache;
	}
}

this will only work if cache is an ivar, not for arguments passed to 
methods.

or something similiar would seem to be the right approach.

Reassigning the reference removes it as a strong root for GC and thus 
allows the elements to be collected if not referenced elsewhere.

Of course I could be absolutely wrong.

hope that helps

Justin

Gerd Janssen wrote:

 Hi,
 
 I use an associative array as cache: 
 
 void clearCache() {
 
 char[][] keys = cache.keys;	
 for (int i = 0; i < keys.length; i++)
 delete cache(keys[i]);
 }
 
 Is there a faster way to clear the cache?
 
 
 Thanks 
 Gerd
 
 

Oct 09 2003
prev sibling next sibling parent "Matthew Wilson" <matthew stlsoft.org> writes:
Clearly, associative arrays should have a clear() method. I'm surprised that
they don't


"Gerd Janssen" <Gerd_member pathlink.com> wrote in message
news:bm35la$290l$1 digitaldaemon.com...
 Hi,

 I use an associative array as cache:

 void clearCache() {

 char[][] keys = cache.keys;
 for (int i = 0; i < keys.length; i++)
 delete cache(keys[i]);
 }

 Is there a faster way to clear the cache?


 Thanks
 Gerd

Oct 09 2003
prev sibling parent reply "Vathix" <vathix dprogramming.com> writes:
I think you only need to set it to null.
cache = null;


"Gerd Janssen" <Gerd_member pathlink.com> wrote in message
news:bm35la$290l$1 digitaldaemon.com...
 Hi,

 I use an associative array as cache:

 void clearCache() {

 char[][] keys = cache.keys;
 for (int i = 0; i < keys.length; i++)
 delete cache(keys[i]);
 }

 Is there a faster way to clear the cache?


 Thanks
 Gerd

Oct 09 2003
parent "Matthew Wilson" <matthew stlsoft.org> writes:
That's not clearing the aa, that's "deleting" the instance. The two are
separate.

It's important, because you may have shared references to the same aa, and
wish to empty it. If you delete/replace the instance in one part, all the
others will still point to the old, non-empty, instance.

"Vathix" <vathix dprogramming.com> wrote in message
news:bm4k9f$13jr$1 digitaldaemon.com...
 I think you only need to set it to null.
 cache = null;


 "Gerd Janssen" <Gerd_member pathlink.com> wrote in message
 news:bm35la$290l$1 digitaldaemon.com...
 Hi,

 I use an associative array as cache:

 void clearCache() {

 char[][] keys = cache.keys;
 for (int i = 0; i < keys.length; i++)
 delete cache(keys[i]);
 }

 Is there a faster way to clear the cache?


 Thanks
 Gerd


Oct 09 2003