diff --git a/.gitattributes b/.gitattributes index dfe0770..cdf27e1 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,5 @@ # Auto detect text files and perform LF normalization * text=auto + +*.pwn linguist-language=Pawn +*.inc linguist-language=Pawn diff --git a/array_util.inc b/array_util.inc new file mode 100644 index 0000000..6ad70b0 --- /dev/null +++ b/array_util.inc @@ -0,0 +1,100 @@ +/** + * MIT License + * + * Copyright (c) 2018 Vince0789 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * 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 OR COPYRIGHT HOLDERS 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. + */ + +// ----------------------------------------------------------------------------- + +/** + * + * Appends value to the end of array. Pushes the first value off and + * returns it, moves everything down. + * + * The array to append the value to. + * The value to append. + * + * Size of the array. If lower than the actual size of the array then indices + * greater than size are not affected. + * + * The value at the first index. + */ +stock array_shift(array[], value, size = sizeof array) +{ + new returnval = array[0]; + + for(new i; i < size - 1; i++) + { + array[i] = array[i + 1]; + } + + array[size - 1] = value; + return returnval; +} + +/** + * + * Inserts a value at the start of array. Pushes the last value off and returns + * it, moves everything up. + * + * The array to prepend the value to. + * The value to prepend. + * + * Size of the array. If lower than the actual size of the array then the index + * at size - 1 is returned and higher indices are not affected. + * + * The value at size - 1. + */ +stock array_unshift(array[], value, size = sizeof array) +{ + new returnval = array[size - 1]; + + for(new i = size - 1; i > 0; i--) + { + array[i] = array[i - 1]; + } + + array[0] = value; + return returnval; +} + +/** + * + * Checks if a value exists within an array. + * + * The value to search for. + * The array to search in. + * Reference variable to store the index of the first match. + * Size of the array. + * true if found, false otherwise. + */ +stock bool:in_array(needle, const haystack[], &index = 0, size = sizeof haystack) +{ + while(index < size) + { + if(haystack[index++] == needle) + return true; + } + return false; +} + +// ----------------------------------------------------------------------------- +// EOF