View mode: basic / threaded / horizontal-split · Log in · Help
May 11, 2008
Dynamic arrays in D 1.0
In D 1.0 dynamic arrays are the equivalent of C++'s std::vector<T>, with 
slicing replacing iterators in order to access some subrange of the 
sequence. In C++ there is functionality in std::vector::erase for 
erasing elements from the vector and std::vector::insert for inserting 
new elements into the vector. Where is that functionality in D's dynamic 
arrays ?

Furthermore in D 1.0 a char[], a dynamic array of characters, is the 
equivalent of the C++ std::string. In the C++ std::string class there is 
a rich set of functionality for manipulating the char elements in the 
string, for finding particular elements in the string, and for comparing 
the character elements in strings with other strings, but I see little 
of this in dynamic array functionality. What am I missing ?

I am guessing this must be provided in D libraries of functions ( Phobos 
? Tango ? ) templated on the dynamic array type. But, if so, this seems 
a poor way of providing dynamic array functionality as compared to 
built-in dynamic array functionality in order to provide the sort of 
functionality mentioned above, especially as D's dynamic arrays are part 
of the D language as opposed to a separate library like C++'s 
std::vector<T> and std::string.
May 11, 2008
Re: Dynamic arrays in D 1.0
Edward Diener wrote:
> In D 1.0 dynamic arrays are the equivalent of C++'s std::vector<T>, with 
> slicing replacing iterators in order to access some subrange of the 
> sequence. In C++ there is functionality in std::vector::erase for 
> erasing elements from the vector and std::vector::insert for inserting 
> new elements into the vector. Where is that functionality in D's dynamic 
> arrays ?

Yes, erase & insert and many other things are conspicuously missing from 
D1 Phobos (not sure about D2 Phobos).  I like the collection of such 
routines provided by the Cashew library in the cashew.utils.Array module.

> Furthermore in D 1.0 a char[], a dynamic array of characters, is the 
> equivalent of the C++ std::string. In the C++ std::string class there is 
> a rich set of functionality for manipulating the char elements in the 
> string, for finding particular elements in the string, and for comparing 
> the character elements in strings with other strings, but I see little 
> of this in dynamic array functionality. What am I missing ?

Those functions are in std.string in Phobos.  And scattered all over the 
place in Tango.

> I am guessing this must be provided in D libraries of functions ( Phobos 
> ? Tango ? ) templated on the dynamic array type. 

Right.  Except for generic arrays, there's nothing in D1 Phobos.

> But, if so, this seems 
> a poor way of providing dynamic array functionality as compared to 
> built-in dynamic array functionality in order to provide the sort of 
> functionality mentioned above, especially as D's dynamic arrays are part 
> of the D language as opposed to a separate library like C++'s 
> std::vector<T> and std::string.

Why?  What's wrong with providing such functionality as a library when a 
library does the trick?

Are you aware of the pseudo-member trick that works for arrays?  Example:

  T get_elem(T)(T[] array, size_t idx) {
     return array[idx];
  }
  ...
  float[] numbers = [1.f, 2,3];
  float first = numbers.get_elem(0);

That works today in D1 and D2.  Any function that takes an array as a 
first element can be treated as if it were a (non-virtual) member of the 
array type.

So given that, I think there's really no reason for the bulk of D's 
array functionality to not be in a library.

--bb
May 11, 2008
Re: Dynamic arrays in D 1.0
On 11/05/2008, Edward Diener <eddielee_no_spam_here@tropicsoft.com> wrote:
> In D 1.0 dynamic arrays are the equivalent of C++'s std::vector<T>, with
> slicing replacing iterators in order to access some subrange of the
> sequence. In C++ there is functionality in std::vector::erase for erasing
> elements from the vector and std::vector::insert for inserting new elements
> into the vector. Where is that functionality in D's dynamic arrays ?

To erase the elements from index i to index j in place:

   array[i..$+i-j][] = array[j..$].dup;
   array = array[0..$+i-j];

