www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to sort the container Array from std.container

reply Flaze07 <christianseiji.cs gmail.com> writes:
I know that sort accepts Range( I am correct right ? ), so,
Array!uint arr;
//inserts element to arr
sort( arr.Range );
don't work, it says cannot pass RangeT!(Array!uint) as function 
argument
Jun 06 2018
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 6 June 2018 at 13:44:09 UTC, Flaze07 wrote:
 sort( arr.Range );
 don't work, it says cannot pass RangeT!(Array!uint) as function 
 argument
Range is the type, you want the value I think you can do sort(arr[]) maybe
Jun 06 2018
parent reply Flaze07 <christianseiji.cs gmail.com> writes:
On Wednesday, 6 June 2018 at 13:46:41 UTC, Adam D. Ruppe wrote:
 On Wednesday, 6 June 2018 at 13:44:09 UTC, Flaze07 wrote:
 sort( arr.Range );
 don't work, it says cannot pass RangeT!(Array!uint) as 
 function argument
Range is the type, you want the value I think you can do sort(arr[]) maybe
I see why it works, so, [] is called slice operator right ? and in https://dlang.org/phobos/std_container_array.html#.Array.opSlice it returns range, so that's why it worked
Jun 06 2018
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 07/06/2018 1:58 AM, Flaze07 wrote:
 On Wednesday, 6 June 2018 at 13:46:41 UTC, Adam D. Ruppe wrote:
 On Wednesday, 6 June 2018 at 13:44:09 UTC, Flaze07 wrote:
 sort( arr.Range );
 don't work, it says cannot pass RangeT!(Array!uint) as function argument
Range is the type, you want the value I think you can do sort(arr[]) maybe
I see why it works, so, [] is called slice operator right ? and in https://dlang.org/phobos/std_container_array.html#.Array.opSlice it returns range, so that's why it worked
Yes.
Jun 06 2018
parent reply Flaze07 <christianseiji.cs gmail.com> writes:
On Wednesday, 6 June 2018 at 14:06:54 UTC, rikki cattermole wrote:
 On 07/06/2018 1:58 AM, Flaze07 wrote:
 On Wednesday, 6 June 2018 at 13:46:41 UTC, Adam D. Ruppe wrote:
 On Wednesday, 6 June 2018 at 13:44:09 UTC, Flaze07 wrote:
 sort( arr.Range );
 don't work, it says cannot pass RangeT!(Array!uint) as 
 function argument
