1 // Copyright 2015 go-swagger maintainers
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
24 // indexOfInitialisms is a thread-safe implementation of the sorted index of initialisms.
25 // Since go1.9, this may be implemented with sync.Map.
26 type indexOfInitialisms struct {
31 func newIndexOfInitialisms() *indexOfInitialisms {
32 return &indexOfInitialisms{
33 sortMutex: new(sync.Mutex),
38 func (m *indexOfInitialisms) load(initial map[string]bool) *indexOfInitialisms {
40 defer m.sortMutex.Unlock()
41 for k, v := range initial {
47 func (m *indexOfInitialisms) isInitialism(key string) bool {
48 _, ok := m.index.Load(key)
52 func (m *indexOfInitialisms) add(key string) *indexOfInitialisms {
53 m.index.Store(key, true)
57 func (m *indexOfInitialisms) sorted() (result []string) {
59 defer m.sortMutex.Unlock()
60 m.index.Range(func(key, value interface{}) bool {
62 result = append(result, k)
65 sort.Sort(sort.Reverse(byInitialism(result)))