November 07, 2015 Re: opCmp with structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | On Saturday, 7 November 2015 at 14:36:25 UTC, Mike Parker wrote:
>>
>> So my general question is: why immutable variables shouldn't be able to be moved (inside an array)?
>>
> To be pedantic, sort isn't actually moving anything. It's reassigning elements, i.e. a[1] = a[2]. The immutable member makes it illegal to assign one instance of ku to another:
> But sort doesn't work that way. You'll need to take a different approach to sort an array of ku.
Yeah... and I'm in the acceptance phase now ;) Do you have a hint, how this approach could be like?
|
November 07, 2015 Re: opCmp with structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | On 07.11.2015 15:36, Mike Parker wrote:
> It's actually possible to use move one instance
> into another, though:
>
> void main() {
> import std.algorithm : move;
> ku k1 = ku(1);
> ku k2 = ku(2);
> k2.move(k1);
> assert(k1.id == 2);
> }
Wat. It even compiles with @safe. That's not good.
|
November 07, 2015 Re: opCmp with structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex | Found something useful, here: http://dlang.org/phobos/std_algorithm_sorting.html#makeIndex with that I can achieve the following void main(){ ku[] tt = [ku(2), ku(1)]; //writeln(tt); auto index3 = new size_t[tt.length]; makeIndex!("a.id < b.id")(tt, index3); //writeln(index3); } so my index3 contains the right permutation of the tt array. What I still need is the possibility to take the values of the tt array in the order of the generated index. Can somebody give a hint how to achieve this? |
November 07, 2015 Re: opCmp with structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex | Ok... found the solution. The technical at least. import std.algorithm; import std.range; void main(){ ku[] tt = [ku(2), ku(1)]; //writeln(tt); auto index3 = new size_t[tt.length]; makeIndex!("a.id < b.id")(tt, index3); auto ind = indexed(tt, index3); //writeln(ind); } This yield in 'ind' the array of 'tt', ordered in the expected way. So, the remaining open questions are just ideologic now... Thanks for helping and commenting! |
November 10, 2015 Re: opCmp with structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | On 07.11.2015 16:59, anonymous wrote: > Wat. It even compiles with @safe. That's not good. Filed an issue: https://issues.dlang.org/show_bug.cgi?id=15315 |
Copyright © 1999-2021 by the D Language Foundation