Range is the type, you want the value I think you can do sort(arr[]) maybe
I see why it works, so, [] is called slice operator right ? and in https://dlang.org/phobos/std_container_array.html#.Array.opSlice it returns range, so that's why it worked
Yes.
hmm, and sorry for asking more, what about removing an element from it ? I found no remove operation that can remove from the middle ( removeAny and removeBack both removes the latest element, linearRemove receive Array!uint...which don't know how to provide )
Jun 06 2018
next sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 07/06/2018 2:20 AM, Flaze07 wrote:
 On Wednesday, 6 June 2018 at 14:06:54 UTC, rikki cattermole wrote:
 On 07/06/2018 1:58 AM, Flaze07 wrote:
 On Wednesday, 6 June 2018 at 13:46:41 UTC, Adam D. Ruppe wrote:
 On Wednesday, 6 June 2018 at 13:44:09 UTC, Flaze07 wrote:
 sort( arr.Range );
 don't work, it says cannot pass RangeT!(Array!uint) as function 
 argument
Range is the type, you want the value I think you can do sort(arr[]) maybe
I see why it works, so, [] is called slice operator right ? and in https://dlang.org/phobos/std_container_array.html#.Array.opSlice it returns range, so that's why it worked
Yes.
hmm, and sorry for asking more, what about removing an element from it ? I found no remove operation that can remove from the middle ( removeAny and removeBack both removes the latest element, linearRemove receive Array!uint...which  don't know how to provide )
filter will remove any and all occurrences of whatever you tell it to. But only in the range not the origin data structure.
Jun 06 2018
parent reply Flaze07 <christianseiji.cs gmail.com> writes:
On Wednesday, 6 June 2018 at 14:24:15 UTC, rikki cattermole wrote:
 On 07/06/2018 2:20 AM, Flaze07 wrote:
 On Wednesday, 6 June 2018 at 14:06:54 UTC, rikki cattermole 
 wrote:
 On 07/06/2018 1:58 AM, Flaze07 wrote:
 [...]
Yes.
hmm, and sorry for asking more, what about removing an element from it ? I found no remove operation that can remove from the middle ( removeAny and removeBack both removes the latest element, linearRemove receive Array!uint...which  don't know how to provide )
filter will remove any and all occurrences of whatever you tell it to. But only in the range not the origin data structure.
what about removing certain index ?
Jun 06 2018
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 07/06/2018 2:27 AM, Flaze07 wrote:
 On Wednesday, 6 June 2018 at 14:24:15 UTC, rikki cattermole wrote:
 On 07/06/2018 2:20 AM, Flaze07 wrote:
 On Wednesday, 6 June 2018 at 14:06:54 UTC, rikki cattermole wrote:
 On 07/06/2018 1:58 AM, Flaze07 wrote:
 [...]
Yes.
hmm, and sorry for asking more, what about removing an element from it ? I found no remove operation that can remove from the middle ( removeAny and removeBack both removes the latest element, linearRemove receive Array!uint...which  don't know how to provide )
filter will remove any and all occurrences of whatever you tell it to. But only in the range not the origin data structure.
what about removing certain index ?
Indexes and ranges don't usually go together.
Jun 06 2018
parent Flaze07 <christianseiji.cs gmail.com> writes:
On Wednesday, 6 June 2018 at 14:29:28 UTC, rikki cattermole wrote:
 On 07/06/2018 2:27 AM, Flaze07 wrote:
 On Wednesday, 6 June 2018 at 14:24:15 UTC, rikki cattermole 
 wrote:
 On 07/06/2018 2:20 AM, Flaze07 wrote:
 On Wednesday, 6 June 2018 at 14:06:54 UTC, rikki cattermole 
 wrote:
 [...]
hmm, and sorry for asking more, what about removing an element from it ? I found no remove operation that can remove from the middle ( removeAny and removeBack both removes the latest element, linearRemove receive Array!uint...which  don't know how to provide )
filter will remove any and all occurrences of whatever you tell it to. But only in the range not the origin data structure.
what about removing certain index ?
Indexes and ranges don't usually go together.
welp, ok then, thank you
Jun 06 2018
prev sibling next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/6/18 10:20 AM, Flaze07 wrote:
 On Wednesday, 6 June 2018 at 14:06:54 UTC, rikki cattermole wrote:
 On 07/06/2018 1:58 AM, Flaze07 wrote:
 On Wednesday, 6 June 2018 at 13:46:41 UTC, Adam D. Ruppe wrote:
 On Wednesday, 6 June 2018 at 13:44:09 UTC, Flaze07 wrote:
 sort( arr.Range );
 don't work, it says cannot pass RangeT!(Array!uint) as function 
 argument
Range is the type, you want the value I think you can do sort(arr[]) maybe
I see why it works, so, [] is called slice operator right ? and in https://dlang.org/phobos/std_container_array.html#.Array.opSlice it returns range, so that's why it worked
Yes.
hmm, and sorry for asking more, what about removing an element from it ? I found no remove operation that can remove from the middle ( removeAny and removeBack both removes the latest element, linearRemove receive Array!uint...which  don't know how to provide )
To remove element 5, for example: arr.linearRemove(arr[5 .. 6]); -Steve
Jun 06 2018
prev sibling parent reply ag0aep6g <anonymous example.com> writes:
On 06/06/2018 04:20 PM, Flaze07 wrote:
 hmm, and sorry for asking more, what about removing an element from it ? 
 I found no remove operation that can remove from the middle ( removeAny 
 and removeBack both removes the latest element, linearRemove receive 
 Array!uint...which  don't know how to provide )
I think removeKey would be the container primitive for that. I don't know if there's a reason why it isn't implemented for Array. Maybe it's just an oversight. You can use linearRemove like this: ---- import std.container.array: Array; import std.stdio: writeln; void main() { Array!int a = [1, 2, 100, 200, 300, 3, 4]; a.linearRemove(a[2 .. 5]); /* Removes elements at indices 2, 3, and 4. */ writeln(a[]); /* Prints "[1, 2, 3, 4]". */ } ----
Jun 06 2018
parent reply Flaze07 <christianseiji.cs gmail.com> writes:
On Wednesday, 6 June 2018 at 14:46:56 UTC, ag0aep6g wrote:
 On 06/06/2018 04:20 PM, Flaze07 wrote:
 hmm, and sorry for asking more, what about removing an element 
 from it ? I found no remove operation that can remove from the 
 middle ( removeAny and removeBack both removes the latest 
 element, linearRemove receive Array!uint...which  don't know 
 how to provide )
I think removeKey would be the container primitive for that. I don't know if there's a reason why it isn't implemented for Array. Maybe it's just an oversight. You can use linearRemove like this: ---- import std.container.array: Array; import std.stdio: writeln; void main() { Array!int a = [1, 2, 100, 200, 300, 3, 4]; a.linearRemove(a[2 .. 5]); /* Removes elements at indices 2, 3, and 4. */ writeln(a[]); /* Prints "[1, 2, 3, 4]". */ } ----
ah...well thank you, well...I did finds another way, but it is probably better to use linearRemove I used arr = make!( Array!uint )( remove( arr[], 2 ); so linearRemove is probably better
Jun 08 2018
parent ag0aep6g <anonymous example.com> writes:
On 06/08/2018 10:52 AM, Flaze07 wrote:
 ah...well thank you, well...I did finds another way, but it is probably 
 better to use linearRemove
 I used
 arr = make!( Array!uint )( remove( arr[], 2 );
 so linearRemove is probably better
Instead of creating a new array, you could update the length of the existing one: arr.length = remove(arr[], 2).length; But linearRemove is probably clearer.
Jun 08 2018