Efficiently convert Base 1 Array to Base 0 Array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AtariPete

    #1

    Efficiently convert Base 1 Array to Base 0 Array?

    Hi all,

    My scenario is as follows:

    * Receive a base 1 array (index starts at 1 instead of 0) of data from
    a COM component .
    * Need to pass this array to a .net component that requires its array
    index to start at 0.
    * As a result, must convert array from base1 to base 0.

    Currently my solution for this is to create a new array and copy the
    contents of is as follows:
    * Create new based 0 array
    * Copy the content of base 1 array to new array by 1)iterating through
    each element in base 1 array and 2)doing assignment to base 0 array.
    * Assign new array to array used by .net component described above.

    My question is as follows:
    Is there a quicker way to convert a base1 array to base0 without
    having to do iteration and assignment? Does something in the .net
    framework already do this?

    Thanks,
    Peter


  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Efficiently convert Base 1 Array to Base 0 Array?

    Hi,

    "AtariPete" <pcnofelt@gmail .com> wrote in message
    news:1139413493 .593948.121860@ f14g2000cwb.goo glegroups.com.. .[color=blue]
    > Hi all,
    >
    > My scenario is as follows:
    >
    > * Receive a base 1 array (index starts at 1 instead of 0) of data from
    > a COM component .
    > * Need to pass this array to a .net component that requires its array
    > index to start at 0.[/color]

    Just for your clarification, the "start" of the array is something
    artificial based on the language used, in reality what you have is a
    continuos chunk of memory divided in N equals parts where each part hold an
    element of the array.
    The index is used to indicate the compiler the offset from the start to get
    the element you want. therefore is "artificial " , what I mean is that to
    access the first element in that very same array you use index 0 or 1
    depending of the language you use.

    IIRC in some old version of pascal (or was basic ) you could even define
    the start index of the array. it could be negative !


    you do not need to create a new array and copy it.



    --
    Ignacio Machin,
    ignacio.machin AT dot.state.fl.us
    Florida Department Of Transportation



    Comment

    • AtariPete

      #3
      Re: Efficiently convert Base 1 Array to Base 0 Array?

      Hi Ingacio,
      [color=blue]
      >Just for your clarification, the "start" of the array is something
      >artificial based on the language used[/color]

      This I get. In memory, two arrays that have a different base index but
      the same content are really not different.

      I feel the issue at hand is that I am working in a managed environment
      where memory address is kept hidden ( I do not want to use "unsafe"
      code). As a result, i have yet to find an elegant a solution.

      Consider two int arrays (int[] x and int[] y) where x has a base index
      of 1 and y has a base index of 0.

      How do i convert from x to y?

      Comment

      • Ignacio Machin \( .NET/ C# MVP \)

        #4
        Re: Efficiently convert Base 1 Array to Base 0 Array?

        Hi,

        "AtariPete" <pcnofelt@gmail .com> wrote in message
        news:1139416764 .200404.33380@z 14g2000cwz.goog legroups.com...[color=blue]
        > Hi Ingacio,[/color]
        artificial based on the language used[color=blue]
        >
        > This I get. In memory, two arrays that have a different base index but
        > the same content are really not different.[/color]

        Nop, there is no thing like a base index, ALL arrays in C# start at index 0
        [color=blue]
        > I feel the issue at hand is that I am working in a managed environment
        > where memory address is kept hidden ( I do not want to use "unsafe"
        > code). As a result, i have yet to find an elegant a solution.[/color]

        You do not need the memory address, when you receive your memory chunk from
        the COM object it will be ready to be accessed as array[0] to get thr first
        element.
        [color=blue]
        > Consider two int arrays (int[] x and int[] y) where x has a base index
        > of 1 and y has a base index of 0.[/color]

        There is no thing as an array with base 1 in C# , once you accept this
        everything will be clearer.

        how are you receiving the array frmo the COM object? can you post the code ?



        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation


        Comment

        • AtariPete

          #5
          Re: Efficiently convert Base 1 Array to Base 0 Array?

          >There is no thing as an array with base 1 in C# , once you accept this[color=blue]
          >everything will be clearer.[/color]

          I know there base 1 arrays do not exist in C#. But they can be handled
          in C#. No?
          [color=blue]
          >how are you receiving the array frmo the COM object?[/color]

          I am using Microsoft's owc10 excel component. I am accessing a range of
          data in an owc instance. This range is returned as an object. This
          object is actually a 1 based object array (object[,]) of objects which
          are actually strings. I am passing this data to a component that
          required the array to be 0 based. This is why I need to convert from 0
          based to 1 based.

          Comment

          • Rocky

            #6
            Re: Efficiently convert Base 1 Array to Base 0 Array?

            Look into Buffer.BlockCop y. It's a lot faster than going through each item
            and assigning it to another array.


            "AtariPete" <pcnofelt@gmail .com> wrote in message
            news:1139418258 .412130.108800@ o13g2000cwo.goo glegroups.com.. .[color=blue][color=green]
            > >There is no thing as an array with base 1 in C# , once you accept this
            >>everything will be clearer.[/color]
            >
            > I know there base 1 arrays do not exist in C#. But they can be handled
            > in C#. No?
            >[color=green]
            >>how are you receiving the array frmo the COM object?[/color]
            >
            > I am using Microsoft's owc10 excel component. I am accessing a range of
            > data in an owc instance. This range is returned as an object. This
            > object is actually a 1 based object array (object[,]) of objects which
            > are actually strings. I am passing this data to a component that
            > required the array to be 0 based. This is why I need to convert from 0
            > based to 1 based.
            >[/color]


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Efficiently convert Base 1 Array to Base 0 Array?

              <"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.mach in AT
              dot.state.fl.us >> wrote:[color=blue][color=green]
              > > This I get. In memory, two arrays that have a different base index but
              > > the same content are really not different.[/color]
              >
              > Nop, there is no thing like a base index, ALL arrays in C# start at index 0[/color]

              Hmm... that's only sort of true.

              1) You can certainly have instances of System.Array (rather than C#
              arrays) with a non-zero base
              2) You can have multidimensiona l arrays (created using
              Array.CreateIns tance) which can be cast to (say) int[,] and used as
              normal, even with non-zero bases

              It's all a little bit odd, unfortunately.

              The C# specification claims that C# arrays (as opposed to .NET arrays)
              are always 0-based, but as I say with multidimensiona l arrays that's
              not always true...

              --
              Jon Skeet - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              If replying to the group, please do not mail me too

              Comment

              • Scott C

                #8
                Re: Efficiently convert Base 1 Array to Base 0 Array?

                Jon Skeet [C# MVP] wrote:[color=blue]
                > <"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.mach in AT
                > dot.state.fl.us >> wrote:
                >[color=green][color=darkred]
                >>>This I get. In memory, two arrays that have a different base index but
                >>>the same content are really not different.[/color]
                >>
                >>Nop, there is no thing like a base index, ALL arrays in C# start at index 0[/color]
                >
                >
                > Hmm... that's only sort of true.
                >[/color]

                In principle, I agree, Jon. But that's not what the OP originally
                wanted. He said he had "(int[] x and int[] y)" which were ONE based
                since they came from (for all intents and purposes) outside CSharp.
                Ignacio's point is that the "base" of an int[] in CSharp is *always*
                zero. So when the OP says that the array is ONE-based makes absolutely
                no sense-- an array doesn't have a sense of "based"-ness.

                I'm not sure I'm making myself understood-- I'll stop there.

                Scott

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Efficiently convert Base 1 Array to Base 0 Array?

                  Scott C <sdcoonce@g_m_a _i_l.com> wrote:[color=blue][color=green][color=darkred]
                  > >>Nop, there is no thing like a base index, ALL arrays in C# start at index 0[/color]
                  > >
                  > > Hmm... that's only sort of true.[/color]
                  >
                  > In principle, I agree, Jon. But that's not what the OP originally
                  > wanted. He said he had "(int[] x and int[] y)" which were ONE based
                  > since they came from (for all intents and purposes) outside CSharp.
                  > Ignacio's point is that the "base" of an int[] in CSharp is *always*
                  > zero. So when the OP says that the array is ONE-based makes absolutely
                  > no sense-- an array doesn't have a sense of "based"-ness.
                  >
                  > I'm not sure I'm making myself understood-- I'll stop there.[/color]

                  An array does, but an int[] doesn't. An int[] is always zero-based, but
                  an int[,] isn't, and a System.Array isn't. I was only commenting for
                  the sake of precision, really :)

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                  If replying to the group, please do not mail me too

                  Comment

                  • Scott C

                    #10
                    Re: Efficiently convert Base 1 Array to Base 0 Array?

                    Jon Skeet [C# MVP] wrote:[color=blue]
                    >
                    > An array does, but an int[] doesn't. An int[] is always zero-based, but
                    > an int[,] isn't, and a System.Array isn't. I was only commenting for
                    > the sake of precision, really :)
                    >[/color]
                    Just to beat a dead horse, an int[] isn't zero based, necessarily. In
                    CSharp it is, and in lots of other languages it is, but it's just a
                    convention. Kind of like calling 32°F freezing. The freezing point of
                    water is no more 32-indexed than int[] is zero indexed, it's just the
                    convention for a country or a compiler. (In this non-zero indexed
                    sense, I would say the USA has more in common with VB, and the rest of
                    the world is more akin to CS.)

                    The question would better be phrased as: given a list of numbers,
                    say 9, 4, 7, 1
                    how do we call these using an index. Since most would agree that it
                    makes sense that then _next_ number is index+1, the only real question
                    is how does the compiler "address" the number 9? --My point is that the
                    "list" doesn't change if 9 is addressed by 0, 2, 1.643 or 5.3e10.

                    OK, I think the horse is now sufficiently dead, I feel I can rest quietly.

                    Scott

                    Comment

                    Working...