digitalmars.D.learn - Slicing vs memcpy
- John C (9/9) Nov 21 2005 Does anyone have any code for using slicing instead of memcpy for block ...
- kris (3/19) Nov 21 2005 dst[dstOffset .. dstOffset+count] = src[srcOffset .. srcOffset+count];
- John C (2/20) Nov 21 2005 That seems to do the job nicely, thanks Kris.
-
Stewart Gordon
(21/26)
Nov 22 2005
- kris (3/21) Nov 22 2005 Are you saying that slicing does not account for type-widths? If so,
- BCS (2/23) Nov 22 2005 actualy, IRC memcpy is the one that don't account for type-widths
- John C (2/23) Nov 23 2005 So which is it? Either seems to work, at least in the few tests I've don...
- Stewart Gordon (12/34) Nov 23 2005 That's probably because you've only tried it with a type that is one
- David L. Davis (17/26) Nov 21 2005 John C,
- John C (5/46) Nov 21 2005 Thanks for the links, although I'm not sure they fit the bill. Plus they...
- Chris (24/29) Nov 21 2005 I have a useful addition to that template -- indexr(). which is
- David L. Davis (10/39) Nov 21 2005 Thanks Chris!
- Chris (4/8) Nov 21 2005 thanks for the credit! cheers!
- Chris (6/15) Nov 21 2005 I believe array slicing does not actually copy the slice out of the
- BCS (14/18) Nov 21 2005 However the assignment of a slice to another slice does copy the source ...
- Derek Parnell (8/21) Nov 21 2005 Nice! Would be quite helpful to coders. The compiler can do the arithmet...
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:That seems to do the job nicely, thanks Kris.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 wrote:John C wrote:<snip><snip>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:Are you saying that slicing does not account for type-widths? If so, that would surely be a bug ;-)John C wrote:<snip><snip>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];
Nov 22 2005
In article <dlvhl3$26ma$1 digitaldaemon.com>, kris says...Stewart Gordon wrote:actualy, IRC memcpy is the one that don't account for type-widthskris wrote:Are you saying that slicing does not account for type-widths? If so, that would surely be a bug ;-)John C wrote:<snip><snip>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];
Nov 22 2005
"kris" <fu bar.org> wrote in message news:dlvhl3$26ma$1 digitaldaemon.com...Stewart Gordon wrote:So which is it? Either seems to work, at least in the few tests I've done.kris wrote:Are you saying that slicing does not account for type-widths? If so, that would surely be a bug ;-)John C wrote:<snip><snip>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];
Nov 23 2005
John C wrote:"kris" <fu bar.org> wrote in message news:dlvhl3$26ma$1 digitaldaemon.com...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.Stewart Gordon wrote:So which is it? Either seems to work, at least in the few tests I've done.kris wrote:Are you saying that slicing does not account for type-widths? If so, that would surely be a bug ;-)John C wrote:<snip><snip>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];
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...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.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
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: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.htmlPlus 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
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). ChrisHowever 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 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 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 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 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