| 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. |
| 4 | |
| 5 | // Package stringset provides a way to represent a collection of strings |
| 6 | // compactly. |
| 7 | package stringsetscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/ |
| 8 | |
| 9 | import "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 10 | |
| 11 | // A Set holds a collection of strings that can be looked up by an index number. |
| 12 | type Setscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set# struct { |
| 13 | // These fields are exported to allow for code generation. |
| 14 | |
| 15 | Datascip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#Data. string |
| 16 | Indexscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#Index. []uint16 |
| 17 | } |
| 18 | |
| 19 | // Elem returns the string with index i. It panics if i is out of range. |
| 20 | func (s *Setscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#) Elemscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#Elem().(i int) string { |
| 21 | return s.Datascip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#Data.[s.Indexscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#Index.[i]:s.Indexscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#Index.[i+1]] |
| 22 | } |
| 23 | |
| 24 | // Len returns the number of strings in the set. |
| 25 | func (s *Setscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#) Lenscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#Len().() int { |
| 26 | return len(s.Indexscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#Index.) - 1 |
| 27 | } |
| 28 | |
| 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 Searchscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Search().(s *Setscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#, str string) int { |
| 32 | // TODO: optimize this if it gets used a lot. |
| 33 | n := len(s.Indexscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#Index.) - 1 |
| 34 | p := sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Searchscip-go gomod github.com/golang/go/src go1.25.0 sort/Search().(n, func(i int) bool { |
| 35 | return s.Elemscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#Elem().(i) >= str |
| 36 | }) |
| 37 | if p == n || str != s.Elemscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#Elem().(p) { |
| 38 | return -1 |
| 39 | } |
| 40 | return p |
| 41 | } |
| 42 | |
| 43 | // A Builder constructs Sets. |
| 44 | type Builderscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder# struct { |
| 45 | setscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#set. Setscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set# |
| 46 | indexscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#index. map[string]int |
| 47 | } |
| 48 | |
| 49 | // NewBuilder returns a new and initialized Builder. |
| 50 | func NewBuilderscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/NewBuilder().() *Builderscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder# { |
| 51 | return &Builderscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#{ |
| 52 | setscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#set.: Setscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#{ |
| 53 | Indexscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#Index.: []uint16{0}, |
| 54 | }, |
| 55 | indexscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#index.: map[string]int{}, |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Set creates the set created so far. |
| 60 | func (b *Builderscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#) Setscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#Set().() Setscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set# { |
| 61 | return b.setscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#set. |
| 62 | } |
| 63 | |
| 64 | // Index returns the index for the given string, which must have been added |
| 65 | // before. |
| 66 | func (b *Builderscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#) Indexscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#Index().(s string) int { |
| 67 | return b.indexscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#index.[s] |
| 68 | } |
| 69 | |
| 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 *Builderscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#) Addscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#Add().(ss ...string) { |
| 73 | // First check if the string already exists. |
| 74 | for _, s := range ss { |
| 75 | if _, ok := b.indexscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#index.[s]; ok { |
| 76 | continue |
| 77 | } |
| 78 | b.indexscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#index.[s] = len(b.setscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#set..Indexscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#Index.) - 1 |
| 79 | b.setscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#set..Datascip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#Data. += s |
| 80 | x := len(b.setscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#set..Datascip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#Data.) |
| 81 | if x > 0xFFFF { |
| 82 | panic("Index too > 0xFFFF") |
| 83 | } |
| 84 | b.setscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#set..Indexscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#Index. = append(b.setscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Builder#set..Indexscip-go gomod golang.org/x/text . `golang.org/x/text/internal/stringset`/Set#Index., uint16(x)) |
| 85 | } |
| 86 | } |