cc: [email protected]
Subject: Re: [ast-users] Sort a very large array of compound variables?
--------

In order to get good performance with sorting  > 10M elements
of a compound variable, I will have to add a another option to
ksh93.

I was thinking of adding  a -K option to set which can be
used with -s to sort on keys of a compound variable array.
The -K options can take a comma separated list of key fields
each one optionaly followed by :n to indicate a numerical
sort.

Here is a function that will sort a compound variable on
a single key, but it takes a few minutes just for 1 million
compound elements to sort.

======================cut here====================
function carray_sort # comvar keyfield delim sort_opts
{
        trap 'rm -f /tmp/shsort[12].$$' EXIT
        nameref var=$1
        nameref key=$2
        typeset delim=$3
        typeset v smallest=
        integer i max=${#var[@]}
        [[ $4 == *-n* ]] && ((smallest=-9e999))
        print -- "$smallest&${!var}=(" >  /tmp/shsort1.$$
        for((i=0;i < max; i++))
        do      v=$(typeset -p var[$i])
                print  -r -- "${var[i].key}$delim${v#*=}"
        done >> /tmp/shsort1.$$
        sort    -t"$delim" $4 -k1.1 /tmp/shsort1.$$  | cut -f2- -d$delim > 
/tmp/shsort2.$$
        print ')' >> /tmp/shsort2.$$
        . /tmp/shsort2.$$
}

# here is a sample compound array with 1 million elements
integer i
float x=.1
compound -a c
for ((i=0; i < 1000000; i++))
do      ((x += .1 ))
        c[i]=(key=$((sin(x)))  index=$((i+7)) )
done
carray_sort c key '&' -n 
======================cut here====================

David Korn
[email protected]
_______________________________________________
ast-users mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-users

Reply via email to