www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - overlapping slices

reply B.G. <B.G._member pathlink.com> writes:
a[3 .. 20] = a[4 .. 21];  is prohibited,

Slicing is a great idea leaving much space for optimizations done by compiler,
but the above situation is frequent enought, so is there any way to do it other
than a loop?
Apr 28 2005
next sibling parent reply "Walter" <newshound digitalmars.com> writes:
"B.G." <B.G._member pathlink.com> wrote in message
news:d4r4pq$186g$1 digitaldaemon.com...
 a[3 .. 20] = a[4 .. 21];  is prohibited,

 Slicing is a great idea leaving much space for optimizations done by

 but the above situation is frequent enought, so is there any way to do it

 than a loop?

You can use C's memmove().
Apr 28 2005
parent reply B.G. <B.G._member pathlink.com> writes:
In article <d4r5it$19b5$1 digitaldaemon.com>, Walter says...
"B.G." <B.G._member pathlink.com> wrote in message
news:d4r4pq$186g$1 digitaldaemon.com...
 a[3 .. 20] = a[4 .. 21];  is prohibited,

 Slicing is a great idea leaving much space for optimizations done by

 but the above situation is frequent enought, so is there any way to do it

 than a loop?

You can use C's memmove().

Do you mean casting array ref to a pointer and give it to memmove? Is it safe to move object references this way? There are some posts about moveable memory in future gc implementations, etc, that's actually the reason for my concerns with this issue. thanx
Apr 29 2005
parent reply "Walter" <newshound digitalmars.com> writes:
"B.G." <B.G._member pathlink.com> wrote in message
news:d4tb6a$pj0$1 digitaldaemon.com...
 In article <d4r5it$19b5$1 digitaldaemon.com>, Walter says...
"B.G." <B.G._member pathlink.com> wrote in message
news:d4r4pq$186g$1 digitaldaemon.com...
 a[3 .. 20] = a[4 .. 21];  is prohibited,

 Slicing is a great idea leaving much space for optimizations done by

 but the above situation is frequent enought, so is there any way to do



other
 than a loop?

You can use C's memmove().

Do you mean casting array ref to a pointer and give it to memmove?

Yes.
 Is it safe to move object references this way?
 There are some posts about moveable memory in future gc implementations,

 that's actually the reason for my concerns with this issue.

It's safe if the implementation of memmove() will move pointer sized chunks at a time. DMC's will, but there's no guarantee others will.
Apr 29 2005
next sibling parent B. <B._member pathlink.com> writes:
In article <d4ukpk$20s2$1 digitaldaemon.com>, Walter says...
"B.G." <B.G._member pathlink.com> wrote in message
news:d4tb6a$pj0$1 digitaldaemon.com...
 In article <d4r5it$19b5$1 digitaldaemon.com>, Walter says...
"B.G." <B.G._member pathlink.com> wrote in message
news:d4r4pq$186g$1 digitaldaemon.com...
 a[3 .. 20] = a[4 .. 21];  is prohibited,

 Slicing is a great idea leaving much space for optimizations done by

 but the above situation is frequent enought, so is there any way to do



other
 than a loop?

You can use C's memmove().

Do you mean casting array ref to a pointer and give it to memmove?

Yes.
 Is it safe to move object references this way?
 There are some posts about moveable memory in future gc implementations,

 that's actually the reason for my concerns with this issue.

It's safe if the implementation of memmove() will move pointer sized chunks at a time. DMC's will, but there's no guarantee others will.

thanx.
May 02 2005
prev sibling parent reply B.G. <B.G._member pathlink.com> writes:
In article <d4ukpk$20s2$1 digitaldaemon.com>, Walter says...
"B.G." <B.G._member pathlink.com> wrote in message
news:d4tb6a$pj0$1 digitaldaemon.com...
 In article <d4r5it$19b5$1 digitaldaemon.com>, Walter says...
"B.G." <B.G._member pathlink.com> wrote in message
news:d4r4pq$186g$1 digitaldaemon.com...
 a[3 .. 20] = a[4 .. 21];  is prohibited,

 Slicing is a great idea leaving much space for optimizations done by

 but the above situation is frequent enought, so is there any way to do



other
 than a loop?

You can use C's memmove().

Do you mean casting array ref to a pointer and give it to memmove?

Yes.
 Is it safe to move object references this way?
 There are some posts about moveable memory in future gc implementations,

 that's actually the reason for my concerns with this issue.

It's safe if the implementation of memmove() will move pointer sized chunks at a time. DMC's will, but there's no guarantee others will.

thanx.
May 02 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Mon, 2 May 2005 22:29:03 +0000 (UTC), B.G. wrote:

 In article <d4ukpk$20s2$1 digitaldaemon.com>, Walter says...
"B.G." <B.G._member pathlink.com> wrote in message
news:d4tb6a$pj0$1 digitaldaemon.com...
 In article <d4r5it$19b5$1 digitaldaemon.com>, Walter says...
"B.G." <B.G._member pathlink.com> wrote in message
news:d4r4pq$186g$1 digitaldaemon.com...
 a[3 .. 20] = a[4 .. 21];  is prohibited,

 Slicing is a great idea leaving much space for optimizations done by

 but the above situation is frequent enought, so is there any way to do



other
 than a loop?

You can use C's memmove().

Do you mean casting array ref to a pointer and give it to memmove?

Yes.
 Is it safe to move object references this way?
 There are some posts about moveable memory in future gc implementations,

 that's actually the reason for my concerns with this issue.

It's safe if the implementation of memmove() will move pointer sized chunks at a time. DMC's will, but there's no guarantee others will.

thanx.

DMC == Digital Mars C++ Compiler. http://www.digitalmars.com -- Derek Melbourne, Australia 3/05/2005 9:21:11 AM
May 02 2005
parent B.G. <B.G._member pathlink.com> writes:
In article <1hqz8n1ubnxtt$.xm7d74d5jvd5$.dlg 40tude.net>, Derek Parnell says...
On Mon, 2 May 2005 22:29:03 +0000 (UTC), B.G. wrote:

 In article <d4ukpk$20s2$1 digitaldaemon.com>, Walter says...
"B.G." <B.G._member pathlink.com> wrote in message
news:d4tb6a$pj0$1 digitaldaemon.com...
 In article <d4r5it$19b5$1 digitaldaemon.com>, Walter says...
"B.G." <B.G._member pathlink.com> wrote in message
news:d4r4pq$186g$1 digitaldaemon.com...
 a[3 .. 20] = a[4 .. 21];  is prohibited,

 Slicing is a great idea leaving much space for optimizations done by

 but the above situation is frequent enought, so is there any way to do



other
 than a loop?

You can use C's memmove().

Do you mean casting array ref to a pointer and give it to memmove?

Yes.
 Is it safe to move object references this way?
 There are some posts about moveable memory in future gc implementations,

 that's actually the reason for my concerns with this issue.

It's safe if the implementation of memmove() will move pointer sized chunks at a time. DMC's will, but there's no guarantee others will.

thanx.

DMC == Digital Mars C++ Compiler. http://www.digitalmars.com

Oops! Sorry, Walter ;-)))) The forum's most stupid question since ages, probably. Well, digitalmars means only D to me... Seriously - OK, so memmove is not entirely 'nice' solution... I just thought, it would be NOT BAD, to have a single approved cross-platform function for that purpose, with correct implementation everywhere. IMHO it would be more natural to have it in the language core (it's in fact just a special case of slicing, at least from the programmer's point of view), I just don't have an idea how to plug it into the language in an elegant way. Maybe as a further array property? something like arr.overlap(srcIndex, destIndex, length) or whatever...
-- 
Derek
Melbourne, Australia
3/05/2005 9:21:11 AM

May 02 2005
prev sibling parent reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
Instead of

a[3 .. 20] = a[4 .. 21];
a[3] = something;

you may use just

a.ins( 3, something );

