Skip to content

Commit

Permalink
Change function naming scheme to PascalCase
Browse files Browse the repository at this point in the history
  • Loading branch information
Vince0789 committed Mar 3, 2019
1 parent 1645b1a commit 2cfcb17
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 40 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,36 @@ Include in your code and begin using the library:
```

## Usage
Version 1.2.0 changed the naming convention from `snake_case` to `PascalCase`. The snake case variant of the below functions can still be used, but the compiler will issue a warning. Snake case variants will be removed in 2.0.0.

### array_shift
### ArrayShift
```pawn
stock array_shift(array[], value, size = sizeof array)
stock ArrayShift(array[], value, size = sizeof array)
```
Appends `value` to the end of `array`. Pushes the first value off and returns it, moves everything down.

### array_unshift
### ArrayUnshift
```pawn
stock array_unshift(array[], value, size = sizeof array)
stock ArrayUnshift(array[], value, size = sizeof array)
```
Inserts `value` at the start of `array`. Pushes the last value off and returns it, moves everything up.

### array_sort
### ArraySort
```pawn
stock array_sort(arr[], left = 0, right = sizeof arr)
stock ArraySort(array[], left = 0, right = sizeof array)
```
Sorts `array` in ascending order. Implementation of the QuickSort algorithm. This function does not return any value.

### in_array
### InArray
```pawn
stock bool:in_array(needle, const haystack[], &index = 0, size = sizeof haystack)
stock bool:InArray(needle, const haystack[], &index = 0, size = sizeof haystack)
```
Returns `true` if `needle` is in `haystack`, false otherwise. Using the `index` parameter, this function can be used in a while loop to find all instances of a certain value:

```pawn
new index = -1;
while(in_array(5, theArrayToSearch, index))
while(InArray(5, theArrayToSearch, index))
{
printf("found 5 at index: %d", index);
}
Expand Down
36 changes: 30 additions & 6 deletions array_util.inc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* </param>
* <returns>The value at the first index.</returns>
*/
stock array_shift(array[], value, size = sizeof array)
stock ArrayShift(array[], value, size = sizeof array)
{
new returnval = array[0];

Expand All @@ -50,6 +50,12 @@ stock array_shift(array[], value, size = sizeof array)
return returnval;
}

#pragma deprecated 2.0.0
stock array_shift(array[], value, size = sizeof array)
{
return ArrayShift(array, value, size);
}

/**
* <summary>
* Inserts a value at the start of array. Pushes the last value off and returns
Expand All @@ -63,7 +69,7 @@ stock array_shift(array[], value, size = sizeof array)
* </param>
* <returns>The value at size - 1.</return>
*/
stock array_unshift(array[], value, size = sizeof array)
stock ArrayUnshift(array[], value, size = sizeof array)
{
new returnval = array[size - 1];

Expand All @@ -76,6 +82,12 @@ stock array_unshift(array[], value, size = sizeof array)
return returnval;
}

#pragma deprecated 2.0.0
stock array_unshift(array[], value, size = sizeof array)
{
return ArrayUnshift(array, value, size);
}

/**
* <summary>
* Checks if a value exists within an array.
Expand All @@ -86,7 +98,7 @@ stock array_unshift(array[], value, size = sizeof array)
* <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)
stock bool:InArray(needle, const haystack[], &index = 0, size = sizeof haystack)
{
while(index < size)
{
Expand All @@ -96,6 +108,12 @@ stock bool:in_array(needle, const haystack[], &index = 0, size = sizeof haystack
return false;
}

#pragma deprecated 2.0.0
stock bool:in_array(needle, const haystack[], &index = 0, size = sizeof haystack)
{
return InArray(needle, haystack, index, size);
}

/**
* <summary>
* Sorts array values in ascending order. Implementation of the QuickSort algorithm.
Expand All @@ -104,7 +122,7 @@ stock bool:in_array(needle, const haystack[], &index = 0, size = sizeof haystack
* <param name="left">Index of the lower bound, should normally be 0.</param>
* <param name="right">Index of the upper bound, should normally be the size of the array.</param>
*/
stock array_sort(array[], left = 0, right = sizeof array)
stock ArraySort(array[], left = 0, right = sizeof array)
{
new
i = left,
Expand All @@ -130,10 +148,16 @@ stock array_sort(array[], left = 0, right = sizeof array)

// recursion
if (left < j)
array_sort(array, left, j);
ArraySort(array, left, j);

if (i < right)
array_sort(array, i, right);
ArraySort(array, i, right);
}

#pragma deprecated 2.0.0
stock array_sort(array[], left = 0, right = sizeof array)
{
ArraySort(array, left, right);
}

// -----------------------------------------------------------------------------
Expand Down
50 changes: 25 additions & 25 deletions test.pwn
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,82 @@

main()
{
testShift();
testUnshift();
testInArray();
testSort();
TestShift();
TestUnshift();
TestInArray();
TestSort();
}

testShift()
TestShift()
{
printf("\narray_shift");
printf("\nArrayShift");
printf("-----------");

new arrayToShift[] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55};

printf("before shifting:");
printArray(arrayToShift);
PrintArray(arrayToShift);

new firstValue = array_shift(arrayToShift, 89);
new firstValue = ArrayShift(arrayToShift, 89);

printf("after shifting:");
printArray(arrayToShift);
PrintArray(arrayToShift);
printf("value that was shifted off: %d", firstValue);
}

testUnshift()
TestUnshift()
{
printf("\narray_unshift");
printf("\nArrayUnshift");
printf("-------------");

new arrayToShift[] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55};

printf("before shifting:");
printArray(arrayToShift);
PrintArray(arrayToShift);

new lastValue = array_unshift(arrayToShift, 89);
new lastValue = ArrayUnshift(arrayToShift, 89);

printf("after shifting:");
printArray(arrayToShift);
PrintArray(arrayToShift);
printf("value that was shifted off: %d", lastValue);
}

testInArray()
TestInArray()
{
printf("\nin_array");
printf("\nInArray");
printf("--------");

new testArray[] = {1, 3, 5, 7, 13, 5, 1, 7, 3};

printf("array to search in:");
printArray(testArray);
PrintArray(testArray);
printf("finding all the indexes that contain the number 5:");

new index = -1;

while(in_array(5, testArray, index))
while(InArray(5, testArray, index))
{
printf("found 5 at index: %d", index);
}
}

testSort()
TestSort()
{
printf("\narray_sort");
printf("\nArraySort");
printf("----------");

new arrayToSort[] = {13, 92, 85, 64, 17, 55, 33, 60, 58, 95};

printf("before sort:");
printArray(arrayToSort);
PrintArray(arrayToSort);

array_sort(arrayToSort);
ArraySort(arrayToSort);

printf("after sort:");
printArray(arrayToSort);
PrintArray(arrayToSort);
}

printArray(const arr[], size = sizeof arr)
PrintArray(const arr[], size = sizeof arr)
{
new output[128];

Expand All @@ -96,4 +96,4 @@ printArray(const arr[], size = sizeof arr)
}

print(output);
}
}

0 comments on commit 2cfcb17

Please sign in to comment.