Skip to content

Commit

Permalink
Add array_sort and fully migrate to sampctl
Browse files Browse the repository at this point in the history
  • Loading branch information
Vince0789 committed Mar 2, 2019
1 parent 5a26812 commit 1645b1a
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 3 deletions.
50 changes: 50 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#
# Package only files
#

# Compiled Bytecode, precompiled output and assembly
*.amx
*.lst
*.asm

# Vendor directory for dependencies
dependencies/

# Dependency versions lockfile
pawn.lock


#
# Server/gamemode related files
#

# compiled settings file
# keep `samp.json` file on version control
# but make sure the `rcon_password` field is set externally
# you can use the environment variable `SAMP_RCON_PASSWORD` to do this.
server.cfg

# Plugins directory
plugins/

# binaries
*.exe
*.dll
*.so
announce
samp03svr
samp-npc

# logs
logs/
server_log.txt
crashinfo.txt

# Ban list
samp.ban

#
# Common files
#

*.sublime-workspace
59 changes: 59 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build only",
"type": "shell",
"command": "sampctl package build",
"group": {
"kind": "build",
"isDefault": true
},
"isBackground": false,
"presentation": {
"reveal": "silent",
"panel": "dedicated"
},
"problemMatcher": "$sampctl"
},
{
"label": "build watcher",
"type": "shell",
"command": "sampctl package build --watch",
"group": "build",
"isBackground": true,
"presentation": {
"reveal": "silent",
"panel": "dedicated"
},
"problemMatcher": "$sampctl"
},
{
"label": "run tests",
"type": "shell",
"command": "sampctl package run",
"group": {
"kind": "test",
"isDefault": true
},
"isBackground": true,
"presentation": {
"reveal": "silent",
"panel": "dedicated"
},
"problemMatcher": "$sampctl"
},
{
"label": "run tests watcher",
"type": "shell",
"command": "sampctl package run --watch",
"group": "test",
"isBackground": true,
"presentation": {
"reveal": "silent",
"panel": "dedicated"
},
"problemMatcher": "$sampctl"
}
]
}
89 changes: 89 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# pawn-array-util

[![sampctl](https://shields.southcla.ws/badge/sampctl-pawn--array--util-2f2f2f.svg?style=for-the-badge)](https://github.com/Vince0789/pawn-array-util)

<!--
Short description of your library, why it's useful, some examples, pictures or
videos. Link to your forum release thread too.
Remember: You can use "forumfmt" to convert this readme to forum BBCode!
What the sections below should be used for:
`## Installation`: Leave this section un-edited unless you have some specific
additional installation procedure.
`## Testing`: Whether your library is tested with a simple `main()` and `print`,
unit-tested, or demonstrated via prompting the player to connect, you should
include some basic information for users to try out your code in some way.
And finally, maintaining your version number`:
* Follow [Semantic Versioning](https://semver.org/)
* When you release a new version, update `VERSION` and `git tag` it
* Versioning is important for sampctl to use the version control features
Happy Pawning!
-->

## Installation

Simply install to your project:

```bash
sampctl package install Vince0789/pawn-array-util
```

Include in your code and begin using the library:

```pawn
#include <array_util>
```

## Usage

### array_shift
```pawn
stock array_shift(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
```pawn
stock array_unshift(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
```pawn
stock array_sort(arr[], left = 0, right = sizeof arr)
```
Sorts `array` in ascending order. Implementation of the QuickSort algorithm. This function does not return any value.

### in_array
```pawn
stock bool:in_array(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))
{
printf("found 5 at index: %d", index);
}
```

## Testing

<!--
Depending on whether your package is tested via in-game "demo tests" or
y_testing unit-tests, you should indicate to readers what to expect below here.
-->

To test, simply run the package:

```bash
sampctl package run
```
40 changes: 40 additions & 0 deletions array_util.inc
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,45 @@ stock bool:in_array(needle, const haystack[], &index = 0, size = sizeof haystack
return false;
}

/**
* <summary>
* Sorts array values in ascending order. Implementation of the QuickSort algorithm.
* </summary>
* <param name="arr">The array to sort.</param>
* <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)
{
new
i = left,
j = right;

new pivot = array[(left + right) / 2];

// partition
while (i <= j)
{
while (array[i] < pivot) { i++; }
while (array[j] > pivot) { j--; }

if (i <= j)
{
new temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}

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

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

// -----------------------------------------------------------------------------
// EOF
10 changes: 7 additions & 3 deletions pawn.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"user": "Vince0789",
"repo": "pawn-array-util",
"contributors": ["Vince0789"]
"user": "Vince0789",
"repo": "pawn-array-util",
"entry": "test.pwn",
"output": "test.amx",
"dependencies": [
"sampctl/samp-stdlib"
]
}
99 changes: 99 additions & 0 deletions test.pwn
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <a_samp>
#include "array_util.inc"

main()
{
testShift();
testUnshift();
testInArray();
testSort();
}

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

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

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

new firstValue = array_shift(arrayToShift, 89);

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

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

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

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

new lastValue = array_unshift(arrayToShift, 89);

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

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

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

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

new index = -1;

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

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

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

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

array_sort(arrayToSort);

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

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

for(new i; i < size; i++)
{
new tmp[11];
format(tmp, sizeof tmp, "%d", arr[i]);

if(i > 0)
{
strcat(output, ", ");
}

strcat(output, tmp);
}

print(output);
}

0 comments on commit 1645b1a

Please sign in to comment.