podman
70 строк · 1.7 Кб
1// Copyright 2015 go-swagger maintainers
2//
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
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
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.
14
15//go:build !go1.9
16// +build !go1.9
17
18package swag19
20import (21"sort"22"sync"23)
24
25// indexOfInitialisms is a thread-safe implementation of the sorted index of initialisms.
26// Before go1.9, this may be implemented with a mutex on the map.
27type indexOfInitialisms struct {28getMutex *sync.Mutex29index map[string]bool30}
31
32func newIndexOfInitialisms() *indexOfInitialisms {33return &indexOfInitialisms{34getMutex: new(sync.Mutex),35index: make(map[string]bool, 50),36}37}
38
39func (m *indexOfInitialisms) load(initial map[string]bool) *indexOfInitialisms {40m.getMutex.Lock()41defer m.getMutex.Unlock()42for k, v := range initial {43m.index[k] = v44}45return m46}
47
48func (m *indexOfInitialisms) isInitialism(key string) bool {49m.getMutex.Lock()50defer m.getMutex.Unlock()51_, ok := m.index[key]52return ok53}
54
55func (m *indexOfInitialisms) add(key string) *indexOfInitialisms {56m.getMutex.Lock()57defer m.getMutex.Unlock()58m.index[key] = true59return m60}
61
62func (m *indexOfInitialisms) sorted() (result []string) {63m.getMutex.Lock()64defer m.getMutex.Unlock()65for k := range m.index {66result = append(result, k)67}68sort.Sort(sort.Reverse(byInitialism(result)))69return70}
71