代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。