Dictionary

From VRwiki
Jump to: navigation, search

Status

Last Update: 2009/07/29

Motivations

Dictionary and Sets are missing in XVR and for many applications they are fundamental. This small code allows provides a Hashtable that can be usef for Dictionary

[dictxvr.zip (3KB)]

Keys and Hash

Every key have to be an hash. These are the functions used depending on the type:

  • String: vocong hash function
  • Number: integer
  • Object: looks for __hash__

Usage

function OnInit()
{
	var x = Hashtable();
	x.put("Hello",1);
	x.put("Ciao",2);
	OutputLN(x.get("Hello"));
	OutputLN(x.get("Ciao"));
	OutputLN(x.size());
	OutputLN(x.del("Hello"));
	OutputLN(x.get("Ciao"));
	OutputLN(x.get("Hello"));
	OutputLN(x.del("Ciao"));
	OutputLN(x.get("Ciao"));
}

Interface

class Hashtable
{
	
	get(key); /// gets key
	put(key,value); /// put key and value (optional)
	del(key); /// deletes key
	contains(key); /// contains key
	clear(); /// cleanup
	size(); /// number of elements
 
	var xdata;   // array of buckets
	var xsize;   // size
};


TODO

  • List of items, that requires linked list of buckets
  • Alternative Native implementation based on external DLL
  • move to internal support