diff --git a/README.md b/README.md index 555d736..2c70f83 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ Package stl4go is a generic container and algorithm library for go. - [func AllOf[T any](a []T, pred func(T) bool) bool](<#func-allof>) - [func AnyOf[T any](a []T, pred func(T) bool) bool](<#func-anyof>) - [func Average[T Numeric](a []T) T](<#func-average>) +- [func BinarySearch[T Ordered](a []T, value T) (index int, ok bool)](<#func-binarysearch>) +- [func BinarySearchFunc[T any](a []T, value T, less LessFn[T]) (index int, ok bool)](<#func-binarysearchfunc>) - [func Compare[E Ordered](a, b []E) int](<#func-compare>) - [func Copy[T any](a []T) []T](<#func-copy>) - [func Count[T comparable](a []T, x T) int](<#func-count>) @@ -38,11 +40,14 @@ Package stl4go is a generic container and algorithm library for go. - [func Equal[T comparable](a, b []T) bool](<#func-equal>) - [func Equals[T comparable](a, b T) bool](<#func-equals>) - [func Find[T comparable](a []T, x T) (index int, ok bool)](<#func-find>) +- [func FindIf[T any](a []T, cond func(T) bool) (index int, ok bool)](<#func-findif>) - [func Generate[T any](a []T, gen func() T)](<#func-generate>) - [func Index[T comparable](a []T, x T) int](<#func-index>) - [func IsDescSorted[T Ordered](a []T) bool](<#func-isdescsorted>) - [func IsSorted[T Ordered](a []T) bool](<#func-issorted>) - [func Less[T Ordered](a, b T) bool](<#func-less>) +- [func LowerBound[T Ordered](a []T, value T) int](<#func-lowerbound>) +- [func LowerBoundFunc[T any](a []T, value T, less LessFn[T]) int](<#func-lowerboundfunc>) - [func Max[T Ordered](a, b T) T](<#func-max>) - [func MaxN[T Ordered](a ...T) T](<#func-maxn>) - [func Min[T Ordered](a, b T) T](<#func-min>) @@ -68,6 +73,8 @@ Package stl4go is a generic container and algorithm library for go. - [func TransformTo[R any, T any](a []T, op func(T) R, b []R)](<#func-transformto>) - [func Unique[T comparable](a []T) []T](<#func-unique>) - [func UniqueCopy[T comparable](a []T) []T](<#func-uniquecopy>) +- [func UpperBound[T Ordered](a []T, value T) int](<#func-upperbound>) +- [func UpperBoundFunc[T any](a []T, value T, less LessFn[T]) int](<#func-upperboundfunc>) - [type Container](<#type-container>) - [type DList](<#type-dlist>) - [func NewDList[T any]() *DList[T]](<#func-newdlist>) @@ -144,7 +151,7 @@ Package stl4go is a generic container and algorithm library for go. - [func (v *Vector[T]) Shrink()](<#func-vectort-shrink>) -## func [AllOf]() +## func [AllOf]() ```go func AllOf[T any](a []T, pred func(T) bool) bool @@ -154,7 +161,7 @@ AllOf return true if pred\(e\) returns true for all emements e in a. Complexity: O\(len\(a\)\). -## func [AnyOf]() +## func [AnyOf]() ```go func AnyOf[T any](a []T, pred func(T) bool) bool @@ -172,6 +179,28 @@ func Average[T Numeric](a []T) T Average returns the average value of a. +## func [BinarySearch]() + +```go +func BinarySearch[T Ordered](a []T, value T) (index int, ok bool) +``` + +BinarySearch returns the \(index, true\) to the first element in the ascending ordered slice a such that element == value, or \(\-1, false\) if no such element is found. + +Complexity: O\(log\(len\(a\)\)\). + +## func [BinarySearchFunc]() + +```go +func BinarySearchFunc[T any](a []T, value T, less LessFn[T]) (index int, ok bool) +``` + +BinarySearchFunc returns the \(index, true\) to the first element in the ordered slice a such that less\(element, value\) and less\(value, element\) are both false, or \(\-1, false\) if no such element is found. + +The elements in the slice a should sorted according with compare func less. + +Complexity: O\(log\(len\(a\)\)\). + ## func [Compare]() ```go @@ -252,15 +281,27 @@ func Equals[T comparable](a, b T) bool Equals wraps the '==' operator for comparable types. -## func [Find]() +## func [Find]() ```go func Find[T comparable](a []T, x T) (index int, ok bool) ``` -Find find the value x in the given slice a linearly. return \(index, true\) if found, return \(\_, false\) if not found. Complexity: O\(len\(a\)\). +Find find the first value x in the given slice a linearly. return \(index, true\) if found, return \(\_, false\) if not found. -## func [Generate]() +Complexity: O\(len\(a\)\). + +## func [FindIf]() + +```go +func FindIf[T any](a []T, cond func(T) bool) (index int, ok bool) +``` + +FindIf find the first value x satisfying function cond in the given slice a linearly. return \(index, true\) if found, return \(\_, false\) if not found. + +Complexity: O\(len\(a\)\). + +## func [Generate]() ```go func Generate[T any](a []T, gen func() T) @@ -270,7 +311,7 @@ Generate fill each element of \`a\`\` with \`gen\(\)\`. Complexity: O\(len\(a\)\). -## func [Index]() +## func [Index]() ```go func Index[T comparable](a []T, x T) int @@ -310,6 +351,28 @@ func Less[T Ordered](a, b T) bool Less wraps the '\<' operator for ordered types. +## func [LowerBound]() + +```go +func LowerBound[T Ordered](a []T, value T) int +``` + +LowerBound returns an index to the first element in the ascending ordered slice a that does not satisfy element \< value \(i.e. greater or equal to\), or len\(a\) if no such element is found. + +Complexity: O\(log\(len\(a\)\)\). + +## func [LowerBoundFunc]() + +```go +func LowerBoundFunc[T any](a []T, value T, less LessFn[T]) int +``` + +LowerBoundFunc returns an index to the first element in the ordered slice a that does not satisfy less\(element, value\)\), or len\(a\) if no such element is found. + +The elements in the slice a should sorted according with compare func less. + +Complexity: O\(log\(len\(a\)\)\). + ## func [Max]() ```go @@ -370,7 +433,7 @@ MinN return the minimum value in the sequence \`a\`. Complexity: O\(len\(a\)\). -## func [NoneOf]() +## func [NoneOf]() ```go func NoneOf[T any](a []T, pred func(T) bool) bool @@ -380,13 +443,13 @@ NoneOf return true pred\(e\) returns true for none emements e in a. Complexity: O\(len\(a\)\). -## func [Range]() +## func [Range]() ```go func Range[T Numeric](first, last T) []T ``` -Range make a \[\]T filled with values in the \`\[first, last\)\` sequence. +Range make a \[\]T filled with values in the \`\[first, last\)\` sequence. NOTE: the last is not included in the result. Complexity: O\(last\-first\). @@ -458,7 +521,7 @@ func Sort[T Ordered](a []T) Sort sorts data in ascending order. The order of equal elements is not guaranteed to be preserved. -Complexity: O\(N\*log\(N\)\), N=len\(a\). +Complexity: O\(N\*log\(N\)\), where N=len\(a\). ## func [SortFunc]() @@ -478,7 +541,7 @@ func StableSort[T Ordered](a []T) StableSort sorts data in ascending order stably. The order of equivalent elements is guaranteed to be preserved. -Complexity: O\(N\*log\(N\)\), N=len\(a\). +Complexity: O\(N\*log\(N\)^2\), where N=len\(a\). ## func [StableSortFunc]() @@ -558,6 +621,28 @@ UniqueCopy remove adjacent repeated elements from the input slice. return the re Complexity: O\(len\(a\)\). +## func [UpperBound]() + +```go +func UpperBound[T Ordered](a []T, value T) int +``` + +UpperBound returns an index to the first element in the ascending ordered slice a such that value \< element \(i.e. strictly greater\), or len\(a\) if no such element is found. + +Complexity: O\(log\(len\(a\)\)\). + +## func [UpperBoundFunc]() + +```go +func UpperBoundFunc[T any](a []T, value T, less LessFn[T]) int +``` + +UpperBoundFunc returns an index to the first element in the ordered slice a such that less\(value, element\)\) is true \(i.e. strictly greater\), or len\(a\) if no such element is found. + +The elements in the slice a should sorted according with compare func less. + +Complexity: O\(log\(len\(a\)\)\). + ## type [Container]() Container is a holder object that stores a collection of other objects. diff --git a/README_zh.md b/README_zh.md index 192b932..2303253 100644 --- a/README_zh.md +++ b/README_zh.md @@ -8,7 +8,6 @@ [![Coverage Status](https://coveralls.io/repos/github/chen3feng/stl4go/badge.svg?branch=master)](https://coveralls.io/github/chen3feng/stl4go?branch=master) [![GoReport](https://goreportcard.com/badge/github.com/securego/gosec)](https://goreportcard.com/report/github.com/chen3feng/stl4go) - @@ -26,6 +25,8 @@ Package stl4go is a generic container and algorithm library for go. - [func AllOf[T any](a []T, pred func(T) bool) bool](<#func-allof>) - [func AnyOf[T any](a []T, pred func(T) bool) bool](<#func-anyof>) - [func Average[T Numeric](a []T) T](<#func-average>) +- [func BinarySearch[T Ordered](a []T, value T) (index int, ok bool)](<#func-binarysearch>) +- [func BinarySearchFunc[T any](a []T, value T, less LessFn[T]) (index int, ok bool)](<#func-binarysearchfunc>) - [func Compare[E Ordered](a, b []E) int](<#func-compare>) - [func Copy[T any](a []T) []T](<#func-copy>) - [func Count[T comparable](a []T, x T) int](<#func-count>) @@ -35,11 +36,14 @@ Package stl4go is a generic container and algorithm library for go. - [func Equal[T comparable](a, b []T) bool](<#func-equal>) - [func Equals[T comparable](a, b T) bool](<#func-equals>) - [func Find[T comparable](a []T, x T) (index int, ok bool)](<#func-find>) +- [func FindIf[T any](a []T, cond func(T) bool) (index int, ok bool)](<#func-findif>) - [func Generate[T any](a []T, gen func() T)](<#func-generate>) - [func Index[T comparable](a []T, x T) int](<#func-index>) - [func IsDescSorted[T Ordered](a []T) bool](<#func-isdescsorted>) - [func IsSorted[T Ordered](a []T) bool](<#func-issorted>) - [func Less[T Ordered](a, b T) bool](<#func-less>) +- [func LowerBound[T Ordered](a []T, value T) int](<#func-lowerbound>) +- [func LowerBoundFunc[T any](a []T, value T, less LessFn[T]) int](<#func-lowerboundfunc>) - [func Max[T Ordered](a, b T) T](<#func-max>) - [func MaxN[T Ordered](a ...T) T](<#func-maxn>) - [func Min[T Ordered](a, b T) T](<#func-min>) @@ -65,6 +69,8 @@ Package stl4go is a generic container and algorithm library for go. - [func TransformTo[R any, T any](a []T, op func(T) R, b []R)](<#func-transformto>) - [func Unique[T comparable](a []T) []T](<#func-unique>) - [func UniqueCopy[T comparable](a []T) []T](<#func-uniquecopy>) +- [func UpperBound[T Ordered](a []T, value T) int](<#func-upperbound>) +- [func UpperBoundFunc[T any](a []T, value T, less LessFn[T]) int](<#func-upperboundfunc>) - [type Container](<#type-container>) - [type DList](<#type-dlist>) - [func NewDList[T any]() *DList[T]](<#func-newdlist>) @@ -141,7 +147,7 @@ Package stl4go is a generic container and algorithm library for go. - [func (v *Vector[T]) Shrink()](<#func-vectort-shrink>) -## func [AllOf]() +## func [AllOf]() ```go func AllOf[T any](a []T, pred func(T) bool) bool @@ -151,7 +157,7 @@ AllOf return true if pred\(e\) returns true for all emements e in a. Complexity: O\(len\(a\)\). -## func [AnyOf]() +## func [AnyOf]() ```go func AnyOf[T any](a []T, pred func(T) bool) bool @@ -169,6 +175,28 @@ func Average[T Numeric](a []T) T Average returns the average value of a. +## func [BinarySearch]() + +```go +func BinarySearch[T Ordered](a []T, value T) (index int, ok bool) +``` + +BinarySearch returns the \(index, true\) to the first element in the ascending ordered slice a such that element == value, or \(\-1, false\) if no such element is found. + +Complexity: O\(log\(len\(a\)\)\). + +## func [BinarySearchFunc]() + +```go +func BinarySearchFunc[T any](a []T, value T, less LessFn[T]) (index int, ok bool) +``` + +BinarySearchFunc returns the \(index, true\) to the first element in the ordered slice a such that less\(element, value\) and less\(value, element\) are both false, or \(\-1, false\) if no such element is found. + +The elements in the slice a should sorted according with compare func less. + +Complexity: O\(log\(len\(a\)\)\). + ## func [Compare]() ```go @@ -249,15 +277,27 @@ func Equals[T comparable](a, b T) bool Equals wraps the '==' operator for comparable types. -## func [Find]() +## func [Find]() ```go func Find[T comparable](a []T, x T) (index int, ok bool) ``` -Find find the value x in the given slice a linearly. return \(index, true\) if found, return \(\_, false\) if not found. Complexity: O\(len\(a\)\). +Find find the first value x in the given slice a linearly. return \(index, true\) if found, return \(\_, false\) if not found. + +Complexity: O\(len\(a\)\). -## func [Generate]() +## func [FindIf]() + +```go +func FindIf[T any](a []T, cond func(T) bool) (index int, ok bool) +``` + +FindIf find the first value x satisfying function cond in the given slice a linearly. return \(index, true\) if found, return \(\_, false\) if not found. + +Complexity: O\(len\(a\)\). + +## func [Generate]() ```go func Generate[T any](a []T, gen func() T) @@ -267,7 +307,7 @@ Generate fill each element of \`a\`\` with \`gen\(\)\`. Complexity: O\(len\(a\)\). -## func [Index]() +## func [Index]() ```go func Index[T comparable](a []T, x T) int @@ -307,6 +347,28 @@ func Less[T Ordered](a, b T) bool Less wraps the '\<' operator for ordered types. +## func [LowerBound]() + +```go +func LowerBound[T Ordered](a []T, value T) int +``` + +LowerBound returns an index to the first element in the ascending ordered slice a that does not satisfy element \< value \(i.e. greater or equal to\), or len\(a\) if no such element is found. + +Complexity: O\(log\(len\(a\)\)\). + +## func [LowerBoundFunc]() + +```go +func LowerBoundFunc[T any](a []T, value T, less LessFn[T]) int +``` + +LowerBoundFunc returns an index to the first element in the ordered slice a that does not satisfy less\(element, value\)\), or len\(a\) if no such element is found. + +The elements in the slice a should sorted according with compare func less. + +Complexity: O\(log\(len\(a\)\)\). + ## func [Max]() ```go @@ -367,7 +429,7 @@ MinN return the minimum value in the sequence \`a\`. Complexity: O\(len\(a\)\). -## func [NoneOf]() +## func [NoneOf]() ```go func NoneOf[T any](a []T, pred func(T) bool) bool @@ -377,13 +439,13 @@ NoneOf return true pred\(e\) returns true for none emements e in a. Complexity: O\(len\(a\)\). -## func [Range]() +## func [Range]() ```go func Range[T Numeric](first, last T) []T ``` -Range make a \[\]T filled with values in the \`\[first, last\)\` sequence. +Range make a \[\]T filled with values in the \`\[first, last\)\` sequence. NOTE: the last is not included in the result. Complexity: O\(last\-first\). @@ -455,7 +517,7 @@ func Sort[T Ordered](a []T) Sort sorts data in ascending order. The order of equal elements is not guaranteed to be preserved. -Complexity: O\(N\*log\(N\)\), N=len\(a\). +Complexity: O\(N\*log\(N\)\), where N=len\(a\). ## func [SortFunc]() @@ -475,7 +537,7 @@ func StableSort[T Ordered](a []T) StableSort sorts data in ascending order stably. The order of equivalent elements is guaranteed to be preserved. -Complexity: O\(N\*log\(N\)\), N=len\(a\). +Complexity: O\(N\*log\(N\)^2\), where N=len\(a\). ## func [StableSortFunc]() @@ -555,6 +617,28 @@ UniqueCopy remove adjacent repeated elements from the input slice. return the re Complexity: O\(len\(a\)\). +## func [UpperBound]() + +```go +func UpperBound[T Ordered](a []T, value T) int +``` + +UpperBound returns an index to the first element in the ascending ordered slice a such that value \< element \(i.e. strictly greater\), or len\(a\) if no such element is found. + +Complexity: O\(log\(len\(a\)\)\). + +## func [UpperBoundFunc]() + +```go +func UpperBoundFunc[T any](a []T, value T, less LessFn[T]) int +``` + +UpperBoundFunc returns an index to the first element in the ordered slice a such that less\(value, element\)\) is true \(i.e. strictly greater\), or len\(a\) if no such element is found. + +The elements in the slice a should sorted according with compare func less. + +Complexity: O\(log\(len\(a\)\)\). + ## type [Container]() Container is a holder object that stores a collection of other objects.