2 Star 1 Fork 0

简单 / gostl

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
const_op.go 2.66 KB
一键复制 编辑 原始数据 按行查看 历史
简单 提交于 2023-01-03 19:01 . jhl
package algorithm
import (
"gitee.com/jianhaolin/gostl/utils/comparator"
"gitee.com/jianhaolin/gostl/utils/iterator"
)
// Count returns the number of elements that their value is equal to value in range [first, last)
func Count[T any](first, last iterator.ConstIterator[T], value T, cmp comparator.Comparator[T]) int {
var count int
for iter := first.Clone(); !iter.Equal(last); iter.Next() {
if cmp(iter.Value(), value) == 0 {
count++
}
}
return count
}
// CountIf returns the number of elements are satisfied the function f in range [first, last)
func CountIf[T any](first, last iterator.ConstIterator[T], f func(iterator.ConstIterator[T]) bool) int {
var count int
for iter := first.Clone(); !iter.Equal(last); iter.Next() {
if f(iter) {
count++
}
}
return count
}
// Find finds the first element that its value is equal to value in range [first, last), and returns its iterator, or last if not found
func Find[T any](first, last iterator.ConstIterator[T], value T, cmp comparator.Comparator[T]) iterator.ConstIterator[T] {
for iter := first.Clone(); !iter.Equal(last); iter.Next() {
if cmp(iter.Value(), value) == 0 {
return iter
}
}
return last
}
// FindIf finds the first element that is satisfied the function f, and returns its iterator, or last if not found
func FindIf[T any](first, last iterator.ConstIterator[T], f func(iterator.ConstIterator[T]) bool) iterator.ConstIterator[T] {
for iter := first.Clone(); !iter.Equal(last); iter.Next() {
if f(iter) {
return iter
}
}
return last
}
// MaxElement returns an Iterator to the largest element in the range [first, last). If several elements in the range are equivalent to the largest element, returns the iterator to the first such element. Returns last if the range is empty.
func MaxElement[T any](first, last iterator.ConstIterator[T], cmp comparator.Comparator[T]) iterator.ConstIterator[T] {
if first.Equal(last) {
return last
}
largest := first
for iter := first.Clone(); !iter.Equal(last); iter.Next() {
if cmp(iter.Value(), largest.Value()) > 0 {
largest = iter.Clone()
}
}
return largest
}
// MinElement returns an Iterator to the smallest element value in the range [first, last). If several elements in the range are equivalent to the smallest element value, returns the iterator to the first such element. Returns last if the range is empty.
func MinElement[T any](first, last iterator.ConstIterator[T], cmp comparator.Comparator[T]) iterator.ConstIterator[T] {
if first.Equal(last) {
return last
}
smallest := first
for iter := first.Clone(); !iter.Equal(last); iter.Next() {
if cmp(iter.Value(), smallest.Value()) < 0 {
smallest = iter.Clone()
}
}
return smallest
}
Go
1
https://gitee.com/jianhaolin/gostl.git
git@gitee.com:jianhaolin/gostl.git
jianhaolin
gostl
gostl
v1.3.27

搜索帮助

53164aa7 5694891 3bd8fe86 5694891