Skip to content

Commit

Permalink
Merge pull request #31 from lucasturci/master
Browse files Browse the repository at this point in the history
Add AnyOf, AllOf and NoneOf algorithms
  • Loading branch information
liyue201 authored Jan 2, 2025
2 parents 1401dda + b1e719b commit 89e0edb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,9 @@ func main() {
- FindIf:Find the first element satisfying function f in the iterator interval and return its iterator
- MaxElement : Find the largest element and return its iterator
- MinElement : Find the smallest element and return its iterator
- AnyOf : Returns true if any element satisfies the function f in the iterator interval
- AllOf: Returns true if all elements satisfy the function f in the iterator interval
- NoneOf: Returns true if none of the elements satisfy the function f in the iterator interval

```go
package main
Expand Down Expand Up @@ -728,6 +731,10 @@ func main() {
if !iter.Equal(a.End()) {
fmt.Printf("largest value : %v\n", iter.Value())
}

fmt.Printf("Any even: %v\n", algorithm.AnyOf[int](a.Begin(), a.End(), isEven))
fmt.Printf("All even: %v\n", algorithm.AllOf[int](a.Begin(), a.End(), isEven))
fmt.Printf("None even: %v\n", algorithm.NoneOf[int](a.Begin(), a.End(), isEven))
}


Expand Down
25 changes: 25 additions & 0 deletions algorithm/const_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,28 @@ func MinElement[T any](first, last iterator.ConstIterator[T], cmp comparator.Com
}
return smallest
}

// AnyOf returns whether any of the elements satisfy the function f in range [first, last)
func AnyOf[T any](first, last iterator.ConstIterator[T], f func(iterator.ConstIterator[T]) bool) bool {
for iter := first.Clone(); !iter.Equal(last); iter.Next() {
if f(iter) {
return true
}
}
return false
}

// AllOf returns whether all of the elements satisfy the function f in range [first, last)
func AllOf[T any](first, last iterator.ConstIterator[T], f func(iterator.ConstIterator[T]) bool) bool {
for iter := first.Clone(); !iter.Equal(last); iter.Next() {
if !f(iter) {
return false
}
}
return true
}

// NoneOf returns whether none of the elements satisfy the function f in range [first, last)
func NoneOf[T any](first, last iterator.ConstIterator[T], f func(iterator.ConstIterator[T]) bool) bool {
return !AnyOf(first, last, f)
}
5 changes: 5 additions & 0 deletions examples/algo_const_op/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"

"github.com/liyue201/gostl/algorithm"
"github.com/liyue201/gostl/ds/deque"
"github.com/liyue201/gostl/utils/comparator"
Expand Down Expand Up @@ -46,4 +47,8 @@ func main() {
if !iter.Equal(a.End()) {
fmt.Printf("largest value : %v\n", iter.Value())
}

fmt.Printf("Any even: %v\n", algorithm.AnyOf[int](a.Begin(), a.End(), isEven))
fmt.Printf("All even: %v\n", algorithm.AllOf[int](a.Begin(), a.End(), isEven))
fmt.Printf("None even: %v\n", algorithm.NoneOf[int](a.Begin(), a.End(), isEven))
}

0 comments on commit 89e0edb

Please sign in to comment.