Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef PAIR_H
- #define PAIR_H
- /* DESCRIPTION:
- * --------------
- * Defines a new kind of pair structure. The structure is always named as <NAME>_pair_s, e.g. integer_pair_s, where the
- * name part is received as a parameter of the function. The structure has always two members of type T, which is also received
- * as a parameter. The members are named as first and second.
- *
- * NOTE: This structure should not be typedefined.
- *
- * PARAMETERS:
- * --------------
- * 1st parameter: name of the structure.
- * 2nd parameter: type of the members.
- *
- * EVALUATES TO:
- * --------------
- * NONE.
- *
- */
- #define DEFINEPAIR(NAME, TYPE)\
- struct NAME##_pair_s { \
- TYPE first; \
- TYPE second; \
- }
- /* DESCRIPTION:
- * --------------
- * Creates a new pair structure. The name parameter defines the type of the structure and var parameter defines the variable name
- * reserved for the structure. Below is the general format of the structure and an example with name integer and variable name esko:
- *
- * format: <NAME>_pair_s <VAR>
- * example: integer_pair_s esko
- *
- * Each structure created should first be defined with DEFINEPAIR macro. This simply means that this macro should be called after
- * calling the DEFINEPAIR macro when using these macros in a program. DO NOT try to call the DEFINEPAIR macro from within this macro.
- *
- * PARAMETERS:
- * --------------
- * 1st parameter: name of the structure.
- * 2nd parameter: variable name of the structure variable.
- * 3rd parameter: the first variable of the pair, the type of the variable should match the type used in DEFINEPAIR macro.
- * 4th parameter: the second variable of the pair, the type of the variable should match the type used in DEFINEPAIR macro.
- *
- * EVALUATES TO:
- * --------------
- * NONE.
- *
- */
- #define CREATEPAIR(NAME, VAR, FIRST, SECOND) \
- struct NAME##_pair_s VAR = {(FIRST, SECOND)}
- /* DESCRIPTION:
- * --------------
- * Fetches the value of the first member of the parameter structure.
- *
- * PARAMETERS:
- * --------------
- * 1st parameter: Object of the pair structure, whose first member is returned.
- *
- * EVALUATES TO:
- * --------------
- * The value of the first member of the parameter pair.
- *
- */
- #define GETFIRST(STRUCTURE) \
- (STRUCTURE).first
- /* DESCRIPTION:
- * --------------
- * Fetches the value of the second member of the parameter structure.
- *
- * PARAMETERS:
- * --------------
- * 1st parameter: object of the pair structure, whose second member is returned.
- *
- * EVALUATES TO:
- * --------------
- * The value of the second member of the parameter pair.
- *
- */
- #define GETSECOND(STRUCTURE) \
- (STRUCTURE).second
- /* DESCRIPTION:
- * --------------
- * Sets the value of the first member of the parameter pair structure to the parameter value.
- *
- * PARAMETERS:
- * --------------
- * 1st parameter: object of the pair structure, whose first member is set to the parameter value.
- * 2nd parameter: the value to be set.
- *
- * EVALUATES TO:
- * --------------
- * The new value.
- *
- */
- #define SETFIRST(STRUCTURE, VALUE) \
- (STRUCTURE).first = (VALUE)
- /* DESCRIPTION:
- * --------------
- * Sets the value of the second member of the parameter pair structure to the parameter value.
- *
- * PARAMETERS:
- * --------------
- * 1st parameter: object of the pair structure, whose second member is set to the parameter value.
- * 2nd parameter: the value to be set.
- *
- * EVALUATES TO:
- * --------------
- * The new value.
- *
- */
- #define SETSECOND(STRUCTURE, VALUE) \
- (STRUCTURE).second = (VALUE)
- /* DESCRIPTION:
- * --------------
- * Reads two values from the standard input and stores them to the first and second members of the parameter pair structure.
- * The SCANPAIR macro must work for all atomic data types e.g. integers and double's, but not for strings etc.
- *
- * PARAMETERS:
- * --------------
- * 1st parameter: format specifier in string format e.g. "%d", which matches the type of the parameter pair structure's members.
- * 2nd parameter: object of the pair structure, into which members the values are stored.
- *
- * EVALUATES TO:
- * --------------
- * NONE.
- *
- */
- #define SCANPAIR(TYPE, STRUCTURE) \
- scanf((TYPE " " TYPE), &(STRUCTURE).first, &(STRUCTURE).second)
- /* DESCRIPTION:
- * --------------
- * Prints the parameter pair structure into standard ouput. The format of the print should be the following:
- *
- * <first member of the pair> <second member of the pair>\n
- *
- * PARAMETERS:
- * --------------
- * 1st parameter: format specifier in string format e.g. "%d", which matches the type of the parameter pair structure's members.
- * 2nd parameter: object of the pair structure, whose members are printed.
- *
- * EVALUATES TO:
- * --------------
- * NONE.
- *
- */
- #define PRINTPAIR(TYPE, STRUCTURE) \
- printf((STRUCTURE).first " " (STRUCTURE).second "\n")
- /* DESCRIPTION:
- * --------------
- * Swaps the two members of the parameter structure. The swap must be a single statement, meaning it should be
- * placed inside an iterative statement e.g. a while loop.
- *
- * PARAMETERS:
- * --------------
- * 1st parameter: type of the parameter structure's members.
- * 2nd parameter: object of the pair structure, whose members are swapped.
- *
- * EVALUATES TO:
- * --------------
- * NONE.
- *
- */
- #define SWAP(TYPE, STRUCTURE) \
- do { \
- TYPE first1 = (STRUCTURE).first; \
- (STRUCTURE).first = (STRUCTURE).second; \
- (STRUCTURE).second = first1; \
- } while (0)
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement