Mapping Function

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

    Mapping Function

    Hi all,

    I have the following being defined in a A.cxx file.

    // define in source file. Not exported to the outside world (this
    cannot be
    // moved to the header file [restriction])
    #define CHANNEL_0 0
    #define CHANNEL_1 1
    #define CHANNEL_2 2
    #define CHANNEL_3 3
    #define CHANNEL_4 4
    #define CHANNEL_5 5

    However, in another file B.cxx, I have a fucntion called:

    registerChannel (int channelNumber_)
    {
    // perform registration via channel number (valid range 0 - 5)
    register(channe lNumber_);
    }

    It works fine. However, now there is a new requirements: Channel
    0,2,4 must maps to 0, 1, 2. Other channel maps to an invalid value.

    I was wondering what is the best way to implements the mapping
    function. My idea is as follows:

    // 255 is an invalid value
    #define INVALID_CHANNEL 255
    int ChannelMapArray[] =
    {0, INVALID_CHANNEL , 1, INVALID_CHANNEL , 2, INVALID_VALUE, };


    So I could just use it in registerChannel () as follows:
    registerChannel (int channelNumber_)
    {
    // perform registration via channel number (valid range 0 - 5)
    register(Channe lMapArray[channelNumber_]);
    }


    Is there a better way to implement a mapping function. The array
    implementation seems unclear. Also, is it possible to implement it as
    a 2 ways mapping function (i.e given 1-6 -> 0,1,2,invalid or vice
    versa)? Is there a commonly known technique for this? Is it possible
    to reference the mapping to the #define in A.cxx?

    Thanks All,
    Naruto
  • Denis Remezov

    #2
    Re: Mapping Function

    naruto wrote:[color=blue]
    >
    > Hi all,
    >
    > I have the following being defined in a A.cxx file.
    >
    > // define in source file. Not exported to the outside world (this
    > cannot be
    > // moved to the header file [restriction])[/color]

    A header file does not necessarily mean the outside world. The
    restriction is superficial.

    [color=blue]
    > #define CHANNEL_0 0
    > #define CHANNEL_1 1
    > #define CHANNEL_2 2
    > #define CHANNEL_3 3
    > #define CHANNEL_4 4
    > #define CHANNEL_5 5
    >[/color]

    You might benefit from using enums. In addition, you have two
    different systems of channel values, they could be represented by
    two different enum types to eliminate the confusion as to which
    channel system int channelNumber_ belongs.

    [color=blue]
    > However, in another file B.cxx, I have a fucntion called:
    >
    > registerChannel (int channelNumber_)
    > {
    > // perform registration via channel number (valid range 0 - 5)
    > register(channe lNumber_);
    > }
    >
    > It works fine. However, now there is a new requirements: Channel
    > 0,2,4 must maps to 0, 1, 2. Other channel maps to an invalid value.
    >
    > I was wondering what is the best way to implements the mapping
    > function. My idea is as follows:
    >
    > // 255 is an invalid value
    > #define INVALID_CHANNEL 255
    > int ChannelMapArray[] =
    > {0, INVALID_CHANNEL , 1, INVALID_CHANNEL , 2, INVALID_VALUE, };
    >
    > So I could just use it in registerChannel () as follows:
    > registerChannel (int channelNumber_)
    > {
    > // perform registration via channel number (valid range 0 - 5)
    > register(Channe lMapArray[channelNumber_]);
    > }
    >
    > Is there a better way to implement a mapping function.[/color]

    You've said it: implement a mapping function.

    [color=blue]
    > The array
    > implementation seems unclear. Also, is it possible to implement it as
    > a 2 ways mapping function (i.e given 1-6 -> 0,1,2,invalid or vice
    > versa)? Is there a commonly known technique for this?[/color]


    Something to the effect:

    //I contrived the names; imagined that you have, e.g., "device" and
    //"logical" channels

    class ChannelMapping {
    struct MappingInfo {
    channel_type_lo g channel_log;
    channel_type_de v channel_dev;
    };

    static MappingInfo mapping_info_[];

    public:
    static channel_type_lo g devToLog(channe l_type_dev channel) {...}
    static channel_type_de v logToSrc(channe l_type_log channel) {...}
    };

    Depending on your design you may need not to make these functions static.
    Otherwise you can use a namespace instead of a class. Define mapping_info_
    (give it a better name). Use it directly or write an init() function to
    create an optimised storage for search by the mapping functions for a
    sufficiently large number of values.

    [color=blue]
    > Is it possible
    > to reference the mapping to the #define in A.cxx?[/color]

    Move them to file A_internal_what ever.h

    Denis
    [color=blue]
    >
    > Thanks All,
    > Naruto[/color]

    Comment

    • Denis Remezov

      #3
      Re: Mapping Function

      naruto wrote:[color=blue]
      >
      > Hi all,
      >
      > I have the following being defined in a A.cxx file.
      >
      > // define in source file. Not exported to the outside world (this
      > cannot be
      > // moved to the header file [restriction])[/color]

      A header file does not necessarily mean the outside world. The
      restriction is superficial.

      [color=blue]
      > #define CHANNEL_0 0
      > #define CHANNEL_1 1
      > #define CHANNEL_2 2
      > #define CHANNEL_3 3
      > #define CHANNEL_4 4
      > #define CHANNEL_5 5
      >[/color]

      You might benefit from using enums. In addition, you have two
      different systems of channel values, they could be represented by
      two different enum types to eliminate the confusion as to which
      channel system int channelNumber_ belongs.

      [color=blue]
      > However, in another file B.cxx, I have a fucntion called:
      >
      > registerChannel (int channelNumber_)
      > {
      > // perform registration via channel number (valid range 0 - 5)
      > register(channe lNumber_);
      > }
      >
      > It works fine. However, now there is a new requirements: Channel
      > 0,2,4 must maps to 0, 1, 2. Other channel maps to an invalid value.
      >
      > I was wondering what is the best way to implements the mapping
      > function. My idea is as follows:
      >
      > // 255 is an invalid value
      > #define INVALID_CHANNEL 255
      > int ChannelMapArray[] =
      > {0, INVALID_CHANNEL , 1, INVALID_CHANNEL , 2, INVALID_VALUE, };
      >
      > So I could just use it in registerChannel () as follows:
      > registerChannel (int channelNumber_)
      > {
      > // perform registration via channel number (valid range 0 - 5)
      > register(Channe lMapArray[channelNumber_]);
      > }
      >
      > Is there a better way to implement a mapping function.[/color]

      You've said it: implement a mapping function.

      [color=blue]
      > The array
      > implementation seems unclear. Also, is it possible to implement it as
      > a 2 ways mapping function (i.e given 1-6 -> 0,1,2,invalid or vice
      > versa)? Is there a commonly known technique for this?[/color]


      Something to the effect:

      //I contrived the names; imagined that you have, e.g., "device" and
      //"logical" channels

      class ChannelMapping {
      struct MappingInfo {
      channel_type_lo g channel_log;
      channel_type_de v channel_dev;
      };

      static MappingInfo mapping_info_[];

      public:
      static channel_type_lo g devToLog(channe l_type_dev channel) {...}
      static channel_type_de v logToSrc(channe l_type_log channel) {...}
      };

      Depending on your design you may need not to make these functions static.
      Otherwise you can use a namespace instead of a class. Define mapping_info_
      (give it a better name). Use it directly or write an init() function to
      create an optimised storage for search by the mapping functions for a
      sufficiently large number of values.

      [color=blue]
      > Is it possible
      > to reference the mapping to the #define in A.cxx?[/color]

      Move them to file A_internal_what ever.h

      Denis
      [color=blue]
      >
      > Thanks All,
      > Naruto[/color]

      Comment

      • Thomas Matthews

        #4
        Re: Mapping Function

        naruto wrote:
        [color=blue]
        > Hi all,
        >
        > I have the following being defined in a A.cxx file.
        >
        > // define in source file. Not exported to the outside world (this
        > cannot be
        > // moved to the header file [restriction])
        > #define CHANNEL_0 0
        > #define CHANNEL_1 1
        > #define CHANNEL_2 2
        > #define CHANNEL_3 3
        > #define CHANNEL_4 4
        > #define CHANNEL_5 5
        >
        > However, in another file B.cxx, I have a fucntion called:
        >
        > registerChannel (int channelNumber_)
        > {
        > // perform registration via channel number (valid range 0 - 5)
        > register(channe lNumber_);
        > }
        >
        > It works fine. However, now there is a new requirements: Channel
        > 0,2,4 must maps to 0, 1, 2. Other channel maps to an invalid value.
        >
        > I was wondering what is the best way to implements the mapping
        > function. My idea is as follows:
        >
        > // 255 is an invalid value
        > #define INVALID_CHANNEL 255
        > int ChannelMapArray[] =
        > {0, INVALID_CHANNEL , 1, INVALID_CHANNEL , 2, INVALID_VALUE, };
        >
        >
        > So I could just use it in registerChannel () as follows:
        > registerChannel (int channelNumber_)
        > {
        > // perform registration via channel number (valid range 0 - 5)
        > register(Channe lMapArray[channelNumber_]);
        > }
        >
        >
        > Is there a better way to implement a mapping function. The array
        > implementation seems unclear. Also, is it possible to implement it as
        > a 2 ways mapping function (i.e given 1-6 -> 0,1,2,invalid or vice
        > versa)? Is there a commonly known technique for this? Is it possible
        > to reference the mapping to the #define in A.cxx?
        >
        > Thanks All,
        > Naruto[/color]

        One could always use the std::map container:
        #include <map>
        using std::map;
        typedef map<unsigned int, unsigned int> Mapping_Contain er;

        The mapping would be:
        Mapping_Contain er channel_map;
        //...
        unsigned int original_value;
        unsigned int new_value;

        //...
        new_value = channel_map[original_value];


        --
        Thomas Matthews

        C++ newsgroup welcome message:

        C++ Faq: http://www.parashift.com/c++-faq-lite
        C Faq: http://www.eskimo.com/~scs/c-faq/top.html
        alt.comp.lang.l earn.c-c++ faq:

        Other sites:
        http://www.josuttis.com -- C++ STL Library book

        Comment

        • Thomas Matthews

          #5
          Re: Mapping Function

          naruto wrote:
          [color=blue]
          > Hi all,
          >
          > I have the following being defined in a A.cxx file.
          >
          > // define in source file. Not exported to the outside world (this
          > cannot be
          > // moved to the header file [restriction])
          > #define CHANNEL_0 0
          > #define CHANNEL_1 1
          > #define CHANNEL_2 2
          > #define CHANNEL_3 3
          > #define CHANNEL_4 4
          > #define CHANNEL_5 5
          >
          > However, in another file B.cxx, I have a fucntion called:
          >
          > registerChannel (int channelNumber_)
          > {
          > // perform registration via channel number (valid range 0 - 5)
          > register(channe lNumber_);
          > }
          >
          > It works fine. However, now there is a new requirements: Channel
          > 0,2,4 must maps to 0, 1, 2. Other channel maps to an invalid value.
          >
          > I was wondering what is the best way to implements the mapping
          > function. My idea is as follows:
          >
          > // 255 is an invalid value
          > #define INVALID_CHANNEL 255
          > int ChannelMapArray[] =
          > {0, INVALID_CHANNEL , 1, INVALID_CHANNEL , 2, INVALID_VALUE, };
          >
          >
          > So I could just use it in registerChannel () as follows:
          > registerChannel (int channelNumber_)
          > {
          > // perform registration via channel number (valid range 0 - 5)
          > register(Channe lMapArray[channelNumber_]);
          > }
          >
          >
          > Is there a better way to implement a mapping function. The array
          > implementation seems unclear. Also, is it possible to implement it as
          > a 2 ways mapping function (i.e given 1-6 -> 0,1,2,invalid or vice
          > versa)? Is there a commonly known technique for this? Is it possible
          > to reference the mapping to the #define in A.cxx?
          >
          > Thanks All,
          > Naruto[/color]

          One could always use the std::map container:
          #include <map>
          using std::map;
          typedef map<unsigned int, unsigned int> Mapping_Contain er;

          The mapping would be:
          Mapping_Contain er channel_map;
          //...
          unsigned int original_value;
          unsigned int new_value;

          //...
          new_value = channel_map[original_value];


          --
          Thomas Matthews

          C++ newsgroup welcome message:

          C++ Faq: http://www.parashift.com/c++-faq-lite
          C Faq: http://www.eskimo.com/~scs/c-faq/top.html
          alt.comp.lang.l earn.c-c++ faq:

          Other sites:
          http://www.josuttis.com -- C++ STL Library book

          Comment

          • naruto

            #6
            Re: Mapping Function

            > //I contrived the names; imagined that you have, e.g., "device" and[color=blue]
            > //"logical" channels
            >
            > class ChannelMapping {
            > struct MappingInfo {
            > channel_type_lo g channel_log;
            > channel_type_de v channel_dev;
            > };
            >
            > static MappingInfo mapping_info_[];
            >
            > public:
            > static channel_type_lo g devToLog(channe l_type_dev channel) {...}
            > static channel_type_de v logToSrc(channe l_type_log channel) {...}
            > };
            >
            > Depending on your design you may need not to make these functions static.
            > Otherwise you can use a namespace instead of a class. Define mapping_info_
            > (give it a better name). Use it directly or write an init() function to
            > create an optimised storage for search by the mapping functions for a
            > sufficiently large number of values.
            >
            >[color=green]
            > > Is it possible
            > > to reference the mapping to the #define in A.cxx?[/color]
            >
            > Move them to file A_internal_what ever.h
            >
            > Denis[/color]

            Thanks Denis. I didn't thought of using a class or namespace to
            perform a mapping fuction. Thanks!!

            Naruto

            Comment

            • naruto

              #7
              Re: Mapping Function

              > //I contrived the names; imagined that you have, e.g., "device" and[color=blue]
              > //"logical" channels
              >
              > class ChannelMapping {
              > struct MappingInfo {
              > channel_type_lo g channel_log;
              > channel_type_de v channel_dev;
              > };
              >
              > static MappingInfo mapping_info_[];
              >
              > public:
              > static channel_type_lo g devToLog(channe l_type_dev channel) {...}
              > static channel_type_de v logToSrc(channe l_type_log channel) {...}
              > };
              >
              > Depending on your design you may need not to make these functions static.
              > Otherwise you can use a namespace instead of a class. Define mapping_info_
              > (give it a better name). Use it directly or write an init() function to
              > create an optimised storage for search by the mapping functions for a
              > sufficiently large number of values.
              >
              >[color=green]
              > > Is it possible
              > > to reference the mapping to the #define in A.cxx?[/color]
              >
              > Move them to file A_internal_what ever.h
              >
              > Denis[/color]

              Thanks Denis. I didn't thought of using a class or namespace to
              perform a mapping fuction. Thanks!!

              Naruto

              Comment

              Working...