Skip to content

Commit 586722f

Browse files
authored
[type:feat] support xlist (#98)
* [type:feat] support xmap and lru. (#76) * [type:feat] support xmap and lru. * [type:feat] support xlist * [type:feat] support xlist
1 parent b53eb85 commit 586722f

File tree

3 files changed

+288
-0
lines changed

3 files changed

+288
-0
lines changed

gox/containerx/xlist/list.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package xlist
19+
20+
type List interface {
21+
// PushBack
22+
// @Description:
23+
// @param o
24+
PushBack(o interface{})
25+
26+
// PushFront
27+
// @Description:
28+
// @param o
29+
PushFront(o interface{})
30+
31+
// Remove
32+
// @Description:
33+
// @param o
34+
Remove(o interface{})
35+
36+
// Front
37+
// @Description:
38+
// @return interface{}
39+
Front() interface{}
40+
41+
// Back
42+
// @Description:
43+
// @return interface{}
44+
Back() interface{}
45+
46+
// PopFront
47+
// @Description:
48+
// @return interface{}
49+
PopFront() interface{}
50+
51+
// PopBack
52+
// @Description:
53+
// @return interface{}
54+
PopBack() interface{}
55+
56+
// Len
57+
// @Description:
58+
// @return int
59+
Len() int
60+
61+
// Foreach
62+
// @Description: The function that accepts polling returns true to continue polling and false to terminate polling
63+
// @param f
64+
Foreach(f func(interface{}) bool)
65+
66+
// Find
67+
// @Description:
68+
// @param i
69+
// @return bool
70+
Find(i interface{}) bool
71+
}

gox/containerx/xlist/simple_list.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package xlist
19+
20+
import (
21+
"container/list"
22+
"reflect"
23+
)
24+
25+
type SimpleList struct {
26+
l *list.List
27+
}
28+
29+
func NewSimpleList() *SimpleList {
30+
return &SimpleList{
31+
l: list.New(),
32+
}
33+
}
34+
35+
// PushBack
36+
// @Description: Add an element at the end of the linked list
37+
// @receiver l
38+
// @param o
39+
func (l *SimpleList) PushBack(o interface{}) {
40+
l.l.PushBack(o)
41+
}
42+
43+
// PushFront
44+
// @Description: Add an element at the head of the linked list
45+
// @receiver l
46+
// @param o
47+
func (l *SimpleList) PushFront(o interface{}) {
48+
l.l.PushFront(o)
49+
}
50+
51+
// Remove
52+
// @Description: remove O(N)
53+
// @receiver l
54+
// @param o
55+
func (l *SimpleList) Remove(o interface{}) {
56+
for e := l.l.Front(); e != nil; e = e.Next() {
57+
if reflect.DeepEqual(o, e.Value) {
58+
l.l.Remove(e)
59+
return
60+
}
61+
}
62+
}
63+
64+
// Front
65+
// @Description: First element (first element), if nil is not returned
66+
// @receiver l
67+
// @return interface{}
68+
func (l *SimpleList) Front() interface{} {
69+
e := l.l.Front()
70+
if e != nil {
71+
return e.Value
72+
}
73+
return nil
74+
}
75+
76+
// Back
77+
// @Description: Tail element (the last element), if nil is not returned
78+
// @receiver l
79+
// @return interface{}
80+
func (l *SimpleList) Back() interface{} {
81+
e := l.l.Back()
82+
if e != nil {
83+
return e.Value
84+
}
85+
return nil
86+
}
87+
88+
// PopFront
89+
// @Description: Get the first element and remove it, if nil is not returned
90+
// @receiver l
91+
// @return interface{}
92+
func (l *SimpleList) PopFront() interface{} {
93+
e := l.l.Front()
94+
if e != nil {
95+
l.l.Remove(e)
96+
return e.Value
97+
}
98+
return nil
99+
}
100+
101+
// PopBack
102+
// @Description: Get the tail element and remove it, if nil is not returned
103+
// @receiver l
104+
// @return interface{}
105+
func (l *SimpleList) PopBack() interface{} {
106+
e := l.l.Back()
107+
if e != nil {
108+
l.l.Remove(e)
109+
return e.Value
110+
}
111+
return nil
112+
}
113+
114+
// Len
115+
// @Description:
116+
// @receiver l
117+
// @return int
118+
func (l *SimpleList) Len() int {
119+
return l.l.Len()
120+
}
121+
122+
// Foreach
123+
// @Description:
124+
// @receiver l
125+
// @param f The function that accepts polling returns true to continue polling and false to terminate polling
126+
func (l *SimpleList) Foreach(f func(interface{}) bool) {
127+
for e := l.l.Front(); e != nil; e = e.Next() {
128+
if !f(e.Value) {
129+
return
130+
}
131+
}
132+
}
133+
134+
// Find
135+
// @Description: Query whether there is a parameter object in the linked list
136+
// @receiver l
137+
// @param i
138+
// @return bool bool success is true, fail is false
139+
func (l *SimpleList) Find(i interface{}) bool {
140+
for e := l.l.Front(); e != nil; e = e.Next() {
141+
if reflect.DeepEqual(i, e.Value) {
142+
return true
143+
}
144+
}
145+
return false
146+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package xlist
19+
20+
import (
21+
"github.com/acmestack/godkits/assert"
22+
"testing"
23+
)
24+
25+
func TestNewSimpleList(t *testing.T) {
26+
simpleList := NewSimpleList()
27+
type R struct {
28+
i int
29+
}
30+
r := &R{
31+
i: 1,
32+
}
33+
r2 := &R{
34+
i: 2,
35+
}
36+
simpleList.PushBack(r)
37+
simpleList.PushBack(r2)
38+
simpleList.PushFront(r2)
39+
simpleList.PushFront(r)
40+
41+
var index = 0
42+
43+
simpleList.Foreach(func(key interface{}) bool {
44+
return false
45+
})
46+
simpleList.Foreach(func(key interface{}) bool {
47+
index++
48+
return true
49+
})
50+
assert.NotEqual(t, index, simpleList.Len(), "Foreach error")
51+
52+
assert.NotEqual(t, simpleList.Find(r2), true, "r2 does not exist in the simpleList")
53+
54+
assert.NotEqual(t, simpleList.PopBack(), r2, "PopBack error")
55+
assert.NotEqual(t, simpleList.PopFront(), r, "PopFront error")
56+
57+
simpleList.Remove(r)
58+
assert.NotEqual(t, simpleList.Find(r), false, "Remove error")
59+
60+
assert.NotEqual(t, simpleList.Back(), r2, "Back error")
61+
assert.NotEqual(t, simpleList.Front(), r2, "Front error")
62+
63+
assert.NotEqual(t, simpleList.PopBack(), r2, "PopBack error")
64+
assert.NotEqual(t, simpleList.PopBack(), nil, "PopBack error")
65+
assert.NotEqual(t, simpleList.PopFront(), nil, "PopFront error")
66+
67+
assert.NotEqual(t, simpleList.Len(), 0, "Len error")
68+
assert.NotEqual(t, simpleList.Back(), nil, "Back error")
69+
assert.NotEqual(t, simpleList.Front(), nil, "Front error")
70+
71+
}

0 commit comments

Comments
 (0)