Large Arrays and GC

bearophile bearophileHUGS at lycos.com
Fri May 9 09:20:19 PDT 2008


dsimcha Wrote:
> Does anyone know of another way to manually delete associative arrays?

Time ago I have asked that question, and someone has shown me a way, you can find a modified version of it as the clear() function in my libs:
http://www.fantascienza.net/leonardo/so/libs_d.zip


T clear(T)(ref T items) {
    static if (IsAA!(T)) { // This only works with the current AA implementation
        struct BB {
            void*[] b;
            size_t nodes;
        }
        union ToPtr_clear {
            T x;
            void* ptr;
        }
        ToPtr_clear toptr;

        toptr.x = items;
        BB* b = cast(BB*) toptr.ptr;
        if (b) {
            b.b = null;
            b.nodes = 0;
        }

        return items;
    } else static if (IsDynamicArray!(T)) {
        items.length = 0;
        return items;
    } else
        static assert(0, "clear() accepts only AAs and dynamic arrays.");
}

You can find unittests, ddocs and the IsAA!() into those libs, the "func" module (the IsAA isn't necessary if you just want to use it for AAs).

Bye,
bearophile



More information about the Digitalmars-d mailing list