Release Array functions
#1
Here are a whole heap of array functions I created (should work for strings I think). They all come with examples and usage instructions. You can turn this into a GSC and use #include aswell. Before I was about to release this I realised that common_scripts/utility had some of these functions already. Dodgy

PHP Code:
/* An example of a bad predicate which returns no value at all and doesn't accept a value. Arguments: None. */ bad_predicate() { } /* An example of a 'predicate' which takes an object and returns a boolean value. Arguments: obj - An object that is passed from an array. */ some_predicate(obj) { return obj == 1; } /* Returns a boolean value which determines whether each value in an array matched the criteria of the predicate. Arguments: arr - An array passed through as an argument. predicate - A function pointer where the function returns a boolean value. Example: some_function() { some_array = []; some_array[0] = 1; some_array[1] = 1; // This would display 1 on the screen (true) self iPrintLn(_array_TrueForAll(some_array, ::test_predicate)); some_array[1] = 0; // This would display 0 on the screen (false) self iPrintLn(_array_TrueForAll(some_array, ::test_predicate)); } test_predicate(obj) { return obj == 1; } */ _array_TrueForAll(arr, predicate) { if(isArray(arr) && arr.size > 0 && valid_predicate(predicate, arr[0])) for(i = 0; i < arr.size; i++) if(![[predicate]](arr[i])) return false; return true; } /* Returns a boolean value which checks if the predicate actually returns something. Arguments: predicate - A function pointer which returns a boolean value. obj - An object that is passed from an array. Example: None, used internally by other functions. */ valid_predicate(predicate, obj) { return [[predicate]](obj) != undefined; } /* Returns an array that has been reversed. Arguments: arr - An array passed through as an argument. Example: some_function() { my_array = []; my_array[0] = "T"; my_array[1] = "E"; my_array[2] = "S"; my_array[3] = "T"; for(i = 0; i < 4; i++) self iPrintLn(i + " : " + my_array[i]); // Output from above: // 0 : T // 1 : E // 2 : S // 3 : T // Note that you need to assign the array to the value // returned by the function. my_array = _array_Reverse(my_array); for(i = 0; i < 4; i++) self iPrintLn(i + " : " + my_array[i]); // Output from above: // 0 : T // 1 : S // 2 : E // 3 : T } */ _array_Reverse(arr) { ret_arr = arr; if(isArray(arr)) { for(i = arr.size - 1; i >= 0; i--) ret_arr[arr.size-1-i] = arr[i]; } return ret_arr; } /* Returns an array that has a specific item removed using an index value (first occurence). Arguments: arr - An array passed through as an argument. index - The array index where the item is to be removed. Example: some_function() { my_array = []; my_array[0] = "removeMe"; my_array[1] = "leaveMeAlone"; // This would display 'The array currently has 2 item(s).' self iPrintLn("The array currently has " + my_array.size + " item(s)."); my_array = _array_RemoveAt(my_array, 0); // This would display 'The array now has 1 item(s).' self iPrintLn("The array now has " + my_array.size + " item(s)."); } */ _array_RemoveAt(arr, index) { if(isArray(arr) && isDefined(index) && index != -1) { for(i = index; i < arr.size - 1 && i >= 0; i++) arr[i+1] = arr[i]; arr[arr.size-1] delete(); } return arr; } /* Returns an array that has a specific item removed using an object (first occurence). Arguments: arr - An array passed through as an argument. obj - An object that is used to remove a matching instance. Example: some_function() { this_array = []; this_array[0] = 22; this_array[1] = 33; this_array[2] = 66; // This would display 'The array currently has 3 item(s).' self iPrintLn("The array currently has " + this_array.size + " item(s)."); this_array = _array_Remove(this_array, 33); // This would display 'The array now has 2 item(s).' self iPrintLn("The array now has " + this_array.size + " item(s)."); } */ _array_Remove(arr, obj) { return _array_RemoveAt(arr, _array_IndexOf(arr, obj)); } /* Returns an array that has a new item added to it. Arguments: arr - An array passed through as an argument. obj - An object that is to be added to the end of the array. Example: some_function() { thing_array = []; // This would display 'The array currently has 0 item(s).' self iPrintLn("The array currently has " + thing_array.size + " item(s)."); thing_array = _array_Add(thing_array, "lookAtMe"); // This would display 'The array now has 1 item(s).' self iPrintLn("The array now has " + thing_array.size + " item(s)."); } */ _array_Add(arr, obj) { if(isArray(arr) && isDefined(obj)) arr[arr.size] = obj; return arr; } /* Returns a boolean value determining if an object exists in an array. Arguments: arr - An array passed through as an argument. obj - An object to look for. Example: some_function() { an_array = []; an_array[0] = "findMe"; an_array[1] = "goAway"; // _array_Contains will return true and 'an_array contains 'findMe'' will be displayed if(_array_Contains(an_array, "findMe")) self iPrintLnBold("an_array contains 'findMe'"); else self iPrintLnBold("an_array does not contain 'findMe'"); } */ _array_Contains(arr, obj) { return _array_IndexOf(arr, obj) != -1; } /* Returns an integer value determining at which index of an array an object is (first occurence). If it fails to find the object, it will return -1. Arguments: arr - An array passed through as an argument. obj - An object to look for. Example: some_function() { me_array = []; me_array[0] = "a"; me_array[1] = "b"; me_array[2] = "c"; // This would display 'The index of the value a is at: 0' self iPrintLn("The index of the value a is at: " + _array_IndexOf(me_array, "a")); // This would display 'The index of the value d is at: -1' self iPrintLn("The index of the value d is at: " + _array_IndexOf(me_array, "d")); } */ _array_IndexOf(arr, obj) { if(isArray(arr) && isDefined(obj)) for(i = 0; i < arr.size; i++) if(arr[i] == obj) return i; return -1; } /* Returns an array with all its items removed. Arguments: arr - An array to clear. Example: Not needed, too straight forward. */ _array_Clear(arr) { // Might be a little useless but hey, oh wells. if(isArray(arr)) for(i = arr.size - 1; i <= 0; i--) arr[i] delete(); return arr; } /* Returns an array with an object insert at a specific index. If the index is the same value as the array size, it is added to the end. Arguments: arr - An array passed through as an argument. index - The index from where to insert the object. obj - The object to insert. Example: some_function() { me_array = []; me_array[0] = "a"; me_array[1] = "b"; me_array[2] = "c"; me_array = _array_InsertAt(me_array, 0, "e"); for(i = 0; i < me_array.size; i++) self iPrintLn(me_array[i]); // The above will output: // e // a // b // c } */ _array_InsertAt(arr, index, obj) { if(isArray(arr) && arr.size > 0 && isDefined(index) && index >= 0 && index <= arr.size && isDefined(obj)) { if(index == arr.size) { arr[arr.size] = obj; } else { arr[arr.size] = 0; for(i = arr.size - 2; i >= index; i--) arr[i+1] = arr[i]; arr[index] = obj; } } return arr; }

Note that these functions were inspired by the IList Interface and Array Class from the .NET Framework. Wink
[Image: 30xhrep.png]

A casual conversation between barata and I about Nukem.
Reply

#2
In mw2 _utility.gsc you can find plenty of these, I dont know if there are already in BO. Anyway, good job and nice explanations(really detailled)
Reply

#3
http://pastebin.com/DVG5s0A2
U JELLY?
YouTube 1:Azumikkel- Modding
YouTube 2:DerpShark- Gaming Entertainment
Website:Jensby.me
Contact: im[at]jensby.me
Reply

#4
(09-16-2011, 17:09)AZUMIKKEL Wrote: http://pastebin.com/DVG5s0A2
U JELLY?

Quote:common_scripts/utility had some of these functions already.

Learn to read. Dodgy
[Image: 30xhrep.png]

A casual conversation between barata and I about Nukem.
Reply

#5
(09-17-2011, 14:08)master131 Wrote:
(09-16-2011, 17:09)AZUMIKKEL Wrote: http://pastebin.com/DVG5s0A2
U JELLY?

Quote:common_scripts/utility had some of these functions already.

Learn to read. Dodgy

Learn to tell people to learn something they didn't already do Troll
YouTube 1:Azumikkel- Modding
YouTube 2:DerpShark- Gaming Entertainment
Website:Jensby.me
Contact: im[at]jensby.me
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Release] Matrix Functions Yamato 1 4,035 07-29-2013, 20:14
Last Post: Rendflex
  [C# xna 4] 3d models or vertices array narkos 5 6,242 06-25-2013, 08:14
Last Post: narkos
  [Release] nCr and Factorial Functions Yamato 2 3,749 06-18-2013, 17:24
Last Post: Nekochan
  [Release] Random Functions 1.1 Yamato 22 18,199 04-20-2013, 10:13
Last Post: Dominator56
  Functions surtek 14 14,671 09-19-2012, 07:57
Last Post: Pozzuh
Information [Solved] Info about functions, events and etc. Nerus 2 3,549 09-09-2012, 20:57
Last Post: surtek
  Seldom required functions Phl3x_ 3 4,304 06-03-2012, 10:53
Last Post: RaZ
  Help I find This functions and i need your help. Bloodfocus 5 5,106 04-23-2012, 01:43
Last Post: House
  Help Functions iPrintLnBold() [Z00MBY] Alex 7 7,861 04-16-2012, 08:44
Last Post: [Z00MBY] Alex
  [Release] Random Functions Yamato 12 11,776 08-17-2011, 13:07
Last Post: Nekochan

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum Powered By MyBB, Theme by © 2002-2026 Melroy van den Berg.