www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Array Slice Intention?

reply "Charlie Patterson" <charliep1 excite.com> writes:
I'm curious as to the intention of array slicing 
(http://www.digitalmars.com/d/arrays.html#slicing).  Did it just fall out as 
an easy feature to implement or is there some coding style I'm not thinking 
of?  I noticed it on the comparison chart and no other language had it.
Feb 16 2005
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Charlie Patterson wrote:
 I'm curious as to the intention of array slicing 
 (http://www.digitalmars.com/d/arrays.html#slicing).  Did it just fall out as 
 an easy feature to implement or is there some coding style I'm not thinking 
 of? 
Array slices have their practical uses. Inserting/removing items in a list, sorting algorithms, string manipulation, no doubt many more....
 I noticed it on the comparison chart and no other language had it.
Well, none of the languages on the chart on the D site, but most of the ones added in the Wiki4D version have it. So does Fortran 90. And most if not all BASICs have string slicing in some form, but I'm not sure of any that generalise to array slices. Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Feb 16 2005
next sibling parent reply "Charlie Patterson" <charliep1 excite.com> writes:
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message 
news:cv0011$lcs$1 digitaldaemon.com...
 Charlie Patterson wrote:
 I'm curious as to the intention of array slicing 
 (http://www.digitalmars.com/d/arrays.html#slicing).  Did it just fall out 
 as an easy feature to implement or is there some coding style I'm not 
 thinking of?
Array slices have their practical uses. Inserting/removing items in a list, sorting algorithms, string manipulation, no doubt many more....
Sounds interesting. Care to explain? It appears a slice is a live reference to the original array. Is this considered a side effect or a good thing? That is, am I trying to save memory or am I trying to create a live cross-cut? And do all arrays need to carry the burden of potential slicing? int[] b = a[1..3]; Does that mean that b, and all arrays, must contain a linked list of slice structures which each hold a reference into another array, the number of elements, and a reference? And can I take a slice of a slice? int[] b = a[1..3]; b[2] = c[1..3]; // b[] == a[1],a[2],c[1],c[2] ? int[] d = b[1..3] // ?!?
Feb 16 2005
next sibling parent pragma <pragma_member pathlink.com> writes:
In article <cv04ri$q63$1 digitaldaemon.com>, Charlie Patterson says...
And do all arrays need to carry the burden of potential slicing?

    int[] b = a[1..3];
Yes, all arrays can be sliced. No, there is no extra burden placed on the array structure (read on).
Does that mean that b, and all arrays, must contain a linked list of slice 
structures which each hold a reference into another array, the number of 
elements, and a reference?  And can I take a slice of a slice?

    int[] b = a[1..3];
    b[2] = c[1..3];
    // b[] == a[1],a[2],c[1],c[2] ?
    int[] d = b[1..3] // ?!?
The trick here is that D makes all this possible via Garbage Collection. http://www.digitalmars.com/d/garbage.html In an non-GC style language (like C/C++) you can't achieve slicing like this without the kind of tracking mechanism you mentioned. D pulls this off without such tricks because its GC Engine "knows" what blocks of memory are referenced, and in use, it keeps them around so slices and such don't become invalid. Using your exmaple: once all references to a[], and references to slices of a[], leave the current scope, a[] becomes 'collectable' and will eventually be disposed by the GC. No fuss, no muss. Just trust the GC and code to you heart's content. - EricAnderton at yahoo
Feb 16 2005
prev sibling parent Norbert Nemec <Norbert Nemec-online.de> writes:
Charlie Patterson schrieb:
 "Stewart Gordon" <smjg_1998 yahoo.com> wrote in message 
 news:cv0011$lcs$1 digitaldaemon.com...
 
Charlie Patterson wrote:

I'm curious as to the intention of array slicing 
(http://www.digitalmars.com/d/arrays.html#slicing).  Did it just fall out 
as an easy feature to implement or is there some coding style I'm not 
thinking of?
Array slices have their practical uses. Inserting/removing items in a list, sorting algorithms, string manipulation, no doubt many more....
Sounds interesting. Care to explain? It appears a slice is a live reference to the original array. Is this considered a side effect or a good thing? That is, am I trying to save memory or am I trying to create a live cross-cut? And do all arrays need to carry the burden of potential slicing? int[] b = a[1..3]; Does that mean that b, and all arrays, must contain a linked list of slice structures which each hold a reference into another array, the number of elements, and a reference? And can I take a slice of a slice? int[] b = a[1..3]; b[2] = c[1..3]; // b[] == a[1],a[2],c[1],c[2] ? int[] d = b[1..3] // ?!?
I think, there is a basic misunderstanding here: int[] b = a[1..3]; means that b points to a slice of the physical data of a (no overhead here - each array consists of ptr and length, so does a slice) c[1..3] = a[1..3]; here, you do some actual copying of data. b[2] = c[1..3]; this is not possible. 'Slicing in' - as it exists in some other languages like Python or Matlab - does not work in D. Each array always refers to a continuous block of memory. Everything else would seriously degrade performance.
Feb 16 2005
prev sibling parent reply "Dawid Toton" <dad o2.pl> writes:
 Array slices have their practical uses.  Inserting/removing items in a
 list, sorting algorithms, string manipulation, no doubt many more....
These operations are performed on collecitons. Why arrays are favored? I want to slice any list, ordered set and so on. Do you assume everything can be done in arrays?
Feb 16 2005
parent "Ben Hinkle" <bhinkle mathworks.com> writes:
"Dawid Toton" <dad o2.pl> wrote in message 
news:cv06t2$sma$1 digitaldaemon.com...
 Array slices have their practical uses.  Inserting/removing items in a
 list, sorting algorithms, string manipulation, no doubt many more....
These operations are performed on collecitons. Why arrays are favored? I want to slice any list, ordered set and so on. Do you assume everything can be done in arrays?
Linked lists and sorted sets are provided by user libraries. So far examples of such libraries are DTL (http://www.synsoft.org/d/code/dtl_0_2_1.zip) and MinTL (http://home.comcast.net/~benhinkle/mintl/). There are probably other places that have a List and such so people should add more to the list if they know of more. See the digitalmars.d.dtl newsgroup for info. -Ben
Feb 16 2005