diff --git a/container/glist/glist.go b/container/glist/glist.go index 9e8dce8c1..3676a3850 100644 --- a/container/glist/glist.go +++ b/container/glist/glist.go @@ -20,8 +20,8 @@ import ( type ( List struct { - mu *rwmutex.RWMutex - list *list.List + mu rwmutex.RWMutex + list list.List } Element = list.Element @@ -30,8 +30,8 @@ type ( // New creates and returns a new empty doubly linked list. func New(safe ...bool) *List { return &List{ - mu: rwmutex.New(safe...), - list: list.New(), + mu: rwmutex.Create(safe...), + list: list.List{}, } } @@ -39,12 +39,12 @@ func New(safe ...bool) *List { // The parameter is used to specify whether using list in concurrent-safety, // which is false in default. func NewFrom(array []interface{}, safe ...bool) *List { - l := list.New() + l := list.List{} for _, v := range array { l.PushBack(v) } return &List{ - mu: rwmutex.New(safe...), + mu: rwmutex.Create(safe...), list: l, } } @@ -273,7 +273,7 @@ func (l *List) PushBackList(other *List) { defer other.mu.RUnlock() } l.mu.Lock() - l.list.PushBackList(other.list) + l.list.PushBackList(&other.list) l.mu.Unlock() } @@ -285,7 +285,7 @@ func (l *List) PushFrontList(other *List) { defer other.mu.RUnlock() } l.mu.Lock() - l.list.PushFrontList(other.list) + l.list.PushFrontList(&other.list) l.mu.Unlock() } @@ -332,7 +332,7 @@ func (l *List) Removes(es []*Element) { // RemoveAll removes all elements from list . func (l *List) RemoveAll() { l.mu.Lock() - l.list = list.New() + l.list = list.List{} l.mu.Unlock() } @@ -345,14 +345,14 @@ func (l *List) Clear() { func (l *List) RLockFunc(f func(list *list.List)) { l.mu.RLock() defer l.mu.RUnlock() - f(l.list) + f(&l.list) } // LockFunc locks writing with given callback function within RWMutex.Lock. func (l *List) LockFunc(f func(list *list.List)) { l.mu.Lock() defer l.mu.Unlock() - f(l.list) + f(&l.list) } // Iterator is alias of IteratorAsc. @@ -425,10 +425,6 @@ func (l *List) MarshalJSON() ([]byte, error) { // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. func (l *List) UnmarshalJSON(b []byte) error { - if l.mu == nil { - l.mu = rwmutex.New() - l.list = list.New() - } l.mu.Lock() defer l.mu.Unlock() var array []interface{} @@ -441,10 +437,6 @@ func (l *List) UnmarshalJSON(b []byte) error { // UnmarshalValue is an interface implement which sets any type of value for list. func (l *List) UnmarshalValue(value interface{}) (err error) { - if l.mu == nil { - l.mu = rwmutex.New() - l.list = list.New() - } l.mu.Lock() defer l.mu.Unlock() var array []interface{} diff --git a/container/glist/glist_z_unit_test.go b/container/glist/glist_z_unit_test.go index 9d145d796..cd8b662cc 100644 --- a/container/glist/glist_z_unit_test.go +++ b/container/glist/glist_z_unit_test.go @@ -41,6 +41,44 @@ func checkListPointers(t *gtest.T, l *List, es []*Element) { }) } +func TestVar(t *testing.T) { + var l List + l.PushFront(1) + l.PushFront(2) + if v := l.PopBack(); v != 1 { + t.Errorf("EXPECT %v, GOT %v", 1, v) + } else { + //fmt.Println(v) + } + if v := l.PopBack(); v != 2 { + t.Errorf("EXPECT %v, GOT %v", 2, v) + } else { + //fmt.Println(v) + } + if v := l.PopBack(); v != nil { + t.Errorf("EXPECT %v, GOT %v", nil, v) + } else { + //fmt.Println(v) + } + l.PushBack(1) + l.PushBack(2) + if v := l.PopFront(); v != 1 { + t.Errorf("EXPECT %v, GOT %v", 1, v) + } else { + //fmt.Println(v) + } + if v := l.PopFront(); v != 2 { + t.Errorf("EXPECT %v, GOT %v", 2, v) + } else { + //fmt.Println(v) + } + if v := l.PopFront(); v != nil { + t.Errorf("EXPECT %v, GOT %v", nil, v) + } else { + //fmt.Println(v) + } +} + func TestBasic(t *testing.T) { l := New() l.PushFront(1) diff --git a/container/gring/gring.go b/container/gring/gring.go index 884be1159..fc75335a5 100644 --- a/container/gring/gring.go +++ b/container/gring/gring.go @@ -14,6 +14,7 @@ import ( "github.com/gogf/gf/internal/rwmutex" ) +// Ring is a struct of ring structure. type Ring struct { mu *rwmutex.RWMutex ring *ring.Ring // Underlying ring. @@ -22,6 +23,9 @@ type Ring struct { dirty *gtype.Bool // Dirty, which means the len and cap should be recalculated. It's marked dirty when the size of ring changes. } +// New creates and returns a Ring structure of elements. +// The optional parameter specifies whether using this structure in concurrent safety, +// which is false in default. func New(cap int, safe ...bool) *Ring { return &Ring{ mu: rwmutex.New(safe...), diff --git a/container/gring/gring_unit_test.go b/container/gring/gring_unit_test.go index 9032d9222..144a0d565 100644 --- a/container/gring/gring_unit_test.go +++ b/container/gring/gring_unit_test.go @@ -1,3 +1,9 @@ +// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package gring_test import (