Thread overview | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 24, 2011 removing an item from a dynamic array | ||||
---|---|---|---|---|
| ||||
Attachments:
| I've stumbled in an annoying error while trying to remove an item from a dynamic array. It's an array of Loc and a Loc is a simple struct of two integers. when I used remove from std.algorithm I get C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(5948): Error: incompatible types for ((pos) <= (from)): 'uint' and 'Loc' C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(5949): Error: incompatible types for ((pos) < (from)): 'uint' and 'Loc' is it suposed to do this? |
October 24, 2011 Re: removing an item from a dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to maarten van damme | On 24.10.2011 23:55, maarten van damme wrote:
> I've stumbled in an annoying error while trying to remove an item from a
> dynamic array.
> It's an array of Loc and a Loc is a simple struct of two integers.
> when I used remove from std.algorithm I get
> C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(5948): Error:
> incompatible types for ((pos) <= (from)): 'uint' and 'Loc'
> C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(5949): Error:
> incompatible types for ((pos) < (from)): 'uint' and 'Loc'
>
> is it suposed to do this?
Could you add some sample code?
I think remove wants indexes, not values:
import std.stdio, std.algorithm;
struct S {
int a, b;
}
void main() {
auto arr = [S(0,1), S(2,3), S(4,5)];
writeln(arr);
arr = arr.remove(0, 2);
writeln(arr);
}
prints:
[S(0, 1), S(2, 3), S(4, 5)]
[S(4, 5)]
|
October 25, 2011 Re: removing an item from a dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo Attachments:
| import std.algorithm; struct Loc { uint row; uint col; } void main(){ Loc[] testArray; Loc a={3,2}; Loc b={5,3}; testArray~=a; testArray~=b; remove(testArray,a); } gives the same error |
October 25, 2011 Re: removing an item from a dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to maarten van damme | maarten van damme:
> import std.algorithm;
> struct Loc {
> uint row;
> uint col;
> }
> void main(){
> Loc[] testArray;
> Loc a={3,2};
> Loc b={5,3};
> testArray~=a;
> testArray~=b;
> remove(testArray,a);
> }
> gives the same error
The second argument of remove() needs to be an index, a size_t.
This works:
import std.stdio, std.algorithm;
struct Loc {
uint row, col;
}
void main() {
auto a = Loc(3, 2),
b = Loc(5, 3);
auto data = [a, b];
writeln(remove(data, 0));
writeln(data);
}
It prints:
[Loc(5, 3)]
[Loc(5, 3), Loc(5, 3)]
So curiously remove() doesn't work in-place, I think this is a bug or a design bug.
Bye,
bearophile
|
October 25, 2011 Re: removing an item from a dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 25.10.2011 11:51, bearophile wrote: > maarten van damme: > >> import std.algorithm; >> struct Loc { >> uint row; >> uint col; >> } >> void main(){ >> Loc[] testArray; >> Loc a={3,2}; >> Loc b={5,3}; >> testArray~=a; >> testArray~=b; >> remove(testArray,a); >> } >> gives the same error > > The second argument of remove() needs to be an index, a size_t. > > This works: > > import std.stdio, std.algorithm; > struct Loc { > uint row, col; > } > void main() { > auto a = Loc(3, 2), > b = Loc(5, 3); > auto data = [a, b]; > writeln(remove(data, 0)); > writeln(data); > } > > > It prints: > [Loc(5, 3)] > [Loc(5, 3), Loc(5, 3)] > > So curiously remove() doesn't work in-place, I think this is a bug or a design bug. > > Bye, > bearophile Yes, probably a bug. This example from the documentation fails: import std.stdio, std.algorithm; void main() { int[] a = [ 0, 1, 2, 3 ]; assert(remove!(SwapStrategy.unstable)(a, 1) == [ 0, 3, 2 ]); } |
October 25, 2011 Re: removing an item from a dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | I have written this: http://d.puremagic.com/issues/show_bug.cgi?id=6849 Bye, bearophile |
October 25, 2011 Re: removing an item from a dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile Attachments:
| thank you, meanwhile I'll just use find + remove the index. |
October 25, 2011 Re: removing an item from a dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 25.10.2011 13:51, bearophile wrote: > maarten van damme: > >> import std.algorithm; >> struct Loc { >> uint row; >> uint col; >> } >> void main(){ >> Loc[] testArray; >> Loc a={3,2}; >> Loc b={5,3}; >> testArray~=a; >> testArray~=b; >> remove(testArray,a); >> } >> gives the same error > > The second argument of remove() needs to be an index, a size_t. > > This works: > > import std.stdio, std.algorithm; > struct Loc { > uint row, col; > } > void main() { > auto a = Loc(3, 2), > b = Loc(5, 3); > auto data = [a, b]; > writeln(remove(data, 0)); > writeln(data); > } > > > It prints: > [Loc(5, 3)] > [Loc(5, 3), Loc(5, 3)] > > So curiously remove() doesn't work in-place, I think this is a bug or a design bug. > > Bye, > bearophile No, it's not a bug. It's the same as c++ STL remove - it operates on range but not on container. To shrink container, update it's length. -- Dmitry Olshansky |
October 25, 2011 Re: removing an item from a dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dmitry Olshansky | Dmitry Olshansky:
> No, it's not a bug. It's the same as c++ STL remove - it operates on range but not on container. To shrink container, update it's length.
Thank you for your answer, I didn't know this, and I didn't think about this possibility because it's weird, it's an in-place operation that modifies the data only partially, leaving it in a wrong state. It looks like a bad design, bug prone-too. The design of Python del is better. (Maybe I'll have to bring this in the main D newsgroup too, because Phobos bug reports often get unnoticed). In the meantime I'll add a wrapper function to dlibs2.
Bye,
bearophile
|
October 25, 2011 Re: removing an item from a dynamic array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dmitry Olshansky | On 25.10.2011 18:23, Dmitry Olshansky wrote:
> On 25.10.2011 13:51, bearophile wrote:
>> maarten van damme:
>>
>>> import std.algorithm;
>>> struct Loc {
>>> uint row;
>>> uint col;
>>> }
>>> void main(){
>>> Loc[] testArray;
>>> Loc a={3,2};
>>> Loc b={5,3};
>>> testArray~=a;
>>> testArray~=b;
>>> remove(testArray,a);
>>> }
>>> gives the same error
>>
>> The second argument of remove() needs to be an index, a size_t.
>>
>> This works:
>>
>> import std.stdio, std.algorithm;
>> struct Loc {
>> uint row, col;
>> }
>> void main() {
>> auto a = Loc(3, 2),
>> b = Loc(5, 3);
>> auto data = [a, b];
>> writeln(remove(data, 0));
>> writeln(data);
>> }
>>
>>
>> It prints:
>> [Loc(5, 3)]
>> [Loc(5, 3), Loc(5, 3)]
>>
>> So curiously remove() doesn't work in-place, I think this is a bug or
>> a design bug.
>>
>> Bye,
>> bearophile
>
> No, it's not a bug. It's the same as c++ STL remove - it operates on
> range but not on container. To shrink container, update it's length.
If it's not a bug, there's a bug in the documentation (see my previous post)
|
Copyright © 1999-2021 by the D Language Foundation