"+=" (overloads) with custom array slices on both lhs, and rhs ??

james.p.leblanc james.p.leblanc at gmail.com
Sun Sep 5 20:49:08 UTC 2021


On Sunday, 5 September 2021 at 20:38:29 UTC, Paul Backus wrote:
> On Sunday, 5 September 2021 at 19:43:20 UTC, james.p.leblanc 
> wrote:
>> Dear D-ers,
>>
>> I have constructed a custom array type that works, but is 
>> missing
>> correct functioning on some operator overloads.
>
> [...]
>
>> ```d
>> import std.stdio;
>> import myarray_mod;
>> ```
>
> Please post the source code for `myarray_mod` so that we can 
> reproduce the errors you're seeing.

Hello Paul,

Thanks for having a look ...

James

```d

module myArray_mod;
import std.stdio;

     struct myArray{
        int* ptr;
        size_t length;

        this( int* ptr, size_t length){
           this.ptr = ptr;
           this.length = length;
        }

        myArray opIndex(){
           return this;
        }

        int opIndex(size_t i){
           return ptr[i];
        }

        int[] opIndex( SliceInfo info ){
           return ptr[ info.start .. info.end ];
        }

        void opIndexAssign(int val, size_t i){
           ptr[i] = val;
           return;
        }

        void opIndexAssign(int val, SliceInfo info){
           auto ctr=0;
           foreach( i ; info.start .. info.end ){
              ptr[i] = val;
           }
           return;
        }


        void opIndexAssign(int[] val, SliceInfo info){
           auto ctr=0;
           foreach( i ; info.start .. info.end ){
              ptr[i] = val[ctr++];
           }
           return;
        }

        // 
===========================================================

        void opIndexOpAssign(string op)(int val, int ind){
           writeln("opIndexOpAssign with INTEGER");
           if( (op == "+") || (op == "-") || (op == "*") || (op == 
"/") ){
              mixin(" ptr[ind] " ~ op ~ "= val;");
           }
           return;
        }

        void opIndexOpAssign(string op)(int val, SliceInfo info){
           writeln("opIndexOpAssign with SLICE");
           if( (op == "+") || (op == "-") || (op == "*") || (op == 
"/") ){
              foreach( i ; 0 .. length ){
                 mixin(" ptr[i] " ~ op ~ "= val;");
              }
           }
           return;
        }

        void opIndexOpAssign(string op)(SliceInfo rhs, SliceInfo 
lhs){
           writeln("opIndexOpAssign with LHS SLICE and RHS SLICE 
");
           if( (op == "+") || (op == "-") || (op == "*") || (op == 
"/") ){
              foreach( i ; 0 .. length ){
                 mixin(" ptr[i] " ~ op ~ "= 1;");
              }
           }
           return;
        }

        myArray opBinary(string op)(myArray rhs){
           writeln("opBinary");
              if( (op == "+=") || (op == "-=") || (op == "*=") || 
(op == "/=") ){
                 foreach( i ; 0 .. length ){
                    mixin(" ptr[i] " ~ op ~ " rhs.ptr[i];");
                 }
              }
           return;
        }

        struct SliceInfo{ size_t start, end; }
        SliceInfo opSlice(size_t dim)(size_t start, size_t end){
           return SliceInfo(start, end);
        }

        void toString(scope void delegate(const(char)[]) sink) 
const {
           import std.format;
           sink("[");
           foreach( i ; 0 .. length ){
              formattedWrite( sink, "%s", ptr[i] );
              if( i< length-1 ) sink(", ");
           }
           sink("]");
        }
     }

```




More information about the Digitalmars-d-learn mailing list