Hash from two arrays: Difference between revisions
Content added Content deleted
mNo edit summary |
|||
Line 86: | Line 86: | ||
end Hash_Map_Test;</lang> |
end Hash_Map_Test;</lang> |
||
=={{header|Amazing Hopper}}== |
|||
Example: |
|||
<lang Amazing Hopper> |
|||
#!/usr/bin/hopper |
|||
#include <hopper.h> |
|||
main: |
|||
new hash(h) |
|||
add hash(h,"chile" :: 100) |
|||
add hash(h,"argentina"::200) |
|||
add hash(h,"brasil"::300) |
|||
{"\nLongitud HASH: "},len hash(h),println |
|||
println(hash(h)) |
|||
println( get value("argentina",h) ) |
|||
println( get value("chile",h) ) |
|||
try |
|||
println( get value("guyana",h) ) |
|||
catch(e) |
|||
{"***[",e,"]: "}get str error,println |
|||
finish |
|||
println( get key(300,h) ) |
|||
println( get key(100,h) ) |
|||
mod value("chile",101,h) |
|||
mod key(200,"madagascar",h) |
|||
try |
|||
mod key(130,"londres",h) |
|||
println( get key(130,h) ) |
|||
catch(e) |
|||
{"***[",e,"]: "}get str error,println |
|||
finish |
|||
println( get key(200,h) ) |
|||
println( get value("chile",h) ) |
|||
println("HASH actual: \n") |
|||
println(hash(h)) |
|||
{"\nLongitud HASH: "},len hash(h),println |
|||
put after value (200,"colombia",400,h) |
|||
put value (200,"mexico",401,h) |
|||
println(hash(h)) |
|||
put after key ("chile","antartida",110,h) |
|||
put key ("chile","peru",99,h) |
|||
println("HASH actual: \n") |
|||
println(hash(h)) |
|||
{"\nLongitud HASH: "},len hash(h),println |
|||
del by key("brasil",h) |
|||
del by value(101,h) |
|||
{"\nLongitud HASH: "},len hash(h),println |
|||
println(hash(h)) |
|||
sort hash(h) |
|||
{"\nORDENADO: \n"}println |
|||
println(hash(h)) |
|||
y={} // para un stack de arreglos. |
|||
x=0,{5,5}rand array(x) |
|||
{x}push(y) |
|||
add hash(h,"arreglo 1"::x) |
|||
{"Ojalá que llueva café en el campo"}strtoutf8,push(y) |
|||
clear(x),w=0,{6,2}rand array(w) |
|||
{w}push(y) |
|||
add hash(h,"arreglo 2"::w) |
|||
clear(w) |
|||
add hash(h,"objeto",y) |
|||
println(hash(h)) |
|||
println("arreglo 2?\n") |
|||
get value("arreglo 2",h) |
|||
println |
|||
println("arreglo 1?\n") |
|||
get value("arreglo 1",h) |
|||
println |
|||
/* NO PUEDES ORDENAR UN HASH QUE CONTENGA ARRAYS |
|||
PORQUE SE BLOQUEARA EL PROGRAMA: |
|||
sort hash(h) |
|||
{"\nORDENADO: \n"}println |
|||
println(hash(h)) */ |
|||
println("Objeto?\n") |
|||
get value("objeto",h) |
|||
z=0,mov(z) |
|||
/* Esto fallará, porque no se puede hacer un |
|||
push de pushs*/ |
|||
pop(z),println |
|||
pop(z),println |
|||
pop(z),println |
|||
{"Esto significa que no puedes meter un stack dentro de un hash\nsolo arrays de cualquier dimension"}println |
|||
/* esto está bien, porque es un stack simple |
|||
aunque contenga arreglos como elementos. */ |
|||
pop(y),println |
|||
{"Dato en la última posición del stack:"}strtoutf8,{"\n"},[1:end,1:end]get(y),println |
|||
{"Esto significa que, si vas a meter arreglos dentro de un stack\nsácalos con POP antes de usarlo"}strtoutf8,println |
|||
pop(y),println |
|||
pop(y),println |
|||
pause |
|||
exit(0) |
|||
</lang> |
|||
Macros used in the example, located in "hopper.h". |
|||
(Observation: some of these macros will be converted to libraries, due to their extension.) |
|||
<lang Amazing Hopper> |
|||
/* macros HASH */ |
|||
#defn createhash(_X_) _X__KEY={#VOID},_X__HASH={#VOID} |
|||
#synon createhash newhash |
|||
#defn addhash(_X_,_K_,_H_) {_H_}push(_X__HASH),{_K_}push(_X__KEY) |
|||
#defn getvalue(_X_,_Y_) _Y_03Rx0W91=0,{_X_,_Y__KEY},array(1),dup,zero?do{{"getvalue: key not found"}throw(2000)}\ |
|||
mov(_Y_03Rx0W91),[_Y_03Rx0W91]get(_Y__HASH),clearmark, |
|||
#defn getkey(_X_,_Y_) _Y_03Rx0W91=0,{_X_,_Y__HASH},array(1),dup,zero?do{{"getkey: value not found"}throw(2001)}\ |
|||
mov(_Y_03Rx0W91),[_Y_03Rx0W91]get(_Y__KEY),clearmark, |
|||
#defn modvalue(_K_,_H_,_X_) _Y_03Rx0W91=0,{_K_,_X__KEY},array(1),dup,zero?do{{"modvalue: key not found"}throw(2002)}\ |
|||
mov(_Y_03Rx0W91),[_Y_03Rx0W91]{_H_}put(_X__HASH),clearmark, |
|||
#defn modkey(_H_,_K_,_X_) _Y_03Rx0W91=0,{_H_,_X__HASH},array(1),dup,zero?do{{"modkey: value not found"}throw(2003)}\ |
|||
mov(_Y_03Rx0W91),[_Y_03Rx0W91]{_K_}put(_X__KEY),clearmark, |
|||
#defn putaftervalue(_H_,_K_,_V_,_X_) _X_03Rx0W91=0,{_H_,_X__HASH},array(1),dup,zero?do{{"putaftervalue: value not found"}throw(2006)}\ |
|||
plus(1),mov(_X_03Rx0W91),{_K_}{_X_03Rx0W91,_X__KEY}array(3),\ |
|||
{_V_}{_X_03Rx0W91,_X__HASH}array(3) |
|||
#defn putvalue(_H_,_K_,_V_,_X_) _X_03Rx0W91=0,{_H_,_X__HASH},array(1),dup,zero?do{{"putvalue: value not found"}throw(2006)}\ |
|||
mov(_X_03Rx0W91),{_K_}{_X_03Rx0W91,_X__KEY}array(3),\ |
|||
{_V_}{_X_03Rx0W91,_X__HASH}array(3), |
|||
#defn putafterkey(_H_,_K_,_V_,_X_) _X_03Rx0W91=0,{_H_,_X__KEY},array(1),dup,zero?do{{"putafterkey: key not found"}throw(2007)}\ |
|||
plus(1),mov(_X_03Rx0W91),{_K_}{_X_03Rx0W91,_X__KEY}array(3),\ |
|||
{_V_}{_X_03Rx0W91,_X__HASH}array(3), |
|||
#defn putkey(_H_,_K_,_V_,_X_) _X_03Rx0W91=0,{_H_,_X__KEY},array(1),dup,zero?do{{"putkey: value not found"}throw(2008)}\ |
|||
mov(_X_03Rx0W91),{_K_}{_X_03Rx0W91,_X__KEY}array(3),\ |
|||
{_V_}{_X_03Rx0W91,_X__HASH}array(3), |
|||
#defn delbyvalue(_H_,_X_) {_H_,_X__HASH},array(1),dup,zero?do{{"delbyvalue: value not found"}throw(2004)},\ |
|||
{_X__KEY},keep,array(4),{_X__HASH},array(4),clearstack |
|||
#defn delbykey(_K_,_X_) {_K_,_X__KEY},array(1),dup,zero?do{{"delbykey: key not found"}throw(2005)},\ |
|||
{_X__KEY},keep,array(4),{_X__HASH},array(4),clearstack |
|||
#defn sorthash(_X_) #RAND,_LEN_#RNDV=0,_DUP_H#RNDV=_X__HASH,_DUP_K#RNDV=_X__KEY,\ |
|||
{_X__KEY}keep,length,mov(_LEN_#RNDV),array(0),\ |
|||
_POS_#RNDV=0,_HASH_LOOP_#RNDV:,[_LEN_#RNDV]get(_X__KEY),{_DUP_K#RNDV}array(1),\ |
|||
mov(_POS_#RNDV),[_POS_#RNDV]get(_DUP_H#RNDV),[_LEN_#RNDV]put(_X__HASH),\ |
|||
--_LEN_#RNDV,{_LEN_#RNDV},jnz(_HASH_LOOP_#RNDV),clear(_DUP_H#RNDV),clear(_DUP_K#RNDV) |
|||
#defn lenhash(_X_) {_X__HASH}length, |
|||
#defn hash(_X_) #RAND,_TMP_#RNDV=0,{_X__HASH,_X__KEY}catcol(_TMP_#RNDV),{_TMP_#RNDV},clear(_TMP_#RNDV), |
|||
/* Other... */ |
|||
/* TRY/CATCH */ |
|||
#defn try swtrap( #CATCH ), |
|||
#defn raise(_ERR_,_M_) {_M_}, throw(_ERR_), |
|||
#defn catch(_X_) jmp(#ENDCATCH), %CATCH:, clearstack,_X_=0, gettry(_X_), // gettry hace poptry internamente? |
|||
#defn finish %ENDCATCH:, popcatch |
|||
/* print... */ |
|||
#defn println(_X_) #ATOM #CMPLX,{"\n"} print |
|||
#define println {"\n"}print |
|||
</lang> |
|||
=={{header|Argile}}== |
=={{header|Argile}}== |