constant-time lookups for tag inserts
This commit is contained in:
parent
dca7ed3387
commit
866111f54c
16
message.go
16
message.go
@ -11,19 +11,23 @@ type Tag struct {
|
||||
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 {
|
||||
kv map[string]int // quick lookups for inserts
|
||||
tags []Tag
|
||||
}
|
||||
|
||||
func (t *Tags) Insert(new Tag) {
|
||||
for i := range t.tags {
|
||||
if t.tags[i].Key == new.Key {
|
||||
if t.kv == nil {
|
||||
t.kv = map[string]int{}
|
||||
}
|
||||
i, ok := t.kv[new.Key]
|
||||
if ok {
|
||||
t.tags[i] = new
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
t.kv[new.Key] = len(t.tags)
|
||||
t.tags = append(t.tags, new)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Tags) GetSlice() []Tag {
|
||||
|
Loading…
Reference in New Issue
Block a user