mirror of
https://github.com/tursom/GoCollections.git
synced 2026-08-22 07:03:28 +08:00
add DCL singleton implement
This commit is contained in:
@@ -3,14 +3,18 @@ package lang
|
|||||||
type (
|
type (
|
||||||
Array[T any] []T
|
Array[T any] []T
|
||||||
|
|
||||||
Int8Array Array[int32]
|
Int8Array Array[Int32]
|
||||||
Int16Array Array[int32]
|
Int16Array Array[Int32]
|
||||||
Int32Array Array[int32]
|
Int32Array Array[Int32]
|
||||||
Int64Array Array[int64]
|
Int64Array Array[Int64]
|
||||||
UInt8Array Array[uint32]
|
UInt8Array Array[UInt8]
|
||||||
UInt16Array Array[uint32]
|
UInt16Array Array[UInt16]
|
||||||
UInt32Array Array[uint32]
|
UInt32Array Array[UInt32]
|
||||||
UInt64Array Array[uint64]
|
UInt64Array Array[UInt64]
|
||||||
|
Float32Array Array[Float32]
|
||||||
|
Float64Array Array[Float64]
|
||||||
|
Complex64Array Array[Complex64]
|
||||||
|
Complex128Array Array[Complex128]
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a Array[T]) Array() []T {
|
func (a Array[T]) Array() []T {
|
||||||
@@ -21,7 +25,7 @@ func (a Int8Array) SetBit(bit int, up bool) (old bool) {
|
|||||||
arrIndex := bit / 8
|
arrIndex := bit / 8
|
||||||
i := &a[arrIndex]
|
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) {
|
func (a Int16Array) SetBit(bit int, up bool) (old bool) {
|
||||||
|
|||||||
@@ -99,6 +99,15 @@ func (i Int8) ToFloat64() Float64 {
|
|||||||
return Float64(i)
|
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 {
|
func HashInt8(i int8) int32 {
|
||||||
return HashInt32(int32(i))
|
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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user