-
Notifications
You must be signed in to change notification settings - Fork 12
/
findanyindex.go
71 lines (65 loc) · 1.42 KB
/
findanyindex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2020 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package slice
import (
"sync"
"github.com/searKing/golang/go/util/object"
)
// FindAnyFunc returns an {@link interface{}} describing some element of the stream, or an
// empty {@code Optional} if the stream is empty.
func FindAnyIndexFunc(s interface{}, f func(interface{}) bool) int {
return findAnyIndexFunc(Of(s), f, true)
}
// findAnyFunc is the same as FindAnyFunc.
func findAnyIndexFunc(s []interface{}, f func(interface{}) bool, truth bool) int {
object.RequireNonNil(s, "findAnyIndexFunc called on nil slice")
object.RequireNonNil(f, "findAnyIndexFunc called on nil callfn")
var findc chan int
findc = make(chan int)
defer close(findc)
var mu sync.Mutex
var wg sync.WaitGroup
var found bool
hasFound := func() bool {
mu.Lock()
defer mu.Unlock()
return found
}
for idx, r := range s {
if hasFound() {
break
}
wg.Add(1)
go func(rr interface{}) {
defer wg.Done()
foundYet := func() bool {
mu.Lock()
defer mu.Unlock()
return found
}()
if foundYet {
return
}
if f(rr) == truth {
mu.Lock()
defer mu.Unlock()
if found {
return
}
found = true
findc <- idx
return
}
}(r)
}
go func() {
defer close(findc)
wg.Done()
}()
out, ok := <-findc
if ok {
return out
}
return -1
}