I place this code in the public domain. Feel free to turn it into a
function. :-)
If you don't mind making a new copy, it's even easier - you can do that in one.

   array = array[0..i] ~ array[j..$];





> Furthermore in D 1.0 a char[], a dynamic array of characters, is the
> equivalent of the C++ std::string. In the C++ std::string class there is a
> rich set of functionality for manipulating the char elements in the string,
> for finding particular elements in the string, and for comparing the
> character elements in strings with other strings, but I see little of this
> in dynamic array functionality. What am I missing ?

std.string
std.algorithm
May 11, 2008
Re: Dynamic arrays in D 1.0
Janice Caron wrote:
> On 11/05/2008, Edward Diener <eddielee_no_spam_here@tropicsoft.com> wrote:
>> In D 1.0 dynamic arrays are the equivalent of C++'s std::vector<T>, with
>> slicing replacing iterators in order to access some subrange of the
>> sequence. In C++ there is functionality in std::vector::erase for erasing
>> elements from the vector and std::vector::insert for inserting new elements
>> into the vector. Where is that functionality in D's dynamic arrays ?
> 
> To erase the elements from index i to index j in place:
> 
>     array[i..$+i-j][] = array[j..$].dup;
>     array = array[0..$+i-j];

It can be done more efficiently using memmove to shift contents down. 
Also if you're dropping the tail end, you can just change .length. 
Cashew's erase routine (called "dropRange") does both.

--bb
May 11, 2008
Re: Dynamic arrays in D 1.0
Edward Diener wrote:
> I am guessing this must be provided in D libraries of functions ( Phobos 
> ? Tango ? ) templated on the dynamic array type. But, if so, this seems 
> a poor way of providing dynamic array functionality as compared to 
> built-in dynamic array functionality in order to provide the sort of 
> functionality mentioned above, especially as D's dynamic arrays are part 
> of the D language as opposed to a separate library like C++'s 
> std::vector<T> and std::string.

I disagree. Having it in a library mans its more extensible and can be 
rewritten/altered more easily (for example, if more efficient algorithms 
are found).
May 11, 2008
Re: Dynamic arrays in D 1.0
On 11/05/2008, Bill Baxter <dnewsgroup@billbaxter.com> wrote:
> Janice Caron wrote:

> > To erase the elements from index i to index j in place:
>
>  It can be done more efficiently using memmove to shift contents down. Also
> if you're dropping the tail end, you can just change .length. Cashew's erase
> routine (called "dropRange") does both.

OK, how about this, using std.algorithm.copy:

   array = copy(array[j..$], array[i..$]);
   array = array[0..$+i-j];

The copy is done in-place, like memmove(), but without the danger of
buffer overrun.
You still need the second line to change the length though.

You can even do it ONE LINE, but I have to confess, the code looks
pretty obfuscated.

   array = array[0..$-copy(array[j..$], array[i..$]).length];
May 11, 2008
Re: Dynamic arrays in D 1.0
Bill Baxter wrote:
> Edward Diener wrote:
>> In D 1.0 dynamic arrays are the equivalent of C++'s std::vector<T>, 
>> with slicing replacing iterators in order to access some subrange of 
>> the sequence. In C++ there is functionality in std::vector::erase for 
>> erasing elements from the vector and std::vector::insert for inserting 
>> new elements into the vector. Where is that functionality in D's 
>> dynamic arrays ?
> 
> Yes, erase & insert and many other things are conspicuously missing from 
> D1 Phobos (not sure about D2 Phobos).  I like the collection of such 
> routines provided by the Cashew library in the cashew.utils.Array module.

OK, I will look it up. I did expect such functionlity to be part of the 
D run-time library or the language implementation itself.

> 
>> Furthermore in D 1.0 a char[], a dynamic array of characters, is the 
>> equivalent of the C++ std::string. In the C++ std::string class there 
>> is a rich set of functionality for manipulating the char elements in 
>> the string, for finding particular elements in the string, and for 
>> comparing the character elements in strings with other strings, but I 
>> see little of this in dynamic array functionality. What am I missing ?
> 
> Those functions are in std.string in Phobos.  And scattered all over the 
> place in Tango.

I see the Phobos std.string. It seems, at first glance, not nearly as 
rich an implementation as the C++ std::string functionality.

> 
>> I am guessing this must be provided in D libraries of functions ( 
>> Phobos ? Tango ? ) templated on the dynamic array type. 
> 
> Right.  Except for generic arrays, there's nothing in D1 Phobos.

This is surprising, but I will go with the link above you suggested.

> 
>> But, if so, this seems a poor way of providing dynamic array 
>> functionality as compared to built-in dynamic array functionality in 
>> order to provide the sort of functionality mentioned above, especially 
>> as D's dynamic arrays are part of the D language as opposed to a 
>> separate library like C++'s std::vector<T> and std::string.
> 
> Why?  What's wrong with providing such functionality as a library when a 
> library does the trick?

There is nothing instrinsically wrong with it when the library is 
generic enough, as in the C++ standard algorithms which access 
containers through the iterator concept.

> 
> Are you aware of the pseudo-member trick that works for arrays?  Example:
> 
>   T get_elem(T)(T[] array, size_t idx) {
>      return array[idx];
>   }
>   ...
>   float[] numbers = [1.f, 2,3];
>   float first = numbers.get_elem(0);
> 
> That works today in D1 and D2.  Any function that takes an array as a 
> first element can be treated as if it were a (non-virtual) member of the 
> array type.

Yes I remember seeing this in the documentation but did not appreciate 
its importance vis-a-vis an external dynamic array library.

> 
> So given that, I think there's really no reason for the bulk of D's 
> array functionality to not be in a library.

Agreed. Unfortunately I had missed where such a library exists, and 
assumed it must be part of the normal distribution.
May 11, 2008
Re: Dynamic arrays in D 1.0
Janice Caron wrote:
> On 11/05/2008, Bill Baxter <dnewsgroup@billbaxter.com> wrote:
>> Janice Caron wrote:
> 
>>> To erase the elements from index i to index j in place:
>>  It can be done more efficiently using memmove to shift contents down. Also
>> if you're dropping the tail end, you can just change .length. Cashew's erase
>> routine (called "dropRange") does both.
> 
> OK, how about this, using std.algorithm.copy:
> 
>     array = copy(array[j..$], array[i..$]);
>     array = array[0..$+i-j];
> 
> The copy is done in-place, like memmove(), but without the danger of
> buffer overrun.

Std.algorithm's copy(), in its current form, will be less efficient 
because it copies elements one-at-a-time in a loop.  This was what the 
first version of Cashew did, but it was benchmarked and found that 
memmove is faster.

Of course, std.algorithm.copy could probably be tweaked to use memmove 
when possible, and then that would be just as good.  But right now it 
isn't.

Anyway, the existence of such subtleties is why things like erase should 
just be in the darn standard library so the problem only has to be 
solved once.

> You still need the second line to change the length though.
> 
> You can even do it ONE LINE, but I have to confess, the code looks
> pretty obfuscated.
> 
>     array = array[0..$-copy(array[j..$], array[i..$]).length];

Yeh, don't do that.  :-)

--bb
May 11, 2008
Re: Dynamic arrays in D 1.0
Janice Caron wrote:
>> Furthermore in D 1.0 a char[], a dynamic array of characters, is the
>> equivalent of the C++ std::string. In the C++ std::string class there is a
>> rich set of functionality for manipulating the char elements in the string,
>> for finding particular elements in the string, and for comparing the
>> character elements in strings with other strings, but I see little of this
>> in dynamic array functionality. What am I missing ?
> 
> std.string
> std.algorithm

I do not see a std.algorithm in the D 1.0 Phobos docuemntation.
May 11, 2008
Re: Dynamic arrays in D 1.0
On 11/05/2008, Edward Diener <eddielee_no_spam_here@tropicsoft.com> wrote:
>  I do not see a std.algorithm in the D 1.0 Phobos docuemntation.

It's in D2.
« First   ‹ Prev
1 2 3 4
Top | Discussion index | About this forum | D home