[phobos] Array preallocate function
schveiguy at yahoo.com
Fri Feb 26 14:14:53 PST 2010
Ran into an issue with array properties:
I'm going to make the function not a property for now (at least for the "set capacity" part). Should I wait for this bug to be fixed, or should we discuss a different scheme?
My plans were for this to work:
x.capacity = 10000; // set the capacity to 10000 elements
assert(x.capacity >= 10000); // get the current capacity (elements allocated + available)
int *ptr = x.ptr;
while(x.length < 10000)
x ~= 1;
assert(x.ptr == ptr); // no reallocation
x.length = 0;
x.shrinkToFit(); // name up for debate
x ~= 1;
assert(x.ptr == ptr); // resized the allocation length, so it can be reused as a buffer.
I'll have to change the write property 'capacity' to a function 'setCapacity' for now.
----- Original Message ----
> From: Steve Schveighoffer <schveiguy at yahoo.com>
> To: Discuss the phobos library for D <phobos at puremagic.com>
> Sent: Tue, February 23, 2010 1:47:56 PM
> Subject: Re: [phobos] Array preallocate function
> I understand your objections to minimize. I don't really have a preference, but
> I don't really like shrinkToFit. I'll use that for now, since it's probably
> easy to search/replace later.
> ----- Original Message ----
> > From: Andrei Alexandrescu
> > > What about a "minimize" function, which simply truncates any
> > > "allocated" length after an array. So you would reset an array via:
> > >
> > > arr.length = 0; arr.minimize();
> > >
> > > The advantage here is the array's length is not affected, just the
> > > allocated length is reduced to match the array's length. There are
> > > less invalid cases to worry about (i.e. "shrinking" to something
> > > larger doesn't make any sense).
> > Sounds good. I'd choose a more specific name, e.g. shrinkToFit. Minimize has
> > think of optimization functions.
> > Andrei
> > _______________________________________________
> > phobos mailing list
> > phobos at puremagic.com
> > http://lists.puremagic.com/mailman/listinfo/phobos
> phobos mailing list
> phobos at puremagic.com
More information about the phobos