www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - genetically modified slices - is it possible?

reply Alexandr Druzhinin <drug2004 bk.ru> writes:
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
Nov 15 2013
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
Nov 15 2013
parent reply Alexandr Druzhinin <drug2004 bk.ru> writes:
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.
Nov 15 2013
parent "QAston" <qaston gmail.com> writes:
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.
Nov 15 2013
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Nov 15 2013
parent reply Alexandr Druzhinin <drug2004 bk.ru> writes:
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?
Nov 15 2013
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Nov 16 2013
parent Alexandr Druzhinin <drug2004 bk.ru> writes:
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.
Nov 16 2013
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Nov 16 2013
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Nov 16 2013