Add tags implement

This commit is contained in:
Tnze
2024-07-27 23:53:35 +08:00
parent d88ee401fd
commit 31375289b0
7 changed files with 218 additions and 13 deletions

View File

@ -4,6 +4,7 @@ type Registry[E any] struct {
keys map[string]int32
values []E
indices map[*E]int32
tags map[string][]*E
}
func NewRegistry[E any]() Registry[E] {
@ -11,9 +12,17 @@ func NewRegistry[E any]() Registry[E] {
keys: make(map[string]int32),
values: make([]E, 0, 256),
indices: make(map[*E]int32),
tags: make(map[string][]*E),
}
}
func (r *Registry[E]) Clear() {
r.keys = make(map[string]int32)
r.values = r.values[:0]
r.indices = make(map[*E]int32)
r.tags = make(map[string][]*E)
}
func (r *Registry[E]) Get(key string) (int32, *E) {
id, ok := r.keys[key]
if !ok {
@ -38,8 +47,14 @@ func (r *Registry[E]) Put(name string, data E) (id int32, val *E) {
return
}
func (r *Registry[E]) Clear() {
r.keys = make(map[string]int32)
r.values = r.values[:0]
r.indices = make(map[*E]int32)
}
// func (r *Registry[E]) BindTags(tag string, ids []int32) error {
// values := make([]*E, len(ids))
// for i, id := range ids {
// if id < 0 || id >= int32(len(r.values)) {
// return errors.New("invalid id: " + strconv.Itoa(int(id)))
// }
// values[i] = &r.values[id]
// }
// r.tags[tag] = values
// return nil
// }