containers/ | |
License | Copyright © 2007-2011, FreeBASIC Extended Library Development Group |
ext | |
Macros | |
FBEXT_DECLARE_HASHTABLE | |
Types | |
HashTableIterator(type) | This is a prototype for a subroutine for iterating through a hashtable. |
fbext_HashTable(T_) | Simple to use but powerful HashTable overloaded for all built-in numerical types. |
Functions | |
constructor | Defines an initial size for the hashtable. |
Insert | Inserts a value into the hashtable. |
Find | Searches for a key in the table. |
Find | Searches for a value in the table and returns its key. |
ForEach | Iterates through the table calling the passed subroutine with each key pair. |
Remove | Searches for a key in the table and removes it. |
Properties | |
Count | Returns the number of key pairs in the table. |
Examples | |
Basic Usage of the HashTable Class | |
Using HashTable with UDTs | |
Simple HashTable Iterator example |
Copyright © 2007-2011, FreeBASIC Extended Library Development Group
Distributed under the FreeBASIC Extended Library Group license. See accompanying file LICENSE.txt or copy at http://code.google.com
Macros | |
FBEXT_DECLARE_HASHTABLE | |
Types | |
HashTableIterator(type) | This is a prototype for a subroutine for iterating through a hashtable. |
Simple to use but powerful HashTable overloaded for all built-in numerical types.
This class also supports UDTs. The requirements for UDTs are:
Basic Usage of the HashTable Class | Using HashTable with UDTs
Functions | |
constructor | Defines an initial size for the hashtable. |
Insert | Inserts a value into the hashtable. |
Find | Searches for a key in the table. |
Find | Searches for a value in the table and returns its key. |
ForEach | Iterates through the table calling the passed subroutine with each key pair. |
Remove | Searches for a key in the table and removes it. |
Properties | |
Count | Returns the number of key pairs in the table. |
Examples | |
Basic Usage of the HashTable Class | |
Using HashTable with UDTs | |
Simple HashTable Iterator example |
declare sub Insert ( byref key_ as string, byref value as fbext_TypeName(T_) )
Inserts a value into the hashtable.
key_ | the string key to associate with the value. |
value | the value to insert into the table. |
It is very important that you REMOVE a value with a key that would overlap another. If the key is already located in the table, the value will NOT be overwritten.
declare sub ForEach ( byval iter as fbext_HashTableIterator(T_) )
Iterates through the table calling the passed subroutine with each key pair.
iter | the address of the subroutine to call, this subroutine is of the type HashTableIterator(type). |
You may freely change the value passed to the subroutine, but you may not change the key. To change the key you must remove the current key from the table and insert a new one.
# include once "ext/containers/hashtable.bi" using ext var myHT = HashTable(single)(16) print "Count before insertion: " & myHT.count print !"Inserting \"One\" as 1.0" myHT.insert("One", 1.0) print !"Inserting \"Two\" as 2.0" myHT.insert("Two", 2.0) print !"Inserting \"Three\" as 3.0" myHT.insert("Three", 3.0) print !"Inserting \"Tacos\" as 4.2" myHT.insert("Tacos", 4.2) print !"Inserting \"FBEXT\" as 5.6" myHT.insert("FBEXT", 5.6) print "Count after insertion: " & myHT.count print "One: " & *(myHT.find("One")) print "Removing Tacos" myHT.remove("Tacos") print "Two: " & *(myHT.find("Two")) print "Removing Three" myHT.remove("Three") print "FBEXT: " & *(myHT.find("FBEXT")) print "Count after 2 removes: " & myHT.count
# include once "ext/containers/hashtable.bi" type UDT x as integer y as integer declare constructor( ) 'Required by ext.HashTable declare operator let( byref t as UDT ) 'Required by ext.HashTable end type constructor UDT( ) x = 0 y = 0 end constructor 'This operator is only used by the Find by value and return the key method, 'so if you are not using that method you can safely make this operator simply 'return 0 operator = ( byref lhs as UDT, byref rhs as UDT ) as integer 'Required by ext.HashTable return iif( (lhs.x + lhs.y) = ( rhs.x + rhs.y ), ext.true, ext.false ) end operator operator UDT.let( byref t as UDT ) this.x = t.x this.y = t.y end operator namespace ext 'must be declared within ext's namespace fbext_Instanciate( fbExt_HashTable, ((UDT)) ) end namespace dim as FBEXT_HASHTABLE(UDT) MyUDTHT(10) dim as UDT t1, t2, t3 t1.x = 42 : t1.y = 12 t2.x = 134 : t2.y = 1 t3.x = 0 : t3.y = 396 MyUDTHT.Insert("First One", t1) MyUDTHT.Insert("Third One", t3) MyUDTHT.Insert("Ext Rocks", t2) print MyUDTHT.Find("Ext Rocks")->x 'prints 134
# include once "ext/containers/hashtable.bi" declare sub MyIterator ( byref key as const string, byval value as integer ptr ) var MyHT = FBEXT_HASHTABLE(integer)(10) for n as integer = 1 to 10 MyHT.Insert("Number: " & n, n) next MyHT.ForEach( @MyIterator ) sub MyIterator ( byref key as const string, byval value as integer ptr ) print !"\"" & key & !"\" => " & *value end sub
This is a prototype for a subroutine for iterating through a hashtable.
type fbext_HashTableIterator( T_ ) as sub ( byref key as const string, byval value as fbext_TypeName(T_) ptr )
Defines an initial size for the hashtable.
declare constructor ( byval minsize as ext.SizeType )
Inserts a value into the hashtable.
declare sub Insert ( byref key_ as string, byref value as fbext_TypeName(T_) )
Searches for a key in the table.
declare function Find ( byref key_ as string ) as fbext_TypeName(T_) ptr
Iterates through the table calling the passed subroutine with each key pair.
declare sub ForEach ( byval iter as fbext_HashTableIterator(T_) )
Searches for a key in the table and removes it.
declare sub Remove ( byref key_ as string )
Returns the number of key pairs in the table.
declare property Count ( ) as SizeType