if you will include following somewhere:

//|
//| std D array extenders, these should be intrinsic ops for arrays in ideal 
D
//|
template xarrayT(T)
{
  // append array by one element
  void push(inout T[] elements, T v)
      {
        int l = elements.length;
        elements.length = l + 1;
        elements[l] = v;
      }

  // del last element and return it
  T pop(inout T[] elements)
      {
        int l = elements.length - 1;
        assert(l >= 0);
        T t = elements[l];
        elements.length = l;
        return t;
      }

  // insert element into array
  void ins(inout T[] elements, uint at, T v)
      {
        uint l = elements.length;
        if(at >= l) { elements ~= v; return; }
        // for some reasons this does not compile:
        // elements = elements[0..at] ~ v ~ elements[at..elements.length];
        // so we use C way:
        elements.length = l + 1;
        memmove(&elements[at+1],&elements[at],(l-at)*T.sizeof);
        elements[at] = v;
      }

  // insert array into array
  void insr(inout T[] elements, uint at, T[] va )
      {
        uint l = elements.length;
        if(at >= l) { elements ~= va; return; }
        elements.length = l + va.length;
        memmove(&elements[at + va.length],&elements[at],(l-at)*T.sizeof);
        elements[at..at+va.length] = va;
      }

  // del element and return it
  T cut(inout T[] elements, uint at)
      {
        uint l = elements.length - 1;
        if(at >= l) return pop(elements);
        T t = elements[at];
        memmove(&elements[at],&elements[at+1],(l-at)*T.sizeof);
        elements.length = l;
        return t;
      }

  // del range and return its content
  T[] cutr(inout T[] elements, uint at, uint end)
      {
          assert(at <= end);
          if(at == end) return null;
          T[] t = elements[at..end].dup;
          uint l = elements.length - (end - at);
          if(at < l) { elements.length = at; return t; }
          memmove(&elements[at],&elements[end],(l-at)*T.sizeof);
          elements.length = l;
          return t;
      }
  // just del range
  void del(inout T[] elements, int at, int end)
      {
          assert(at <= end);
          if(at == end) return;
          uint l = elements.length - (end - at);
          if(at >= l) { elements.length = at; return; }
          memmove(&elements[at],&elements[end],(l-at)*T.sizeof);
          elements.length = l;
      }

  // find index of the element in array, -1 if not found
  int index(in T[] elements,T t)
      {
       int i = elements.length - 1;
       for(; i >= 0; --i)
          if( elements[i] === t ) break;
       return i;
      }
}

alias xarrayT!(byte).push     push;
alias xarrayT!(ubyte).push    push;
alias xarrayT!(short).push    push;
alias xarrayT!(ushort).push   push;
alias xarrayT!(int).push      push;
alias xarrayT!(uint).push     push;
alias xarrayT!(long).push     push;
alias xarrayT!(ulong).push    push;
alias xarrayT!(float).push    push;
alias xarrayT!(double).push   push;
alias xarrayT!(real).push     push;
alias xarrayT!(ifloat).push   push;
alias xarrayT!(idouble).push  push;
alias xarrayT!(ireal).push    push;
alias xarrayT!(cfloat).push   push;
alias xarrayT!(cdouble).push  push;
alias xarrayT!(creal).push    push;
alias xarrayT!(char).push     push;
alias xarrayT!(wchar).push    push;
alias xarrayT!(dchar).push    push;

alias xarrayT!(byte).pop     pop;
alias xarrayT!(ubyte).pop    pop;
alias xarrayT!(short).pop    pop;
alias xarrayT!(ushort).pop   pop;
alias xarrayT!(int).pop      pop;
alias xarrayT!(uint).pop     pop;
alias xarrayT!(long).pop     pop;
alias xarrayT!(ulong).pop    pop;
alias xarrayT!(float).pop    pop;
alias xarrayT!(double).pop   pop;
alias xarrayT!(real).pop     pop;
alias xarrayT!(ifloat).pop   pop;
alias xarrayT!(idouble).pop  pop;
alias xarrayT!(ireal).pop    pop;
alias xarrayT!(cfloat).pop   pop;
alias xarrayT!(cdouble).pop  pop;
alias xarrayT!(creal).pop    pop;
alias xarrayT!(char).pop     pop;
alias xarrayT!(wchar).pop    pop;
alias xarrayT!(dchar).pop    pop;

alias xarrayT!(byte).ins     ins;
alias xarrayT!(ubyte).ins    ins;
alias xarrayT!(short).ins    ins;
alias xarrayT!(ushort).ins   ins;
alias xarrayT!(int).ins      ins;
alias xarrayT!(uint).ins     ins;
alias xarrayT!(long).ins     ins;
alias xarrayT!(ulong).ins    ins;
alias xarrayT!(float).ins    ins;
alias xarrayT!(double).ins   ins;
alias xarrayT!(real).ins     ins;
alias xarrayT!(ifloat).ins   ins;
alias xarrayT!(idouble).ins  ins;
alias xarrayT!(ireal).ins    ins;
alias xarrayT!(cfloat).ins   ins;
alias xarrayT!(cdouble).ins  ins;
alias xarrayT!(creal).ins    ins;
alias xarrayT!(char).ins     ins;
alias xarrayT!(wchar).ins    ins;
alias xarrayT!(dchar).ins    ins;

alias xarrayT!(byte).insr     insr;
alias xarrayT!(ubyte).insr    insr;
alias xarrayT!(short).insr    insr;
alias xarrayT!(ushort).insr   insr;
alias xarrayT!(int).insr      insr;
alias xarrayT!(uint).insr     insr;
alias xarrayT!(long).insr     insr;
alias xarrayT!(ulong).insr    insr;
alias xarrayT!(float).insr    insr;
alias xarrayT!(double).insr   insr;
alias xarrayT!(real).insr     insr;
alias xarrayT!(ifloat).insr   insr;
alias xarrayT!(idouble).insr  insr;
alias xarrayT!(ireal).insr    insr;
alias xarrayT!(cfloat).insr   insr;
alias xarrayT!(cdouble).insr  insr;
alias xarrayT!(creal).insr    insr;
alias xarrayT!(char).insr     insr;
alias xarrayT!(wchar).insr    insr;
alias xarrayT!(dchar).insr    insr;

alias xarrayT!(byte).cut     cut;
alias xarrayT!(ubyte).cut    cut;
alias xarrayT!(short).cut    cut;
alias xarrayT!(ushort).cut   cut;
alias xarrayT!(int).cut      cut;
alias xarrayT!(uint).cut     cut;
alias xarrayT!(long).cut     cut;
alias xarrayT!(ulong).cut    cut;
alias xarrayT!(float).cut    cut;
alias xarrayT!(double).cut   cut;
alias xarrayT!(real).cut     cut;
alias xarrayT!(ifloat).cut   cut;
alias xarrayT!(idouble).cut  cut;
alias xarrayT!(ireal).cut    cut;
alias xarrayT!(cfloat).cut   cut;
alias xarrayT!(cdouble).cut  cut;
alias xarrayT!(creal).cut    cut;
alias xarrayT!(char).cut     cut;
alias xarrayT!(wchar).cut    cut;
alias xarrayT!(dchar).cut    cut;

alias xarrayT!(byte).cutr     cutr;
alias xarrayT!(ubyte).cutr    cutr;
alias xarrayT!(short).cutr    cutr;
alias xarrayT!(ushort).cutr   cutr;
alias xarrayT!(int).cutr      cutr;
alias xarrayT!(uint).cutr     cutr;
alias xarrayT!(long).cutr     cutr;
alias xarrayT!(ulong).cutr    cutr;
alias xarrayT!(float).cutr    cutr;
alias xarrayT!(double).cutr   cutr;
alias xarrayT!(real).cutr     cutr;
alias xarrayT!(ifloat).cutr   cutr;
alias xarrayT!(idouble).cutr  cutr;
alias xarrayT!(ireal).cutr    cutr;
alias xarrayT!(cfloat).cutr   cutr;
alias xarrayT!(cdouble).cutr  cutr;
alias xarrayT!(creal).cutr    cutr;
alias xarrayT!(char).cutr     cutr;
alias xarrayT!(wchar).cutr    cutr;
alias xarrayT!(dchar).cutr    cutr;

