summaryrefslogtreecommitdiff
path: root/sourcemod/scripting/include/smlib/arrays.inc
blob: 87f17335a3a2da10302abf40b39fb338bf290e06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#if defined _smlib_array_included
	#endinput
#endif
#define _smlib_array_included

#include <sourcemod>

/**
 * Returns the index for the first occurance of the given value.
 * If the value cannot be found, -1 will be returned.
 *
 * @param array			Static Array.
 * @param size			Size of the Array.
 * @param value			Value to search for.
 * @param start			Optional: Offset where to start (0 - (size-1)).
 * @return				Array index, or -1 if the value couldn't be found.
 */
stock int Array_FindValue(const any[] array, int size, any value, int start=0)
{
	if (start < 0) {
		start = 0;
	}

	for (int i=start; i < size; i++) {

		if (array[i] == value) {
			return i;
		}
	}

	return -1;
}

/**
 * Searchs for the first occurance of a string in the array.
 * If the value cannot be located, -1 will be returned.
 *
 * @param array			Static Array.
 * @param size			Size of the Array.
 * @param value			String to search for.
 * @param start			Optional: Offset where to start(0 - (size-1)).
 * @return				Array index, or -1 if the value couldn't be found.
 */
stock int Array_FindString(const char[][] array, int size, const char[] str, bool caseSensitive=true, int start=0)
{
	if (start < 0) {
		start = 0;
	}

	for (int i=start; i < size; i++) {

		if (StrEqual(array[i], str, caseSensitive)) {
			return i;
		}
	}

	return -1;
}

/**
 * Returns the Index of the Lowest value in the array
 *
 * @param array			Static Array.
 * @param size			Size of the Array.
 * @param start			Optional: Offset where to start (0 - (size-1)).
 * @return				Array index.
 */
stock int Array_FindLowestValue(const any[] array, int size, int start=0)
{
	if (start < 0) {
		start = 0;
	}

	any value = array[start];
	any tempValue;
	int x = start;

	for (int i=start; i < size; i++) {

		tempValue = array[i];

		if (tempValue < value) {
			value = tempValue;
			x = i;
		}

	}

	return x;
}

/**
 * Returns the Index of the Highest value in the array
 *
 * @param array			Static Array.
 * @param size			Size of the Array.
 * @param start			Optional: Offset where to start (0 - (size-1)).
 * @return				Array index.
 */
stock int Array_FindHighestValue(const any[] array, int size, int start=0)
{
	if (start < 0) {
		start = 0;
	}

	any value = array[start];
	any tempValue;
	int x = start;

	for (int i=start; i < size; i++) {

		tempValue = array[i];

		if (tempValue > value) {
			value = tempValue;
			x = i;
		}

	}

	return x;
}

/**
 * Fills an array with a given value in a 1 dimensional static array.
 * You can specify the amount of cells to be written.
 *
 * @param array			Static Array.
 * @param size			Number of cells to write (eg. the array's size)
 * @param value			Fill value.
 * @param start			Optional: Offset where to start (0 - (size-1)).
 */
stock void Array_Fill(any[] array, int size, any value, int start=0)
{
	if (start < 0) {
		start = 0;
	}

	for (int i=start; i < size; i++) {
		array[i] = value;
	}
}

/**
 * Copies a 1 dimensional static array.
 *
 * @param array			Static Array to copy from.
 * @param newArray		New Array to copy to.
 * @param size			Size of the array (or number of cells to copy)
 * @noreturn
 */
stock void Array_Copy(const any[] array, any[] newArray, int size)
{
	for (int i=0; i < size; i++) {
		newArray[i] = array[i];
	}
}