# Procedure to make sequences of values

proc APSSequenceHelp {} {
    return "Usage: APSSequence -start <value> -end <value> -delta <value> -number <value> \[-type {real|integer}\] \[-printFormat <string>]\nThree of the four options are required to specify a sequence.\nE.g., -start 1 -delta 1 -number 100 -type integer => Integers from 1 to 100\n-start 0.1 -end 7.5 -delta 0.1 => 0.1, 0.2, ..., 7.5\n"
}

proc APSSequence {args} {
    set start "not given"
    set end "not given"
    set delta "not given"
    set number "not given"
    set type real
    set printFormat ""
    
    if {[APSStrictParseArguments {start end delta number type printFormat}] || [lsearch -exact [list real integer] $type]==-1} {
        return -code error [APSSequenceHelp]
    }

    set count 0
    if [string compare "not given" $start] {
        set code s
        incr count
    } else {
        set code x
    }
    if [string compare "not given" $end] {
        append code e
        incr count
    } else {
        append code x
    }
    if [string compare "not given" $delta] {
        append code d
        incr count
    } else {
        append code x
    }
    if [string compare "not given" $number] {
        append code n
        incr count
    } else {
        append code x
    }

    if $count!=3 {
        return -code error [APSSequenceHelp]
    }


    if [string compare $type integer]==0 {
        switch $code {
            sedx {
                # Start, end, delta given, need to compute number
                set number [expr int(($end-$start)/$delta+1.5)]
            }
            sexn {
                # Start, end, number given, need to compute delta
                set delta [expr int(($end-$start)/($number-1))]
            }
            sxdn {
                # Start, delta, number given
            }
            xedn {
                # End, delta, number given, need to compute start
                set start [expr $end-$delta*($number-1)]
            }
        }
    } else {
        switch $code {
            sedx {
                # Start, end, delta given, need to compute number
                set number [expr int(($end-$start)/$delta+1.5)]
            }
            sexn {
                # Start, end, number given, need to compute delta
                set delta [expr ($end-$start)/($number-1.0)]
            }
            sxdn {
                # Start, delta, number given
            }
            xedn {
                # End, delta, number given, need to compute start
                set start [expr $end-$delta*($number-1)]
            }
        }
    }
    
    set valueList ""
    for {set i 0} {$i<$number} {incr i} {
        set value [expr $start+$delta*$i]
        if [string length $printFormat] {
            set value [format $printFormat $value]
        }
        lappend valueList $value
    }
    return $valueList
}