alias xarrayT!(byte).del     del;
alias xarrayT!(ubyte).del    del;
alias xarrayT!(short).del    del;
alias xarrayT!(ushort).del   del;
alias xarrayT!(int).del      del;
alias xarrayT!(uint).del     del;
alias xarrayT!(long).del     del;
alias xarrayT!(ulong).del    del;
alias xarrayT!(float).del    del;
alias xarrayT!(double).del   del;
alias xarrayT!(real).del     del;
alias xarrayT!(ifloat).del   del;
alias xarrayT!(idouble).del  del;
alias xarrayT!(ireal).del    del;
alias xarrayT!(cfloat).del   del;
alias xarrayT!(cdouble).del  del;
alias xarrayT!(creal).del    del;
alias xarrayT!(char).del     del;
alias xarrayT!(wchar).del    del;
alias xarrayT!(dchar).del    del;

alias xarrayT!(byte).index     index;
alias xarrayT!(ubyte).index    index;
alias xarrayT!(short).index    index;
alias xarrayT!(ushort).index   index;
alias xarrayT!(int).index      index;
alias xarrayT!(uint).index     index;
alias xarrayT!(long).index     index;
alias xarrayT!(ulong).index    index;
alias xarrayT!(float).index    index;
alias xarrayT!(double).index   index;
alias xarrayT!(real).index     index;
alias xarrayT!(ifloat).index   index;
alias xarrayT!(idouble).index  index;
alias xarrayT!(ireal).index    index;
alias xarrayT!(cfloat).index   index;
alias xarrayT!(cdouble).index  index;
alias xarrayT!(creal).index    index;
alias xarrayT!(char).index     index;
alias xarrayT!(wchar).index    index;
alias xarrayT!(dchar).index    index;







"B.G." <B.G._member pathlink.com> wrote in message 
news:d4r4pq$186g$1 digitaldaemon.com...
 a[3 .. 20] = a[4 .. 21];  is prohibited,

 Slicing is a great idea leaving much space for optimizations done by 
 compiler,
 but the above situation is frequent enought, so is there any way to do it 
 other
 than a loop?


 

Apr 28 2005
next sibling parent reply B.G. <B.G._member pathlink.com> writes:
In article <d4r6r5$1ane$1 digitaldaemon.com>, Andrew Fedoniouk says...
Instead of

a[3 .. 20] = a[4 .. 21];
a[3] = something;

you may use just

a.ins( 3, something );

if you will include following somewhere:

I was trying to write my own. is this a part of some library like dool, mango, etc? BTW, please correct me If I'm wrong without the aliases at the very bottom, I can't use char[] x x.ins(...) Is that the reason why they are there? If I'm applying those templates to a class, do I have to define aliases for that particular class for the arr.func() syntax to work with the class? Hmmmm, funny, now I don't understand how aliasing works, I didn't know you can say alias xarrayT!(char).index index; alias xarrayT!(wchar).index index; How can two and more different things be mapped to one identifier??? That would mean, for a D compiler aliases are not as simple as identifier mapping. Further, if it's not that simple anyway, wouldn't it be more logical to have a compiler to decide in place which template function to use? Something else... already several times I've encountered the situation when there is some code or someone is speaking about features of D, not present at http://www.digitalmars.com/d/. This makes me think that this language description is out of date, Is there a place where ALL language features including most recent or experimental stuff are documented or at least referenced? For instance I couldn't find anything about - opApply, - $ instead of length for slice syntax, - maybe I'm blind, but the array syntax we speak about is not there too, (a really _great_ feature IMHO)
//|
//| std D array extenders, these should be intrinsic ops for arrays in ideal 
D
//|
template xarrayT(T)
{
  // append array by one element
  void push(inout T[] elements, T v)
      {
        int l = elements.length;
        elements.length = l + 1;
        elements[l] = v;
      }

  // del last element and return it
  T pop(inout T[] elements)
      {
        int l = elements.length - 1;
        assert(l >= 0);
        T t = elements[l];
        elements.length = l;
        return t;
      }

  // insert element into array
  void ins(inout T[] elements, uint at, T v)
      {
        uint l = elements.length;
        if(at >= l) { elements ~= v; return; }
        // for some reasons this does not compile:
        // elements = elements[0..at] ~ v ~ elements[at..elements.length];
        // so we use C way:
        elements.length = l + 1;
        memmove(&elements[at+1],&elements[at],(l-at)*T.sizeof);
        elements[at] = v;
      }

  // insert array into array
  void insr(inout T[] elements, uint at, T[] va )
      {
        uint l = elements.length;
        if(at >= l) { elements ~= va; return; }
        elements.length = l + va.length;
        memmove(&elements[at + va.length],&elements[at],(l-at)*T.sizeof);
        elements[at..at+va.length] = va;
      }

  // del element and return it
  T cut(inout T[] elements, uint at)
      {
        uint l = elements.length - 1;
        if(at >= l) return pop(elements);
        T t = elements[at];
        memmove(&elements[at],&elements[at+1],(l-at)*T.sizeof);
        elements.length = l;
        return t;
      }

  // del range and return its content
  T[] cutr(inout T[] elements, uint at, uint end)
      {
          assert(at <= end);
          if(at == end) return null;
          T[] t = elements[at..end].dup;
          uint l = elements.length - (end - at);
          if(at < l) { elements.length = at; return t; }
          memmove(&elements[at],&elements[end],(l-at)*T.sizeof);
          elements.length = l;
          return t;
      }
  // just del range
  void del(inout T[] elements, int at, int end)
      {
          assert(at <= end);
          if(at == end) return;
          uint l = elements.length - (end - at);
          if(at >= l) { elements.length = at; return; }
          memmove(&elements[at],&elements[end],(l-at)*T.sizeof);
          elements.length = l;
      }

  // find index of the element in array, -1 if not found
  int index(in T[] elements,T t)
      {
       int i = elements.length - 1;
       for(; i >= 0; --i)
          if( elements[i] === t ) break;
       return i;
      }
}

alias xarrayT!(byte).push     push;
alias xarrayT!(ubyte).push    push;
alias xarrayT!(short).push    push;
alias xarrayT!(ushort).push   push;
alias xarrayT!(int).push      push;
alias xarrayT!(uint).push     push;
alias xarrayT!(long).push     push;
alias xarrayT!(ulong).push    push;
alias xarrayT!(float).push    push;
alias xarrayT!(double).push   push;
alias xarrayT!(real).push     push;
alias xarrayT!(ifloat).push   push;
alias xarrayT!(idouble).push  push;
alias xarrayT!(ireal).push    push;
alias xarrayT!(cfloat).push   push;
alias xarrayT!(cdouble).push  push;
alias xarrayT!(creal).push    push;
alias xarrayT!(char).push     push;
alias xarrayT!(wchar).push    push;
alias xarrayT!(dchar).push    push;

alias xarrayT!(byte).pop     pop;
alias xarrayT!(ubyte).pop    pop;
alias xarrayT!(short).pop    pop;
alias xarrayT!(ushort).pop   pop;
alias xarrayT!(int).pop      pop;
alias xarrayT!(uint).pop     pop;
alias xarrayT!(long).pop     pop;
alias xarrayT!(ulong).pop    pop;
alias xarrayT!(float).pop    pop;
alias xarrayT!(double).pop   pop;
alias xarrayT!(real).pop     pop;
alias xarrayT!(ifloat).pop   pop;
alias xarrayT!(idouble).pop  pop;
alias xarrayT!(ireal).pop    pop;
alias xarrayT!(cfloat).pop   pop;
alias xarrayT!(cdouble).pop  pop;
alias xarrayT!(creal).pop    pop;
alias xarrayT!(char).pop     pop;
alias xarrayT!(wchar).pop    pop;
alias xarrayT!(dchar).pop    pop;

