-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
164 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,55 @@ | ||
package Pool | ||
|
||
import ( | ||
"reflect" | ||
"sync" | ||
) | ||
|
||
type Var interface { | ||
New() | ||
Set(...any) | ||
Reset() | ||
type Var[T any] interface { | ||
OnNew() | ||
OnGet(...any) | ||
OnPut() | ||
~*T | ||
} | ||
|
||
type Pool[T Var] struct { | ||
type Pool[T any, PT Var[T]] struct { | ||
len int | ||
cap int | ||
sync.Pool | ||
} | ||
|
||
func (p *Pool[T]) Get(args ...any) T { | ||
t := p.Pool.Get().(T) | ||
t.Set(args...) | ||
return t | ||
func (p *Pool[T, PT]) Get(args ...any) (pt PT) { | ||
pt = p.Pool.Get().(PT) | ||
pt.OnGet(args...) | ||
p.len++ | ||
return | ||
} | ||
|
||
func (p *Pool[T]) Put(ts ...T) { | ||
for _, t := range ts { | ||
t.Reset() | ||
p.Pool.Put(t) | ||
func (p *Pool[T, PT]) Put(pt ...PT) { | ||
for _, v := range pt { | ||
if v == nil { | ||
continue | ||
} | ||
v.OnPut() | ||
p.Pool.Put(v) | ||
p.len-- | ||
} | ||
} | ||
|
||
func zero(typ reflect.Type) any { | ||
if typ.Kind() == reflect.Ptr { | ||
return reflect.New(typ.Elem()).Interface() | ||
} | ||
return reflect.Zero(typ).Interface() | ||
func (p *Pool[T, PT]) Len() int { | ||
return p.len | ||
} | ||
|
||
func (p *Pool[T, PT]) Cap() int { | ||
return p.cap | ||
} | ||
|
||
func New[T Var](t T) (p Pool[T]) { | ||
typ := reflect.TypeOf(t) | ||
func New[T any, PT Var[T]](_ ...PT) *Pool[T, PT] { | ||
p := new(Pool[T, PT]) | ||
p.New = func() any { | ||
i := zero(typ).(T) | ||
i.New() | ||
return i | ||
var v PT = new(T) | ||
v.OnNew() | ||
p.cap++ | ||
return v | ||
} | ||
return | ||
return p | ||
} | ||
|
||
// None is a null struct which implement Var(interface). | ||
// | ||
// You can composite it in your own struct so that you don't need to implement Var. | ||
type None struct{} | ||
|
||
func (n *None) New() {} | ||
func (n *None) Set(...any) {} | ||
func (*None) Reset() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,54 @@ | ||
package Pool_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/Drelf2018/TypeGo/Pool" | ||
"github.com/Drelf2018/TypeGo/test" | ||
) | ||
|
||
type Class struct { | ||
Students map[int]string | ||
} | ||
|
||
func (c *Class) OnNew() { | ||
c.Students = make(map[int]string) | ||
} | ||
|
||
func (c *Class) OnPut() { | ||
for k := range c.Students { | ||
delete(c.Students, k) | ||
} | ||
} | ||
|
||
func (c *Class) OnGet(stu ...any) { | ||
l := len(stu) | ||
if l&1 == 1 { | ||
panic(errors.New("odd params")) | ||
} | ||
for i := 0; i < l; i += 2 { | ||
c.Students[stu[i].(int)] = stu[i+1].(string) | ||
} | ||
} | ||
|
||
func TestPool(t *testing.T) { | ||
pool := Pool.New(&test.Student{}) | ||
pool := Pool.New(new(Class)) | ||
|
||
c1 := pool.Get(1, "Alice") | ||
t.Logf("c1: %v\n", c1) | ||
|
||
pool.Put(c1) | ||
t.Logf("c1: %v\n", c1) | ||
|
||
s1 := pool.Get("张三") | ||
t.Logf("s1: %v\n", s1) | ||
c2 := pool.Get(2, "Bob", 3, "Carol") | ||
t.Logf("c1: %v c2: %v\n", c1, c2) | ||
|
||
pool.Put(s1) | ||
t.Logf("s1: %v\n", s1) | ||
c3 := pool.Get(4, "Dave") | ||
t.Logf("c3: %v\n", c3) | ||
|
||
s2 := pool.Get("李四") | ||
t.Logf("s2: %v\n", s2) | ||
pool.Put(c2, c3, nil) | ||
t.Logf("pool.Len(): %v pool.Cap(): %v\n", pool.Len(), pool.Cap()) | ||
|
||
s3 := pool.Get("王五") | ||
t.Logf("s3: %v\n", s3) | ||
_ = pool.Get() | ||
t.Logf("pool.Len(): %v pool.Cap(): %v\n", pool.Len(), pool.Cap()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.