1*906afcb8SAndy Fiddaman#!/usr/bin/ksh93 2*906afcb8SAndy Fiddaman 3*906afcb8SAndy Fiddaman# 4*906afcb8SAndy Fiddaman# CDDL HEADER START 5*906afcb8SAndy Fiddaman# 6*906afcb8SAndy Fiddaman# The contents of this file are subject to the terms of the 7*906afcb8SAndy Fiddaman# Common Development and Distribution License (the "License"). 8*906afcb8SAndy Fiddaman# You may not use this file except in compliance with the License. 9*906afcb8SAndy Fiddaman# 10*906afcb8SAndy Fiddaman# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 11*906afcb8SAndy Fiddaman# or http://www.opensolaris.org/os/licensing. 12*906afcb8SAndy Fiddaman# See the License for the specific language governing permissions 13*906afcb8SAndy Fiddaman# and limitations under the License. 14*906afcb8SAndy Fiddaman# 15*906afcb8SAndy Fiddaman# When distributing Covered Code, include this CDDL HEADER in each 16*906afcb8SAndy Fiddaman# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 17*906afcb8SAndy Fiddaman# If applicable, add the following below this CDDL HEADER, with the 18*906afcb8SAndy Fiddaman# fields enclosed by brackets "[]" replaced with your own identifying 19*906afcb8SAndy Fiddaman# information: Portions Copyright [yyyy] [name of copyright owner] 20*906afcb8SAndy Fiddaman# 21*906afcb8SAndy Fiddaman# CDDL HEADER END 22*906afcb8SAndy Fiddaman# 23*906afcb8SAndy Fiddaman 24*906afcb8SAndy Fiddaman# 25*906afcb8SAndy Fiddaman# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 26*906afcb8SAndy Fiddaman# 27*906afcb8SAndy Fiddaman 28*906afcb8SAndy Fiddaman# 29*906afcb8SAndy Fiddaman# numtree1 - basic compound variable tree demo+benchmark 30*906afcb8SAndy Fiddaman# 31*906afcb8SAndy Fiddaman 32*906afcb8SAndy Fiddaman# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant 33*906afcb8SAndy Fiddamanexport PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin 34*906afcb8SAndy Fiddaman 35*906afcb8SAndy Fiddaman# Make sure all math stuff runs in the "C" locale to avoid problems 36*906afcb8SAndy Fiddaman# with alternative # radix point representations (e.g. ',' instead of 37*906afcb8SAndy Fiddaman# '.' in de_DE.*-locales). This needs to be set _before_ any 38*906afcb8SAndy Fiddaman# floating-point constants are defined in this script). 39*906afcb8SAndy Fiddamanif [[ "${LC_ALL}" != "" ]] ; then 40*906afcb8SAndy Fiddaman export \ 41*906afcb8SAndy Fiddaman LC_MONETARY="${LC_ALL}" \ 42*906afcb8SAndy Fiddaman LC_MESSAGES="${LC_ALL}" \ 43*906afcb8SAndy Fiddaman LC_COLLATE="${LC_ALL}" \ 44*906afcb8SAndy Fiddaman LC_CTYPE="${LC_ALL}" 45*906afcb8SAndy Fiddaman unset LC_ALL 46*906afcb8SAndy Fiddamanfi 47*906afcb8SAndy Fiddamanexport LC_NUMERIC=C 48*906afcb8SAndy Fiddaman 49*906afcb8SAndy Fiddamanfunction fatal_error 50*906afcb8SAndy Fiddaman{ 51*906afcb8SAndy Fiddaman print -u2 "${progname}: $*" 52*906afcb8SAndy Fiddaman exit 1 53*906afcb8SAndy Fiddaman} 54*906afcb8SAndy Fiddaman 55*906afcb8SAndy Fiddamanfunction add_number_to_tree 56*906afcb8SAndy Fiddaman{ 57*906afcb8SAndy Fiddaman typeset treename=$1 58*906afcb8SAndy Fiddaman integer num=$2 59*906afcb8SAndy Fiddaman integer i 60*906afcb8SAndy Fiddaman typeset nodepath # full name of compound variable 61*906afcb8SAndy Fiddaman integer -a pe # path elements 62*906afcb8SAndy Fiddaman integer len 63*906afcb8SAndy Fiddaman typeset revnums="$(rev <<<"${num}")" 64*906afcb8SAndy Fiddaman 65*906afcb8SAndy Fiddaman # first built an array containing the names of each path element 66*906afcb8SAndy Fiddaman # (e.g. "135" results in an array containing "( 1 3 5 )") 67*906afcb8SAndy Fiddaman # 10#<number> is used to prevent leading zeros being interpreted 68*906afcb8SAndy Fiddaman # as octals 69*906afcb8SAndy Fiddaman for (( len=${#revnums} , i=$( printf "10#%s\n" "${revnums}" ) ; len > 0 ; len--, i=i/10 )) ; do 70*906afcb8SAndy Fiddaman pe+=( $((i % 10)) ) 71*906afcb8SAndy Fiddaman done 72*906afcb8SAndy Fiddaman 73*906afcb8SAndy Fiddaman # walk path described via the "pe" array and build nodes if 74*906afcb8SAndy Fiddaman # there aren't any nodes yet 75*906afcb8SAndy Fiddaman nodepath="${treename}" 76*906afcb8SAndy Fiddaman for (( i=0 ; i < ${#pe[@]} ; i++ )) ; do 77*906afcb8SAndy Fiddaman nameref x="${nodepath}" 78*906afcb8SAndy Fiddaman 79*906afcb8SAndy Fiddaman # [[ -v ]] does not work for arrays because [[ -v ar ]] 80*906afcb8SAndy Fiddaman # is equal to [[ -v ar[0] ]]. In this case we can 81*906afcb8SAndy Fiddaman # use the output of typeset +p x.nodes 82*906afcb8SAndy Fiddaman [[ "${ typeset +p x.nodes ;}" == "" ]] && compound -a x.nodes 83*906afcb8SAndy Fiddaman 84*906afcb8SAndy Fiddaman nodepath+=".nodes[${pe[i]}]" 85*906afcb8SAndy Fiddaman done 86*906afcb8SAndy Fiddaman 87*906afcb8SAndy Fiddaman # insert element (leaf) 88*906afcb8SAndy Fiddaman nameref node="${nodepath}" 89*906afcb8SAndy Fiddaman [[ "${ typeset +p node.elements ;}" == "" ]] && integer -a node.elements 90*906afcb8SAndy Fiddaman node.elements+=( ${num} ) 91*906afcb8SAndy Fiddaman 92*906afcb8SAndy Fiddaman # DEBUG only 93*906afcb8SAndy Fiddaman [[ "${!node.elements[*]}" != "" ]] || fatal_error "assertion $LINENO FAILED" 94*906afcb8SAndy Fiddaman [[ "${ typeset +p node.elements ;}" == *-a* ]] || fatal_error "assertion $LINENO FAILED" 95*906afcb8SAndy Fiddaman [[ "${ typeset +p node.elements ;}" == *-i* ]] || fatal_error "assertion $LINENO FAILED" 96*906afcb8SAndy Fiddaman [[ -v node ]] || fatal_error "assertion $LINENO FAILED" 97*906afcb8SAndy Fiddaman [[ -R node ]] || fatal_error "assertion $LINENO FAILED" 98*906afcb8SAndy Fiddaman [[ "${ typeset +p ${!node} ;}" == *-C* ]] || fatal_error "assertion $LINENO FAILED" 99*906afcb8SAndy Fiddaman [[ "${!x.nodes[*]}" != "" ]] || fatal_error "assertion $LINENO FAILED" 100*906afcb8SAndy Fiddaman [[ "${ typeset +p x.nodes ;}" == *-a* ]] || fatal_error "assertion $LINENO FAILED" 101*906afcb8SAndy Fiddaman [[ "${ typeset +p x.nodes ;}" == *-C* ]] || fatal_error "assertion $LINENO FAILED" 102*906afcb8SAndy Fiddaman 103*906afcb8SAndy Fiddaman return 0 104*906afcb8SAndy Fiddaman} 105*906afcb8SAndy Fiddaman 106*906afcb8SAndy Fiddaman 107*906afcb8SAndy Fiddaman# floating-point version of "seq" 108*906afcb8SAndy Fiddamanfunction floatseq 109*906afcb8SAndy Fiddaman{ 110*906afcb8SAndy Fiddaman float i 111*906afcb8SAndy Fiddaman float arg1=$1 112*906afcb8SAndy Fiddaman float arg2=$2 113*906afcb8SAndy Fiddaman float arg3=$3 114*906afcb8SAndy Fiddaman 115*906afcb8SAndy Fiddaman case $# in 116*906afcb8SAndy Fiddaman 1) 117*906afcb8SAndy Fiddaman for (( i=1. ; i <= arg1 ; i=i+1. )) ; do 118*906afcb8SAndy Fiddaman printf "%a\n" i 119*906afcb8SAndy Fiddaman done 120*906afcb8SAndy Fiddaman ;; 121*906afcb8SAndy Fiddaman 2) 122*906afcb8SAndy Fiddaman for (( i=arg1 ; i <= arg2 ; i=i+1. )) ; do 123*906afcb8SAndy Fiddaman printf "%a\n" i 124*906afcb8SAndy Fiddaman done 125*906afcb8SAndy Fiddaman ;; 126*906afcb8SAndy Fiddaman 3) 127*906afcb8SAndy Fiddaman for (( i=arg1 ; i <= arg3 ; i+=arg2 )) ; do 128*906afcb8SAndy Fiddaman printf "%a\n" i 129*906afcb8SAndy Fiddaman done 130*906afcb8SAndy Fiddaman ;; 131*906afcb8SAndy Fiddaman *) 132*906afcb8SAndy Fiddaman print -u2 -f "%s: Illegal number of arguments %d\n" "$0" $# 133*906afcb8SAndy Fiddaman return 1 134*906afcb8SAndy Fiddaman ;; 135*906afcb8SAndy Fiddaman esac 136*906afcb8SAndy Fiddaman 137*906afcb8SAndy Fiddaman return 0 138*906afcb8SAndy Fiddaman} 139*906afcb8SAndy Fiddaman 140*906afcb8SAndy Fiddaman 141*906afcb8SAndy Fiddamanfunction usage 142*906afcb8SAndy Fiddaman{ 143*906afcb8SAndy Fiddaman OPTIND=0 144*906afcb8SAndy Fiddaman getopts -a "${progname}" "${numtree1_usage}" OPT '-?' 145*906afcb8SAndy Fiddaman exit 2 146*906afcb8SAndy Fiddaman} 147*906afcb8SAndy Fiddaman 148*906afcb8SAndy Fiddaman# main 149*906afcb8SAndy Fiddamanbuiltin basename 150*906afcb8SAndy Fiddamanbuiltin rev 151*906afcb8SAndy Fiddaman 152*906afcb8SAndy Fiddamanset -o noglob 153*906afcb8SAndy Fiddamanset -o errexit 154*906afcb8SAndy Fiddamanset -o nounset 155*906afcb8SAndy Fiddaman 156*906afcb8SAndy Fiddamancompound base 157*906afcb8SAndy Fiddaman 158*906afcb8SAndy Fiddamancompound bench=( 159*906afcb8SAndy Fiddaman float start 160*906afcb8SAndy Fiddaman float stop 161*906afcb8SAndy Fiddaman) 162*906afcb8SAndy Fiddaman 163*906afcb8SAndy Fiddamaninteger i 164*906afcb8SAndy Fiddaman 165*906afcb8SAndy Fiddamantypeset progname="${ basename "${0}" ; }" 166*906afcb8SAndy Fiddaman 167*906afcb8SAndy Fiddamantypeset -r numtree1_usage=$'+ 168*906afcb8SAndy Fiddaman[-?\n@(#)\$Id: numtree1 (Roland Mainz) 2010-03-27 \$\n] 169*906afcb8SAndy Fiddaman[-author?Roland Mainz <roland.mainz@nrubsig.org>] 170*906afcb8SAndy Fiddaman[+NAME?numtree1 - generate sorted variable tree containing numbers] 171*906afcb8SAndy Fiddaman[+DESCRIPTION?\bnumtree1\b is a simple variable tree generator 172*906afcb8SAndy Fiddaman sorts a given set of numbers into a ksh compound variable tree). 173*906afcb8SAndy Fiddaman the application supports two different modes: \'seq\' takes 174*906afcb8SAndy Fiddaman 1-3 arguments to specify the set of numbers via seq(1) and 175*906afcb8SAndy Fiddaman \'stdin\' reads the numbers from stdin (one per line)] 176*906afcb8SAndy Fiddaman 177*906afcb8SAndy Fiddamanmethod [ arguments ] 178*906afcb8SAndy Fiddaman 179*906afcb8SAndy Fiddaman[+SEE ALSO?\bksh93\b(1), \bseq\b(1)] 180*906afcb8SAndy Fiddaman' 181*906afcb8SAndy Fiddaman 182*906afcb8SAndy Fiddamanwhile getopts -a "${progname}" "${numtree1_usage}" OPT ; do 183*906afcb8SAndy Fiddaman# printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|" 184*906afcb8SAndy Fiddaman case ${OPT} in 185*906afcb8SAndy Fiddaman *) usage ;; 186*906afcb8SAndy Fiddaman esac 187*906afcb8SAndy Fiddamandone 188*906afcb8SAndy Fiddamanshift $((OPTIND-1)) 189*906afcb8SAndy Fiddaman 190*906afcb8SAndy Fiddaman# prechecks 191*906afcb8SAndy Fiddaman(( $# > 0 )) || usage 192*906afcb8SAndy Fiddaman 193*906afcb8SAndy Fiddamancmd=$1 194*906afcb8SAndy Fiddamanshift 195*906afcb8SAndy Fiddaman 196*906afcb8SAndy Fiddaman# Read numbers from stdin outside benchmark loop 197*906afcb8SAndy Fiddamanif [[ ${cmd} == 'stdin' ]] ; then 198*906afcb8SAndy Fiddaman stdin_numbers="$( cat /dev/stdin )" || fatal_error "stdin read error" 199*906afcb8SAndy Fiddamanfi 200*906afcb8SAndy Fiddaman 201*906afcb8SAndy Fiddaman(( bench.start=SECONDS )) 202*906afcb8SAndy Fiddaman 203*906afcb8SAndy Fiddamancase ${cmd} in 204*906afcb8SAndy Fiddaman "seq") 205*906afcb8SAndy Fiddaman for i in ${ floatseq "$@" ; } ; do 206*906afcb8SAndy Fiddaman add_number_to_tree base "${i}" 207*906afcb8SAndy Fiddaman done 208*906afcb8SAndy Fiddaman ;; 209*906afcb8SAndy Fiddaman "stdin") 210*906afcb8SAndy Fiddaman for i in ${stdin_numbers} ; do 211*906afcb8SAndy Fiddaman add_number_to_tree base "${i}" 212*906afcb8SAndy Fiddaman done 213*906afcb8SAndy Fiddaman ;; 214*906afcb8SAndy Fiddaman "demo1") 215*906afcb8SAndy Fiddaman for i in 1 32 33 34 34 38 90 ; do 216*906afcb8SAndy Fiddaman add_number_to_tree base "${i}" 217*906afcb8SAndy Fiddaman done 218*906afcb8SAndy Fiddaman ;; 219*906afcb8SAndy Fiddaman "demo2") 220*906afcb8SAndy Fiddaman for (( i=1000000000 ; i < 1000000000+10 ; i++ )) ; do 221*906afcb8SAndy Fiddaman add_number_to_tree base "$i" 222*906afcb8SAndy Fiddaman done 223*906afcb8SAndy Fiddaman ;; 224*906afcb8SAndy Fiddaman *) 225*906afcb8SAndy Fiddaman fatal_error "Invalid command ${cmd}." 226*906afcb8SAndy Fiddaman ;; 227*906afcb8SAndy Fiddamanesac 228*906afcb8SAndy Fiddaman 229*906afcb8SAndy Fiddaman(( bench.stop=SECONDS )) 230*906afcb8SAndy Fiddaman 231*906afcb8SAndy Fiddamanprint -u2 -f "# time used: %f\n" $((bench.stop - bench.start)) 232*906afcb8SAndy Fiddaman 233*906afcb8SAndy Fiddaman# print tree 234*906afcb8SAndy Fiddamanprint -v base 235*906afcb8SAndy Fiddaman 236*906afcb8SAndy Fiddamanexit 0 237*906afcb8SAndy Fiddaman# EOF. 238