alias xarrayT!(byte).ins     ins;
alias xarrayT!(ubyte).ins    ins;
alias xarrayT!(short).ins    ins;
alias xarrayT!(ushort).ins   ins;
alias xarrayT!(int).ins      ins;
alias xarrayT!(uint).ins     ins;
alias xarrayT!(long).ins     ins;
alias xarrayT!(ulong).ins    ins;
alias xarrayT!(float).ins    ins;
alias xarrayT!(double).ins   ins;
alias xarrayT!(real).ins     ins;
alias xarrayT!(ifloat).ins   ins;
alias xarrayT!(idouble).ins  ins;
alias xarrayT!(ireal).ins    ins;
alias xarrayT!(cfloat).ins   ins;
alias xarrayT!(cdouble).ins  ins;
alias xarrayT!(creal).ins    ins;
alias xarrayT!(char).ins     ins;
alias xarrayT!(wchar).ins    ins;
alias xarrayT!(dchar).ins    ins;

alias xarrayT!(byte).insr     insr;
alias xarrayT!(ubyte).insr    insr;
alias xarrayT!(short).insr    insr;
alias xarrayT!(ushort).insr   insr;
alias xarrayT!(int).insr      insr;
alias xarrayT!(uint).insr     insr;
alias xarrayT!(long).insr     insr;
alias xarrayT!(ulong).insr    insr;
alias xarrayT!(float).insr    insr;
alias xarrayT!(double).insr   insr;
alias xarrayT!(real).insr     insr;
alias xarrayT!(ifloat).insr   insr;
alias xarrayT!(idouble).insr  insr;
alias xarrayT!(ireal).insr    insr;
alias xarrayT!(cfloat).insr   insr;
alias xarrayT!(cdouble).insr  insr;
alias xarrayT!(creal).insr    insr;
alias xarrayT!(char).insr     insr;
alias xarrayT!(wchar).insr    insr;
alias xarrayT!(dchar).insr    insr;

alias xarrayT!(byte).cut     cut;
alias xarrayT!(ubyte).cut    cut;
alias xarrayT!(short).cut    cut;
alias xarrayT!(ushort).cut   cut;
alias xarrayT!(int).cut      cut;
alias xarrayT!(uint).cut     cut;
alias xarrayT!(long).cut     cut;
alias xarrayT!(ulong).cut    cut;
alias xarrayT!(float).cut    cut;
alias xarrayT!(double).cut   cut;
alias xarrayT!(real).cut     cut;
alias xarrayT!(ifloat).cut   cut;
alias xarrayT!(idouble).cut  cut;
alias xarrayT!(ireal).cut    cut;
alias xarrayT!(cfloat).cut   cut;
alias xarrayT!(cdouble).cut  cut;
alias xarrayT!(creal).cut    cut;
alias xarrayT!(char).cut     cut;
alias xarrayT!(wchar).cut    cut;
alias xarrayT!(dchar).cut    cut;

alias xarrayT!(byte).cutr     cutr;
alias xarrayT!(ubyte).cutr    cutr;
alias xarrayT!(short).cutr    cutr;
alias xarrayT!(ushort).cutr   cutr;
alias xarrayT!(int).cutr      cutr;
alias xarrayT!(uint).cutr     cutr;
alias xarrayT!(long).cutr     cutr;
alias xarrayT!(ulong).cutr    cutr;
alias xarrayT!(float).cutr    cutr;
alias xarrayT!(double).cutr   cutr;
alias xarrayT!(real).cutr     cutr;
alias xarrayT!(ifloat).cutr   cutr;
alias xarrayT!(idouble).cutr  cutr;
alias xarrayT!(ireal).cutr    cutr;
alias xarrayT!(cfloat).cutr   cutr;
alias xarrayT!(cdouble).cutr  cutr;
alias xarrayT!(creal).cutr    cutr;
alias xarrayT!(char).cutr     cutr;
alias xarrayT!(wchar).cutr    cutr;
alias xarrayT!(dchar).cutr    cutr;

alias xarrayT!(byte).del     del;
alias xarrayT!(ubyte).del    del;
alias xarrayT!(short).del    del;
alias xarrayT!(ushort).del   del;
alias xarrayT!(int).del      del;
alias xarrayT!(uint).del     del;
alias xarrayT!(long).del     del;
alias xarrayT!(ulong).del    del;
alias xarrayT!(float).del    del;
alias xarrayT!(double).del   del;
alias xarrayT!(real).del     del;
alias xarrayT!(ifloat).del   del;
alias xarrayT!(idouble).del  del;
alias xarrayT!(ireal).del    del;
alias xarrayT!(cfloat).del   del;
alias xarrayT!(cdouble).del  del;
alias xarrayT!(creal).del    del;
alias xarrayT!(char).del     del;
alias xarrayT!(wchar).del    del;
alias xarrayT!(dchar).del    del;

alias xarrayT!(byte).index     index;
alias xarrayT!(ubyte).index    index;
alias xarrayT!(short).index    index;
alias xarrayT!(ushort).index   index;
alias xarrayT!(int).index      index;
alias xarrayT!(uint).index     index;
alias xarrayT!(long).index     index;
alias xarrayT!(ulong).index    index;
alias xarrayT!(float).index    index;
alias xarrayT!(double).index   index;
alias xarrayT!(real).index     index;
alias xarrayT!(ifloat).index   index;
alias xarrayT!(idouble).index  index;
alias xarrayT!(ireal).index    index;
alias xarrayT!(cfloat).index   index;
alias xarrayT!(cdouble).index  index;
alias xarrayT!(creal).index    index;
alias xarrayT!(char).index     index;
alias xarrayT!(wchar).index    index;
alias xarrayT!(dchar).index    index;







"B.G." <B.G._member pathlink.com> wrote in message 
news:d4r4pq$186g$1 digitaldaemon.com...
 a[3 .. 20] = a[4 .. 21];  is prohibited,

 Slicing is a great idea leaving much space for optimizations done by 
 compiler,
 but the above situation is frequent enought, so is there any way to do it 
 other
 than a loop?


 


Apr 28 2005
parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
 I was trying to write my own.
 is this a part of some library like dool, mango, etc?

It is part of Harmonia library which is not published yet.
 BTW, please correct me If I'm wrong
 without the aliases at the very bottom, I can't use
 char[] x
 x.ins(...)

 Is that the reason why they are there?

Yes. You are right.
 If I'm applying those templates to a class, do I have to define aliases 
 for that
 particular class for the arr.func() syntax to work with the class?

 Hmmmm, funny, now I don't understand how aliasing works, I didn't know you 
 can
 say

 alias xarrayT!(char).index     index;
 alias xarrayT!(wchar).index    index;

 How can two and more different things be mapped to one identifier???

The same mechanism as with e,g, two declarations index(char c) and index(int i) if I understood Walter correctly.
 That would mean, for a D compiler aliases are not as simple as identifier
 mapping. Further, if it's not that simple anyway, wouldn't it be more 
 logical to
 have a compiler to decide in place which template function to use?

 Something else... already several times I've encountered the situation 
 when
 there is some code or someone is speaking about features of D, not present
 at http://www.digitalmars.com/d/. This makes me think that this language
 description is out of date, Is there a place where ALL language features
 including most recent or experimental stuff are documented or at least
 referenced?

 For instance I couldn't find anything about
 - opApply,
 - $ instead of length for slice syntax,
 - maybe I'm blind, but the array syntax we speak about is not there too,
 (a really _great_ feature IMHO)

$ is experimental alias for 'length'. Seems like excitement about it already vanished. Sic transit gloria mundi. opApply defined here: http://www.digitalmars.com/d/statement.html#foreach Yep. sort of thesaurus (index) in documentation will be extremely nice.
Apr 28 2005
prev sibling parent reply David L. Davis <SpottedTiger yahoo.com> writes:
In article <d4r6r5$1ane$1 digitaldaemon.com>, Andrew Fedoniouk says...
Instead of

a[3 .. 20] = a[4 .. 21];
a[3] = something;

you may use just

a.ins( 3, something );

if you will include following somewhere:


Andrew, thanks for being kind enough to share your xarrayt.d code back in April (of which, I've just now gotten around to checking it out)...hope you don't mind, but I thought I'd post a minor bug fixed / addtional unittest to the code. I'm pretty sure others will find this code you've written as being very useful in their own projects! # /+ # ' Source : XArrayT.d - D array extenders # ' : Template functions to handle array overlapping slices # ' Author : Andrew Fedoniouk <news[at]terrainformatica[dot]com> # ' Created : 28.Apr.05 with D v0.122 # ' Modified : 31.Jul.05 by David L. Davis # ' : Changed one '===' (which is now deprecated) with an 'is', plus # ' : added the missing *memmove() C function, then created a # ' : unittest for all the functions, and complied / tested # ' : under D v0.128. # ' : # ' Reference : digitalmars.D/22651 # ' -------------------------------------------------------------------------- # ' To complie for a unittest: # ' C:\dmd>bin\dmd xarrayt.d -debug=xarrayt -unittest # ' # ' To compile a D program that uses xarrayt.d: # ' C:\dmd>bin\dmd MySource.d xarrayt.d # +/ # module xarrayt; # # debug( xarrayt ) private import std.stdio; # # // Need to import the C memmove() function # extern (C) void *memmove(void*, void*, uint); # # //| # //| std D array extenders, these should be intrinsic # //| ops for arrays in ideal D # //| # template xarrayT(T) # { # // append array by one element # void push(inout T[] elements, T v) # { # int l = elements.length; # elements.length = l + 1; # elements[l] = v; # } # # // del last element and return it # T pop(inout T[] elements) # { # int l = elements.length - 1; # assert(l >= 0); # T t = elements[l]; # elements.length = l; # return t; # } # # // insert element into array # void ins(inout T[] elements, uint at, T v) # { # uint l = elements.length; # if (at >= l) { elements ~= v; return; } # // for some reasons this does not compile: # // elements = elements[0..at] ~ v ~ elements[at..elements.length]; # // so we use C way: # elements.length = l + 1; # memmove(&elements[at + 1],&elements[at],(l - at) * T.sizeof); # elements[at] = v; # } # # // insert array into array # void insr(inout T[] elements, uint at, T[] va ) # { # uint l = elements.length; # if (at >= l) { elements ~= va; return; } # elements.length = l + va.length; # memmove(&elements[at + va.length],&elements[at],(l - at) * T.sizeof); # elements[at..at + va.length] = va; # } # # // del element and return it # T cut(inout T[] elements, uint at) # { # uint l = elements.length - 1; # if (at >= l) return pop(elements); # T t = elements[at]; # memmove(&elements[at],&elements[at + 1],(l - at) * T.sizeof); # elements.length = l; # return t; # } # # // del range and return its content # T[] cutr(inout T[] elements, uint at, uint end) # { # assert(at <= end); # if (at == end) return null; # T[] t = elements[at..end].dup; # uint l = elements.length - (end - at); # //if (at < l) { elements.length = at; return t; } # if (l - at <= 0) { elements.length = at; return t; } # memmove(&elements[at],&elements[end],(l - at) * T.sizeof); # elements.length = l; # return t; # } # # // just del range # void del(inout T[] elements, int at, int end) # { # assert(at <= end); # if (at == end) return; # uint l = elements.length - (end - at); # if (at >= l) { elements.length = at; return; } # memmove(&elements[at],&elements[end],(l - at) * T.sizeof); # elements.length = l; # } # # // find index of the element in array, -1 if not found # int index(in T[] elements,T t) # { # int i = elements.length - 1; # for (; i >= 0; --i) # if ( elements[i] is t ) break; # return i; # } # } # # alias xarrayT!(byte).push push; # alias xarrayT!(ubyte).push push; # alias xarrayT!(short).push push; # alias xarrayT!(ushort).push push; # alias xarrayT!(int).push push; # alias xarrayT!(uint).push push; # alias xarrayT!(long).push push; # alias xarrayT!(ulong).push push; # alias xarrayT!(float).push push; # alias xarrayT!(double).push push; # alias xarrayT!(real).push push; # alias xarrayT!(ifloat).push push; # alias xarrayT!(idouble).push push; # alias xarrayT!(ireal).push push; # alias xarrayT!(cfloat).push push; # alias xarrayT!(cdouble).push push; # alias xarrayT!(creal).push push; # alias xarrayT!(char).push push; # alias xarrayT!(wchar).push push; # alias xarrayT!(dchar).push push; # # alias xarrayT!(byte).pop pop; # alias xarrayT!(ubyte).pop pop; # alias xarrayT!(short).pop pop; # alias xarrayT!(ushort).pop pop; # alias xarrayT!(int).pop pop; # alias xarrayT!(uint).pop pop; # alias xarrayT!(long).pop pop; # alias xarrayT!(ulong).pop pop; # alias xarrayT!(float).pop pop; # alias xarrayT!(double).pop pop; # alias xarrayT!(real).pop pop; # alias xarrayT!(ifloat).pop pop; # alias xarrayT!(idouble).pop pop; # alias xarrayT!(ireal).pop pop; # alias xarrayT!(cfloat).pop pop; # alias xarrayT!(cdouble).pop pop; # alias xarrayT!(creal).pop pop; # alias xarrayT!(char).pop pop; # alias xarrayT!(wchar).pop pop; # alias xarrayT!(dchar).pop pop; # # alias xarrayT!(byte).ins ins; # alias xarrayT!(ubyte).ins ins; # alias xarrayT!(short).ins ins; # alias xarrayT!(ushort).ins ins; # alias xarrayT!(int).ins ins; # alias xarrayT!(uint).ins ins; # alias xarrayT!(long).ins ins; # alias xarrayT!(ulong).ins ins; # alias xarrayT!(float).ins ins; # alias xarrayT!(double).ins ins; # alias xarrayT!(real).ins ins; # alias xarrayT!(ifloat).ins ins; # alias xarrayT!(idouble).ins ins; # alias xarrayT!(ireal).ins ins; # alias xarrayT!(cfloat).ins ins; # alias xarrayT!(cdouble).ins ins; # alias xarrayT!(creal).ins ins; # alias xarrayT!(char).ins ins; # alias xarrayT!(wchar).ins ins; # alias xarrayT!(dchar).ins ins; # # alias xarrayT!(byte).insr insr; # alias xarrayT!(ubyte).insr insr; # alias xarrayT!(short).insr insr; # alias xarrayT!(ushort).insr insr; # alias xarrayT!(int).insr insr; # alias xarrayT!(uint).insr insr; # alias xarrayT!(long).insr insr; # alias xarrayT!(ulong).insr insr; # alias xarrayT!(float).insr insr; # alias xarrayT!(double).insr insr; # alias xarrayT!(real).insr insr; # alias xarrayT!(ifloat).insr insr; # alias xarrayT!(idouble).insr insr; # alias xarrayT!(ireal).insr insr; # alias xarrayT!(cfloat).insr insr; # alias xarrayT!(cdouble).insr insr; # alias xarrayT!(creal).insr insr; # alias xarrayT!(char).insr insr; # alias xarrayT!(wchar).insr insr; # alias xarrayT!(dchar).insr insr; # # alias xarrayT!(byte).cut cut; # alias xarrayT!(ubyte).cut cut; # alias xarrayT!(short).cut cut; # alias xarrayT!(ushort).cut cut; # alias xarrayT!(int).cut cut; # alias xarrayT!(uint).cut cut; # alias xarrayT!(long).cut cut; # alias xarrayT!(ulong).cut cut; # alias xarrayT!(float).cut cut; # alias xarrayT!(double).cut cut; # alias xarrayT!(real).cut cut; # alias xarrayT!(ifloat).cut cut; # alias xarrayT!(idouble).cut cut; # alias xarrayT!(ireal).cut cut; # alias xarrayT!(cfloat).cut cut; # alias xarrayT!(cdouble).cut cut; # alias xarrayT!(creal).cut cut; # alias xarrayT!(char).cut cut; # alias xarrayT!(wchar).cut cut; # alias xarrayT!(dchar).cut cut; # # alias xarrayT!(byte).cutr cutr; # alias xarrayT!(ubyte).cutr cutr; # alias xarrayT!(short).cutr cutr; # alias xarrayT!(ushort).cutr cutr; # alias xarrayT!(int).cutr cutr; # alias xarrayT!(uint).cutr cutr; # alias xarrayT!(long).cutr cutr; # alias xarrayT!(ulong).cutr cutr; # alias xarrayT!(float).cutr cutr; # alias xarrayT!(double).cutr cutr; # alias xarrayT!(real).cutr cutr; # alias xarrayT!(ifloat).cutr cutr; # alias xarrayT!(idouble).cutr cutr; # alias xarrayT!(ireal).cutr cutr; # alias xarrayT!(cfloat).cutr cutr; # alias xarrayT!(cdouble).cutr cutr; # alias xarrayT!(creal).cutr cutr; # alias xarrayT!(char).cutr cutr; # alias xarrayT!(wchar).cutr cutr; # alias xarrayT!(dchar).cutr cutr; # # alias xarrayT!(byte).del del; # alias xarrayT!(ubyte).del del; # alias xarrayT!(short).del del; # alias xarrayT!(ushort).del del; # alias xarrayT!(int).del del; # alias xarrayT!(uint).del del; # alias xarrayT!(long).del del; # alias xarrayT!(ulong).del del; # alias xarrayT!(float).del del; # alias xarrayT!(double).del del; # alias xarrayT!(real).del del; # alias xarrayT!(ifloat).del del; # alias xarrayT!(idouble).del del; # alias xarrayT!(ireal).del del; # alias xarrayT!(cfloat).del del; # alias xarrayT!(cdouble).del del; # alias xarrayT!(creal).del del; # alias xarrayT!(char).del del; # alias xarrayT!(wchar).del del; # alias xarrayT!(dchar).del del; # # alias xarrayT!(byte).index index; # alias xarrayT!(ubyte).index index; # alias xarrayT!(short).index index; # alias xarrayT!(ushort).index index; # alias xarrayT!(int).index index; # alias xarrayT!(uint).index index; # alias xarrayT!(long).index index; # alias xarrayT!(ulong).index index; # alias xarrayT!(float).index index; # alias xarrayT!(double).index index; # alias xarrayT!(real).index index; # alias xarrayT!(ifloat).index index; # alias xarrayT!(idouble).index index; # alias xarrayT!(ireal).index index; # alias xarrayT!(cfloat).index index; # alias xarrayT!(cdouble).index index; # alias xarrayT!(creal).index index; # alias xarrayT!(char).index index; # alias xarrayT!(wchar).index index; # alias xarrayT!(dchar).index index; # # debug( xarrayt ) # { # void displayLongArray(in long[] la) # { # if (la.length > 1) # for ( int i = 0; i < la.length - 1; i++ ) # writef("%d,", la[i]); # # writefln("%d", la[la.length - 1]); # } # # unittest # { # writefln( "xarryt.unittest started." ); # char[] s; # char[] st = "To Kill a Mocking Bird!"; # char[] sub; # static long[] lsa = [ 10L, 20L, 21L, 25L, 30L, 40L, 45L, 50L ]; # long[] la = lsa; # long[] la2; # int i; # # writefln("String (char[]) Array Test."); # # // Appends one char at a time # for ( int i = 0; i < st.length; i++ ) # s.push(st[i]); # writefln("push() s=\"%s\"", s); # # // Removes the last character # for ( uint i = 0; i < st.length; i++ ) # s.pop(); # writefln("pop() s=\"%s\" (Looped thru to remove all the characters)", s); # # // Inserts one character into a string # s = st; # s.ins(3, 'S'); # writefln("ins() s=\"%s\"", s); # # // Delete a character from [start] to [end] within a string # s.del(3,4); # writefln("del() s=\"%s\"", s); # # // Replace a substring within the string # s.del(3,7); # s.insr(3, "Raise"); # writefln("del() and insr() s=\"%s\"", s); # # // Replace a substring within the string # s = st; # s.del(5,7); # s.insr(5, "ss"); # writefln("del() and insr() s=\"%s\"", s); # # // Cut a substring out of the string and append to it # sub = s.cutr(10, 23); # s ~= "puppy Dog!"; # writefln("cutr() and '~=' s=\"%s\" sub=\"%s\"", s, sub); # # // Get the position of a value # i = s.index('K'); # writefln("index() 'K' found at position %d", i ); # # // Remove a value # s.cut(3); # writefln("cut() 'K' s=\"%s\"", s); # # // Numerical test # //la = [ 10, 20, 21, 25, 30, 40, 45, 50 ]; # writefln(); # writefln("Numerical (long[]) Array Test."); # writef("la[]="); # displayLongArray(la); # # // Add 60 and 70 to the long array # la.push(60); # la.push(70); # writef("push() la[]="); # displayLongArray(la); # # // Removes the last value from the array # la.pop(); # writef("pop() la[]="); # displayLongArray(la); # # // Inserts a new value into the array # la.ins(1, 15); # writef("ins() la[]="); # displayLongArray(la); # # // Removes a value from the array # la.del(3,4); # writef("del() la[]="); # displayLongArray(la); # # // Cut a range of values to another array # la2 = la.cutr(1, 4); # writef("cutr() la[]="); # displayLongArray(la); # writef("la's cut to la2[]="); # displayLongArray(la2); # # // Find and cut value from the original array # i =la.index(45); # writefln("index() value 45 found at position %d", i); # la2.length = 1; # la2[0] = la.cut(i); # writef("cut() la[]="); # displayLongArray(la); # writef("la's cut to la2[]="); # displayLongArray(la2); # } # } # # debug( xarrayt ) # { # int main() # { # std.stdio.writefln( "xarrayt.unittest finished!" ); # return 0; # } # } Output: ------- C:\dmd>dmd xarrayt.d -debug=xarrayt -unittest C:\dmd\bin\..\..\dm\bin\link.exe xarrayt,,,user32+kernel32/noi; C:\dmd>xarrayt xarryt.unittest started. String (char[]) Array Test. push() s="To Kill a Mocking Bird!" pop() s="" (Looped thru to remove all the characters) ins() s="To SKill a Mocking Bird!" del() s="To Kill a Mocking Bird!" del() and insr() s="To Raise a Mocking Bird!" del() and insr() s="To Kiss a Mocking Bird!" cutr() and '~=' s="To Kiss a puppy Dog!" sub="Mocking Bird!" index() 'K' found at position 3 cut() 'K' s="To iss a puppy Dog!" Numerical (long[]) Array Test. la[]=10,20,21,25,30,40,45,50 push() la[]=10,20,21,25,30,40,45,50,60,70 pop() la[]=10,20,21,25,30,40,45,50,60 ins() la[]=10,15,20,21,25,30,40,45,50,60 del() la[]=10,15,20,25,30,40,45,50,60 cutr() la[]=10,30,40,45,50,60 la's cut to la2[]=15,20,25 index() value 45 found at position 3 cut() la[]=10,30,40,50,60 la's cut to la2[]=45 xarrayt.unittest finished! C:\dmd> Thanks again for sharing it, 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
Jul 31 2005
parent Holger <Holger_member pathlink.com> writes:
Seems very useful indeed. Thanks for bringing this to public
attention (again).

Cheers,
Holger


In article <dcj82c$jn0$1 digitaldaemon.com>, David L. Davis says...
In article <d4r6r5$1ane$1 digitaldaemon.com>, Andrew Fedoniouk says...
Instead of

a[3 .. 20] = a[4 .. 21];
a[3] = something;

you may use just

a.ins( 3, something );

if you will include following somewhere:


Andrew, thanks for being kind enough to share your xarrayt.d code back in April (of which, I've just now gotten around to checking it out)...hope you don't mind, but I thought I'd post a minor bug fixed / addtional unittest to the code. I'm pretty sure others will find this code you've written as being very useful in their own projects! # /+ # ' Source : XArrayT.d - D array extenders # ' : Template functions to handle array overlapping slices # ' Author : Andrew Fedoniouk <news[at]terrainformatica[dot]com> # ' Created : 28.Apr.05 with D v0.122 # ' Modified : 31.Jul.05 by David L. Davis # ' : Changed one '===' (which is now deprecated) with an 'is', plus # ' : added the missing *memmove() C function, then created a # ' : unittest for all the functions, and complied / tested # ' : under D v0.128. # ' : # ' Reference : digitalmars.D/22651 # ' -------------------------------------------------------------------------- # ' To complie for a unittest: # ' C:\dmd>bin\dmd xarrayt.d -debug=xarrayt -unittest # ' # ' To compile a D program that uses xarrayt.d: # ' C:\dmd>bin\dmd MySource.d xarrayt.d # +/ # module xarrayt; # # debug( xarrayt ) private import std.stdio; # # // Need to import the C memmove() function # extern (C) void *memmove(void*, void*, uint); # # //| # //| std D array extenders, these should be intrinsic # //| ops for arrays in ideal D # //| # template xarrayT(T) # { # // append array by one element # void push(inout T[] elements, T v) # { # int l = elements.length; # elements.length = l + 1; # elements[l] = v; # } # # // del last element and return it # T pop(inout T[] elements) # { # int l = elements.length - 1; # assert(l >= 0); # T t = elements[l]; # elements.length = l; # return t; # } # # // insert element into array # void ins(inout T[] elements, uint at, T v) # { # uint l = elements.length; # if (at >= l) { elements ~= v; return; } # // for some reasons this does not compile: # // elements = elements[0..at] ~ v ~ elements[at..elements.length]; # // so we use C way: # elements.length = l + 1; # memmove(&elements[at + 1],&elements[at],(l - at) * T.sizeof); # elements[at] = v; # } # # // insert array into array # void insr(inout T[] elements, uint at, T[] va ) # { # uint l = elements.length; # if (at >= l) { elements ~= va; return; } # elements.length = l + va.length; # memmove(&elements[at + va.length],&elements[at],(l - at) * T.sizeof); # elements[at..at + va.length] = va; # } # # // del element and return it # T cut(inout T[] elements, uint at) # { # uint l = elements.length - 1; # if (at >= l) return pop(elements); # T t = elements[at]; # memmove(&elements[at],&elements[at + 1],(l - at) * T.sizeof); # elements.length = l; # return t; # } # # // del range and return its content # T[] cutr(inout T[] elements, uint at, uint end) # { # assert(at <= end); # if (at == end) return null; # T[] t = elements[at..end].dup; # uint l = elements.length - (end - at); # //if (at < l) { elements.length = at; return t; } # if (l - at <= 0) { elements.length = at; return t; } # memmove(&elements[at],&elements[end],(l - at) * T.sizeof); # elements.length = l; # return t; # } # # // just del range # void del(inout T[] elements, int at, int end) # { # assert(at <= end); # if (at == end) return; # uint l = elements.length - (end - at); # if (at >= l) { elements.length = at; return; } # memmove(&elements[at],&elements[end],(l - at) * T.sizeof); # elements.length = l; # } # # // find index of the element in array, -1 if not found # int index(in T[] elements,T t) # { # int i = elements.length - 1; # for (; i >= 0; --i) # if ( elements[i] is t ) break; # return i; # } # } # # alias xarrayT!(byte).push push; # alias xarrayT!(ubyte).push push; # alias xarrayT!(short).push push; # alias xarrayT!(ushort).push push; # alias xarrayT!(int).push push; # alias xarrayT!(uint).push push; # alias xarrayT!(long).push push; # alias xarrayT!(ulong).push push; # alias xarrayT!(float).push push; # alias xarrayT!(double).push push; # alias xarrayT!(real).push push; # alias xarrayT!(ifloat).push push; # alias xarrayT!(idouble).push push; # alias xarrayT!(ireal).push push; # alias xarrayT!(cfloat).push push; # alias xarrayT!(cdouble).push push; # alias xarrayT!(creal).push push; # alias xarrayT!(char).push push; # alias xarrayT!(wchar).push push; # alias xarrayT!(dchar).push push; # # alias xarrayT!(byte).pop pop; # alias xarrayT!(ubyte).pop pop; # alias xarrayT!(short).pop pop; # alias xarrayT!(ushort).pop pop; # alias xarrayT!(int).pop pop; # alias xarrayT!(uint).pop pop; # alias xarrayT!(long).pop pop; # alias xarrayT!(ulong).pop pop; # alias xarrayT!(float).pop pop; # alias xarrayT!(double).pop pop; # alias xarrayT!(real).pop pop; # alias xarrayT!(ifloat).pop pop; # alias xarrayT!(idouble).pop pop; # alias xarrayT!(ireal).pop pop; # alias xarrayT!(cfloat).pop pop; # alias xarrayT!(cdouble).pop pop; # alias xarrayT!(creal).pop pop; # alias xarrayT!(char).pop pop; # alias xarrayT!(wchar).pop pop; # alias xarrayT!(dchar).pop pop; # # alias xarrayT!(byte).ins ins; # alias xarrayT!(ubyte).ins ins; # alias xarrayT!(short).ins ins; # alias xarrayT!(ushort).ins ins; # alias xarrayT!(int).ins ins; # alias xarrayT!(uint).ins ins; # alias xarrayT!(long).ins ins; # alias xarrayT!(ulong).ins ins; # alias xarrayT!(float).ins ins; # alias xarrayT!(double).ins ins; # alias xarrayT!(real).ins ins; # alias xarrayT!(ifloat).ins ins; # alias xarrayT!(idouble).ins ins; # alias xarrayT!(ireal).ins ins; # alias xarrayT!(cfloat).ins ins; # alias xarrayT!(cdouble).ins ins; # alias xarrayT!(creal).ins ins; # alias xarrayT!(char).ins ins; # alias xarrayT!(wchar).ins ins; # alias xarrayT!(dchar).ins ins; # # alias xarrayT!(byte).insr insr; # alias xarrayT!(ubyte).insr insr; # alias xarrayT!(short).insr insr; # alias xarrayT!(ushort).insr insr; # alias xarrayT!(int).insr insr; # alias xarrayT!(uint).insr insr; # alias xarrayT!(long).insr insr; # alias xarrayT!(ulong).insr insr; # alias xarrayT!(float).insr insr; # alias xarrayT!(double).insr insr; # alias xarrayT!(real).insr insr; # alias xarrayT!(ifloat).insr insr; # alias xarrayT!(idouble).insr insr; # alias xarrayT!(ireal).insr insr; # alias xarrayT!(cfloat).insr insr; # alias xarrayT!(cdouble).insr insr; # alias xarrayT!(creal).insr insr; # alias xarrayT!(char).insr insr; # alias xarrayT!(wchar).insr insr; # alias xarrayT!(dchar).insr insr; # # alias xarrayT!(byte).cut cut; # alias xarrayT!(ubyte).cut cut; # alias xarrayT!(short).cut cut; # alias xarrayT!(ushort).cut cut; # alias xarrayT!(int).cut cut; # alias xarrayT!(uint).cut cut; # alias xarrayT!(long).cut cut; # alias xarrayT!(ulong).cut cut; # alias xarrayT!(float).cut cut; # alias xarrayT!(double).cut cut; # alias xarrayT!(real).cut cut; # alias xarrayT!(ifloat).cut cut; # alias xarrayT!(idouble).cut cut; # alias xarrayT!(ireal).cut cut; # alias xarrayT!(cfloat).cut cut; # alias xarrayT!(cdouble).cut cut; # alias xarrayT!(creal).cut cut; # alias xarrayT!(char).cut cut; # alias xarrayT!(wchar).cut cut; # alias xarrayT!(dchar).cut cut; # # alias xarrayT!(byte).cutr cutr; # alias xarrayT!(ubyte).cutr cutr; # alias xarrayT!(short).cutr cutr; # alias xarrayT!(ushort).cutr cutr; # alias xarrayT!(int).cutr cutr; # alias xarrayT!(uint).cutr cutr; # alias xarrayT!(long).cutr cutr; # alias xarrayT!(ulong).cutr cutr; # alias xarrayT!(float).cutr cutr; # alias xarrayT!(double).cutr cutr; # alias xarrayT!(real).cutr cutr; # alias xarrayT!(ifloat).cutr cutr; # alias xarrayT!(idouble).cutr cutr; # alias xarrayT!(ireal).cutr cutr; # alias xarrayT!(cfloat).cutr cutr; # alias xarrayT!(cdouble).cutr cutr; # alias xarrayT!(creal).cutr cutr; # alias xarrayT!(char).cutr cutr; # alias xarrayT!(wchar).cutr cutr; # alias xarrayT!(dchar).cutr cutr; # # alias xarrayT!(byte).del del; # alias xarrayT!(ubyte).del del; # alias xarrayT!(short).del del; # alias xarrayT!(ushort).del del; # alias xarrayT!(int).del del; # alias xarrayT!(uint).del del; # alias xarrayT!(long).del del; # alias xarrayT!(ulong).del del; # alias xarrayT!(float).del del; # alias xarrayT!(double).del del; # alias xarrayT!(real).del del; # alias xarrayT!(ifloat).del del; # alias xarrayT!(idouble).del del; # alias xarrayT!(ireal).del del; # alias xarrayT!(cfloat).del del; # alias xarrayT!(cdouble).del del; # alias xarrayT!(creal).del del; # alias xarrayT!(char).del del; # alias xarrayT!(wchar).del del; # alias xarrayT!(dchar).del del; # # alias xarrayT!(byte).index index; # alias xarrayT!(ubyte).index index; # alias xarrayT!(short).index index; # alias xarrayT!(ushort).index index; # alias xarrayT!(int).index index; # alias xarrayT!(uint).index index; # alias xarrayT!(long).index index; # alias xarrayT!(ulong).index index; # alias xarrayT!(float).index index; # alias xarrayT!(double).index index; # alias xarrayT!(real).index index; # alias xarrayT!(ifloat).index index; # alias xarrayT!(idouble).index index; # alias xarrayT!(ireal).index index; # alias xarrayT!(cfloat).index index; # alias xarrayT!(cdouble).index index; # alias xarrayT!(creal).index index; # alias xarrayT!(char).index index; # alias xarrayT!(wchar).index index; # alias xarrayT!(dchar).index index; # # debug( xarrayt ) # { # void displayLongArray(in long[] la) # { # if (la.length > 1) # for ( int i = 0; i < la.length - 1; i++ ) # writef("%d,", la[i]); # # writefln("%d", la[la.length - 1]); # } # # unittest # { # writefln( "xarryt.unittest started." ); # char[] s; # char[] st = "To Kill a Mocking Bird!"; # char[] sub; # static long[] lsa = [ 10L, 20L, 21L, 25L, 30L, 40L, 45L, 50L ]; # long[] la = lsa; # long[] la2; # int i; # # writefln("String (char[]) Array Test."); # # // Appends one char at a time # for ( int i = 0; i < st.length; i++ ) # s.push(st[i]); # writefln("push() s=\"%s\"", s); # # // Removes the last character # for ( uint i = 0; i < st.length; i++ ) # s.pop(); # writefln("pop() s=\"%s\" (Looped thru to remove all the characters)", s); # # // Inserts one character into a string # s = st; # s.ins(3, 'S'); # writefln("ins() s=\"%s\"", s); # # // Delete a character from [start] to [end] within a string # s.del(3,4); # writefln("del() s=\"%s\"", s); # # // Replace a substring within the string # s.del(3,7); # s.insr(3, "Raise"); # writefln("del() and insr() s=\"%s\"", s); # # // Replace a substring within the string # s = st; # s.del(5,7); # s.insr(5, "ss"); # writefln("del() and insr() s=\"%s\"", s); # # // Cut a substring out of the string and append to it # sub = s.cutr(10, 23); # s ~= "puppy Dog!"; # writefln("cutr() and '~=' s=\"%s\" sub=\"%s\"", s, sub); # # // Get the position of a value # i = s.index('K'); # writefln("index() 'K' found at position %d", i ); # # // Remove a value # s.cut(3); # writefln("cut() 'K' s=\"%s\"", s); # # // Numerical test # //la = [ 10, 20, 21, 25, 30, 40, 45, 50 ]; # writefln(); # writefln("Numerical (long[]) Array Test."); # writef("la[]="); # displayLongArray(la); # # // Add 60 and 70 to the long array # la.push(60); # la.push(70); # writef("push() la[]="); # displayLongArray(la); # # // Removes the last value from the array # la.pop(); # writef("pop() la[]="); # displayLongArray(la); # # // Inserts a new value into the array # la.ins(1, 15); # writef("ins() la[]="); # displayLongArray(la); # # // Removes a value from the array # la.del(3,4); # writef("del() la[]="); # displayLongArray(la); # # // Cut a range of values to another array # la2 = la.cutr(1, 4); # writef("cutr() la[]="); # displayLongArray(la); # writef("la's cut to la2[]="); # displayLongArray(la2); # # // Find and cut value from the original array # i =la.index(45); # writefln("index() value 45 found at position %d", i); # la2.length = 1; # la2[0] = la.cut(i); # writef("cut() la[]="); # displayLongArray(la); # writef("la's cut to la2[]="); # displayLongArray(la2); # } # } # # debug( xarrayt ) # { # int main() # { # std.stdio.writefln( "xarrayt.unittest finished!" ); # return 0; # } # } Output: ------- C:\dmd>dmd xarrayt.d -debug=xarrayt -unittest C:\dmd\bin\..\..\dm\bin\link.exe xarrayt,,,user32+kernel32/noi; C:\dmd>xarrayt xarryt.unittest started. String (char[]) Array Test. push() s="To Kill a Mocking Bird!" pop() s="" (Looped thru to remove all the characters) ins() s="To SKill a Mocking Bird!" del() s="To Kill a Mocking Bird!" del() and insr() s="To Raise a Mocking Bird!" del() and insr() s="To Kiss a Mocking Bird!" cutr() and '~=' s="To Kiss a puppy Dog!" sub="Mocking Bird!" index() 'K' found at position 3 cut() 'K' s="To iss a puppy Dog!" Numerical (long[]) Array Test. la[]=10,20,21,25,30,40,45,50 push() la[]=10,20,21,25,30,40,45,50,60,70 pop() la[]=10,20,21,25,30,40,45,50,60 ins() la[]=10,15,20,21,25,30,40,45,50,60 del() la[]=10,15,20,25,30,40,45,50,60 cutr() la[]=10,30,40,45,50,60 la's cut to la2[]=15,20,25 index() value 45 found at position 3 cut() la[]=10,30,40,50,60 la's cut to la2[]=45 xarrayt.unittest finished! C:\dmd> Thanks again for sharing it, 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

Jul 31 2005