-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
50 lines (41 loc) · 786 Bytes
/
list.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
package main
type List[T comparable] struct {
l []T
}
func (l *List[T]) Push(v ...T) {
l.l = append(l.l, v...)
}
func (l *List[T]) Insert(v T) {
l.InsertAt(0, v)
return
}
func (l *List[T]) InsertAt(pos int, v T) {
l.l, l.l[0] = append(l.l[:pos+1], l.l[pos:]...), v
return
}
func (l *List[T]) Remove(i int) {
l.l = l.l[:i+copy(l.l[i:], l.l[i+1:])]
return
}
func (l *List[T]) Equals(rhs *List[T]) bool {
if len(l.l) != len(rhs.l) {
return false
}
for i := 0; i < len(l.l); i++ {
if l.l[i] != rhs.l[i] {
return false
}
}
return true
}
func (l *List[T]) Clone() *List[T] {
ll := &List[T]{l: make([]T, len(l.l))}
copy(ll.l, l.l)
return ll
}
func (l *List[T]) Slice() []T {
return l.l
}
func NewList[T comparable](t []T) *List[T] {
return &List[T]{l: t}
}