Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 15, 2013 genetically modified slices - is it possible? | ||||
---|---|---|---|---|
| ||||
I'd like to get slice that's consist of begining one other slice and end of yet another slice (all slices belong to the same array of course). Is it possible? With iterators it's simple, but I can't manage do it with slices. http://dpaste.dzfl.pl/443cd4a1 |
November 15, 2013 Re: genetically modified slices - is it possible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alexandr Druzhinin | You could make it work like this: auto slice3 = array[ slice1.length + (slice1.ptr - array.ptr) .. (slice2.ptr - array.ptr)]; Since the slices all start into array somewhere, subtracting the pointers gives their start index. |
November 15, 2013 Re: genetically modified slices - is it possible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 15.11.2013 22:09, Adam D. Ruppe пишет:
> You could make it work like this:
>
> auto slice3 = array[
> slice1.length + (slice1.ptr - array.ptr)
> ..
> (slice2.ptr - array.ptr)];
>
>
> Since the slices all start into array somewhere, subtracting the
> pointers gives their start index.
Thank you very much! I forget about .ptr.
What about ranges in general? They haven't .ptr.
|
November 15, 2013 Re: genetically modified slices - is it possible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alexandr Druzhinin | Alexandr Druzhinin:
> I'd like to get slice that's consist of begining one other slice and end of yet another slice (all slices belong to the same array of course). Is it possible? With iterators it's simple, but I can't manage do it with slices.
> http://dpaste.dzfl.pl/443cd4a1
A simple solution is to keep two indexes, and use them to find the slices when you need them.
If you want to keep only slices around, then the third slice could be computed from the other two slices using a small function written by you, using the .ptr pointer of the two slices and some pointer arithmetic, plus some run-time preconditions to assert at run-time they are from the same array.
Bye,
bearophile
|
November 15, 2013 Re: genetically modified slices - is it possible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 15.11.2013 22:13, bearophile пишет:
> Alexandr Druzhinin:
>
> A simple solution is to keep two indexes, and use them to find the
> slices when you need them.
I did it the first. But then I decided to make it more D-ish and stumbled upon the issue. Wasn't it wrong decision and would be better to stay with indices?
|
November 15, 2013 Re: genetically modified slices - is it possible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alexandr Druzhinin | On Friday, 15 November 2013 at 15:12:23 UTC, Alexandr Druzhinin wrote:
> 15.11.2013 22:09, Adam D. Ruppe пишет:
>> You could make it work like this:
>>
>> auto slice3 = array[
>> slice1.length + (slice1.ptr - array.ptr)
>> ..
>> (slice2.ptr - array.ptr)];
>>
>>
>> Since the slices all start into array somewhere, subtracting the
>> pointers gives their start index.
> Thank you very much! I forget about .ptr.
> What about ranges in general? They haven't .ptr.
Ranges in general are not arrays, they're objects - you can't get what's in between two objects.
|
November 16, 2013 Re: genetically modified slices - is it possible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alexandr Druzhinin | On Friday, November 15, 2013 22:21:03 Alexandr Druzhinin wrote:
> 15.11.2013 22:13, bearophile пишет:
> > Alexandr Druzhinin:
> >
> > A simple solution is to keep two indexes, and use them to find the slices when you need them.
>
> I did it the first. But then I decided to make it more D-ish and stumbled upon the issue. Wasn't it wrong decision and would be better to stay with indices?
Well, if you want to get a slice which refers to the same thing as that slice but includes elements which are in what is being sliced but which aren't in that slice, you can only get the slice that you want be reslicing whatever the slices came from. So, that means that you have to have a range which refers to all of the elements that you want your target range to refer to and possibly more and then either slice that range to exactly the elements that you want or pop off elements until you get the range that you want. Doing that generally requires knowing how many elements to pop off or the indices to slice, which basically means that you have to keep track of indices.
In general, it doesn't work very well to increase a range so that it covers more elements. They're designed to be reduced, not increased, and so the more "D" way of doing things with ranges essentially goes against what you're trying to do, forcing you to come at the problem from a different angle. And in this case, it sounds like that probably means keeping track of indices.
- Jonathan M Davis
|
November 16, 2013 Re: genetically modified slices - is it possible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 16.11.2013 18:38, Jonathan M Davis пишет:
>
> Well, if you want to get a slice which refers to the same thing as that slice
> but includes elements which are in what is being sliced but which aren't in
> that slice, you can only get the slice that you want be reslicing whatever the
> slices came from. So, that means that you have to have a range which refers to
> all of the elements that you want your target range to refer to and possibly
> more and then either slice that range to exactly the elements that you want or
> pop off elements until you get the range that you want. Doing that generally
> requires knowing how many elements to pop off or the indices to slice, which
> basically means that you have to keep track of indices.
>
> In general, it doesn't work very well to increase a range so that it covers
> more elements. They're designed to be reduced, not increased, and so the more
> "D" way of doing things with ranges essentially goes against what you're
> trying to do, forcing you to come at the problem from a different angle. And in
> this case, it sounds like that probably means keeping track of indices.
>
> - Jonathan M Davis
>
Thanks for reply. It shows that there are different tools for different jobs and I should use appropriate tool for specific job.
|
November 16, 2013 Re: genetically modified slices - is it possible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alexandr Druzhinin | On 11/15/2013 06:55 AM, Alexandr Druzhinin wrote:
> I'd like to get slice that's consist of begining one other slice and end
> of yet another slice (all slices belong to the same array of course). Is
> it possible? With iterators it's simple, but I can't manage do it with
> slices.
> http://dpaste.dzfl.pl/443cd4a1
std.range.chain
Ali
|
November 16, 2013 Re: genetically modified slices - is it possible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Saturday, November 16, 2013 08:10:30 Ali Çehreli wrote:
> On 11/15/2013 06:55 AM, Alexandr Druzhinin wrote:
> > I'd like to get slice that's consist of begining one other slice and end
> > of yet another slice (all slices belong to the same array of course). Is
> > it possible? With iterators it's simple, but I can't manage do it with
> > slices.
> > http://dpaste.dzfl.pl/443cd4a1
>
> std.range.chain
Ah, good point, though that creates a new range, not a slice of the original, so whether it solves your problem depends on what you're trying to do.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation