1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00
This commit is contained in:
2023-08-25 15:31:00 +08:00
parent 04aecd4abc
commit b0a97978d8
58 changed files with 1330 additions and 476 deletions

View File

@ -1,9 +1,5 @@
package list
import (
"github.com/charlienet/go-mixed/locker"
)
const minCapacity = 16
type ArrayList[T any] struct {
@ -31,7 +27,7 @@ func NewArrayList[T any](elems ...T) *ArrayList[T] {
}
l := &ArrayList[T]{
list: list[T]{size: size, locker: locker.EmptyLocker},
list: list[T]{size: size},
buf: buf,
tail: tail,
minCap: minCap,
@ -45,8 +41,8 @@ func NewArrayList[T any](elems ...T) *ArrayList[T] {
}
func (l *ArrayList[T]) PushFront(v T) {
l.locker.Lock()
defer l.locker.Unlock()
l.mu.Lock()
defer l.mu.Unlock()
l.grow()
@ -56,8 +52,8 @@ func (l *ArrayList[T]) PushFront(v T) {
}
func (l *ArrayList[T]) PushBack(v T) {
l.locker.Lock()
defer l.locker.Unlock()
l.mu.Lock()
defer l.mu.Unlock()
l.grow()
@ -68,8 +64,8 @@ func (l *ArrayList[T]) PushBack(v T) {
}
func (l *ArrayList[T]) PopFront() T {
l.locker.Lock()
defer l.locker.Unlock()
l.mu.Lock()
defer l.mu.Unlock()
if l.size <= 0 {
panic("list: PopFront() called on empty list")
@ -86,8 +82,8 @@ func (l *ArrayList[T]) PopFront() T {
}
func (l *ArrayList[T]) PopBack() T {
l.locker.Lock()
defer l.locker.Unlock()
l.mu.Lock()
defer l.mu.Unlock()
l.tail = l.prev(l.tail)
@ -105,8 +101,8 @@ func (l *ArrayList[T]) RemoveAt(at int) T {
panic(ErrorOutOffRange)
}
l.locker.Lock()
defer l.locker.Unlock()
l.mu.Lock()
defer l.mu.Unlock()
rm := (l.head + at) & (len(l.buf) - 1)
if at*2 < l.size {
@ -127,22 +123,22 @@ func (l *ArrayList[T]) RemoveAt(at int) T {
}
func (l *ArrayList[T]) Front() T {
l.locker.RLock()
defer l.locker.RUnlock()
l.mu.RLock()
defer l.mu.RUnlock()
return l.buf[l.head]
}
func (l *ArrayList[T]) Back() T {
l.locker.RLock()
defer l.locker.RUnlock()
l.mu.RLock()
defer l.mu.RUnlock()
return l.buf[l.tail]
}
func (l *ArrayList[T]) ForEach(fn func(T)) {
l.locker.RLock()
defer l.locker.RUnlock()
l.mu.RLock()
defer l.mu.RUnlock()
n := l.head
for i := 0; i < l.size; i++ {

View File

@ -1,9 +1,5 @@
package list
import (
"github.com/charlienet/go-mixed/locker"
)
type LinkedList[T any] struct {
list[T]
front, tail *LinkedNode[T]
@ -16,9 +12,7 @@ type LinkedNode[T any] struct {
// NewLinkedList 初始化链表
func NewLinkedList[T any](elems ...T) *LinkedList[T] {
l := &LinkedList[T]{
list: list[T]{locker: locker.EmptyLocker},
}
l := &LinkedList[T]{}
for _, e := range elems {
l.pushBackNode(&LinkedNode[T]{Value: e})
@ -28,8 +22,8 @@ func NewLinkedList[T any](elems ...T) *LinkedList[T] {
}
func (l *LinkedList[T]) PushBack(v T) *LinkedList[T] {
l.locker.Lock()
defer l.locker.Unlock()
l.mu.Lock()
defer l.mu.Unlock()
l.pushBackNode(&LinkedNode[T]{Value: v})
@ -37,8 +31,8 @@ func (l *LinkedList[T]) PushBack(v T) *LinkedList[T] {
}
func (l *LinkedList[T]) PushFront(v T) *LinkedList[T] {
l.locker.Lock()
defer l.locker.Unlock()
l.mu.Lock()
defer l.mu.Unlock()
l.pushFrontNode(&LinkedNode[T]{Value: v})
@ -65,8 +59,8 @@ func (l *LinkedList[T]) Back() T {
}
func (l *LinkedList[T]) ForEach(fn func(T) bool) {
l.locker.RLock()
defer l.locker.RUnlock()
l.mu.RLock()
defer l.mu.RUnlock()
for current := l.front; current != nil; current = current.Next {
if fn(current.Value) {
@ -77,12 +71,10 @@ func (l *LinkedList[T]) ForEach(fn func(T) bool) {
func (l *LinkedList[T]) GetAt(i int) T {
if i <= l.Size() {
var n int
for current := l.front; current != nil; current = current.Next {
for n, current := 0, l.front; current != nil; current, n = current.Next, n+1 {
if n == i {
return current.Value
}
n++
}
}
@ -90,8 +82,8 @@ func (l *LinkedList[T]) GetAt(i int) T {
}
func (l *LinkedList[T]) Remove(n *LinkedNode[T]) {
l.locker.Lock()
defer l.locker.Unlock()
l.mu.Lock()
defer l.mu.Unlock()
if n.Next != nil {
n.Next.Prev = n.Prev
@ -112,8 +104,8 @@ func (l *LinkedList[T]) Remove(n *LinkedNode[T]) {
}
func (l *LinkedList[T]) RemoveAt(index int) {
l.locker.Lock()
defer l.locker.Unlock()
l.mu.Lock()
defer l.mu.Unlock()
var i int
for current := l.front; current != nil; current = current.Next {

View File

@ -12,12 +12,12 @@ type List[T any] interface {
}
type list[T any] struct {
size int
locker locker.RWLocker
size int
mu locker.WithRWLocker
}
func (l *list[T]) Synchronize() {
l.locker = locker.NewRWLocker()
l.mu.Synchronize()
}
func (l *list[T]) ForEach(fn func(T) bool) { panic("Not Implemented") }