mirror of
https://github.com/jeremyd/ergo.git
synced 2026-07-09 01:48:10 -07:00
tweaks to NAMES implementation (#2058)
* tweaks to NAMES implementation * tweak member caching * add a benchmark for NAMES
This commit is contained in:
committed by
GitHub
parent
378d88fee2
commit
eeec481b8d
@@ -48,6 +48,13 @@ func BitsetSet(set []uint32, position uint, on bool) (changed bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// BitsetClear clears the bitset in-place.
|
||||
func BitsetClear(set []uint32) {
|
||||
for i := 0; i < len(set); i++ {
|
||||
atomic.StoreUint32(&set[i], 0)
|
||||
}
|
||||
}
|
||||
|
||||
// BitsetEmpty returns whether the bitset is empty.
|
||||
// This has false positives under concurrent modification (i.e., it can return true
|
||||
// even though w.r.t. the sequence of atomic modifications, there was no point at
|
||||
|
||||
@@ -125,6 +125,28 @@ func (t *TokenLineBuilder) Add(token string) {
|
||||
t.buf.WriteString(token)
|
||||
}
|
||||
|
||||
// AddParts concatenates `parts` into a token and adds it to the line,
|
||||
// creating a new line if necessary.
|
||||
func (t *TokenLineBuilder) AddParts(parts ...string) {
|
||||
var tokenLen int
|
||||
for _, part := range parts {
|
||||
tokenLen += len(part)
|
||||
}
|
||||
if t.buf.Len() != 0 {
|
||||
tokenLen += len(t.delim)
|
||||
}
|
||||
if t.lineLen < t.buf.Len()+tokenLen {
|
||||
t.result = append(t.result, t.buf.String())
|
||||
t.buf.Reset()
|
||||
}
|
||||
if t.buf.Len() != 0 {
|
||||
t.buf.WriteString(t.delim)
|
||||
}
|
||||
for _, part := range parts {
|
||||
t.buf.WriteString(part)
|
||||
}
|
||||
}
|
||||
|
||||
// Lines terminates the line-building and returns all the lines.
|
||||
func (t *TokenLineBuilder) Lines() (result []string) {
|
||||
result = t.result
|
||||
|
||||
@@ -43,3 +43,26 @@ func TestBuildTokenLines(t *testing.T) {
|
||||
val = BuildTokenLines(10, []string{"abcd", "efgh", "ijkl"}, ",")
|
||||
assertEqual(val, []string{"abcd,efgh", "ijkl"}, t)
|
||||
}
|
||||
|
||||
func TestTLBuilderAddParts(t *testing.T) {
|
||||
var tl TokenLineBuilder
|
||||
tl.Initialize(20, " ")
|
||||
tl.Add("bob")
|
||||
tl.AddParts("@", "alice")
|
||||
tl.AddParts("@", "ErgoBot__")
|
||||
assertEqual(tl.Lines(), []string{"bob @alice", "@ErgoBot__"}, t)
|
||||
}
|
||||
|
||||
func BenchmarkTokenLines(b *testing.B) {
|
||||
tokens := strings.Fields(monteCristo)
|
||||
b.ResetTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
var tl TokenLineBuilder
|
||||
tl.Initialize(400, " ")
|
||||
for _, tok := range tokens {
|
||||
tl.Add(tok)
|
||||
}
|
||||
tl.Lines()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user