digitalmars.D.learn - genetically modified slices - is it possible?
- Alexandr Druzhinin (5/5) Nov 15 2013 I'd like to get slice that's consist of begining one other slice and end...
- Adam D. Ruppe (7/7) Nov 15 2013 You could make it work like this:
- Alexandr Druzhinin (3/10) Nov 15 2013 Thank you very much! I forget about .ptr.
- QAston (4/18) Nov 15 2013 Ranges in general are not arrays, they're objects - you can't get
- bearophile (10/15) Nov 15 2013 A simple solution is to keep two indexes, and use them to find
- Alexandr Druzhinin (4/7) Nov 15 2013 I did it the first. But then I decided to make it more D-ish and
- Jonathan M Davis (16/25) Nov 16 2013 Well, if you want to get a slice which refers to the same thing as that ...
- Alexandr Druzhinin (3/18) Nov 16 2013 Thanks for reply. It shows that there are different tools for different
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (3/8) Nov 16 2013 std.range.chain
- Jonathan M Davis (4/12) Nov 16 2013 Ah, good point, though that creates a new range, not a slice of the orig...
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
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
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
On Friday, 15 November 2013 at 15:12:23 UTC, Alexandr Druzhinin wrote:15.11.2013 22:09, Adam D. Ruppe пишет:Ranges in general are not arrays, they're objects - you can't get what's in between two objects.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
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/443cd4a1A 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
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
On Friday, November 15, 2013 22:21:03 Alexandr Druzhinin wrote:15.11.2013 22:13, bearophile пишет: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 DavisAlexandr 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 16 2013
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 DavisThanks for reply. It shows that there are different tools for different jobs and I should use appropriate tool for specific job.
Nov 16 2013
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/443cd4a1std.range.chain Ali
Nov 16 2013
On Saturday, November 16, 2013 08:10:30 Ali Çehreli wrote:On 11/15/2013 06:55 AM, Alexandr Druzhinin wrote: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 DavisI'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/443cd4a1std.range.chain
Nov 16 2013