Advertisement
Guest User

BASH: associative array implementation using hashes.

a guest
Apr 17th, 2012
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.92 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. : ${_PROF_INIT:=-1}
  4. : ${_PROF_START:=0}
  5. : ${_PROF_STDOUT:=1}
  6.  
  7. function __log() {
  8.         local fmt=$1
  9.         if [ $DEBUG -eq 1 ]; then
  10.                         shift 1
  11.                         if [ "x${fmt//\\n}" == "x${fmt}" ]; then
  12.                                 fmt="${fmt}\n";
  13.                         fi
  14.                 printf "${fmt}" "$@" 1>&2
  15.         fi
  16. }
  17.  
  18. function _profiler_calc () {
  19.         echo "$@" | bc -l | xargs printf "%7.3f\n"
  20. }
  21.  
  22. function _profiler_ts() {
  23.         python -c'import time; print repr(time.time())'
  24. }
  25.  
  26. function _profiler_mark() {
  27.         _PROF_START=$(_profiler_ts)
  28.         if [ "x${_PROF_INIT}" == "x-1" ]; then
  29.                 _PROF_INIT=${_PROF_START}
  30.         fi
  31. }
  32.  
  33. function _profiler_elapsed() {
  34.         local ARGS=$@
  35.         NOW="$(_profiler_ts)"
  36.         ELAPSED=$(_profiler_calc "${NOW} - ${_PROF_START}")
  37.         ELAPSED_INIT=$(_profiler_calc "${NOW} - ${_PROF_INIT}")
  38.         if [ ${_PROF_STDOUT} -eq 1 ]; then
  39.                 printf "%s\n" "$ELAPSED [${ELAPSED_INIT}] ${ARGS}"
  40.         else
  41.                 __log "%s\n" "$ELAPSED [${ELAPSED_INIT}] ${ARGS}"
  42.         fi
  43. }
  44.  
  45. function __hash_init() {
  46.         local name=$1
  47.         if [ $(hash_size ${name}) -eq 0 ]; then
  48.                 __log "%s\n" "INIT: [$name]"
  49.                 eval __hash_${name}___ptr="__NULL__"
  50.         fi
  51. }
  52.  
  53. function hash_put() {
  54.         local name=$1 key=$2 val=$3 exist=0 ptr
  55.         key=$(echo "${key}" | sed -e 's/(\(.*\))/\1/g' | tr -d ' ')
  56.         #key=$(echo "${key}" | md5)
  57.         __hash_init ${name}
  58.         if [ "x$key" == "x__auto" ]; then
  59.                 ptr=$(hash_get ${name} __ptr)
  60.                 if [ "x${ptr}" == "x__NULL__" ]; then
  61.                         random=1
  62.                         #random=${RANDOM}
  63.                         #random=$(od -vAn -N4 -tu4 < /dev/random)
  64.                         ptr=${random}
  65.                 else
  66.                         let ptr+=1;
  67.                 fi
  68.                 key=$ptr
  69.                 __log "%s\n" "AUTO: [$name] key=$key"
  70.                 eval __hash_${name}___ptr=${ptr}
  71.         fi
  72.         __log "%s\n" "PUT : [$name] key=$key value=$val"
  73.         eval __hash_${name}_${key}=${val}
  74. }
  75.  
  76. function hash_get() {
  77.         local name=$1 key=$2 _var var
  78.         key=$(echo "${key}" | sed -e 's/(\(.*\))/\1/g' | tr -d ' ')
  79.         #key=$(echo "${key}" | md5)
  80.         _var=__hash_${name}_${key}
  81.         var=${!_var}
  82.         __log "%s\n" "FIND: [$name] key=$key ret=$var {${_var}}"
  83.         echo ${var}
  84. }
  85.  
  86. function hash_clear() {
  87.         local name=$1 res
  88.         res=$(compgen -A variable __hash_${name} | xargs)
  89.         __log "%s\n" "CLR : name=${name} [$res]"
  90.         unset $res
  91. }
  92.  
  93. function hash_keys() {
  94.         local name=$1
  95.         list=$(compgen -A variable __hash_${name} | sed -e "s/__hash_${name}_//" | grep -v "__ptr")
  96.         __log "=======> LIST: %s\n" $list
  97.         echo $list
  98. }
  99.  
  100. function hash_size() {
  101.         local name=$1
  102.         val=$(hash_keys ${name} | wc -l)
  103.         echo $val
  104. }
  105.  
  106. function hash_print() {
  107.         local name=$1
  108.         __log "%s\n" "PRN : [${name}]"
  109.         for key in $(hash_keys ${name}); do
  110.                 hash_get ${name} "${key}"
  111.         done
  112. }
  113.  
  114. : ${DEBUG:=0}
  115.  
  116. hash_put lookup "entry1" 1
  117. hash_put lookup "__empty__" 13
  118.  
  119. _profiler_mark
  120. for ((i=0; i<4000; i++)); do
  121.         hash_put testtable "key_${i}_entry" $i
  122. done
  123. _profiler_elapsed "insert testtable"
  124.  
  125. hash_size testtable
  126. #while IFS=\; read key val _ _; do
  127. #       __log "%s\n" "READ: key=$key"
  128. #       if [ "x$key" == "x" ]; then
  129. #               key="__empty__"
  130. #       fi
  131. #       idx=$(hash_get lookup ${key})
  132. #       hash_put table_visitas_$idx 19 "$val"
  133. #done < <(echo ";param2;param3;param4")
  134.  
  135. #for key in $(hash_keys lookup); do
  136. #       idx=$(hash_get lookup "$key")
  137. #       #hash_size table_visitas_$idx
  138. #       printf "=======> FOUND [table_visitas_$idx]: %s\n" $(hash_print table_visitas_$idx)
  139. #done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement