constant-time lookups for tag inserts
This commit is contained in:
parent
dca7ed3387
commit
866111f54c
18
message.go
18
message.go
@ -11,19 +11,23 @@ type Tag struct {
|
|||||||
Value string
|
Value string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tags is an key-order-preserving, last-insert-wins slice
|
// Tags is an key-order-preserving, last-insert-wins "set" of Tag
|
||||||
type Tags struct {
|
type Tags struct {
|
||||||
|
kv map[string]int // quick lookups for inserts
|
||||||
tags []Tag
|
tags []Tag
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tags) Insert(new Tag) {
|
func (t *Tags) Insert(new Tag) {
|
||||||
for i := range t.tags {
|
if t.kv == nil {
|
||||||
if t.tags[i].Key == new.Key {
|
t.kv = map[string]int{}
|
||||||
t.tags[i] = new
|
}
|
||||||
return
|
i, ok := t.kv[new.Key]
|
||||||
}
|
if ok {
|
||||||
|
t.tags[i] = new
|
||||||
|
} else {
|
||||||
|
t.kv[new.Key] = len(t.tags)
|
||||||
|
t.tags = append(t.tags, new)
|
||||||
}
|
}
|
||||||
t.tags = append(t.tags, new)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tags) GetSlice() []Tag {
|
func (t *Tags) GetSlice() []Tag {
|
||||||
|
Loading…
Reference in New Issue
Block a user