(1.0) Clearing out an Associative Array

Bjoern nanali at nospam-wanadoo.fr
Mon Jan 28 11:01:41 PST 2008


jicman schrieb:
> Greetings.
> 
> Long time no write. :-)
> 
> I know that I can get the keys of an Associative array and do a foreach loop and remove the keys of the array, but is there an easier and quicker way?  I know that I can set any non-Associative array to [], ie,
> 
> myArr = [];
> 
> and that is sufficient or set the length to 0; but how about the Associative arrays?  Is there a quick way of doing this?
> 
> thanks,
> 
> josé

To clear an AA
// some Associative Array properties
bool hasData(T,E)(T[E] aa) { return aa.length > 0; }

// by Ben Hinkle
private struct BB { void*[]b; size_t nodes; }

private union ToPtr(T) {T x; void * ptr; }

void clear(T,E)(T[E] aa) {
     ToPtr!(typeof(aa)) toptr;
     toptr.x = aa;
     BB* b = cast(BB*) toptr.ptr;
     if (b) {
         b.b = null;
         b.nodes = 0;
     }
}

aa.clear()
//D 1.x
If you mean, clearing just the keys, sorry dunno
Bjoern


More information about the Digitalmars-d-learn mailing list