daily pastebin goal
9%
SHARE
TWEET

Untitled

a guest Aug 12th, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef PAIR_H
  2. #define PAIR_H
  3.  
  4. /* DESCRIPTION:
  5.  * --------------
  6.  * Defines a new kind of pair structure. The structure is always named as <NAME>_pair_s, e.g. integer_pair_s, where the
  7.  * name part is received as a parameter of the function. The structure has always two members of type T, which is also received
  8.  * as a parameter. The members are named as first and second.
  9.  *
  10.  * NOTE: This structure should not be typedefined.
  11.  *
  12.  * PARAMETERS:
  13.  * --------------
  14.  * 1st parameter: name of the structure.
  15.  * 2nd parameter: type of the members.
  16.  *
  17.  * EVALUATES TO:
  18.  * --------------
  19.  * NONE.
  20.  *
  21.  */
  22. #define DEFINEPAIR(NAME, TYPE)\
  23.     struct NAME##_pair_s { \
  24.         TYPE first; \
  25.         TYPE second; \
  26.     }
  27.    
  28.  
  29. /* DESCRIPTION:
  30.  * --------------
  31.  * Creates a new pair structure. The name parameter defines the type of the structure and var parameter defines the variable name
  32.  * reserved for the structure. Below is the general format of the structure and an example with name integer and variable name esko:
  33.  *
  34.  *    format: <NAME>_pair_s <VAR>
  35.  *    example: integer_pair_s esko
  36.  *
  37.  * Each structure created should first be defined with DEFINEPAIR macro. This simply means that this macro should be called after
  38.  * calling the DEFINEPAIR macro when using these macros in a program. DO NOT try to call the DEFINEPAIR macro from within this macro.
  39.  *
  40.  * PARAMETERS:
  41.  * --------------
  42.  * 1st parameter: name of the structure.
  43.  * 2nd parameter: variable name of the structure variable.
  44.  * 3rd parameter: the first variable of the pair, the type of the variable should match the type used in DEFINEPAIR macro.
  45.  * 4th parameter: the second variable of the pair, the type of the variable should match the type used in DEFINEPAIR macro.  
  46.  *
  47.  * EVALUATES TO:
  48.  * --------------
  49.  * NONE.
  50.  *
  51.  */
  52. #define CREATEPAIR(NAME, VAR, FIRST, SECOND) \
  53.     struct NAME##_pair_s VAR = {(FIRST, SECOND)}
  54.        
  55.    
  56.  
  57. /* DESCRIPTION:
  58.  * --------------
  59.  * Fetches the value of the first member of the parameter structure.
  60.  *
  61.  * PARAMETERS:
  62.  * --------------
  63.  * 1st parameter: Object of the pair structure, whose first member is returned.
  64.  *
  65.  * EVALUATES TO:
  66.  * --------------
  67.  * The value of the first member of the parameter pair.
  68.  *
  69.  */
  70. #define GETFIRST(STRUCTURE) \
  71.     (STRUCTURE).first
  72.  
  73.  
  74. /* DESCRIPTION:
  75.  * --------------
  76.  * Fetches the value of the second member of the parameter structure.
  77.  *
  78.  * PARAMETERS:
  79.  * --------------
  80.  * 1st parameter: object of the pair structure, whose second member is returned.
  81.  *
  82.  * EVALUATES TO:
  83.  * --------------
  84.  * The value of the second member of the parameter pair.
  85.  *
  86.  */
  87. #define GETSECOND(STRUCTURE) \
  88.     (STRUCTURE).second
  89.  
  90. /* DESCRIPTION:
  91.  * --------------
  92.  * Sets the value of the first member of the parameter pair structure to the parameter value.
  93.  *
  94.  * PARAMETERS:
  95.  * --------------
  96.  * 1st parameter: object of the pair structure, whose first member is set to the parameter value.
  97.  * 2nd parameter: the value to be set.
  98.  *
  99.  * EVALUATES TO:
  100.  * --------------
  101.  * The new value.
  102.  *
  103.  */
  104. #define SETFIRST(STRUCTURE, VALUE) \
  105.     (STRUCTURE).first = (VALUE)
  106.  
  107. /* DESCRIPTION:
  108.  * --------------
  109.  * Sets the value of the second member of the parameter pair structure to the parameter value.
  110.  *
  111.  * PARAMETERS:
  112.  * --------------
  113.  * 1st parameter: object of the pair structure, whose second member is set to the parameter value.
  114.  * 2nd parameter: the value to be set.
  115.  *
  116.  * EVALUATES TO:
  117.  * --------------
  118.  * The new value.
  119.  *
  120.  */
  121. #define SETSECOND(STRUCTURE, VALUE) \
  122.     (STRUCTURE).second = (VALUE)
  123.  
  124. /* DESCRIPTION:
  125.  * --------------
  126.  * Reads two values from the standard input and stores them to the first and second members of the parameter pair structure.
  127.  * The SCANPAIR macro must work for all atomic data types e.g. integers and double's, but not for strings etc.
  128.  *
  129.  * PARAMETERS:
  130.  * --------------
  131.  * 1st parameter: format specifier in string format e.g. "%d", which matches the type of the parameter pair structure's members.
  132.  * 2nd parameter: object of the pair structure, into which members the values are stored.
  133.  *
  134.  * EVALUATES TO:
  135.  * --------------
  136.  * NONE.
  137.  *
  138.  */
  139. #define SCANPAIR(TYPE, STRUCTURE) \
  140.     scanf((TYPE " " TYPE), &(STRUCTURE).first, &(STRUCTURE).second)
  141.    
  142.    
  143.  
  144. /* DESCRIPTION:
  145.  * --------------
  146.  * Prints the parameter pair structure into standard ouput. The format of the print should be the following:
  147.  *
  148.  *    <first member of the pair> <second member of the pair>\n
  149.  *
  150.  * PARAMETERS:
  151.  * --------------
  152.  * 1st parameter: format specifier in string format e.g. "%d", which matches the type of the parameter pair structure's members.
  153.  * 2nd parameter: object of the pair structure, whose members are printed.
  154.  *
  155.  * EVALUATES TO:
  156.  * --------------
  157.  * NONE.
  158.  *
  159.  */
  160. #define PRINTPAIR(TYPE, STRUCTURE) \
  161.     printf((STRUCTURE).first " " (STRUCTURE).second "\n")
  162.  
  163. /* DESCRIPTION:
  164.  * --------------
  165.  * Swaps the two members of the parameter structure. The swap must be a single statement, meaning it should be
  166.  * placed inside an iterative statement e.g. a while loop.
  167.  *
  168.  * PARAMETERS:
  169.  * --------------
  170.  * 1st parameter: type of the parameter structure's members.
  171.  * 2nd parameter: object of the pair structure, whose members are swapped.
  172.  *
  173.  * EVALUATES TO:
  174.  * --------------
  175.  * NONE.
  176.  *
  177.  */
  178. #define SWAP(TYPE, STRUCTURE) \
  179.     do { \
  180.         TYPE first1 = (STRUCTURE).first; \
  181.         (STRUCTURE).first = (STRUCTURE).second; \
  182.         (STRUCTURE).second = first1; \
  183.         } while (0)
  184.        
  185. #endif
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top