Documentation ¶
Overview ¶
doc.go (c) 2013-2015 David Rook - all rights reserved
Eclectic Set of Utility Functions
Features ======== Most of the functions are short and easily understood Examples available for the less obvious
Real BUGS -##0{ None Known - but beware of limitations
Limitations ----------- GetKey() doesn't hide key entry Also see README-mdr.md for more info
While the polyline can be used to depict GPS coordinates that is NOT a requirement. Use []GPSloc aka GPSlist if you need that (3D) capability.
Index ¶
- Constants
- Variables
- func AbsF64(a float64) float64
- func AbsInt64(a int64) int64
- func BufCRC64(bufr []byte) uint64
- func BufMD5(buf []byte) string
- func BufSHA256(buf []byte) string
- func Celcius(c int16) int16
- func Centigrade(f int16) int16
- func CloseToF64(a, b, near float64) bool
- func CommaFmtInt64(n int64) string
- func ConfineLoHi(lo, i, hi int) int
- func Crash(reason string)
- func DegreeFromSemicircle(semi int32) float64
- func DequoteFloat64(s string) (float64, error)
- func DigestToString(digest []byte) string
- func ExtractFloat32(s string) float32
- func ExtractFloat64(s string) float64
- func ExtractInt16(s string) int16
- func ExtractInt32(s string) int32
- func ExtractInt64(s string) int64
- func ExtractInt8(s string) int8
- func ExtractUint16(s string) uint16
- func ExtractUint32(s string) uint32
- func ExtractUint64(s string) uint64
- func ExtractUint8(s string) uint8
- func Factorial(n int) int
- func Fahrenheit(c int16) int16
- func FatalError(err error)
- func FileCRC64(fname string) (uint64, error)
- func FileGID(fname string) (int, error)
- func FileIsRegular(fname string) (bool, error)
- func FileLength(fname string) (int64, error)
- func FileLinkCt(fname string) (int, error)
- func FileMD5(fname string) (string, error)
- func FileNameIsCollection(fname string) (bool, string)
- func FileSHA256(fname string) (string, error)
- func FileUID(fname string) (int, error)
- func Fit2Time(t uint32) time.Time
- func FitCRC_Calc16(data []byte, size uint32) uint16
- func FitCRC_Get16(crc uint16, bite byte) uint16
- func FitCRC_Update16(crc uint16, data_ptr []byte, size uint32) uint16
- func FlipCoin() bool
- func Float16To64(v int16) float64
- func ForceRangeF64(a, b, c float64) float64
- func ForceRangeI64(a, b, c int64) int64
- func GenFlipCoin() bool
- func GenRandF64Between(lo, hi float64) float64
- func GenRandIntBetween(lo, hi int) int
- func GenRandIntBtw(lo, hi int) int
- func GenRandomNormal(mu, stdev float64) float64
- func GenRandomPoisson(lambda float64) int
- func GenRandomUniform(r float64) float64
- func GenRandomUniformLoHi(low, high float64) float64
- func GenRandomZNormal() float64
- func GetAllArgs() []string
- func GetKey() string
- func HasInterface(hostIPStr string) bool
- func HumanTime(t time.Duration) (rs string)
- func IPFromUint32(adr uint32) net.IP
- func InRangeF64(a, b, c float64) bool
- func InRangeI(a, b, c int) bool
- func InRangeInt64(a, b, c int64) bool
- func Int64FromLSBytes(b []byte) int64
- func Int64FromMSBytes(b []byte) int64
- func KmBetween(a, b GPS2dT) float64
- func KmBetweenGC(a, b GPS2dT) float64
- func LSBytesFromInt64(n int64) []byte
- func LSBytesFromUint32(u uint32) []byte
- func LeapYear(when time.Time) bool
- func LoHi(ary []int) (lo int, hi int)
- func LoadSHA256Names(fname string) ([]string, error)
- func LoadSHA256asDirMap(fname string) (map[string]*DirNode, error)
- func LoadSHA256asList(fname string) ([]string, error)
- func LoadSHA256asMap(fname string) (map[string]string, error)
- func MSBytesFromInt64(n int64) []byte
- func MSBytesFromUint32(u uint32) []byte
- func MaxI(a, b int) int
- func MiFromCm(cm uint32) float64
- func MinI(a, b int) int
- func MphFromMMs(mms uint32) float64
- func MphFromMps(mps float64) float64
- func Polar(loc Pointi) (theta, r float64)
- func PolarAngle(loc Pointi) float64
- func PseudoRandomBlock(blksize int) []byte
- func Radians(degrees float64) float64
- func RangeLoHiF64Slice(v []float64) (lo, hi float64)
- func Reverse(b []byte)
- func RmPrefix(s, pre string) string
- func RmSuffix(s, suf string) string
- func SetTtyRawMode()
- func SingleCharRead() byte
- func SixteenBit(n []byte) uint16
- func Smooth(d []int64, n int) []float64
- func Spinner()
- func StarDate(when time.Time) float64
- func StringIsMember(s string, ary []string) bool
- func StringsCombine(a, b []string) []string
- func StringsIntersection(a, b []string) []string
- func StringsSimilar(a, b []string) bool
- func StringsUnion(a, b []string) []string
- func StringsUnique(tags []string) (rv []string)
- func ThirtyTwoBit(n []byte) uint32
- func ThirtyTwoNet(n []byte) uint32
- func UdevRandomBlock(blksize int) []byte
- func Uint32FromIP(ip net.IP) uint32
- func Uint32FromMSBytes(b []byte) uint32
- func ValidDate(year, month, day, hour, minute, second int) bool
- func ValidDecChar(c byte) bool
- func ValidDecString(s string) bool
- func ValidHexChar(c byte) bool
- func ValidHexString(s string) bool
- type ArchiveType
- type BBox
- type ByName
- type CompressType
- type DblPair
- type DirNode
- func (d *DirNode) AddFile(f FileRec)
- func (d *DirNode) DeleteFile(fname string)
- func (d *DirNode) Dump()
- func (d *DirNode) Finalize() (string, int64)
- func (d *DirNode) IndexOf(fname string) int
- func (d *DirNode) IndexOfBrute(fname string) int
- func (d *DirNode) IsSortedProperly() bool
- func (d *DirNode) SortFilesByName()
- func (d *DirNode) UnTouchAll()
- func (d *DirNode) Update(fname string) error
- type FileRec
- type GPS2dList
- type GPS2dT
- type GPS3dT
- type HeadingDeg
- type IntPair
- type Ints
- type Pointe
- type PointeList
- type Pointi
- type PointiList
- type PolyLine
- type ProgStateT
- type Rec256
- type SemiT
- type Shape
- type SliceI64
- type Table
- type VerboseType
- Bugs
Examples ¶
Constants ¶
const (
G_datetimeFmt = "2006-01-02:15_04_05"
)
Variables ¶
var ( PiX2 = math.Pi * 2.0 RadiansPerDegree = math.Pi / 180.0 Km2miles = 0.621371 M2ft = 3.28084 // meters -> feet TwoTo31th = float64(1 << 31) // 2 ^ 31 GaiaKm = 6371.0 // Earth's radius in kilometers. )
var ( ForceRangeFlag bool = true // forces out of range values to fit CantHappenError = errors.New("Can't-happen") DivideByZeroError = errors.New("Divide By Zero") OutOfRangeError = errors.New("Value outside expected range") InvalidTableError = errors.New("Not a valid table") )
var CantCreateRec = errors.New("mdr: cant create Rec256 ")
var CompileDateTime = "2021-03-12 10:59:21.107357431 -0500 EST m=+0.000032378"
var CompilerVersion = " go version go1.16 linux/amd64"
var DegPerRad = 180.0 / math.Pi
var Fit_crc_table [16]uint16 = [16]uint16{
0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400,
}
var (
G_crcTable *crc64.Table
)
var (
Paranoid = false
)
var ( // these would normally be C static vars inside Spinner() UpdateDelayMs = 1000 // delay in millisec between updates )
Functions ¶
func Centigrade ¶
func CloseToF64 ¶
func CommaFmtInt64 ¶
CommaFmtInt64 returns a comma inserted number string
12345 becomes "12,345" NOTE! uses USA format, not internationalized Name of a generic function might be DecimalFmtdInt64()
func ConfineLoHi ¶
func DegreeFromSemicircle ¶
func DequoteFloat64 ¶
convert() dequotes "123.4" and returns a float64
func DigestToString ¶
func ExtractFloat32 ¶
ExtractFloat32 assumes convention of period for decimal
func ExtractFloat64 ¶
ExtractFloat64 assumes convention of period for decimal
func ExtractInt16 ¶
func ExtractInt32 ¶
func ExtractInt64 ¶
func ExtractInt8 ¶
func ExtractUint16 ¶
func ExtractUint32 ¶
func ExtractUint64 ¶
func ExtractUint8 ¶
func Factorial ¶
Factorial computes value recursively. BUG(mdr) Factorial using recursion may not be the best choice ... BUG(mdr) check for overflow in Factorial or limit choice of n to good range
since it gets big quickly
func Fahrenheit ¶
func FatalError ¶
func FatalError(err error)
func FileIsRegular ¶
BUG(mdr) FileIsRegular now OBE (since go 1.1)
func FileLength ¶
func FileLinkCt ¶
func FileNameIsCollection ¶
func FileSHA256 ¶
Compute sha256 for given file Test is Test_005
func FitCRC_Calc16 ¶
func FitCRC_Get16 ¶
func Float16To64 ¶
func ForceRangeF64 ¶
true IFF a <= b <= c || a >= b >= c, note a < c not required
func ForceRangeI64 ¶
func GenRandF64Between ¶
GenRandF64Between endpoints may occur in output
func GenRandIntBetween ¶
GenRandIntBetween endpoints may occur in output
func GenRandIntBtw ¶
RandIntBtw endpoints may occur (HasTest widget).
func GenRandomNormal ¶
GenRandomNormal returns a float64 with average mu and standard deviation stdev depending on mu and stdev picked the values returned could be virtually any float64
func GenRandomPoisson ¶
GenRandomPoisson returns an int with Poisson distribution typically used to determine the number of time units before some event occurs
func GenRandomUniform ¶
GenRandomUniform returns a float64 in range {0 .. r}
func GenRandomUniformLoHi ¶
GenRandomUniformLo returns a float64 in range {low .. high} high > low is NOT required, not sure if this is right or should panic 8888 ?
func GenRandomZNormal ¶
func GenRandomZNormal() float64
GenRandomZNormal returns a float64 with average of 0 and standard deviation of 1.0 as implemented the range of values returned will be in [-60..60]
func GetAllArgs ¶
func GetAllArgs() []string
GetAllArgs is a helper with filters to collect args from input stream limited to RAM for size of return []string.Note that if used with 'find' in a pipeline it must wait till find is done to proceed.
func HasInterface ¶
checkInterfaces - see if listener is bound to correct interface first is localhost, second should be IP4 of active card, third is IP6 localhost, fourth is IP6 for active card (on this system) on BSD it's [ IP4 IP6 LocalHostIP4 LocalHostIP6 LocalHostIP4]
Order isn't important as long as requested inteface is there somewhere actual check needs to do a string match of interfaces we have with a target of the requested interface. If target isn't present then stop.
func HumanTime ¶
turns duration into decimal minutes,hours,days as appropriate, maximum duration is about 290 years Test_002
func IPFromUint32 ¶
IPFromUint32 beware - also presumes knowledge of net.IP internals order
Inverse of Uint32FromIP
func InRangeF64 ¶
true IFF a <= b <= c || a >= b >= c, note a < c not required
func InRangeInt64 ¶
true IFF a <= b <= c || a >= b >= c, note a < c not required
func Int64FromLSBytes ¶
Int64FromLSBytes converts an 8 byte array into an int64
func Int64FromMSBytes ¶
Int64FromMSBytes converts an 8 byte slice (BigEndian - MSB First) into an int64
func KmBetweenGC ¶
KmBetweenGC calculates the shortest path between two GPS coordinates. Assumes both points are at sealevel.
NOTE! Earth is not a sphere. Haversine has more error in E-W calculations than N-S.
Distance of 1 degree differs ( E.W. vs N.S. )
Error is 6:40,000 E.W. vs 0:40000 N.S. https://en.wikipedia.org/wiki/Earth_radius source for GaiaKm
Uses haversine method to calculate great circle distances
func LSBytesFromInt64 ¶
LSBytesFromInt64 converts an int64 into an 8 byte array, LSB first (LittleEndian) test is Test_011
func LSBytesFromUint32 ¶
convert uint32 to [0:4]byte slice in LSB first order reverse function is ThirtyTwoBit()
func LoadSHA256Names ¶
LoadSHA256Names returns the array of pathnames
func LoadSHA256asDirMap ¶
LoadSHA256asDirMap returns *dirMap[directoryPath]*DirNode
uses scanner to allow large file.256 to be used as input
Used when its necessary to compute something with directory as input
func LoadSHA256asList ¶
LoadSHA256asList returns lines of file as []string , nil on success
each line is known to split without error
comments are stripped
func LoadSHA256asMap ¶
LoadSHA256asMap returns map[SHA256]path , nil on success
func MSBytesFromInt64 ¶
MSBytesFromInt64 converts int64 to [0:8]byte slice with MSB first (BigEndian) reverse function is
func MSBytesFromUint32 ¶
convert uint32 to [0:4]byte slice in MSB first (aka 'Net') order reverse function is ThirtyTwoNet()
func MphFromMMs ¶
MphFromMMs converts millimeters per second to miles per hour
func MphFromMps ¶
MphFromMps converts meters per second to miles per hour
func PolarAngle ¶
PolarAngle returns the angle produced by a line from Point{0,0} to loc.
func PseudoRandomBlock ¶
returns buffer of specified size filled with random bytes test funcs are Test_010 and Benchmark_010
func RangeLoHiF64Slice ¶
Return the lo and hi of given array
func SingleCharRead ¶
func SingleCharRead() byte
SingleCharRead might return newline if no other input
otherwise first character if more than one on a line
^D will cause EOF to be printed and ? as the char returned - better choice is ...
func SixteenBit ¶
SixteenBit converts from little endian two byte slice to int16 aka Uint16FromLSBytes
func Smooth ¶
Smooth creates a new array with smoothed float64 values where n is number of
values over which to average. Returns an array of same size as original, so the first and last 'n' values will be "short", ie more sensitive to any variation.
func Spinner ¶
func Spinner()
keep user entertained while something happens behind the curtain
see example from mdr_test.go for usage Choose Spinner() if progress bar can't be used because endpoint not known
func StringIsMember ¶
StringIsMember() returns true if s is a member of list ary. working test_?
func StringsCombine ¶
StringsCombine() returns a list which combines two lists of strings. test_?
func StringsIntersection ¶
StringsIntersection() returns a list where all members appear in both a,b. test_?
func StringsSimilar ¶
StringsSimilar() returns true if both string lists have the same members duplicate members are ignored. To be SAME the member count must be equal. test_?
func StringsUnion ¶
func StringsUnique ¶
StringsUnique returns a list with duplicate members removed. test_?
func ThirtyTwoBit ¶
convert from LITTLE endian four byte slice to int32 reverse function is LSBytesFromUint32
AKA Uint32FromLSBytes
func ThirtyTwoNet ¶
ThirtyTwoNet is a synonym for Uint32FromMSBytes
func UdevRandomBlock ¶
returns a buffer of specified size filled with random bits from /dev/urandom there is a limited supply (about 4096 bits) so read will fail if urandom has not had time to accumulate enough bits to satisfy the read. Max readable under optimum conditios ? actual test => 1020,1932,1020 1 ms sleep not enough(1940fail), 10 not enough (1020fail), 50 not enough 1020fail
func Uint32FromIP ¶
Uint32FromIP returns a uint32 from IPv4 so we can use as index to map
-BEWARE- there are magic numbers that presume knowledge of net.IP internals order
func Uint32FromMSBytes ¶
convert from BIG endian four byte slice to int32 reverse function is MSBytesFromUint32
func ValidDate ¶
ValidDate tries to determine validity of given date. It's not very much use in pre-Gregorian times and doesn't do BCE at all. https://en.wikipedia.org/wiki/Julian_calendar may help understanding, or may just confuse one further :-)
func ValidDecChar ¶
func ValidDecString ¶
func ValidHexChar ¶
func ValidHexString ¶
Types ¶
type ArchiveType ¶
type ArchiveType int
const ( ArchiveNoMatchType ArchiveType = iota ArchiveTarType ArchiveZipType ArchiveJarType // not quite Zip but similar ArchiveCpioType ArchiveMailboxType )
func WhichArchiveType ¶
func WhichArchiveType(s string) ArchiveType
examine name to determine archive method used should match magic numbers but that's a different function
type BBox ¶
type BBox struct {
MinX, MinY, MaxX, MaxY float64
}
============================================================================ BBox
func (BBox) Corners ¶
Corners() returns all 4 corners though only two are needed to define a box. Easier to draw if you have all 4, and they are in drawable order.
func (*BBox) ExpandByPolyLine ¶
func (*BBox) ExpandByPts ¶
func (BBox) Intersects ¶
Intersects() returns true if box areas overlap at any point. Failed when one box completely encloses the other so add second test Should return true if one encloses the other
type CompressType ¶
type CompressType int
const ( CompressNoMatchType CompressType = iota CompressZipType CompressGzipType CompressBz2Type CompressZcompressType // .Z not common except possibly in Japan CompresszPackType // .z deprecated - very rare in last 20 years CompressBz1Type // .bz deprecated - very rare in last 20+ years CompressShrinkType // found in zip files of old CompressCompactType // ?? deprecated CompressFreezeType // ?? deprecated Compress7zType // new kid on the block CompressXvType // even newer kid on the block )
func WhichCompressType ¶
func WhichCompressType(s string) CompressType
examine name to determine compression method used should match magic numbers but that's a different function
type DirNode ¶
type DirNode struct { Pathname string IsSortedByName bool Files []FileRec Size int64 SHA256 string }
func (*DirNode) DeleteFile ¶
func (*DirNode) IndexOf ¶
special case speedup if we check last returned value +1 first? better handles case where we're stepping through list ??? Returns -1 if not found
func (*DirNode) IsSortedProperly ¶
func (*DirNode) SortFilesByName ¶
func (d *DirNode) SortFilesByName()
func (*DirNode) UnTouchAll ¶
func (d *DirNode) UnTouchAll()
type GPS2dList ¶
type GPS2dList []GPS2dT
============================================================================= GPS2dList
type GPS2dT ¶
func (GPS2dT) ValidGPS2d ¶
type HeadingDeg ¶
type HeadingDeg float64
func (HeadingDeg) String ¶
func (h HeadingDeg) String() string
type IntPair ¶
type IntPair struct {
X, Y int
}
func JobSplit ¶
split n into NumCPUs ranges,
JobSplit(10,1) -> returns [ {0,9} ] JobSplit(10,2) -> returns [ {0,4},{5,9} ] JobSplit(10,3) -> returns [ {0,3}, {4,6}, {7,9} ] if not all slices are same length, longer ones will occur first
Test_001 See also ExampleJobSplit()
Example ¶
// 888_test.go // go test '-bench=.' # expression matches everything, runs all benchmarks // go test -run="Test_000" to run just one function package main import ( "fmt" //"testing" //"time" ) // /////////////////////// E X A M P L E S //////////////////////////// func workerFunction(w IntPair) { fmt.Printf("work on items from %d through %d\n", w.X, w.Y) } func main() { nCPU := 4 totalWork := 100 jobrange := JobSplit(totalWork, nCPU) for i := 0; i < nCPU; i++ { go workerFunction(jobrange[i]) } } /* func ExampleProgressBar() { var ( status int64 endNumber int64 = 100 ) progChan := make(chan int64, 2) go ProgressBar(50, progChan, endNumber) // start the display handler progChan <- 0 // make first progress display visible for { // ... do something to advance status towards endNumber ... time.Sleep(time.Second) status += 10 progChan <- status if status >= endNumber { break } } progChan <- -1 // close up shop } */
Output:
type Ints ¶
type Ints []int
func PermutedInts ¶
PermutedInts returns all the permutaions of the original array length of array must be in range of [0..6]
type Pointe ¶
============================================================================ Pointe
func (Pointe) ValidGPS2d ¶
type PointeList ¶
type PointeList []Pointe
============================================================================ PointeList
type Pointi ¶
============================================================================ Pointi
func Cartesian ¶
Cartesian returns Cartesian point from polar point. NoteBene: NOT the usual [in trig x=cos(a) y=sin(a)] because y axis is inverted in 'conventional' computer graphics.
func RangeMinMaxPoint ¶
func RangeMinMaxPoint(v PointiList) (minPt, maxPt Pointi)
RangeMinMaxPoint returns bounds of point array. probably a better name would help... 8888
func (*Pointi) RotateOnPivotPt ¶
RotateOnPivotPt rotate a point using another point as a pivot (mimic mechanical compass drawing).
type PointiList ¶
type PointiList []Pointi
============================================================================ PointiList
func CreateBezierPts ¶
func CreateBezierPts(p1, p2, p3 Pointi, segments int) PointiList
CreateBezierPts start, control, end pts for quadratic bezier. segments is the number of line segments to create, more means smoother curve. Intended use is mostly with ring5 package, but MIGHT be genericly useful.
func (PointiList) Split ¶
func (pts PointiList) Split() (xs []int, ys []int)
Split separates the X and Y values into their own arrays
type PolyLine ¶
type PolyLine [][]Pointe
========================================================================
func (PolyLine) AddNewPart ¶
func (PolyLine) BBox ¶
BBox() returns the smallest box which encloses all the points of the PolyLine
func (*PolyLine) ExtendByPolyLine ¶
type ProgStateT ¶
type ProgStateT struct { Label string ShowCount bool ShowTime bool // contains filtered or unexported fields }
func OneProgressBar ¶
func OneProgressBar(goal int64) *ProgStateT
func (*ProgStateT) Stop ¶
func (ps *ProgStateT) Stop()
func (*ProgStateT) Tag ¶
func (ps *ProgStateT) Tag(t string)
func (*ProgStateT) Update ¶
func (ps *ProgStateT) Update(val int64)
type SemiT ¶
type SemiT int32
============================================================================ SemiT
type SliceI64 ¶
type SliceI64 []int64
============================================================================ SliceI64
func (SliceI64) SliceI64Avg ¶
type Table ¶
type Table struct { Name string Data []DblPair RevData []DblPair // contains filtered or unexported fields }
var NormalZtable *Table
func (*Table) ReverseEval ¶
returns multiple hits where appropriate
type VerboseType ¶
type VerboseType bool
var (
Verbose VerboseType
)
func (VerboseType) Printf ¶
func (v VerboseType) Printf(s string, a ...interface{})
Notes ¶
Bugs ¶
GetKey() key is visible during entry
remove duplicates if necessary, these happen in adjacent spots when a value is on the end of a pair, can happen elsewhere as well, but not treating those yet. This method is quick, but inadequate for long run. Need insertion sort or map for final solution. currently just document that the resulting array may not have unique values
? be paranoid and check if we have any slashes in fname?
Factorial using recursion may not be the best choice ...
check for overflow in Factorial or limit choice of n to good range
since it gets big quickly
FileIsRegular now OBE (since go 1.1)
!Sane or just not init??? BBox.Sane() makes basic checks, usually employed only if Paranoid turned on
see BBox.Contains() above as example
or is this an error ?
Source Files ¶
- compileDate.go
- doc.go
- mdr.go
- mdr_archives.go
- mdr_constants.go
- mdr_cryptoHelp.go
- mdr_csv.go
- mdr_datatable.go
- mdr_datetime.go
- mdr_dirNode.go
- mdr_fit.go
- mdr_floats.go
- mdr_hash.go
- mdr_ints.go
- mdr_jobsplit.go
- mdr_loadSHA256.go
- mdr_maps.go
- mdr_misc.go
- mdr_points.go
- mdr_polyline.go
- mdr_progress.go
- mdr_randgen.go
- mdr_shapefiles.go
- mdr_sliceI64.go
- mdr_strings.go
- verbose.go