mirror of
https://github.com/tursom/GoCollections.git
synced 2025-03-13 17:00:18 +08:00
add DCL singleton implement
This commit is contained in:
parent
7bca6fe5cf
commit
c7f63caccc
@ -3,14 +3,18 @@ package lang
|
||||
type (
|
||||
Array[T any] []T
|
||||
|
||||
Int8Array Array[int32]
|
||||
Int16Array Array[int32]
|
||||
Int32Array Array[int32]
|
||||
Int64Array Array[int64]
|
||||
UInt8Array Array[uint32]
|
||||
UInt16Array Array[uint32]
|
||||
UInt32Array Array[uint32]
|
||||
UInt64Array Array[uint64]
|
||||
Int8Array Array[Int32]
|
||||
Int16Array Array[Int32]
|
||||
Int32Array Array[Int32]
|
||||
Int64Array Array[Int64]
|
||||
UInt8Array Array[UInt8]
|
||||
UInt16Array Array[UInt16]
|
||||
UInt32Array Array[UInt32]
|
||||
UInt64Array Array[UInt64]
|
||||
Float32Array Array[Float32]
|
||||
Float64Array Array[Float64]
|
||||
Complex64Array Array[Complex64]
|
||||
Complex128Array Array[Complex128]
|
||||
)
|
||||
|
||||
func (a Array[T]) Array() []T {
|
||||
@ -21,7 +25,7 @@ func (a Int8Array) SetBit(bit int, up bool) (old bool) {
|
||||
arrIndex := bit / 8
|
||||
i := &a[arrIndex]
|
||||
|
||||
return SwapBit(i, bit%8, up)
|
||||
return a[arrIndex].SwapBit(bit%8, up)
|
||||
}
|
||||
|
||||
func (a Int16Array) SetBit(bit int, up bool) (old bool) {
|
||||
|
@ -99,6 +99,15 @@ func (i Int8) ToFloat64() Float64 {
|
||||
return Float64(i)
|
||||
}
|
||||
|
||||
func (i *Int8) BitLength() int {
|
||||
return 8
|
||||
}
|
||||
|
||||
func (i *Int8) SetBit(bit int, up bool) (old bool) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func HashInt8(i int8) int32 {
|
||||
return HashInt32(int32(i))
|
||||
}
|
||||
|
33
util/Singleton.go
Normal file
33
util/Singleton.go
Normal file
@ -0,0 +1,33 @@
|
||||
package util
|
||||
|
||||
import "sync"
|
||||
|
||||
// Singleton DCL singleton implement
|
||||
type Singleton[T any] struct {
|
||||
value T
|
||||
init func() T
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func NewSingleton[T any](init func() T) *Singleton[T] {
|
||||
if init == nil {
|
||||
panic("nil singleton initializer")
|
||||
}
|
||||
|
||||
return &Singleton[T]{
|
||||
init: init,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Singleton[T]) Get() T {
|
||||
if s.init != nil {
|
||||
s.lock.Lock()
|
||||
s.lock.Unlock()
|
||||
if s.init != nil {
|
||||
s.value = s.init()
|
||||
s.init = nil
|
||||
}
|
||||
}
|
||||
|
||||
return s.value
|
||||
}
|
Loading…
Reference in New Issue
Block a user