schastsp/context/context.go

78 lines
1.8 KiB
Go
Raw Normal View History

package context
import (
"errors"
)
/* (1) Definitions
---------------------------------------------------------*/
/* (1) Default values */
const DefaultMin = 0x00f0
const DefaultMax = 0x0fff
const DefaultThr = 0x000a
/* (2) Struct attributes */
type T struct {
win float64; // 'timeid' window size
min uint16; // minimum scha depth
max uint16; // maximum scha depth
thr uint16; // scha depth threshold
}
/* (3) Constructor
*
* @win<float64> Window size for 'timeid' package
* @min<uint16> [OPT] Minimum scha depth
* @thr<uint16> [OPT] scha depth threshold before renewal
* @max<uint16> [OPT] Maximum scha depth
*
* @return outName<outType> outDesc
*
---------------------------------------------------------*/
func Create(win float64, optional... uint16) (*T, error) {
2018-04-21 23:05:51 +00:00
var inst = new(T);
/* (1) Window size error */
if win < 0 { return nil, errors.New("Window size must be positive and is negative") }
2018-04-21 23:05:51 +00:00
inst.win = win;
/* (2) Default values */
2018-04-21 23:05:51 +00:00
inst.min = DefaultMin
inst.thr = DefaultThr
inst.max = DefaultMax
/* (3) Optional 'min' */
if len(optional) > 0 {
2018-04-21 23:05:51 +00:00
if optional[0] < 0x0f {
return nil, errors.New("Minimum depth must be greater than 0x0f (decimal 15) for consistency issues")
}
inst.min = optional[0];
}
/* (4) Optional 'thr' */
if len(optional) > 1 {
2018-04-21 23:05:51 +00:00
inst.thr = optional[1];
}
/* (5) Optional 'max' */
if len(optional) > 2 {
2018-04-21 23:05:51 +00:00
if optional[2] <= inst.min+inst.thr {
return nil, errors.New("Minimum depth must be greater than 0x0f (decimal 15) for consistency issues")
}
2018-04-21 23:05:51 +00:00
inst.max = optional[2];
}
2018-04-21 23:05:51 +00:00
return inst, nil
}
/* (4) Getters */
func (c T) Window() float64 { return c.win }
func (c T) MinDepth() uint16 { return c.min }
func (c T) MaxDepth() uint16 { return c.max }
func (c T) DepthThreshold() uint16 { return c.thr }