1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package stringset provides a way to represent a collection of strings
11 // A Set holds a collection of strings that can be looked up by an index number.
13 // These fields are exported to allow for code generation.
19 // Elem returns the string with index i. It panics if i is out of range.
20 func (s *Set) Elem(i int) string {
21 return s.Data[s.Index[i]:s.Index[i+1]]
24 // Len returns the number of strings in the set.
25 func (s *Set) Len() int {
26 return len(s.Index) - 1
29 // Search returns the index of the given string or -1 if it is not in the set.
30 // The Set must have been created with strings in sorted order.
31 func Search(s *Set, str string) int {
32 // TODO: optimize this if it gets used a lot.
34 p := sort.Search(n, func(i int) bool {
35 return s.Elem(i) >= str
37 if p == n || str != s.Elem(p) {
43 // A Builder constructs Sets.
49 // NewBuilder returns a new and initialized Builder.
50 func NewBuilder() *Builder {
55 index: map[string]int{},
59 // Set creates the set created so far.
60 func (b *Builder) Set() Set {
64 // Index returns the index for the given string, which must have been added
66 func (b *Builder) Index(s string) int {
70 // Add adds a string to the index. Strings that are added by a single Add will
71 // be stored together, unless they match an existing string.
72 func (b *Builder) Add(ss ...string) {
73 // First check if the string already exists.
74 for _, s := range ss {
75 if _, ok := b.index[s]; ok {
78 b.index[s] = len(b.set.Index) - 1
82 panic("Index too > 0xFFFF")
84 b.set.Index = append(b.set.Index, uint16(x))