Created
July 4, 2023 18:58
-
-
Save doxy-ai/162adfb275a467b2b2e0598bc33f4b89 to your computer and use it in GitHub Desktop.
A simple file that creates a repeat loop which simply repeats the provided code the specified number of times!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or distribute | |
this software, either in source code form or as a compiled binary, for any | |
purpose, commercial or non-commercial, and by any means. | |
In jurisdictions that recognize copyright laws, the author or authors of | |
this software dedicate any and all copyright interest in the software to | |
the public domain. We make this dedication for the benefit of the public | |
at large and to the detriment of our heirs and successors. We intend this | |
dedication to be an overt act of relinquishment in perpetuity of all present | |
and future rights to this software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <http://unlicense.org/> | |
*/ | |
/* ----------------------------------------------------------------------------- | |
SYNTAX EXAMPLE | |
----------------------------------------------------------------------------- */ | |
/* | |
repeat(x){ | |
// code here | |
} | |
Will repeat the provided code x times. (A counter "_i" storing the iteration | |
index (which counts down by default) will always be present) | |
Ex: | |
repeat(5) | |
std::cout << "This line will be repeated 5 times" << std::endl; | |
*/ | |
/* | |
repeat(x, index){ | |
// code here | |
} | |
Will repeat the provided code x times, storing the current index in a | |
variable with the name of the second input. | |
Ex: | |
repeat(5, index) | |
std::cout << "Current index: " << index << std::endl; | |
*/ | |
/* | |
repeat(x, type, index){ | |
// code here | |
} | |
Will repeat the provided code x times, storing the current index in a | |
variable with type dictated by the second input and a name provided by the third | |
This option shouldn't be necessary for 99% of applications. | |
*/ | |
/* | |
rrepeat(x){ | |
// code here | |
} | |
rrepeat(x, index){ | |
// code here | |
} | |
rrepeat(x, type, index){ | |
// code here | |
} | |
Along with reverse_repeat, reverseRepeat, and ReverseRepeat variations all | |
work the same as their repeat counterparts except they will always count | |
from x - 1 to 0. | |
By default repeat loops are reverse repeat loops | |
*/ | |
/* | |
frepeat(x){ | |
// code here | |
} | |
frepeat(x, index){ | |
// code here | |
} | |
frepeat(x, type, index){ | |
// code here | |
} | |
Along with forward_repeat, forwardRepeat, and ForwardRepeat variations all | |
work the same as their repeat counterparts except they will always count | |
from 0 to x - 1. | |
If you wish for the default repeat loop to be a reverse repeat loop you may | |
define: | |
#define REPEAT_FORWARD_DEFAULT | |
before including this file. | |
*/ | |
#ifndef _REPEAT_H_ | |
#define _REPEAT_H_ | |
// Macros defining Repeat Syntax | |
#define _REPEAT_TIMES_TYPE_(x, type, y) for(type y = x; y--;) | |
#define _FORWARD_REPEAT_TIMES_TYPE_(x, type, y) for(type y = 0; y < x; y++) | |
#define _REPEAT_TIMES_(x, y) _REPEAT_TIMES_TYPE_(x, size_t, y) | |
#define _FORWARD_REPEAT_TIMES_(x, y) _FORWARD_REPEAT_TIMES_TYPE_(x, size_t, y) | |
#define _REPEAT_(x) _REPEAT_TIMES_(x, _i) | |
#define _FORWARD_REPEAT_(x) _FORWARD_REPEAT_TIMES_(x, _i) | |
// Macro overloading from: https://stackoverflow.com/questions/11761703/overloading-macro-on-number-of-arguments | |
#define _GET_REPEAT_(_1,_2,_3, NAME,...) NAME | |
// Reverse (optimized) repeat | |
#define rrepeat(...) _GET_REPEAT_(__VA_ARGS__, _REPEAT_TIMES_TYPE_, _REPEAT_TIMES_, _REPEAT_)(__VA_ARGS__) | |
#define reverse_repeat(...) rrepeat(__VA_ARGS__) | |
#define reverseRepeat(...) rrepeat(__VA_ARGS__) | |
#define ReverseRepeat(...) rrepeat(__VA_ARGS__) | |
// Forward (index based) repeat | |
#define frepeat(...) _GET_REPEAT_(__VA_ARGS__, _FORWARD_REPEAT_TIMES_TYPE_, _FORWARD_REPEAT_TIMES_, _FORWARD_REPEAT_)(__VA_ARGS__) | |
#define forward_repeat(...) frepeat(__VA_ARGS__) | |
#define forwardRepeat(...) frepeat(__VA_ARGS__) | |
#define ForwardRepeat(...) frepeat(__VA_ARGS__) | |
// Default repeat direction | |
#ifdef REPEAT_FORWARD_DEFAULT | |
# define repeat(...) frepeat(__VA_ARGS__) | |
#else // REPEAT_FORWARD_DEFAULT | |
# define repeat(...) rrepeat(__VA_ARGS__) | |
#endif // REPEAT_FORWARD_DEFAULT | |
#endif // _REPEAT_H_ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment