mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-17 16:12:42 +08:00
iter
This commit is contained in:
1
collections/node.go
Normal file
1
collections/node.go
Normal file
@ -0,0 +1 @@
|
||||
package collections
|
@ -54,6 +54,24 @@ func NewRsa(h Hash, opts ...rsaOption) (*rsaInstance, error) {
|
||||
return o, nil
|
||||
}
|
||||
|
||||
func ParsePKCS8PrivateKey(p []byte) rsaOption {
|
||||
return func(o *rsaInstance) error {
|
||||
block, _ := pem.Decode(p)
|
||||
if block == nil {
|
||||
return errors.New("failed to decode private key")
|
||||
}
|
||||
|
||||
prk, err := x509.ParsePKCS8PrivateKey(block.Bytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
o.prk = prk.(*rsa.PrivateKey)
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func ParsePKCS1PrivateKey(p []byte) rsaOption {
|
||||
return func(o *rsaInstance) error {
|
||||
block, _ := pem.Decode(p)
|
||||
|
@ -3,7 +3,6 @@ package crypto
|
||||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
s "github.com/tjfoc/gmsm/sm2"
|
||||
x "github.com/tjfoc/gmsm/x509"
|
||||
@ -54,8 +53,6 @@ func NewSm2(opts ...option) (*sm2Instance, error) {
|
||||
|
||||
func ParseSm2PrivateKey(p []byte, pwd []byte) option {
|
||||
return func(so *sm2Instance) error {
|
||||
fmt.Println(string(p))
|
||||
|
||||
priv, err := x.ReadPrivateKeyFromPem(p, pwd)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -7,8 +7,19 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/charlienet/go-mixed/hash"
|
||||
"github.com/charlienet/go-mixed/rand"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestHashComplie(t *testing.T) {
|
||||
abc, err := hash.New("MD5")
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
b, _ := hex.DecodeString(rand.Hex.Generate(16))
|
||||
assert.False(t, abc.Verify([]byte("source"), b))
|
||||
}
|
||||
|
||||
func TestEncode(t *testing.T) {
|
||||
t.Log(hash.Sha1([]byte{0x31}).Base64())
|
||||
t.Log(hash.Sha1([]byte{0x31}).Hex())
|
||||
|
@ -49,6 +49,17 @@ func (m *ConcurrnetMap[K, V]) Exist(key K) bool {
|
||||
return mm.Exist(key)
|
||||
}
|
||||
|
||||
func (m *ConcurrnetMap[K, V]) Iter() <-chan *Entry[K, V] {
|
||||
num := int(m.numOfBuckets)
|
||||
ch := make(chan *Entry[K, V], m.Count())
|
||||
for i := 0; i < num; i++ {
|
||||
c := m.buckets[i].Iter()
|
||||
ch <- <-c
|
||||
}
|
||||
|
||||
return ch
|
||||
}
|
||||
|
||||
func (m *ConcurrnetMap[K, V]) ForEach(f func(K, V)) {
|
||||
var wg sync.WaitGroup
|
||||
|
||||
|
@ -34,6 +34,22 @@ func (m *hashMap[K, V]) Exist(key K) bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
func (m *hashMap[K, V]) Iter() <-chan *Entry[K, V] {
|
||||
ch := make(chan *Entry[K, V], m.Count())
|
||||
go func() {
|
||||
for k, v := range m.m {
|
||||
ch <- &Entry[K, V]{
|
||||
Key: k,
|
||||
Value: v,
|
||||
}
|
||||
}
|
||||
|
||||
close(ch)
|
||||
}()
|
||||
|
||||
return ch
|
||||
}
|
||||
|
||||
func (m *hashMap[K, V]) ForEach(f func(K, V)) {
|
||||
for k, v := range m.m {
|
||||
f(k, v)
|
||||
|
@ -10,5 +10,11 @@ type Map[K constraints.Ordered, V any] interface {
|
||||
Clone() Map[K, V]
|
||||
Clear()
|
||||
Count() int
|
||||
Iter() <-chan *Entry[K, V]
|
||||
ForEach(f func(K, V))
|
||||
}
|
||||
|
||||
type Entry[K constraints.Ordered, V any] struct {
|
||||
Key K
|
||||
Value V
|
||||
}
|
||||
|
@ -49,6 +49,13 @@ func (m *rw_map[K, V]) Count() int {
|
||||
return m.m.Count()
|
||||
}
|
||||
|
||||
func (m *rw_map[K, V]) Iter() <-chan *Entry[K, V] {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
return m.m.Iter()
|
||||
}
|
||||
|
||||
func (m *rw_map[K, V]) ForEach(f func(K, V)) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
@ -64,6 +64,23 @@ func (m *sorted_map[K, V]) Clone() Map[K, V] {
|
||||
return &sorted_map[K, V]{maps: m.maps.Clone(), keys: getKeys(m.maps)}
|
||||
}
|
||||
|
||||
func (m *sorted_map[K, V]) Iter() <-chan *Entry[K, V] {
|
||||
c := make(chan *Entry[K, V], m.Count())
|
||||
go func() {
|
||||
for _, k := range m.keys {
|
||||
v, _ := m.maps.Get(k)
|
||||
|
||||
c <- &Entry[K, V]{
|
||||
Key: k,
|
||||
Value: v,
|
||||
}
|
||||
}
|
||||
close(c)
|
||||
}()
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (m *sorted_map[K, V]) ForEach(f func(K, V)) {
|
||||
m.maps.ForEach(f)
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ func (s hash_set[T]) Contain(value T) bool {
|
||||
|
||||
func (s hash_set[T]) Clone() hash_set[T] {
|
||||
set := NewHashSet[T]()
|
||||
set.Add(s.Values()...)
|
||||
set.Add(s.ToSlice()...)
|
||||
return set
|
||||
}
|
||||
|
||||
@ -38,7 +38,7 @@ func (s hash_set[T]) Iterate(fn func(value T)) {
|
||||
// Union creates a new set contain all element of set s and other
|
||||
func (s hash_set[T]) Union(other hash_set[T]) hash_set[T] {
|
||||
set := s.Clone()
|
||||
set.Add(other.Values()...)
|
||||
set.Add(other.ToSlice()...)
|
||||
return set
|
||||
}
|
||||
|
||||
@ -54,7 +54,7 @@ func (s hash_set[T]) Intersection(other hash_set[T]) hash_set[T] {
|
||||
return set
|
||||
}
|
||||
|
||||
func (s hash_set[T]) Values() []T {
|
||||
func (s hash_set[T]) ToSlice() []T {
|
||||
values := make([]T, 0, s.Size())
|
||||
s.Iterate(func(value T) {
|
||||
values = append(values, value)
|
||||
|
@ -1,4 +1,8 @@
|
||||
package sets
|
||||
|
||||
type Set[T comparable] interface {
|
||||
Add(values ...T)
|
||||
Remove(v T)
|
||||
Contain(value T) bool
|
||||
IsEmpty() bool
|
||||
}
|
||||
|
@ -5,4 +5,12 @@ type sorted_set[T comparable] struct {
|
||||
set Set[T]
|
||||
}
|
||||
|
||||
func NewSortedSet[T comparable]() *sorted_set[T] {
|
||||
return &sorted_set[T]{
|
||||
set: NewHashSet[T](),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *sorted_set[T]) ToSlice() []T {
|
||||
return s.sorted
|
||||
}
|
||||
|
Reference in New Issue
Block a user