Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 09-09-2006, 08:37 PM
JkWhoSaysNi JkWhoSaysNi is offline
Ruler of the graalaxy
Join Date: Feb 2005
Location: Great Britain
Posts: 488
JkWhoSaysNi is on a distinguished road
Send a message via ICQ to JkWhoSaysNi
Associative Array Class

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.

PHP Code:
//Associative array by JkWhoSaysNi
//#CLIENTSIDE
function duplicates(array) {
  
//recursivly check for duplicates in an array
  
duplicate false;
  if(array.
size() != 0) {
    for (
1<= array.size()-1i++ ) {
      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:

PHP Code:
 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.
__________________

Coming soon (Hopefully:P)
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 06:06 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.