Jump to page: 1 2
Thread overview
Slicing vs memcpy
Nov 21, 2005
John C
Nov 21, 2005
kris
Nov 21, 2005
John C
Nov 22, 2005
Stewart Gordon
Nov 22, 2005
kris
Nov 22, 2005
BCS
Nov 23, 2005
John C
Nov 23, 2005
Stewart Gordon
Nov 21, 2005
David L. Davis
Nov 21, 2005
John C
Nov 21, 2005
Chris
Nov 22, 2005
David L. Davis
Nov 22, 2005
Chris
Nov 21, 2005
Chris
Nov 21, 2005
BCS
Alternate slicing syntax (was: Slicing vs memcpy)
Nov 21, 2005
Derek Parnell
November 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.


November 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)
November 21, 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
November 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.


November 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
November 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


November 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.


November 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
November 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
November 22, 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
« First   ‹ Prev
1 2