Question regarding generics and type constraints

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

    Question regarding generics and type constraints

    I'm new to Generics (years and years of VS.NET 2003 development, but very
    little .NET 2.0+). I have some routines for conversion from RGB to different
    color spaces and back. I would like the routines to support, at least, byte,
    int, and double types without having to create different versions of the
    routines. Is there any way to do this with generics?

    The problem is, if I do something like this:

    public static void RGB_to_YIQ<T>(T[] R, T[] G, T[] B, T[] Y, T[] I, T[] Q)
    {
    Y[index] = 0.299 * R[index] + 0.587 * G[index] + 0.114 * B[index];
    ...
    }

    I get an error: "Operator '*' cannot be applied to operands of type 'double'
    and 'T'"

    Like I said, I'm a newb to the whole generics thing. I read the "Constraint s
    on Type Parameters" stuff using "where" in MSDN, but I'm not sure how I can
    use that, if I can.

    Thanks.



  • Marc Gravell

    #2
    Re: Question regarding generics and type constraints

    There is an issue with using operators with generics.

    If you have .NET 3.5, I have a full and easy solution to this problem here:

    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    (also briefly explains the problem)

    Note the "usage" page, and the download via "MiscUtil" (the two links in the
    opening lines).

    Basically, you can various things involving Operator.Multip ly(x,y),
    Operator.Add(x, y) - you might also need Operator.Conver t<float,T(or
    something) to handle your literals (0.299 etc).

    I'm about to run away for a few hours, but post back if you want more
    info...

    Marc


    Comment

    • Fredo

      #3
      Re: Question regarding generics and type constraints

      Marc,

      Interesting stuff. This code is generally used in performance-critical areas
      and it looks like your code would add a good bit of overhead converting all
      the operators to method calls, though I don't know enough about it to be
      sure.

      If it's going to impede performance significantly, I'll just stick to
      separate versions of the code.



      "Marc Gravell" <marc.gravell@g mail.comwrote in message
      news:OGt9HzlbIH A.1132@TK2MSFTN GP06.phx.gbl...
      There is an issue with using operators with generics.
      >
      If you have .NET 3.5, I have a full and easy solution to this problem
      here:
      >
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      (also briefly explains the problem)
      >
      Note the "usage" page, and the download via "MiscUtil" (the two links in
      the opening lines).
      >
      Basically, you can various things involving Operator.Multip ly(x,y),
      Operator.Add(x, y) - you might also need Operator.Conver t<float,T(or
      something) to handle your literals (0.299 etc).
      >
      I'm about to run away for a few hours, but post back if you want more
      info...
      >
      Marc
      >

      Comment

      • Rene

        #4
        Re: Question regarding generics and type constraints

        Below is a link that may help you understand why you are having this
        problem:






        "Fredo" <fredo@hotmail. comwrote in message
        news:i-OdnQiS0Ocpii7an Z2dnUVZ_tSknZ2d @giganews.com.. .
        I'm new to Generics (years and years of VS.NET 2003 development, but very
        little .NET 2.0+). I have some routines for conversion from RGB to
        different color spaces and back. I would like the routines to support, at
        least, byte, int, and double types without having to create different
        versions of the routines. Is there any way to do this with generics?
        >
        The problem is, if I do something like this:
        >
        public static void RGB_to_YIQ<T>(T[] R, T[] G, T[] B, T[] Y, T[] I, T[] Q)
        {
        Y[index] = 0.299 * R[index] + 0.587 * G[index] + 0.114 * B[index];
        ...
        }
        >
        I get an error: "Operator '*' cannot be applied to operands of type
        'double' and 'T'"
        >
        Like I said, I'm a newb to the whole generics thing. I read the
        "Constraint s on Type Parameters" stuff using "where" in MSDN, but I'm not
        sure how I can use that, if I can.
        >
        Thanks.
        >
        >
        >

        Comment

        • Marc Gravell

          #5
          Re: Question regarding generics and type constraints

          Interesting stuff. This code is generally used in performance-critical areas
          and it looks like your code would add a good bit of overhead converting all
          the operators to method calls, though I don't know enough about it to be
          sure.
          It actually inlines pretty well, especially for non-nullable types -
          but if this is 100% performance critical, then I'd have separate
          Int32, Byte, etc methods; but in all seriousness - the slowdown isn't
          as bad as you might expect, since this the expression is compiled once
          (only) per type and re-used. You lose a bit of time in delegate
          invoke, but thats about it.

          Marc

          Comment

          • Marc Gravell

            #6
            Re: Question regarding generics and type constraints

            "TestDecima l"

            it was just the method name that was wrong; this should have been
            TestDouble; results stand "as is" though.

            Basically, I started with decimal and then moved to double because
            that was in your list; I forgot to rename.

            Comment

            Working...