Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
October 09, 2003 Fastest way to clear an Associative Array? | ||||
---|---|---|---|---|
| ||||
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 |
October 09, 2003 Re: Fastest way to clear an Associative Array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gerd Janssen | 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
>
>
|
October 09, 2003 Re: Fastest way to clear an Associative Array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gerd Janssen | 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 > > |
October 09, 2003 Re: Fastest way to clear an Associative Array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gerd Janssen | 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 > |
October 09, 2003 Re: Fastest way to clear an Associative Array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | 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 > > > > |
Copyright © 1999-2021 by the D Language Foundation