[phobos] Array preallocate function
Steve Schveighoffer
schveiguy at yahoo.com
Thu Mar 4 10:37:40 PST 2010
I outlined the ideas below on the newsgroup, and one poster had a valid point.
With x.setCapacity(...) or x.capacity = ..., it implies that the exact capacity is being set. In other words, shrinking the allocated size may be allowed, and it would not allocate more than requested. It also implies the following:
a.capacity = x;
assert(a.capacity == x);
Which is not true. The capacity request is only the *minimum* size the GC should allocate. It is allowed to allocate more.
The poster (Clemens) suggested to name it reserve(), after the STL's equivalent member functions. It does sound more correct, but I kind of liked the idea of having it be a property (which reserve doesn't make sense as).
I would still use the name capacity as a property for the getter.
Does anyone else have any opinions?
-Steve
----- Original Message ----
> From: Steve Schveighoffer <schveiguy at yahoo.com>
>
> 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
> > To: Discuss the phobos library for D
> > 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
>
>
>
>
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
More information about the phobos
mailing list