[phobos] Array preallocate function

Steve Schveighoffer schveiguy at yahoo.com
Fri Feb 26 14:14:53 PST 2010


Ran into an issue with array properties:

http://d.puremagic.com/issues/show_bug.cgi?id=3857

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:

int[] x;

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.

-Steve




----- 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.
> 
> -Steve
> 
> 
> 
> ----- 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 
> me 
> > 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
> http://lists.puremagic.com/mailman/listinfo/phobos



      


More information about the phobos mailing list