Skip to content

Commit

Permalink
Add the actual file
Browse files Browse the repository at this point in the history
  • Loading branch information
Vince0789 committed Jul 1, 2018
1 parent f6bf0a3 commit 7349779
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Auto detect text files and perform LF normalization
* text=auto

*.pwn linguist-language=Pawn
*.inc linguist-language=Pawn
100 changes: 100 additions & 0 deletions array_util.inc
Original file line number Diff line number Diff line change
@@ -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.
*/

// -----------------------------------------------------------------------------

/**
* <summary>
* Appends <c>value</c> to the end of array. Pushes the first value off and
* returns it, moves everything down.
* </summary>
* <param name="array">The array to append the value to.</param>
* <param name="value">The value to append.</param>
* <param name=size">
* Size of the array. If lower than the actual size of the array then indices
* greater than size are not affected.
* </param>
* <returns>The value at the first index.</returns>
*/
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;
}

/**
* <summary>
* Inserts a value at the start of array. Pushes the last value off and returns
* it, moves everything up.
* </summary>
* <param name="array">The array to prepend the value to.</param>
* <param name="value">The value to prepend.</param>
* <param name=size">
* 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.
* </param>
* <returns>The value at size - 1.</return>
*/
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;
}

/**
* <summary>
* Checks if a value exists within an array.
* </summary>
* <param name="needle">The value to search for.</param>
* <param name="haystack">The array to search in.</param>
* <param name="index">Reference variable to store the index of the first match.</param>
* <param name="size">Size of the array.</param>
* <returns>true if found, false otherwise.</returns>
*/
stock bool:in_array(needle, const haystack[], &index = 0, size = sizeof haystack)
{
while(index < size)
{
if(haystack[index++] == needle)
return true;
}
return false;
}

// -----------------------------------------------------------------------------
// EOF

0 comments on commit 7349779

Please sign in to comment.