PDA

View Full Version : Associative Array Class


JkWhoSaysNi
09-09-2006, 09:37 PM
This gives a completley functional associative array class so you can store an array using string indexes not integers, also you can iterate through it.

//Associative array by JkWhoSaysNi
//#CLIENTSIDE
function duplicates(array) {
//recursivly check for duplicates in an array
duplicate = false;
if(array.size() != 0) {
for (i = 1; i <= array.size()-1; i++ ) {
if (array[0] == array[i]) {
duplicate = true;
}
}
if (duplicate == false) {
array.delete(0);
duplicate = duplicates(array);
}
}
return duplicate;
}
/////////////////
//Constructor
/////////////////
public function make(keys,values) {

//required because keys is passed as a reference to duplicates, which then empties it.
this.tempkeys = keys;

if (keys.size() != values.size() || duplicates(this.tempkeys)) return false;
else {
this.cursor = 0;
this.keys = keys;
this.values = values;
return true;
}
}
/////////////////
//Access
/////////////////
public function has(key) {
return (this.keys.index(key) >= 0);
}
public function item(key) {
if (has(key)) return this.values[this.keys.index(key)];
else return false;
}
public function has_item(value) {
return (this.values.index(value) >= 0);
}
public function keys() {
return this.keys;
}
public function values() {
return this.values;
}
public function item_for_iteration() {
return this.values[this.cursor];
}
public function key_for_iteration() {
return this.keys[this.cursor];
}
public function cursor() {
return this.cursor;
}
public function count() {
return this.values.size();
}
public function off() {
//has the cursor reached the end of the array?
return (this.cursor > this.keys.size()-1);
}

/////////////////
//Element change
/////////////////
public function put(key,value) {
//add or replace
if (has(key)) {
this.values[this.keys.index(key)] = value;
return 1;
}
else {
this.keys.add(key);
this.values.add(value);
return 2;
}
}
public function remove_item(key) {
if (has(key)) {
this.values.delete(this.keys.index(key));
this.keys.delete(this.keys.index(key));
return true;
}
else return false;
}

public function replace_key(oldkey,newkey) {
if (has(oldkey)) {
this.keys[this.keys.index(oldkey)] = newkey;
return true;
}
else return false;
}
public function clear_all() {
this.keys.clear();
this.values.clear();
this.cursor = 0;
}
/////////////////
//Cursor movement
/////////////////
public function forth() {
if (!off()) {
this.cursor++;
return true;
}
else return false;
}
public function back() {
if (this.cursor > this.keys.size()-1) {
this.cursor--;
return true;
}
else return false;
}
public function start() {
//Move cursor back to the start
this.cursor = 0;
}
public function end() {
this.cursor = this.keys.size()-1;
}
//
//invariant this.keys.size() == this.values.size();
//invariant this.cursor < this.values.size();
// would be nice but not supported.


usage:


arr = new TStaticVar();
arr.join("associative_array");

//create the array. first param is an array of keys, .second is values.
//if it works will return true, if there is a problem (e.g. 2 keys the same) it will return false.
arr.make({"a","b","c"},{"one","two","three"});
//so it's possible to do things like:
if (arr.make(....)) {

}

//add another element
arr.put("d","four");

//retrieve value for key "d"
var = arr.item("d"); //var == "four"

//change value for key
arr.put("d","five"); //arr.item("d") == "five"

//remove item
arr.remove_item("d");

//iterate through the array
for(arr.start();!arr.off();arr.forth()) {
player.chat = player.chat @ " " @ arr.item_for_iteration();
}


theres a few more functions you can see if you look at the code. They're pretty self explanitory.

Enjoy! Hope this helps some people out.