Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. short[] to int[] -- Generic? Overload?

short[] to int[] -- Generic? Overload?

Scheduled Pinned Locked Moved C#
performancealgorithmsdata-structureshelptutorial
6 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PhilDanger
    wrote on last edited by
    #1

    Hi, I've inherited a batch of functions whose signatures differ only in the fact that one group takes a short[] as the first parameter and one takes an int[] as the first parameter. I'd like to merge the bulk of the code into one function so that changes don't have to be replicated accross versions (there already is some code difference, but no functional difference!). My two ideas were: to use a generic method, or to have one of the overloaded versions call the other version. The problem with the first (generics) is that I only want it to be able to accept int[] or short[], not some other type, and there are some explicit assignments such as "shortArray[i] = -12345;" that the compiler doesn't appreciate when I try to make it generic. The problem with the second, is that I cannot figure out how to convert from a short[] to an int[] without copying to another array and converting it in the loop (.CopyTo), the arrays are massive, so every time I have to copy is a performance hit (it isn't exactly performance critical at the moment, but I'd like to avoid needless operations right now to save time later during optimization). Any suggestions? Thanks, Phil

    P L 2 Replies Last reply
    0
    • P PhilDanger

      Hi, I've inherited a batch of functions whose signatures differ only in the fact that one group takes a short[] as the first parameter and one takes an int[] as the first parameter. I'd like to merge the bulk of the code into one function so that changes don't have to be replicated accross versions (there already is some code difference, but no functional difference!). My two ideas were: to use a generic method, or to have one of the overloaded versions call the other version. The problem with the first (generics) is that I only want it to be able to accept int[] or short[], not some other type, and there are some explicit assignments such as "shortArray[i] = -12345;" that the compiler doesn't appreciate when I try to make it generic. The problem with the second, is that I cannot figure out how to convert from a short[] to an int[] without copying to another array and converting it in the loop (.CopyTo), the arrays are massive, so every time I have to copy is a performance hit (it isn't exactly performance critical at the moment, but I'd like to avoid needless operations right now to save time later during optimization). Any suggestions? Thanks, Phil

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Use a generic function and then check the Type to make sure that it is a valid type - i.e. throw an exception if it's not a short[] or int[]. Hint - use the is operator.

      Deja View - the feeling that you've seen this post before.

      P 1 Reply Last reply
      0
      • P Pete OHanlon

        Use a generic function and then check the Type to make sure that it is a valid type - i.e. throw an exception if it's not a short[] or int[]. Hint - use the is operator.

        Deja View - the feeling that you've seen this post before.

        P Offline
        P Offline
        PhilDanger
        wrote on last edited by
        #3

        Hi Pete, I'm sure this idea would work (with a bit of effort), but I don't know if it will work for me. Correct me if I'm wrong, but I'd still have to cast it to the appropriate type (short[] or int[]) and then carry around whichever version is the correct one along with flags signifying which to use (or copy it over to another short[] if it is an int[], which brings me to the 2nd problem presented in the original post). Adding an additional type - long, for instance, would require even more logic to be added all over the function to use the correct version. Furthermore, I'd like it to throw a compile time error and not a run-time exception.

        P 1 Reply Last reply
        0
        • P PhilDanger

          Hi Pete, I'm sure this idea would work (with a bit of effort), but I don't know if it will work for me. Correct me if I'm wrong, but I'd still have to cast it to the appropriate type (short[] or int[]) and then carry around whichever version is the correct one along with flags signifying which to use (or copy it over to another short[] if it is an int[], which brings me to the 2nd problem presented in the original post). Adding an additional type - long, for instance, would require even more logic to be added all over the function to use the correct version. Furthermore, I'd like it to throw a compile time error and not a run-time exception.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          All you need to do to support part of your problem is do something like:

          public void Test<T>(IList<T> list)
          {
            if (list.Count > 0)
            {
              Console.WriteLine("Hello");
            }
          }
          

          Then, you can pass in short[] and int[] to your hearts content. It doesn't solve the constraining to short[] and int[] issues, but it does solve the other side (and you don't have to copy the arrays over).

          Deja View - the feeling that you've seen this post before.

          1 Reply Last reply
          0
          • P PhilDanger

            Hi, I've inherited a batch of functions whose signatures differ only in the fact that one group takes a short[] as the first parameter and one takes an int[] as the first parameter. I'd like to merge the bulk of the code into one function so that changes don't have to be replicated accross versions (there already is some code difference, but no functional difference!). My two ideas were: to use a generic method, or to have one of the overloaded versions call the other version. The problem with the first (generics) is that I only want it to be able to accept int[] or short[], not some other type, and there are some explicit assignments such as "shortArray[i] = -12345;" that the compiler doesn't appreciate when I try to make it generic. The problem with the second, is that I cannot figure out how to convert from a short[] to an int[] without copying to another array and converting it in the loop (.CopyTo), the arrays are massive, so every time I have to copy is a performance hit (it isn't exactly performance critical at the moment, but I'd like to avoid needless operations right now to save time later during optimization). Any suggestions? Thanks, Phil

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi Phil, how about this: - build one method DoIt() that accepts IArray as its main operand - define an interface IArray similar to an array, supporting whatever Doit() needs - define a class that implements IArray when given an int[] - build a method that accepts an int[], instantiates said class and calls DoIt() - define a class that implements IArray when given a short[] - build a method that accepts a short[], instantiates said class and calls DoIt() That's it, solved without generics. Would even run on 1.x All this at the expense of a glue layer (the two classes described). :)

            Luc Pattyn


            try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


            P 1 Reply Last reply
            0
            • L Luc Pattyn

              Hi Phil, how about this: - build one method DoIt() that accepts IArray as its main operand - define an interface IArray similar to an array, supporting whatever Doit() needs - define a class that implements IArray when given an int[] - build a method that accepts an int[], instantiates said class and calls DoIt() - define a class that implements IArray when given a short[] - build a method that accepts a short[], instantiates said class and calls DoIt() That's it, solved without generics. Would even run on 1.x All this at the expense of a glue layer (the two classes described). :)

              Luc Pattyn


              try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


              P Offline
              P Offline
              PhilDanger
              wrote on last edited by
              #6

              Yes, perfect! I only need an indexer, so the Interface would be nice and light. Thanks again Luc!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups