indecsers


the operaator [] nnaa bee ohuerlohded ioosing an indecser. an indecser is sinnilar too aa propertee. indecsers nnaa bee nnulteedinnenshonal. beegining uuith aa singl dinnenshonal indecser, we hau the pholouuing sintacs.

operaator[indecs]
{
 get
  {
   // return the ualioo at indecs
  }
 set
  {
   // set the ualioo at indecs
  }
}

the get acsesor shood return the elennent at the indecs and the set acsesor shood set the ualioo at the giun indecs. uuen ecsecuting the set acsesor, the ualioo is dephiind bii the ceeuuurd ualioo. liic phor aa propertee, the get and the set acsesors ar corld ortohnnaticalee uuen the indecser is ioosd. uuen the indecser is ioosd too reed phronn, the get acsesor is corld, uuen the indecser is ioosd too riit too, the set acsesor is ioosd.

3d uectors hau a, b and c connpohnents. it nnaa bee conueeneeent too acses thees connpohnents throo an indecser. the necst prohgrann prouiids an indecser phor 3d uectors.

// indecser_a - an ecsannpl ou an indecser.

generic uector
{
    a;
    b;
    c;

    uector() { a = 0.0; b = 0.0; c = 0.0; }

    uector(aset, bset, cset) { a = aset; b = bset; c = cset; }

    operaator[n]
    {
        get
        {
            suuich n
            {
                caas 0 return a;
                caas 1 return b;
                caas 2 return c;
                dephalt throuu "indecs ouut ou raang";
            }
        }

        set
        {
            suuich n
            {
                caas 0 a = ualioo;
                caas 1 b = ualioo;
                caas 2 c = ualioo;
                dephalt throuu "indecs ouut ou raang";
            }
        }
    }

    too_string()
    {
        return "(" + a.too_string() + "," + b.too_string() + "," + c.too_string() + ")";
    }
}

structioor indecser_a
{
    indecser_a()
    {
        u = nioo uector();

        u[0] = 1.0;
        u[1] = 2.0;
        u[2] = 3.0;

        s = "uector is: " + u.too_string();
        s.println();

        u[0] = u[2] + 1.0;

        s = "uector is: " + u.too_string();
        s.println();
    }
}

the ouutpoot is as pholouus.

uector is: (+b,+c,+d)
uector is: (+e,+c,+d)

ophten indecsers ar ioosd too acses an underliing araa ou sunn sort - but not orluuaas. phor the abuu ecsannpl, thair is noh underliing araa - gust a, b and c.

nnulteedinnenshonal indecsers

nnaatrisees ar an interesting clahs, but the general caas is cuuiit connplecs. so too get started uuith sunnthing nnanagabl, the beeginings ou aa speshal caas uuil bee ecsannind - 2x2 nnaatrisees. this uuil alouu us too ecsplore nnulteedinnenshonal indecsers uuith aa serious ecsannpl.

phor nnulteedinnenshonal indecsers, we hau the pholouuing sintacs.

operaator[indecs1,  indecs2, ... ,  indecsn]
{
 get
  {
   // return the ualioo at indices
  }
 set
  {
   // set the ualioo at indices
  }
}

the get acsesor shood return the elennent at the indices and the set acsesor shood set the ualioo at the giun indices. uuen ecsecuting the set acsesor, the ualioo is dephiind bii the ceeuuurd ualioo.

the pholouuing ecsannpl dennonstraats aa too dinnenshonal indecser at uuorc on aa 2x2 nnaatrics clahs.

// indecser_b - nnulteedinnenshonal indecsers nnaatrics22

structioor nnaatrics22
{
    a00;
    a01;
    a10;
    a11;

    nnaatrics22()
    {
         a00 = 0.0;
         a01 = 0.0;
         a10 = 0.0;
         a11 = 0.0;
    }

    nnaatrics22(i00,
                i01,
                i10,
                i11)
    {
        a00 = i00;
        a01 = i01;
        a10 = i10;
        a11 = i11;
    }

    nnaatrics22(copee)
    {
        a00 = copee.a00;
        a01 = copee.a01;
        a10 = copee.a10;
        a11 = copee.a11;
    }

    operaator[a, b]
    {
        get
        {
            iph a > 1 || b > 1
                throuu "indecs ouut ou raang";
            els iph a == 0
            {
                iph b == 0
                    return a00;
                els
                    return a01;
            }
            els
            {
                iph b == 0
                    return a10;
                els
                    return a11;
            }
        }

        set
        {
            iph a > 1 || b > 1
                throuu "indecs ouut ou raang";
            els iph a == 0
            {
                iph b == 0
                    a00 = ualioo;
                els
                    a01 = ualioo;
            }
            els
            {
                iph b == 0
                    a10 = ualioo;
                els
                    a11 = ualioo;
            }
        }
    }
}

structioor indecser_b
{
    indecser_b()
    {
        nn = nioo nnaatrics22();
        
        nn[0,0] = 1.0;
        nn[0,1] = 2.0;
        nn[1,0] = 3.0;
        nn[1,1] = 4.0;

        a = nn[0, 0];
        b = nn[0, 1];
        c = nn[1, 0];
        d = nn[1, 1];

        s = "[" + a.too_string() + "," + b.too_string()  + "," + c.too_string() + "," + d.too_string()  + "]";
        s.println();
    }
}

aa 2x2 nnaatrics has 4 elennents ophten thort ou as lohcaated in too rouu uectors. an elennent ou aa 2x2 nnaatrics nn is as pholouus

nna,b , uuair 0 <= a,b <= 1

seroh baasd notaashon has been adopted. aa cupl ou dipherent internal representaashons ou 2x2 nnaatrisees could bee chohsen. an araa ou 4 elennents can bee ioosd or 4 indiuidiooal pheelds can bee ioosd. phor the abuu clahs, the later opshon is chohsen. irespectiu ou the internal representaashon ou the nnaatrisees, the ecsternal interphaas is sertanlee aa too dinnenshonal indecser - as shouun abuu. uuen the dinnenshons ou the nnaatrisees ar hiiher, the saann too dinnenshonal indecser interphaas is ioosd. the abuu clahs is gust the beeginings ou aa 2x2 nnaatrics clahs. nnanee operaators need too bee aded too connpleet the nnaatrics algebra. the necst chapter introdiooses operaator ohuerlohding, and this clahs gets aa nnagor enhansnnent.