digitalmars.D.learn - Slicing vs memcpy
- "John C" <johnch_atms hotmail.com> Nov 21 2005
- kris <fu bar.org> Nov 21 2005
- "John C" <johnch_atms hotmail.com> Nov 21 2005
- Stewart Gordon <smjg_1998 yahoo.com> Nov 22 2005
- kris <fu bar.org> Nov 22 2005
- BCS <BCS_member pathlink.com> Nov 22 2005
- "John C" <johnch_atms hotmail.com> Nov 23 2005
- Stewart Gordon <smjg_1998 yahoo.com> Nov 23 2005
- David L. Davis <SpottedTiger yahoo.com> Nov 21 2005
- "John C" <johnch_atms hotmail.com> Nov 21 2005
- Chris <ctlajoie yahoo.com> Nov 21 2005
- David L. Davis <SpottedTiger yahoo.com> Nov 21 2005
- Chris <ctlajoie yahoo.com> Nov 21 2005
- Chris <ctlajoie yahoo.com> Nov 21 2005
- BCS <BCS_member pathlink.com> Nov 21 2005
- Derek Parnell <derek psych.ward> Nov 21 2005
Does anyone have any code for using slicing instead of memcpy for block copy
operations? I won't post my own sorry effort (it generated access
violations).
Here's what I'm currently doing:
memcpy(dest.ptr + destOffset, src.ptr + srcOffset, count);
But I'm sure I should be using slicing. What's the equivalent? I feel very
dumb not being able to work it out myself.
dest[destOffset .. ??] = src[srcOffset .. ??];
Cheers.
Nov 21 2005
John C wrote:Does anyone have any code for using slicing instead of memcpy for block copy operations? I won't post my own sorry effort (it generated access violations). Here's what I'm currently doing: memcpy(dest.ptr + destOffset, src.ptr + srcOffset, count); But I'm sure I should be using slicing. What's the equivalent? I feel very dumb not being able to work it out myself. dest[destOffset .. ??] = src[srcOffset .. ??]; Cheers.
dst[dstOffset .. dstOffset+count] = src[srcOffset .. srcOffset+count]; (note: these cannot be overlapped, and range-checks will usually be applied)
Nov 21 2005
"kris" <fu bar.org> wrote in message news:dlstg5$25o$1 digitaldaemon.com...John C wrote:Does anyone have any code for using slicing instead of memcpy for block copy operations? I won't post my own sorry effort (it generated access violations). Here's what I'm currently doing: memcpy(dest.ptr + destOffset, src.ptr + srcOffset, count); But I'm sure I should be using slicing. What's the equivalent? I feel very dumb not being able to work it out myself. dest[destOffset .. ??] = src[srcOffset .. ??]; Cheers.
dst[dstOffset .. dstOffset+count] = src[srcOffset .. srcOffset+count]; (note: these cannot be overlapped, and range-checks will usually be applied)
That seems to do the job nicely, thanks Kris.
Nov 21 2005
kris wrote:John C wrote:
memcpy(dest.ptr + destOffset, src.ptr + srcOffset, count);
dst[dstOffset .. dstOffset+count] = src[srcOffset .. srcOffset+count];
Almost: size_t items = count / src[0].sizeof; dst[dstOffset .. dstOffset + items] = src[srcOffset .. srcOffset + items]; (In a real application, you'd usually calculate items from scratch rather than convert it from a byte count.)(note: these cannot be overlapped, and range-checks will usually be applied)
memcpy can't handle overlapped slices either. But it's certainly an advantage that slicing does array bounds checking, and another reason to use slicing rather than memcpy. Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- C++ a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Nov 22 2005
Stewart Gordon wrote:kris wrote:John C wrote:
<snip>memcpy(dest.ptr + destOffset, src.ptr + srcOffset, count);
<snip>dst[dstOffset .. dstOffset+count] = src[srcOffset .. srcOffset+count];
Almost: size_t items = count / src[0].sizeof; dst[dstOffset .. dstOffset + items] = src[srcOffset .. srcOffset + items];
Are you saying that slicing does not account for type-widths? If so, that would surely be a bug ;-)
Nov 22 2005
In article <dlvhl3$26ma$1 digitaldaemon.com>, kris says...Stewart Gordon wrote:kris wrote:John C wrote:
<snip>memcpy(dest.ptr + destOffset, src.ptr + srcOffset, count);
<snip>dst[dstOffset .. dstOffset+count] = src[srcOffset .. srcOffset+count];
Almost: size_t items = count / src[0].sizeof; dst[dstOffset .. dstOffset + items] = src[srcOffset .. srcOffset + items];
Are you saying that slicing does not account for type-widths? If so, that would surely be a bug ;-)
actualy, IRC memcpy is the one that don't account for type-widths
Nov 22 2005
"kris" <fu bar.org> wrote in message news:dlvhl3$26ma$1 digitaldaemon.com...Stewart Gordon wrote:kris wrote:John C wrote:
<snip>memcpy(dest.ptr + destOffset, src.ptr + srcOffset, count);
<snip>dst[dstOffset .. dstOffset+count] = src[srcOffset .. srcOffset+count];
Almost: size_t items = count / src[0].sizeof; dst[dstOffset .. dstOffset + items] = src[srcOffset .. srcOffset + items];
Are you saying that slicing does not account for type-widths? If so, that would surely be a bug ;-)
So which is it? Either seems to work, at least in the few tests I've done.
Nov 23 2005
John C wrote:"kris" <fu bar.org> wrote in message news:dlvhl3$26ma$1 digitaldaemon.com...Stewart Gordon wrote:kris wrote:John C wrote:
memcpy(dest.ptr + destOffset, src.ptr + srcOffset, count);
dst[dstOffset .. dstOffset+count] = src[srcOffset .. srcOffset+count];
Almost: size_t items = count / src[0].sizeof; dst[dstOffset .. dstOffset + items] = src[srcOffset .. srcOffset + items];
Are you saying that slicing does not account for type-widths? If so, that would surely be a bug ;-)
So which is it? Either seems to work, at least in the few tests I've done.
That's probably because you've only tried it with a type that is one byte long. Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- C++ a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Nov 23 2005
In article <dlssbt$172$1 digitaldaemon.com>, John C says...Does anyone have any code for using slicing instead of memcpy for block copy operations? I won't post my own sorry effort (it generated access violations). Here's what I'm currently doing: memcpy(dest.ptr + destOffset, src.ptr + srcOffset, count); But I'm sure I should be using slicing. What's the equivalent? I feel very dumb not being able to work it out myself. dest[destOffset .. ??] = src[srcOffset .. ??]; Cheers.
John C, Andrew Fedoniouk in April 2005 created a very neat set of template functions for handling "overlapping slices," in which those posted messages are archived at http://www.digitalmars.com/d/archives/digitalmars/D/22645.html. Plus in July 2005 I've updated it, and have a also added this updated version to my site recently at http://spottedtiger.tripod.com/D_Language/D_Sourcery_XArrayT.html, so just copy and paste it for your own use. Hopefully this is the kind of thing you were looking for. Kudos to Andrew Fedoniouk for having shared these wonderful templated array slicing functions with the D community! :) David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" ------------------------------------------------------------------- MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
Nov 21 2005
"David L. Davis" <SpottedTiger yahoo.com> wrote in message news:dlt0is$52h$1 digitaldaemon.com...In article <dlssbt$172$1 digitaldaemon.com>, John C says...Does anyone have any code for using slicing instead of memcpy for block copy operations? I won't post my own sorry effort (it generated access violations). Here's what I'm currently doing: memcpy(dest.ptr + destOffset, src.ptr + srcOffset, count); But I'm sure I should be using slicing. What's the equivalent? I feel very dumb not being able to work it out myself. dest[destOffset .. ??] = src[srcOffset .. ??]; Cheers.
John C, Andrew Fedoniouk in April 2005 created a very neat set of template functions for handling "overlapping slices," in which those posted messages are archived at http://www.digitalmars.com/d/archives/digitalmars/D/22645.html. Plus in July 2005 I've updated it, and have a also added this updated version to my site recently at http://spottedtiger.tripod.com/D_Language/D_Sourcery_XArrayT.html, so just copy and paste it for your own use. Hopefully this is the kind of thing you were looking for. Kudos to Andrew Fedoniouk for having shared these wonderful templated array slicing functions with the D community! :) David L.
Thanks for the links, although I'm not sure they fit the bill. Plus they use memmove underneath, which, like memcpy, I wanted to avoid - really for no other reason that to explore the built-in array slicing.------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" ------------------------------------------------------------------- MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
Nov 21 2005
On Mon, 21 Nov 2005 17:36:28 +0000 (UTC), David L. Davis <SpottedTiger yahoo.com> wrote:Plus in July 2005 I've updated it, and have a also added this updated version to my site recently at http://spottedtiger.tripod.com/D_Language/D_Sourcery_XArrayT.html, so just copy and paste it for your own use. Hopefully this is the kind of thing you were looking for.
I have a useful addition to that template -- indexr(). which is analogous to indexOf in java: // find first index of range of elements in array, -1 if not found int indexr(in T[] elements, in T[] t) { if(t.length == 0) return 0; int i = elements.length - 1; for(; i >= 0; --i) { if(elements[i] is t[0]) { bit m = true; for(int j=1; j<t.length; ++j) { if(elements[i+j] != t[j]) { m = false; break; } } if(m) break; } } return i; } Chris
Nov 21 2005
In article <loa4o1tr3oo1gmvlb3721gbv4recc36m8j 4ax.com>, Chris says...On Mon, 21 Nov 2005 17:36:28 +0000 (UTC), David L. Davis <SpottedTiger yahoo.com> wrote:Plus in July 2005 I've updated it, and have a also added this updated version to my site recently at http://spottedtiger.tripod.com/D_Language/D_Sourcery_XArrayT.html, so just copy and paste it for your own use. Hopefully this is the kind of thing you were looking for.
I have a useful addition to that template -- indexr(). which is analogous to indexOf in java: // find first index of range of elements in array, -1 if not found int indexr(in T[] elements, in T[] t) { if(t.length == 0) return 0; int i = elements.length - 1; for(; i >= 0; --i) { if(elements[i] is t[0]) { bit m = true; for(int j=1; j<t.length; ++j) { if(elements[i+j] != t[j]) { m = false; break; } } if(m) break; } } return i; } Chris
Thanks Chris! I've added your function indexr() to the XArrayT template functions (http://spottedtiger.tripod.com/D_Language/D_Sourcery_XArrayT.html) for myself and others to use, and gave you credit for it in the modified section. David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" ------------------------------------------------------------------- MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
Nov 21 2005
On Tue, 22 Nov 2005 02:09:20 +0000 (UTC), David L. Davis <SpottedTiger yahoo.com> wrote:Thanks Chris! I've added your function indexr() to the XArrayT template functions (http://spottedtiger.tripod.com/D_Language/D_Sourcery_XArrayT.html) for myself and others to use, and gave you credit for it in the modified section.
thanks for the credit! cheers! Chris
Nov 21 2005
On Mon, 21 Nov 2005 16:24:28 -0000, "John C" <johnch_atms hotmail.com> wrote:Does anyone have any code for using slicing instead of memcpy for block copy operations? I won't post my own sorry effort (it generated access violations). Here's what I'm currently doing: memcpy(dest.ptr + destOffset, src.ptr + srcOffset, count); But I'm sure I should be using slicing. What's the equivalent? I feel very dumb not being able to work it out myself. dest[destOffset .. ??] = src[srcOffset .. ??]; Cheers.
I believe array slicing does not actually copy the slice out of the array, but rather returns a reference to an index within the old array (with your specified length of course). Chris
Nov 21 2005
In article <5l14o1516irorhpca9g2erpbksen6et0p5 4ax.com>, Chris says...
...I believe array slicing does not actually copy the slice out of the array, but rather returns a reference to an index within the old array (with your specified length of course). Chris
However the assignment of a slice to another slice does copy the source into the given segment of memory. On another note, why isn't there a syntax for "a slice of an array starting at N that is M items long". It could look something like Array[n#m] (with # being whatever token is chosen). Several advantages to this come to mind; Shorter/more readable code, faster/more optimized code, if m is const than the length can be error checked at compile time, thus the following wouldn't need a runtime size check array1[var1 # 5] = array2[var2 # 5] it could also be cast as a static array.
Nov 21 2005
On Mon, 21 Nov 2005 18:13:49 +0000 (UTC), BCS wrote:On another note, why isn't there a syntax for "a slice of an array starting at N that is M items long". It could look something like Array[n#m] (with # being whatever token is chosen). Several advantages to this come to mind; Shorter/more readable code, faster/more optimized code, if m is const than the length can be error checked at compile time, thus the following wouldn't need a runtime size check array1[var1 # 5] = array2[var2 # 5] it could also be cast as a static array.
Nice! Would be quite helpful to coders. The compiler can do the arithmetic for us, which is sort one its jobs anyhow. -- Derek (skype: derek.j.parnell) Melbourne, Australia 22/11/2005 10:19:52 AM
Nov 21 2005









"John C" <johnch_atms hotmail.com> 