-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset64.go
More file actions
120 lines (107 loc) · 2.04 KB
/
Copy pathset64.go
File metadata and controls
120 lines (107 loc) · 2.04 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package ds
import (
"errors"
"sync"
)
type Arrint64 []int64
type Set64 struct {
Length int
S Arrint64
M map[int64]struct{}
Mu *sync.RWMutex
}
func (ai Arrint64) add(value int64) Arrint64 {
if len(ai) == 0 {
ai = append(ai, value)
return ai
}
var temp Arrint64
if cap(ai) > len(ai)+1 {
temp = make([]int64, 0, cap(ai))
} else {
temp = make([]int64, 0, cap(ai)*2)
}
for i, v := range ai {
// 1, 2, 4, 5
// 如果值大于最大的值, 直接添加到末尾
if i == len(ai)-1 && value > v {
temp = append(ai, value)
return temp
}
if value < v {
if i == 0 {
temp = append(temp, value)
temp = append(temp, ai...)
return temp
} else {
temp = append(temp, ai[:i]...)
temp = append(temp, value)
temp = append(temp, ai[i:]...)
return temp
}
}
}
return ai
}
func NewSet64() *Set64 {
return &Set64{
S: make([]int64, 0),
M: make(map[int64]struct{}),
Mu: &sync.RWMutex{},
}
}
func NewValueSet64(value []int64) *Set64 {
set := NewSet64()
for _, v := range value {
set.Add(v)
}
return set
}
func (s *Set64) Add(value int64) *Set64 {
s.Mu.Lock()
defer s.Mu.Unlock()
if _, ok := s.M[value]; !ok {
s.Length++
s.M[value] = struct{}{}
s.S = s.S.add(value)
}
return s
}
func (s *Set64) Get() []int64 {
s.Mu.RLock()
defer s.Mu.RUnlock()
return s.S
}
// 判断是否存在某个值
func (s *Set64) Exsit(v int64) bool {
s.Mu.RLock()
defer s.Mu.RUnlock()
_, ok := s.M[v]
return ok
}
// 通过索引获取值
func (s *Set64) GetIndex(i int) (int64, error) {
s.Mu.RLock()
defer s.Mu.RUnlock()
if s.Length >= i || i < 0 {
return 0, errors.New("not found this index")
}
return s.S[i], nil
}
// 是否完全包含传入的值
func (s *Set64) Contents(value []int64) bool {
s.Mu.RLock()
defer s.Mu.RUnlock()
new := NewValueSet64(value)
// 如果比传入过来的值小, 那么肯定不完全包含
if s.Length < new.Length {
return false
}
for i := 0; i < new.Length; i++ {
v, _ := new.GetIndex(i)
if !s.Exsit(v) {
return false
}
}
return true
}