por vcatafesta » 25 Jul 2021 19:44
Olá pessoal,
Minha contribuição de hash em classe.
#include "hbclass.ch"
CLASS THash
EXPORTED:
DATA xNil INIT nil
DATA Hash INIT {=>}
public:
METHOD new CONSTRUCTOR
DESTRUCTOR Destroy()
METHOD Create()
METHOD Value(xHash, xValue) SETGET
METHOD Execute(xValue, nRow, nCol)
METHOD Position(nPosition)
MESSAGE SetValue method value
MESSAGE GetValue method value
MESSAGE add method value
ENDCLASS
METHOD New()
::Hash := ::Create()
return self
METHOD Destroy()
self := nil
return self
METHOD Create()
LOCAL aHash := {=>}
HSetAutoAdd( aHash, .T.)
HSetCaseMatch( aHash, .F. ) // desabilita o case-sensitive
return aHash
METHOD value(xHash, xValue)
if pcount() == 2
::Hash[xHash] := xValue
endif
return ::Hash[xHash]
METHOD execute(xValue, nRow, nCol)
LOCAL bBlock := ::Hash[xValue]
if pcount() == 1
if HB_ISBLOCK( bBlock )
eval( bBlock)
return self
endif
endif
if pCount() = 2
hb_default(@nRow, 2)
if HB_ISBLOCK( bBlock[nRow])
eval( bBlock[nRow])
return self
endif
endif
if pCount() > 2
hb_default(@nRow, 2)
hb_default(@nCol, 1)
if HB_ISBLOCK( bBlock[nRow, nCol])
eval( bBlock[nRow, nCol])
return self
endif
endif
return self
METHOD position(nPosition, nPos)
if pcount() == 1
return hb_hPairAt( ::Hash, nPosition )
endif
hb_default(@nPos, 1)
return hb_hPairAt(::Hash, nPosition)[nPos]
def THashNew()
return( THash():New())
endef
Codigo para testes:
function main
LOCAL oh := THashNEw()
oh:setvalue("COM1", {||Funcao2()})
oh:setvalue("COM2", 'COM2')
oh:add("COM3", 'COM3')
altd()
oh:execute('COM1')
? oh:getvalue('COM2')
? oh:getvalue('COM3')
oh:setvalue("MULTI", {{||Funcao2()}, 1.01})
oh:execute('MULTI',1)
? oh:getvalue('MULTI')[2]
oh:SetValue(6.01, {"Layout Janela", {||Funcao3() }})
? menu := oh:position(5)[1]
? oh:execute(6.01, 2)
inkey(0)
function funcao2()
alert("OK")
return nil
function funcao3()
alert("FUNCAO 3")
return nil
Harbour, C, C++, Ruby, Python, Go, Delphi, Haskell, Html, PHP, mingw, gtwvg, multithread, dbfcdx, letodb
=================================================
Senhor, sem Ti, nada podemos fazer!