mirror of
https://github.com/tursom/GoCollections.git
synced 2025-03-13 17:00:18 +08:00
update
This commit is contained in:
parent
f72933280c
commit
693d0a45c0
BIN
GoCollections
BIN
GoCollections
Binary file not shown.
51
concurrent/lock/CLH.go
Normal file
51
concurrent/lock/CLH.go
Normal file
@ -0,0 +1,51 @@
|
||||
package lock
|
||||
|
||||
import (
|
||||
"github.com/petermattis/goid"
|
||||
|
||||
"github.com/tursom/GoCollections/exceptions"
|
||||
"github.com/tursom/GoCollections/lang/atomic"
|
||||
)
|
||||
|
||||
type (
|
||||
CLH struct {
|
||||
tail atomic.Reference[clhNode]
|
||||
state atomic.Reference[clhNode]
|
||||
}
|
||||
|
||||
clhNode struct {
|
||||
prev *clhNode
|
||||
locked atomic.Bool
|
||||
gid int64
|
||||
}
|
||||
)
|
||||
|
||||
func (c *CLH) Lock() {
|
||||
node := &clhNode{
|
||||
prev: c.tail.Load(),
|
||||
locked: 1, // true
|
||||
gid: goid.Get(),
|
||||
}
|
||||
|
||||
for !c.tail.CompareAndSwap(node.prev, node) {
|
||||
node.prev = c.tail.Load()
|
||||
}
|
||||
|
||||
if node.prev == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for node.prev.locked.Load() {
|
||||
}
|
||||
|
||||
c.state.Store(node)
|
||||
}
|
||||
|
||||
func (c *CLH) Unlock() {
|
||||
node := c.state.Load()
|
||||
if node.gid != goid.Get() {
|
||||
panic(exceptions.NewIllegalAccessException("unlock with wrong goroutine", nil))
|
||||
}
|
||||
|
||||
node.locked.Store(false)
|
||||
}
|
18
exceptions/IllegalAccess.go
Normal file
18
exceptions/IllegalAccess.go
Normal file
@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright (c) 2022 tursom. All rights reserved.
|
||||
* Use of this source code is governed by a GPL-3
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
package exceptions
|
||||
|
||||
type IllegalAccessException struct {
|
||||
RuntimeException
|
||||
}
|
||||
|
||||
func NewIllegalAccessException(message string, config *ExceptionConfig) *IllegalAccessException {
|
||||
return &IllegalAccessException{
|
||||
*NewRuntimeException(message, config.AddSkipStack(1).
|
||||
SetExceptionName("github.com.tursom.GoCollections.exceptions.IllegalParameterException")),
|
||||
}
|
||||
}
|
2
go.mod
2
go.mod
@ -1,6 +1,6 @@
|
||||
module github.com/tursom/GoCollections
|
||||
|
||||
go 1.19
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/petermattis/goid v0.0.0-20220302125637-5f11c28912df
|
||||
|
@ -3,8 +3,8 @@ package lang
|
||||
type (
|
||||
Array[T any] []T
|
||||
|
||||
Int8Array Array[Int32]
|
||||
Int16Array Array[Int32]
|
||||
Int8Array Array[Int8]
|
||||
Int16Array Array[Int16]
|
||||
Int32Array Array[Int32]
|
||||
Int64Array Array[Int64]
|
||||
UInt8Array Array[UInt8]
|
||||
@ -25,54 +25,54 @@ func (a Int8Array) SetBit(bit int, up bool) (old bool) {
|
||||
arrIndex := bit / 8
|
||||
i := &a[arrIndex]
|
||||
|
||||
return a[arrIndex].SwapBit(bit%8, up)
|
||||
return SwapBit[int8]((*int8)(i), bit%8, up)
|
||||
}
|
||||
|
||||
func (a Int16Array) SetBit(bit int, up bool) (old bool) {
|
||||
arrIndex := bit / 16
|
||||
i := &a[arrIndex]
|
||||
|
||||
return SwapBit(i, bit%16, up)
|
||||
return SwapBit[int16]((*int16)(i), bit%16, up)
|
||||
}
|
||||
|
||||
func (a Int32Array) SetBit(bit int, up bool) (old bool) {
|
||||
arrIndex := bit / 32
|
||||
i := &a[arrIndex]
|
||||
|
||||
return SwapBit(i, bit%32, up)
|
||||
return SwapBit[int32]((*int32)(i), bit%32, up)
|
||||
}
|
||||
|
||||
func (a Int64Array) SetBit(bit int, up bool) (old bool) {
|
||||
arrIndex := bit / 64
|
||||
i := &a[arrIndex]
|
||||
|
||||
return SwapBit(i, bit%64, up)
|
||||
return SwapBit[int64]((*int64)(i), bit%64, up)
|
||||
}
|
||||
|
||||
func (a UInt8Array) SetBit(bit int, up bool) (old bool) {
|
||||
arrIndex := bit / 8
|
||||
i := &a[arrIndex]
|
||||
|
||||
return SwapBit(i, bit%8, up)
|
||||
return SwapBit[uint8]((*uint8)(i), bit%8, up)
|
||||
}
|
||||
|
||||
func (a UInt16Array) SetBit(bit int, up bool) (old bool) {
|
||||
arrIndex := bit / 16
|
||||
i := &a[arrIndex]
|
||||
|
||||
return SwapBit(i, bit%16, up)
|
||||
return SwapBit[uint16]((*uint16)(i), bit%16, up)
|
||||
}
|
||||
|
||||
func (a UInt32Array) SetBit(bit int, up bool) (old bool) {
|
||||
arrIndex := bit / 32
|
||||
i := &a[arrIndex]
|
||||
|
||||
return SwapBit(i, bit%32, up)
|
||||
return SwapBit[uint32]((*uint32)(i), bit%32, up)
|
||||
}
|
||||
|
||||
func (a UInt64Array) SetBit(bit int, up bool) (old bool) {
|
||||
arrIndex := bit / 64
|
||||
i := &a[arrIndex]
|
||||
|
||||
return SwapBit(i, bit%64, up)
|
||||
return SwapBit[uint64]((*uint64)(i), bit%64, up)
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ type Number interface {
|
||||
ToFloat64() Float64
|
||||
}
|
||||
|
||||
func SwapBit[T int32 | int64 | uint32 | uint64](p *T, bit int, new bool) (old bool) {
|
||||
func SwapBit[T int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64](p *T, bit int, new bool) (old bool) {
|
||||
location := T(1) << bit
|
||||
oldValue := *p
|
||||
var newValue T
|
||||
|
19
lang/functional/functional.go
Normal file
19
lang/functional/functional.go
Normal file
@ -0,0 +1,19 @@
|
||||
package functional
|
||||
|
||||
type (
|
||||
Function interface {
|
||||
invoke()
|
||||
}
|
||||
|
||||
FunctionErred interface {
|
||||
invoke() error
|
||||
}
|
||||
|
||||
Function1[A1 any] interface {
|
||||
invoke(a1 A1)
|
||||
}
|
||||
|
||||
Function1Erred[A1 any] interface {
|
||||
invoke(a1 A1) error
|
||||
}
|
||||
)
|
6
lang/functional/simplify.go
Normal file
6
lang/functional/simplify.go
Normal file
@ -0,0 +1,6 @@
|
||||
package functional
|
||||
|
||||
type (
|
||||
F = Function
|
||||
FE = FunctionErred
|
||||
)
|
Loading…
Reference in New Issue
Block a user