My last include for today, working with hashtables (key+value pairs or maps) is something that's been discussed in the forums a few times recently, this is my solution to the problem
supports the following functions
HashLoad(table, filepath) - Loads a saved HashTable
HashSave(table, filepath) - Saves a HashTable to disk
HashSetString(table, key, value) - Sets a string value
HashSetFloat(table, key, value) - Sets a float value
HashSetInt(table, key, value) - Sets a integer value()
HashGetString(table, key) - Gets a string value
HashGetFloat(table, key) - Gets a float value
HashGetInt(table, key) - Gets a integer value
HashCount(table) - Counts the number of hash pairs
HashRemove(table, key) - removes a hash pair
HashGetKey(table, index) - Gets the name of a key by its index
EDIT: Updated to use .insertsorted() and .sort() on item removal.
HashTable.agc
// Project: HashTable Include
// Created: 2017-06-04 By PartTimeCoder
/*
HashLoad(table, filepath) - Loads a saved HashTable
HashSave(table, filepath) - Saves a HashTable to disk
HashSetString(table, key, value) - Sets a string value
HashSetFloat(table, key, value) - Sets a float value
HashSetInt(table, key, value) - Sets a integer value()
HashGetString(table, key) - Gets a string value
HashGetFloat(table, key) - Gets a float value
HashGetInt(table, key) - Gets a integer value
HashCount(table) - Counts the number of hash pairs
HashRemove(table, key) - removes a hash pair
HashGetKey(table, index) - Gets the name of a key by its index
*/
Type HashPair
key as string
valueS as string
valueI as integer
valueF as float
EndType
Function HashSetString(table ref as HashPair[], key as string, value as string)
index = table.find(key)
if index>-1
table[index].valueS=value
else
tmp as HashPair
tmp.key=key
tmp.valueS=value
table.insertsorted(tmp)
endif
EndFunction
Function HashSetFloat(table ref as HashPair[], key as string, value as float)
index = table.find(key)
if index>-1
table[index].valueF=value
else
tmp as HashPair
tmp.key=key
tmp.valueF=value
table.insertsorted(tmp)
endif
EndFunction
Function HashSetInt(table ref as HashPair[], key as string, value as integer)
index = table.find(key)
if index>-1
table[index].valueI=value
else
tmp as HashPair
tmp.key=key
tmp.valueI=value
table.insertsorted(tmp)
endif
EndFunction
Function HashCount(table ref as HashPair[])
result = table.length
EndFunction result
Function HashRemove(table ref as HashPair[], key as string)
index = table.find(key)
if index > -1
table.remove(index)
table.sort()
endif
EndFunction
Function HashGetString(table as HashPair[], key as string)
result as string
index = table.find(key)
if index > -1
result=table[index].valueS
endif
EndFunction result
Function HashGetFloat(table as HashPair[], key as string)
result as float
index = table.find(key)
if index > -1
result=table[index].valueF
endif
EndFunction result
Function HashGetInt(table as HashPair[], key as string)
result as integer
index = table.find(key)
if index > -1
result=table[index].valueI
endif
EndFunction result
Function HashGetKey(table as HashPair[], index)
result as string
if index<=table.length
result = table[index].key
endif
EndFunction result
Function HashSave(table as HashPair[], filepath as string)
result as integer
file = OpenToWrite(filepath, 0)
if file
for index=0 to table.length
WriteLine(file, table[index].key+"="+table[index].valueS)
WriteLine(file, table[index].key+"="+str(table[index].valueF))
WriteLine(file, table[index].key+"="+str(table[index].valueI))
next
result=1
CloseFile(file)
else
result=0
endif
EndFunction result
Function HashLoad(table ref as HashPair[], filepath as string)
result as integer
file = OpenToRead(filepath)
if file
while FileEOF(file) = 0
pair1$ = ReadLine(file)
if pair1$<>""
tmp as HashPair
tmp.key=GetStringToken(pair1$, "=", 1)
tmp.valueS = GetStringToken(pair1$, "=", 2)
tmp.valueF = ValFloat(GetStringToken(ReadLine(file), "=", 2))
tmp.valueI = Val(GetStringToken(ReadLine(file), "=", 2))
table.insertsorted(tmp)
endif
endwhile
CloseFile(file)
result=1
else
result=0
endif
EndFunction result
I'v lots of other 'drop in' includes but most are unfinished, I'll see if I can get some ready for posting.