Skip to content

Commit

Permalink
Merge pull request #2 from songzhibin97/struct_safe
Browse files Browse the repository at this point in the history
add: structure safe
  • Loading branch information
songzhibin97 authored Apr 26, 2023
2 parents 183ee01 + 1e1af44 commit b4387cd
Show file tree
Hide file tree
Showing 27 changed files with 2,694 additions and 6 deletions.
123 changes: 123 additions & 0 deletions structure/lists/arraylist/arraylist_safe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package arraylist

import (
"github.com/songzhibin97/go-baseutils/base/bcomparator"
"github.com/songzhibin97/go-baseutils/structure/lists"
"sync"
)

var _ lists.List[any] = (*ListSafe[any])(nil)

func NewSafe[E any](values ...E) *ListSafe[E] {
return &ListSafe[E]{
unsafe: New(values...),
}
}

type ListSafe[E any] struct {
unsafe *List[E]
lock sync.Mutex
}

func (s *ListSafe[E]) Add(values ...E) {
s.lock.Lock()
defer s.lock.Unlock()
s.unsafe.Add(values...)

}

func (s *ListSafe[E]) Get(index int) (E, bool) {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.Get(index)
}

func (s *ListSafe[E]) Remove(index int) {
s.lock.Lock()
defer s.lock.Unlock()
s.unsafe.Remove(index)

}

func (s *ListSafe[E]) Contains(values ...E) bool {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.Contains(values...)
}

func (s *ListSafe[E]) Values() []E {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.Values()
}

func (s *ListSafe[E]) IndexOf(value E) int {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.IndexOf(value)
}

func (s *ListSafe[E]) Empty() bool {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.Empty()
}

func (s *ListSafe[E]) Size() int {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.Size()
}

func (s *ListSafe[E]) Clear() {
s.lock.Lock()
defer s.lock.Unlock()
s.unsafe.Clear()

}

func (s *ListSafe[E]) Sort(comparator bcomparator.Comparator[E]) {
s.lock.Lock()
defer s.lock.Unlock()
s.unsafe.Sort(comparator)

}

func (s *ListSafe[E]) Swap(i int, j int) {
s.lock.Lock()
defer s.lock.Unlock()
s.unsafe.Swap(i, j)

}

func (s *ListSafe[E]) Insert(index int, values ...E) {
s.lock.Lock()
defer s.lock.Unlock()
s.unsafe.Insert(index, values...)

}

func (s *ListSafe[E]) Set(index int, value E) {
s.lock.Lock()
defer s.lock.Unlock()
s.unsafe.Set(index, value)

}

func (s *ListSafe[E]) String() string {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.String()
}

func (s *ListSafe[E]) UnmarshalJSON(bytes []byte) error {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.UnmarshalJSON(bytes)
}

func (s *ListSafe[E]) MarshalJSON() ([]byte, error) {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.MarshalJSON()
}
138 changes: 138 additions & 0 deletions structure/lists/doublylinkedlist/doublylinkedlist_safe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package doublylinkedlist

import (
"github.com/songzhibin97/go-baseutils/base/bcomparator"
"github.com/songzhibin97/go-baseutils/structure/lists"
"sync"
)

var _ lists.List[any] = (*ListSafe[any])(nil)

func NewSafe[E any](values ...E) *ListSafe[E] {
return &ListSafe[E]{
unsafe: New(values...),
}
}


type ListSafe[E any] struct {
unsafe *List[E]
lock sync.Mutex
}

func (s *ListSafe[E]) Add(values ...E) {
s.lock.Lock()
defer s.lock.Unlock()
s.unsafe.Add(values...)

}

func (s *ListSafe[E]) Append(values ...E) {
s.lock.Lock()
defer s.lock.Unlock()
s.unsafe.Append(values...)

}

func (s *ListSafe[E]) Prepend(values ...E) {
s.lock.Lock()
defer s.lock.Unlock()
s.unsafe.Prepend(values...)

}

func (s *ListSafe[E]) Get(index int) (E, bool) {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.Get(index)
}

func (s *ListSafe[E]) Remove(index int) {
s.lock.Lock()
defer s.lock.Unlock()
s.unsafe.Remove(index)

}

func (s *ListSafe[E]) Contains(values ...E) bool {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.Contains(values...)
}

func (s *ListSafe[E]) Values() []E {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.Values()
}

func (s *ListSafe[E]) IndexOf(value E) int {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.IndexOf(value)
}

func (s *ListSafe[E]) Empty() bool {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.Empty()
}

func (s *ListSafe[E]) Size() int {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.Size()
}

func (s *ListSafe[E]) Clear() {
s.lock.Lock()
defer s.lock.Unlock()
s.unsafe.Clear()

}

func (s *ListSafe[E]) Sort(comparator bcomparator.Comparator[E]) {
s.lock.Lock()
defer s.lock.Unlock()
s.unsafe.Sort(comparator)

}

func (s *ListSafe[E]) Swap(i int, j int) {
s.lock.Lock()
defer s.lock.Unlock()
s.unsafe.Swap(i, j)

}

func (s *ListSafe[E]) Insert(index int, values ...E) {
s.lock.Lock()
defer s.lock.Unlock()
s.unsafe.Insert(index, values...)

}

func (s *ListSafe[E]) Set(index int, value E) {
s.lock.Lock()
defer s.lock.Unlock()
s.unsafe.Set(index, value)

}

func (s *ListSafe[E]) String() string {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.String()
}

func (s *ListSafe[E]) UnmarshalJSON(bytes []byte) error {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.UnmarshalJSON(bytes)
}

func (s *ListSafe[E]) MarshalJSON() ([]byte, error) {
s.lock.Lock()
defer s.lock.Unlock()
return s.unsafe.MarshalJSON()
}
6 changes: 6 additions & 0 deletions structure/lists/singlylinkedlist/singlylinkedlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import (
// Assert List implementation
var _ lists.List[any] = (*List[any])(nil)

func NewSafe[E any](values ...E) *ListSafe[E] {
return &ListSafe[E]{
unsafe: New(values...),
}
}

// List holds the elements, where each element points to the next element
type List[E any] struct {
first *element[E]
Expand Down
Loading

0 comments on commit b4387cd

Please sign in to comment.